Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
1,109 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ProviderCrypto } from "../provider"; | ||
import { KeyUsages } from "../types"; | ||
import { BufferSourceConverter } from "../utils"; | ||
|
||
export abstract class HkdfProvider extends ProviderCrypto { | ||
|
||
public name = "HKDF"; | ||
public hashAlgorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"]; | ||
public usages: KeyUsages = ["deriveKey", "deriveBits"]; | ||
|
||
public checkAlgorithmParams(algorithm: HkdfParams) { | ||
// hash | ||
this.checkRequiredProperty(algorithm, "hash"); | ||
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms); | ||
|
||
// salt | ||
this.checkRequiredProperty(algorithm, "salt"); | ||
if (!BufferSourceConverter.isBufferSource(algorithm.salt)) { | ||
throw new TypeError("salt: Is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
} | ||
|
||
// info | ||
this.checkRequiredProperty(algorithm, "info"); | ||
if (!BufferSourceConverter.isBufferSource(algorithm.info)) { | ||
throw new TypeError("salt: Is not of type '(ArrayBuffer or ArrayBufferView)'"); | ||
} | ||
} | ||
|
||
public checkImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]) { | ||
super.checkImportKey(format, keyData, algorithm, extractable, keyUsages); | ||
if (extractable) { | ||
// If extractable is not false, then throw a SyntaxError | ||
throw new SyntaxError("extractable: Must be False"); | ||
} | ||
} | ||
|
||
public abstract onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>; | ||
public abstract onDeriveBits(algorithm: Pbkdf2Params, baseKey: CryptoKey, length: number): Promise<ArrayBuffer>; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./hkdf"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import assert from "assert"; | ||
import "reflect-metadata"; | ||
import { HkdfProvider, OperationError } from "../src"; | ||
|
||
context("HKDF", () => { | ||
|
||
const provider = Reflect.construct(HkdfProvider, []) as HkdfProvider; | ||
|
||
context("checkAlgorithmParams", () => { | ||
|
||
it("error if `hash` is missing", () => { | ||
assert.throws(() => { | ||
provider.checkAlgorithmParams({ salt: new Uint8Array(4), info: new Uint8Array(4) } as any); | ||
}, Error); | ||
}); | ||
|
||
it("error if `hash` is wrong", () => { | ||
assert.throws(() => { | ||
provider.checkAlgorithmParams({ hash: { name: "WRONG" }, salt: new Uint8Array(4), info: new Uint8Array(4) } as any); | ||
}, OperationError); | ||
}); | ||
|
||
it("error if `salt` is missing", () => { | ||
assert.throws(() => { | ||
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, info: new Uint8Array(4) } as any); | ||
}, Error); | ||
}); | ||
|
||
it("error if `salt` wrong type", () => { | ||
assert.throws(() => { | ||
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, salt: "wrong", info: new Uint8Array(4) } as any); | ||
}, TypeError); | ||
}); | ||
|
||
it("error if `info` is missing", () => { | ||
assert.throws(() => { | ||
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, salt: new Uint8Array(4) } as any); | ||
}, Error); | ||
}); | ||
|
||
it("error if `info` wrong type", () => { | ||
assert.throws(() => { | ||
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, info: "wrong", salt: new Uint8Array(4) } as any); | ||
}, TypeError); | ||
}); | ||
|
||
it("correct value", () => { | ||
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, salt: new Uint8Array(4), info: new Uint8Array(4) } as any); | ||
}); | ||
|
||
}); | ||
|
||
context("checkImportKey", () => { | ||
|
||
it("throw error if extractable is true", () => { | ||
assert.throws(() => { | ||
provider.checkImportKey("raw", new ArrayBuffer(0), { name: "HKDF" }, true, ["deriveBits"]); | ||
}, SyntaxError); | ||
}); | ||
|
||
it("correct extractable value", () => { | ||
provider.checkImportKey("raw", new ArrayBuffer(0), { name: "HKDF" }, false, ["deriveBits"]); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters