diff --git a/src/jwt.ts b/src/jwt.ts index ac5ada4..967ef6f 100644 --- a/src/jwt.ts +++ b/src/jwt.ts @@ -10,7 +10,7 @@ export class JWTcontext { public static async gen_key(): Promise { log.trace('generate key') - return jose.generateSecret('HS512') + return await jose.generateSecret('HS512') } public static new(key: Key): JWTcontext { return new JWTcontext(key) diff --git a/src/kdf.ts b/src/kdf.ts index 9e3b52f..6e0826f 100644 --- a/src/kdf.ts +++ b/src/kdf.ts @@ -54,7 +54,7 @@ export async function hkdf(key: Uint8Array, usage: Usage): Promise { } const info = new TextEncoder().encode(info_txt) - return crypto.subtle.deriveKey( + return await crypto.subtle.deriveKey( { name: 'HKDF', hash: 'SHA-512', @@ -86,10 +86,10 @@ export async function pbkdf(salt: Uint8Array, password: string): Promise { +export async function ecdh(privkey: CryptoKey, pubkey: CryptoKey, usage: DHusage): Promise { log.trace('ecdh') const outputUsage: KeyUsage[] = usage === DHusage.box ? ['encrypt', 'decrypt'] : ['wrapKey', 'unwrapKey'] - return crypto.subtle.deriveKey( + return await crypto.subtle.deriveKey( { name: consts.ECDH.name, public: pubkey diff --git a/src/private-box.ts b/src/private-box.ts index 9207cfe..8f42dab 100644 --- a/src/private-box.ts +++ b/src/private-box.ts @@ -12,16 +12,9 @@ export default class PrivateBox { private readonly box: SecretBox ) {} - public static gen(extractable: boolean = true): Promise { + public static async gen(extractable: boolean = true): Promise { log.trace('generate keypair') - log.trace('Extractable :', extractable) - try { - return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise - } catch (e) { - log.warn('Failed to generate a key') - log.debug('Error :', e) - throw e - } + return await crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as CryptoKeyPair } public static async encrypt(data: Uint8Array, pubkey: CryptoKey): Promise { @@ -34,7 +27,7 @@ export default class PrivateBox { public async decrypt(privkey: CryptoKey): Promise { log.trace('decrypt') const key = await ecdh(privkey, this.pubkey, DHusage.box) - return this.box.decrypt(key) + return await this.box.decrypt(key) } public async toString(): Promise { @@ -57,10 +50,10 @@ export default class PrivateBox { return new PrivateBox(pubkey, box) } - public static pubkey_toString(pubkey: CryptoKey): Promise { - return misc.pubkey_toString(pubkey) + public static async pubkey_toString(pubkey: CryptoKey): Promise { + return await misc.pubkey_toString(pubkey) } - public static pubkey_fromString(pubkey: string): Promise { - return misc.pubkey_fromString(pubkey, misc.Usage.ecdh) + public static async pubkey_fromString(pubkey: string): Promise { + return await misc.pubkey_fromString(pubkey, misc.Usage.ecdh) } } diff --git a/src/private-wrap.ts b/src/private-wrap.ts index 662c09e..7b90ed4 100644 --- a/src/private-wrap.ts +++ b/src/private-wrap.ts @@ -12,9 +12,9 @@ export default class PrivateWrap { private readonly pubkey: CryptoKey ) {} - public static gen(extractable: boolean = true): Promise { + public static async gen(extractable: boolean = true): Promise { log.trace('generate keypair') - return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise + return await crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as CryptoKeyPair } public static async wrap(data: CryptoKey, pubkey: CryptoKey): Promise { @@ -27,7 +27,7 @@ export default class PrivateWrap { public async unwrap(privkey: CryptoKey): Promise { log.trace('unwrap') const kd = await ecdh(privkey, this.pubkey, DHusage.wrap) - return this.box.unwrap(kd) + return await this.box.unwrap(kd) } public async toString(): Promise { @@ -50,10 +50,10 @@ export default class PrivateWrap { return new PrivateWrap(box, pubkey) } - public static pubkey_toString(pubkey: CryptoKey): Promise { - return misc.pubkey_toString(pubkey) + public static async pubkey_toString(pubkey: CryptoKey): Promise { + return await misc.pubkey_toString(pubkey) } - public static pubkey_fromString(pubkey: string): Promise { - return misc.pubkey_fromString(pubkey, misc.Usage.ecdh) + public static async pubkey_fromString(pubkey: string): Promise { + return await misc.pubkey_fromString(pubkey, misc.Usage.ecdh) } } diff --git a/src/pwd-box.ts b/src/pwd-box.ts index 569c3b4..09fc033 100644 --- a/src/pwd-box.ts +++ b/src/pwd-box.ts @@ -13,7 +13,7 @@ export default class PwdBox { private static async derive(pwd: string, salt: Uint8Array): Promise { const k = await pbkdf(salt, pwd) - return (await hkdf(k, Usage.box)) as CryptoKey + return await hkdf(k, Usage.box) as CryptoKey } public static async encrypt(data: Uint8Array, pwd: string): Promise { @@ -26,7 +26,7 @@ export default class PwdBox { public async decrypt(pwd: string): Promise { log.trace('decrypt') const k = await PwdBox.derive(pwd, this.salt) - return this.box.decrypt(k) + return await this.box.decrypt(k) } public toString(): string { diff --git a/src/pwd-wrap.ts b/src/pwd-wrap.ts index 5b6e94b..ce9d9a7 100644 --- a/src/pwd-wrap.ts +++ b/src/pwd-wrap.ts @@ -13,7 +13,7 @@ export default class PwdWrap { private static async derive(pwd: string, salt: Uint8Array): Promise { const k = await pbkdf(salt, pwd) - return (await hkdf(k, Usage.wrap)) as CryptoKey + return await hkdf(k, Usage.wrap) as CryptoKey } public static async wrap(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise { @@ -26,7 +26,7 @@ export default class PwdWrap { public async unwrap(pwd: string): Promise { log.trace('unwrap') const k = await PwdWrap.derive(pwd, this.salt) - return this.box.unwrap(k) + return await this.box.unwrap(k) } public toString(): string { diff --git a/src/secret-box.ts b/src/secret-box.ts index 8d70af2..065482a 100644 --- a/src/secret-box.ts +++ b/src/secret-box.ts @@ -10,9 +10,9 @@ export default class SecretBox { private readonly cipher: Uint8Array ) {} - public static gen(extractable: boolean = true): Promise { + public static async gen(extractable: boolean = true): Promise { log.trace('generate key') - return crypto.subtle.generateKey( + return await crypto.subtle.generateKey( { name: consts.ENCRYPTION, length: 256 diff --git a/src/secret-wrap.ts b/src/secret-wrap.ts index 7e5e2da..e7e6a4c 100644 --- a/src/secret-wrap.ts +++ b/src/secret-wrap.ts @@ -13,9 +13,9 @@ export default class SecretWrap { private readonly iv: Uint8Array ) {} - public static gen(extractable: boolean = true): Promise { + public static async gen(extractable: boolean = true): Promise { log.trace('generate key') - return crypto.subtle.generateKey( + return await crypto.subtle.generateKey( { name: consts.ENCRYPTION, length: 256 diff --git a/src/signature.ts b/src/signature.ts index a307d6e..e818ffb 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -5,7 +5,7 @@ import * as misc from './misc' import * as libmisc from 'misc' export async function gen(extractable: boolean = true): Promise { - return crypto.subtle.generateKey(consts.SIGNATURE_KEY, extractable, ['sign', 'verify']) + return await crypto.subtle.generateKey(consts.SIGNATURE_KEY, extractable, ['sign', 'verify']) } export async function sign(message: Uint8Array, privkey: CryptoKey): Promise { log.trace('sign') @@ -14,14 +14,14 @@ export async function sign(message: Uint8Array, privkey: CryptoKey): Promise { log.trace('verify') - return crypto.subtle.verify(consts.SIGNATURE_ALGO, pubkey, signature, message) + return await crypto.subtle.verify(consts.SIGNATURE_ALGO, pubkey, signature, message) } -export function pubkey_toString(pubkey: CryptoKey): Promise { - return misc.pubkey_toString(pubkey) +export async function pubkey_toString(pubkey: CryptoKey): Promise { + return await misc.pubkey_toString(pubkey) } export async function pubkey_fromString(pubkey: string): Promise { - return misc.pubkey_fromString(pubkey, misc.Usage.sign) + return await misc.pubkey_fromString(pubkey, misc.Usage.sign) } export function signature_toString(signature: Uint8Array): string {