Skip to content

Commit 695fc73

Browse files
authored
Include all public values in hash (#144)
* Include all public values in hash * prettier * id -> gid * lint
1 parent ffb04d4 commit 695fc73

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

packages/seal/src/decrypt.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Mysten Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { fromHex } from '@mysten/bcs';
45
import { combine as externalCombine } from 'shamir-secret-sharing';
56

67
import type { EncryptedObject } from './bcs.js';
@@ -59,6 +60,7 @@ export async function decrypt({ encryptedObject, keys }: DecryptOptions): Promis
5960
nonce,
6061
keys.get(`${fullId}:${objectId}`)!,
6162
encryptedShares[i],
63+
fromHex(fullId),
6264
info,
6365
);
6466
// The Shamir secret sharing library expects the index/x-coordinate to be at the end of the share.

packages/seal/src/ibe.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class BonehFranklinBLS12381Services extends IBEServers {
7373
}
7474
const [r, nonce, keys] = encapBatched(this.publicKeys, id);
7575
const encryptedShares = msgAndInfos.map((msgAndInfo, i) =>
76-
xor(msgAndInfo.msg, kdf(keys[i], msgAndInfo.info)),
76+
xor(msgAndInfo.msg, kdf(keys[i], nonce, id, msgAndInfo.info)),
7777
);
7878
const encryptedRandomness = xor(randomnessKey, r.toBytes());
7979

@@ -113,9 +113,10 @@ export class BonehFranklinBLS12381Services extends IBEServers {
113113
nonce: G2Element,
114114
sk: G1Element,
115115
ciphertext: Uint8Array,
116+
id: Uint8Array,
116117
info: Uint8Array,
117118
): Uint8Array {
118-
return xor(ciphertext, kdf(decap(nonce, sk), info));
119+
return xor(ciphertext, kdf(decap(nonce, sk), nonce, id, info));
119120
}
120121
}
121122

packages/seal/src/kdf.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { hkdf } from '@noble/hashes/hkdf';
55
import { hmac } from '@noble/hashes/hmac';
66
import { sha3_256 } from '@noble/hashes/sha3';
77

8-
import type { GTElement } from './bls12381.js';
8+
import { G1Element } from './bls12381.js';
9+
import type { G2Element, GTElement } from './bls12381.js';
910

1011
/**
1112
* The default key derivation function.
@@ -14,7 +15,12 @@ import type { GTElement } from './bls12381.js';
1415
* @param info Optional context and application specific information.
1516
* @returns The derived key.
1617
*/
17-
export function kdf(element: GTElement, info: Uint8Array): Uint8Array {
18+
export function kdf(
19+
element: GTElement,
20+
nonce: G2Element,
21+
id: Uint8Array,
22+
info: Uint8Array,
23+
): Uint8Array {
1824
// This permutation flips the order of 6 pairs of coefficients of the GT element.
1925
// The permutation may be computed as:
2026
// for i in 0..3 {
@@ -34,7 +40,12 @@ export function kdf(element: GTElement, info: Uint8Array): Uint8Array {
3440
pi * COEFFICIENT_SIZE,
3541
);
3642
});
37-
return hkdf(sha3_256, permutedBytes, '', info, 32);
43+
const inputBytes = new Uint8Array([
44+
...permutedBytes,
45+
...nonce.toBytes(),
46+
...G1Element.hashToCurve(id).toBytes(),
47+
]);
48+
return hkdf(sha3_256, inputBytes, '', info, 32);
3849
}
3950

4051
export enum KeyPurpose {

packages/seal/test/unit/encrypt.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,10 @@ describe('Seal encryption tests', () => {
355355
const x = G1Element.generator().pairing(
356356
G2Element.generator().multiply(Scalar.fromNumber(12345)),
357357
);
358-
const key = kdf(x, new Uint8Array([]));
358+
const nonce = G2Element.generator().multiply(Scalar.fromNumber(12345));
359+
const key = kdf(x, nonce, new Uint8Array([0]), new Uint8Array([]));
359360
expect(key).toEqual(
360-
fromHex('55e99a131b254f1687727bbf1f255e73bb80fcfac8901c371e53df32f45c1fb3'),
361+
fromHex('57d43441a0b561088d4162a1b38ea8a2d443dd2c50ec4aca0610a1a79c057f74'),
361362
);
362363
});
363364

0 commit comments

Comments
 (0)