await promises in place

This commit is contained in:
2024-09-10 00:32:25 +02:00
parent 9de49c228d
commit 7e858b3556
9 changed files with 31 additions and 38 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ export class JWTcontext {
public static async gen_key(): Promise<Key> {
log.trace('generate key')
return jose.generateSecret('HS512')
return await jose.generateSecret('HS512')
}
public static new(key: Key): JWTcontext {
return new JWTcontext(key)
+3 -3
View File
@@ -54,7 +54,7 @@ export async function hkdf(key: Uint8Array, usage: Usage): Promise<CryptoKey> {
}
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<Uint8Ar
return new Uint8Array(buffer)
}
export function ecdh(privkey: CryptoKey, pubkey: CryptoKey, usage: DHusage): Promise<CryptoKey> {
export async function ecdh(privkey: CryptoKey, pubkey: CryptoKey, usage: DHusage): Promise<CryptoKey> {
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
+7 -14
View File
@@ -12,16 +12,9 @@ export default class PrivateBox {
private readonly box: SecretBox
) {}
public static gen(extractable: boolean = true): Promise<CryptoKeyPair> {
public static async gen(extractable: boolean = true): Promise<CryptoKeyPair> {
log.trace('generate keypair')
log.trace('Extractable :', extractable)
try {
return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise<CryptoKeyPair>
} 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<PrivateBox> {
@@ -34,7 +27,7 @@ export default class PrivateBox {
public async decrypt(privkey: CryptoKey): Promise<Uint8Array | null> {
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<string> {
@@ -57,10 +50,10 @@ export default class PrivateBox {
return new PrivateBox(pubkey, box)
}
public static pubkey_toString(pubkey: CryptoKey): Promise<string> {
return misc.pubkey_toString(pubkey)
public static async pubkey_toString(pubkey: CryptoKey): Promise<string> {
return await misc.pubkey_toString(pubkey)
}
public static pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
return misc.pubkey_fromString(pubkey, misc.Usage.ecdh)
public static async pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
return await misc.pubkey_fromString(pubkey, misc.Usage.ecdh)
}
}
+7 -7
View File
@@ -12,9 +12,9 @@ export default class PrivateWrap {
private readonly pubkey: CryptoKey
) {}
public static gen(extractable: boolean = true): Promise<CryptoKeyPair> {
public static async gen(extractable: boolean = true): Promise<CryptoKeyPair> {
log.trace('generate keypair')
return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise<CryptoKeyPair>
return await crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as CryptoKeyPair
}
public static async wrap(data: CryptoKey, pubkey: CryptoKey): Promise<PrivateWrap> {
@@ -27,7 +27,7 @@ export default class PrivateWrap {
public async unwrap(privkey: CryptoKey): Promise<CryptoKey | null> {
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<string> {
@@ -50,10 +50,10 @@ export default class PrivateWrap {
return new PrivateWrap(box, pubkey)
}
public static pubkey_toString(pubkey: CryptoKey): Promise<string> {
return misc.pubkey_toString(pubkey)
public static async pubkey_toString(pubkey: CryptoKey): Promise<string> {
return await misc.pubkey_toString(pubkey)
}
public static pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
return misc.pubkey_fromString(pubkey, misc.Usage.ecdh)
public static async pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
return await misc.pubkey_fromString(pubkey, misc.Usage.ecdh)
}
}
+2 -2
View File
@@ -13,7 +13,7 @@ export default class PwdBox {
private static async derive(pwd: string, salt: Uint8Array): Promise<CryptoKey> {
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<PwdBox> {
@@ -26,7 +26,7 @@ export default class PwdBox {
public async decrypt(pwd: string): Promise<Uint8Array | null> {
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 {
+2 -2
View File
@@ -13,7 +13,7 @@ export default class PwdWrap {
private static async derive(pwd: string, salt: Uint8Array): Promise<CryptoKey> {
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<PwdWrap> {
@@ -26,7 +26,7 @@ export default class PwdWrap {
public async unwrap(pwd: string): Promise<CryptoKey | null> {
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 {
+2 -2
View File
@@ -10,9 +10,9 @@ export default class SecretBox {
private readonly cipher: Uint8Array
) {}
public static gen(extractable: boolean = true): Promise<CryptoKey> {
public static async gen(extractable: boolean = true): Promise<CryptoKey> {
log.trace('generate key')
return crypto.subtle.generateKey(
return await crypto.subtle.generateKey(
{
name: consts.ENCRYPTION,
length: 256
+2 -2
View File
@@ -13,9 +13,9 @@ export default class SecretWrap {
private readonly iv: Uint8Array
) {}
public static gen(extractable: boolean = true): Promise<CryptoKey> {
public static async gen(extractable: boolean = true): Promise<CryptoKey> {
log.trace('generate key')
return crypto.subtle.generateKey(
return await crypto.subtle.generateKey(
{
name: consts.ENCRYPTION,
length: 256
+5 -5
View File
@@ -5,7 +5,7 @@ import * as misc from './misc'
import * as libmisc from 'misc'
export async function gen(extractable: boolean = true): Promise<CryptoKeyPair> {
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<Uint8Array> {
log.trace('sign')
@@ -14,14 +14,14 @@ export async function sign(message: Uint8Array, privkey: CryptoKey): Promise<Uin
}
export async function verify(message: Uint8Array, pubkey: CryptoKey, signature: Uint8Array): Promise<boolean> {
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<string> {
return misc.pubkey_toString(pubkey)
export async function pubkey_toString(pubkey: CryptoKey): Promise<string> {
return await misc.pubkey_toString(pubkey)
}
export async function pubkey_fromString(pubkey: string): Promise<CryptoKey | null> {
return misc.pubkey_fromString(pubkey, misc.Usage.sign)
return await misc.pubkey_fromString(pubkey, misc.Usage.sign)
}
export function signature_toString(signature: Uint8Array): string {