This commit is contained in:
+6
-6
@@ -17,17 +17,17 @@ export default class PrivateWrap {
|
||||
return crypto.subtle.generateKey(consts.ECDH, extractable, ['deriveKey']) as Promise<CryptoKeyPair>
|
||||
}
|
||||
|
||||
public static async encrypt(data: CryptoKey, pubkey: CryptoKey): Promise<PrivateWrap> {
|
||||
log.trace('encrypt')
|
||||
public static async wrap(data: CryptoKey, pubkey: CryptoKey): Promise<PrivateWrap> {
|
||||
log.trace('wrap')
|
||||
const tmp_keypair = await PrivateWrap.gen()
|
||||
const kd = await ecdh(tmp_keypair.privateKey, pubkey, DHusage.wrap)
|
||||
const box = await SecretWrap.encrypt(data, kd)
|
||||
const box = await SecretWrap.wrap(data, kd)
|
||||
return new this(box, tmp_keypair.publicKey)
|
||||
}
|
||||
public async decrypt(privkey: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('decrypt')
|
||||
public async unwrap(privkey: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('unwrap')
|
||||
const kd = await ecdh(privkey, this.pubkey, DHusage.wrap)
|
||||
return this.box.decrypt(kd)
|
||||
return this.box.unwrap(kd)
|
||||
}
|
||||
|
||||
public async toString(): Promise<string> {
|
||||
|
||||
+6
-6
@@ -16,17 +16,17 @@ export default class PwdWrap {
|
||||
return (await hkdf(k, Usage.wrap)) as CryptoKey
|
||||
}
|
||||
|
||||
public static async encrypt(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise<PwdWrap> {
|
||||
log.trace('encrypt')
|
||||
public static async wrap(data: CryptoKey, pwd: string, salt?: Uint8Array): Promise<PwdWrap> {
|
||||
log.trace('wrap')
|
||||
salt = salt ?? crypto.getRandomValues(new Uint8Array(16))
|
||||
const k = await PwdWrap.derive(pwd, salt)
|
||||
const box = await SecretWrap.encrypt(data, k)
|
||||
const box = await SecretWrap.wrap(data, k)
|
||||
return new PwdWrap(box, salt)
|
||||
}
|
||||
public async decrypt(pwd: string): Promise<CryptoKey | null> {
|
||||
log.trace('decrypt')
|
||||
public async unwrap(pwd: string): Promise<CryptoKey | null> {
|
||||
log.trace('unwrap')
|
||||
const k = await PwdWrap.derive(pwd, this.salt)
|
||||
return this.box.decrypt(k)
|
||||
return this.box.unwrap(k)
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
|
||||
+4
-4
@@ -36,15 +36,15 @@ export default class SecretWrap {
|
||||
}
|
||||
}
|
||||
|
||||
public static async encrypt(data: CryptoKey, key: CryptoKey): Promise<SecretWrap> {
|
||||
log.trace('encrypt')
|
||||
public static async wrap(data: CryptoKey, key: CryptoKey): Promise<SecretWrap> {
|
||||
log.trace('wrap')
|
||||
const format = SecretWrap.format(data.type)
|
||||
const iv = crypto.getRandomValues(new Uint8Array(12))
|
||||
const box = await crypto.subtle.wrapKey(format, data, key, {name: consts.ENCRYPTION, iv})
|
||||
return new SecretWrap(new Uint8Array(box), data.algorithm, data.usages, format, iv)
|
||||
}
|
||||
public async decrypt(key: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('decrypt')
|
||||
public async unwrap(key: CryptoKey): Promise<CryptoKey | null> {
|
||||
log.trace('unwrap')
|
||||
try {
|
||||
return await crypto.subtle.unwrapKey(
|
||||
this.type,
|
||||
|
||||
+2
-2
@@ -24,8 +24,8 @@ test('Private wrap', async () => {
|
||||
const message = crypto.getRandomValues(new Uint8Array(8))
|
||||
const k_wrapped = await SecretBox.gen(true)
|
||||
const boxed_message = await SecretBox.encrypt(message, k_wrapped)
|
||||
const box = await PrivateWrap.encrypt(k_wrapped, de!)
|
||||
const unbox = await box.decrypt(k.privateKey)
|
||||
const box = await PrivateWrap.wrap(k_wrapped, de!)
|
||||
const unbox = await box.unwrap(k.privateKey)
|
||||
expect(unbox).not.toBeNull()
|
||||
|
||||
const unboxed_message = await boxed_message.decrypt(unbox!)
|
||||
|
||||
@@ -13,13 +13,13 @@ beforeAll(async () => {
|
||||
})
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await PrivateWrap.encrypt(message, k1.publicKey)
|
||||
const unboxed = await box.decrypt(k1.privateKey)
|
||||
const box = await PrivateWrap.wrap(message, k1.publicKey)
|
||||
const unboxed = await box.unwrap(k1.privateKey)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await PrivateWrap.encrypt(message, k1.publicKey)
|
||||
const unboxed = await box.decrypt(k2.privateKey)
|
||||
const box = await PrivateWrap.wrap(message, k1.publicKey)
|
||||
const unboxed = await box.unwrap(k2.privateKey)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
@@ -35,7 +35,7 @@ test('Key generation', async () => {
|
||||
})
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await PrivateWrap.encrypt(message, k1.publicKey)
|
||||
const box = await PrivateWrap.wrap(message, k1.publicKey)
|
||||
|
||||
const ser = await box.toString()
|
||||
const de = await PrivateWrap.fromString(ser)
|
||||
|
||||
@@ -12,18 +12,18 @@ beforeAll(async () => {
|
||||
})
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await PwdWrap.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k1)
|
||||
const box = await PwdWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k1)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await PwdWrap.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k2)
|
||||
const box = await PwdWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k2)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await PwdWrap.encrypt(message, k1)
|
||||
const box = await PwdWrap.wrap(message, k1)
|
||||
|
||||
const ser = box.toString()
|
||||
const de = PwdWrap.fromString(ser)
|
||||
|
||||
@@ -12,13 +12,13 @@ beforeAll(async () => {
|
||||
})
|
||||
|
||||
test('base case', async () => {
|
||||
const box = await SecretWrap.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k1)
|
||||
const box = await SecretWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k1)
|
||||
expect(unboxed).toEqual(message)
|
||||
})
|
||||
test("Different key can't decrypt", async () => {
|
||||
const box = await SecretWrap.encrypt(message, k1)
|
||||
const unboxed = await box.decrypt(k2)
|
||||
const box = await SecretWrap.wrap(message, k1)
|
||||
const unboxed = await box.unwrap(k2)
|
||||
expect(unboxed).toBeNull()
|
||||
})
|
||||
|
||||
@@ -28,7 +28,7 @@ test('Key generation', async () => {
|
||||
})
|
||||
|
||||
test('serialization', async () => {
|
||||
const box = await SecretWrap.encrypt(message, k1)
|
||||
const box = await SecretWrap.wrap(message, k1)
|
||||
|
||||
const ser = box.toString()
|
||||
const de = SecretWrap.fromString(ser)
|
||||
|
||||
+9
-9
@@ -8,14 +8,14 @@ beforeAll(async () => {
|
||||
})
|
||||
|
||||
async function seal(key: CryptoKey): Promise<CryptoKey> {
|
||||
const box = await SecretWrap.encrypt(key, wrap_key)
|
||||
const unboxed = await box.decrypt(wrap_key)
|
||||
const box = await SecretWrap.wrap(key, wrap_key)
|
||||
const unboxed = await box.unwrap(wrap_key)
|
||||
expect(unboxed).not.toBeNull()
|
||||
return unboxed!
|
||||
}
|
||||
async function cant_seal(key: CryptoKey): Promise<void> {
|
||||
expect(key.extractable).toBeFalse()
|
||||
expect(SecretWrap.encrypt(key, wrap_key)).rejects.toThrow()
|
||||
expect(SecretWrap.wrap(key, wrap_key)).rejects.toThrow()
|
||||
}
|
||||
|
||||
test('Signature', async () => {
|
||||
@@ -29,10 +29,10 @@ test('Signature', async () => {
|
||||
})
|
||||
test('SecretWrap', async () => {
|
||||
let k = await SecretWrap.gen(true)
|
||||
const box = await SecretWrap.encrypt(k, k) // Please kids, never do this for real !
|
||||
const box = await SecretWrap.wrap(k, k) // Please kids, never do this for real !
|
||||
k = await seal(k)
|
||||
|
||||
const unbox = await box.decrypt(k)
|
||||
const unbox = await box.unwrap(k)
|
||||
expect(unbox).not.toBeNull()
|
||||
expect(unbox).toEqual(k)
|
||||
})
|
||||
@@ -48,10 +48,10 @@ test('SecretBox', async () => {
|
||||
})
|
||||
test('PrivateWrap', async () => {
|
||||
const k = await PrivateWrap.gen(true)
|
||||
const box = await PrivateWrap.encrypt(wrap_key, k.publicKey)
|
||||
const box = await PrivateWrap.wrap(wrap_key, k.publicKey)
|
||||
const privk = await seal(k.privateKey)
|
||||
|
||||
const unbox = await box.decrypt(privk)
|
||||
const unbox = await box.unwrap(privk)
|
||||
expect(unbox).not.toBeNull()
|
||||
expect(unbox).toEqual(wrap_key)
|
||||
})
|
||||
@@ -67,8 +67,8 @@ test('PrivateBox', async () => {
|
||||
})
|
||||
test("Can't unwrap with a different key", async () => {
|
||||
const wrap_k2 = await SecretWrap.gen(false)
|
||||
const box = await SecretWrap.encrypt(wrap_key, wrap_key) // Never do this kids !
|
||||
const unbox = await box.decrypt(wrap_k2)
|
||||
const box = await SecretWrap.wrap(wrap_key, wrap_key) // Never do this kids !
|
||||
const unbox = await box.unwrap(wrap_k2)
|
||||
expect(unbox).toBeNull()
|
||||
})
|
||||
test('Cant wrap non-extractable', async () => {
|
||||
|
||||
Reference in New Issue
Block a user