Skip to content

Commit c0e70de

Browse files
committed
Moving sjcl_random preparation to a function.
1 parent f5d6cc9 commit c0e70de

11 files changed

+98
-107
lines changed

package-lock.json

+49-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
"prettier": "3.3.3",
5454
"rimraf": "6.0.1",
5555
"typescript": "5.6.2",
56-
"typescript-eslint": "8.7.0"
56+
"typescript-eslint": "8.8.0"
5757
}
5858
}

sjcl.Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ ${SJCL_OUTPUT_PATH}/index.d.ts:
2424
npm un -D @types/sjcl
2525

2626
clean:
27-
rm -f ${SJCL_OUTPUT_PATH}/index.d.ts ${SJCL_OUTPUT_PATH}/index.js
27+
rm -f ${SJCL_OUTPUT_PATH}/index.js ${SJCL_OUTPUT_PATH}/index.d.ts

src/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,10 @@ export function getSuiteByName<T>(
130130
new (params: BlindRSAParams & BlindRSAPlatformParams): T;
131131
},
132132
name: string,
133-
params?: BlindRSAPlatformParams,
133+
params: BlindRSAPlatformParams = { supportsRSARAW: false },
134134
): T {
135135
for (const suiteParams of Object.values(Params)) {
136136
if (name.toLowerCase() === suiteParams.name.toLowerCase()) {
137-
if (!params) {
138-
params = { supportsRSARAW: false };
139-
}
140-
141137
return new newT({ ...suiteParams, ...params });
142138
}
143139
}

src/partially_blindrsa.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import {
1515
random_integer_uniform,
1616
rsasp1,
1717
rsavp1,
18+
inverseMod,
19+
rsaRawBlingSign,
20+
prepare_sjcl_random_generator,
1821
type BigPublicKey,
1922
type BigSecretKey,
2023
type BigKeyPair,
21-
inverseMod,
22-
rsaRawBlingSign,
2324
} from './util.js';
2425
import { PrepareType, type BlindRSAParams, type BlindRSAPlatformParams } from './blindrsa.js';
2526

@@ -282,11 +283,7 @@ export class PartiallyBlindRSA {
282283
algorithm: Pick<RsaHashedKeyGenParams, 'modulusLength' | 'publicExponent' | 'hash'>,
283284
generateSafePrimeSync: (length: number) => sjcl.BigNumber | bigint = generateSafePrime,
284285
): Promise<CryptoKeyPair> {
285-
// It requires to seed the internal random number generator.
286-
while (!sjcl.random.isReady(undefined)) {
287-
const buffer = crypto.getRandomValues(new Uint32Array(4));
288-
sjcl.random.addEntropy(Array.from(buffer), 128, 'undefined');
289-
}
286+
prepare_sjcl_random_generator();
290287

291288
// 1. p = SafePrime(bits / 2)
292289
// 2. q = SafePrime(bits / 2)

src/util.ts

+8
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,11 @@ export type BigSecretKey = {
346346
};
347347

348348
export type BigKeyPair = { publicKey: BigPublicKey; secretKey: BigSecretKey };
349+
350+
export function prepare_sjcl_random_generator() {
351+
// It requires to seed the internal random number generator.
352+
const source = 'crypto.getRandomValues';
353+
while (!sjcl.random.isReady(undefined)) {
354+
sjcl.random.addEntropy(Array.from(crypto.getRandomValues(new Uint32Array(4))), 128, source);
355+
}
356+
}

test/blindrsa.test.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
33

44
import { jest } from '@jest/globals';
5-
import sjcl from '../src/sjcl/index.js';
65

6+
import sjcl from '../src/sjcl/index.js';
77
import { i2osp } from '../src/util.js';
88
import { BlindRSA, RSABSSA, getSuiteByName } from '../src/index.js';
99

@@ -131,33 +131,35 @@ describe.each(vectors)('TestVectors', (v: Vector) => {
131131
.mockReturnValueOnce(rBytes); // mock for random blind
132132
});
133133

134-
const params = [[], [{ supportsRSARAW: true }]];
134+
const params = [undefined, { supportsRSARAW: true }];
135135

136-
test.each(params)(
137-
`_${v.name}`,
138-
async (...params) => {
139-
const blindRSA = getSuiteByName(BlindRSA, v.name, ...params);
140-
expect(blindRSA.toString()).toBe(v.name);
136+
describe.each(params)(`_${v.name}`, (params) => {
137+
test(
138+
`supportsRSARAW/${params ? params.supportsRSARAW : false}`,
139+
async () => {
140+
const blindRSA = getSuiteByName(BlindRSA, v.name, params);
141+
expect(blindRSA.toString()).toBe(v.name);
141142

142-
const msg = hexToUint8(v.msg);
143-
const inputMsg = blindRSA.prepare(msg);
144-
expect(uint8ToHex(inputMsg)).toBe(v.input_msg);
143+
const msg = hexToUint8(v.msg);
144+
const inputMsg = blindRSA.prepare(msg);
145+
expect(uint8ToHex(inputMsg)).toBe(v.input_msg);
145146

146-
const { publicKey, privateKey } = await keysFromVector(v, true);
147+
const { publicKey, privateKey } = await keysFromVector(v, true);
147148

148-
const { blindedMsg, inv } = await blindRSA.blind(publicKey, inputMsg);
149-
expect(uint8ToHex(blindedMsg)).toBe(v.blinded_msg);
150-
expect(uint8ToHex(inv)).toBe(v.inv.slice(2));
149+
const { blindedMsg, inv } = await blindRSA.blind(publicKey, inputMsg);
150+
expect(uint8ToHex(blindedMsg)).toBe(v.blinded_msg);
151+
expect(uint8ToHex(inv)).toBe(v.inv.slice(2));
151152

152-
const blindedSig = await blindRSA.blindSign(privateKey, blindedMsg);
153-
expect(uint8ToHex(blindedSig)).toBe(v.blind_sig);
153+
const blindedSig = await blindRSA.blindSign(privateKey, blindedMsg);
154+
expect(uint8ToHex(blindedSig)).toBe(v.blind_sig);
154155

155-
const signature = await blindRSA.finalize(publicKey, inputMsg, blindedSig, inv);
156-
expect(uint8ToHex(signature)).toBe(v.sig);
156+
const signature = await blindRSA.finalize(publicKey, inputMsg, blindedSig, inv);
157+
expect(uint8ToHex(signature)).toBe(v.sig);
157158

158-
const isValid = await blindRSA.verify(publicKey, signature, inputMsg);
159-
expect(isValid).toBe(true);
160-
},
161-
20 * 1000,
162-
);
159+
const isValid = await blindRSA.verify(publicKey, signature, inputMsg);
160+
expect(isValid).toBe(true);
161+
},
162+
20 * 1000,
163+
);
164+
});
163165
});

test/misc.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright (c) 2023 Cloudflare, Inc.
22
// Licensed under the Apache-2.0 license found in the LICENSE file or at https://opensource.org/licenses/Apache-2.0
33

4-
import sjcl from '../src/sjcl/index.js';
54
import { jest } from '@jest/globals';
65

76
import { emsa_pss_encode, is_coprime, random_integer_uniform } from '../src/util.js';
7+
import sjcl from '../src/sjcl/index.js';
8+
89
// Test vector in file pss_test.go from: https://cs.opensource.google/go/go/+/refs/tags/go1.18.2:src/crypto/rsa/pss_test.go
910
// Test vector in file pss-int.txt from: ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1-vec.zip
1011
import vector from './testdata/emsa_pss_vectors.json';

test/partially_blindrsa.test.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sjcl from '../src/sjcl/index.js';
55
import { jest } from '@jest/globals';
66

7-
import { i2osp } from '../src/util.js';
7+
import { i2osp, prepare_sjcl_random_generator } from '../src/util.js';
88
import { PartiallyBlindRSA, RSAPBSSA, getSuiteByName } from '../src/index.js';
99
import { isSafePrime } from '../src/prime.js';
1010

@@ -125,15 +125,7 @@ describe.each(vectors)('Errors-vec$#', (v: Vector) => {
125125
});
126126

127127
test.each(vectors)('TestVector_$#/safePrimes', (v: Vector) => {
128-
// It requires to seed the internal random number generator.
129-
while (!sjcl.random.isReady(undefined)) {
130-
sjcl.random.addEntropy(
131-
Array.from(crypto.getRandomValues(new Uint32Array(4))),
132-
128,
133-
'undefined',
134-
);
135-
}
136-
128+
prepare_sjcl_random_generator();
137129
expect(isSafePrime(new sjcl.bn(v.p))).toBe(true);
138130
expect(isSafePrime(new sjcl.bn(v.q))).toBe(true);
139131
});

test/primes.test.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { jest } from '@jest/globals';
55

66
import sjcl from '../src/sjcl/index.js';
7+
import { prepare_sjcl_random_generator } from '../src/util.js';
78

89
import { generatePrime, generateSafePrime, isPrime, isSafePrime } from '../src/prime.js';
910

@@ -59,16 +60,7 @@ const SAFE_PRIMES = [
5960
'0x9f62917a38e8136a8d942aa6854637800713ad3bd0b58d971910c5c233',
6061
];
6162

62-
beforeEach(() => {
63-
// It requires to seed the internal random number generator.
64-
while (!sjcl.random.isReady(undefined)) {
65-
sjcl.random.addEntropy(
66-
Array.from(crypto.getRandomValues(new Uint32Array(4))),
67-
128,
68-
'undefined',
69-
);
70-
}
71-
});
63+
beforeEach(prepare_sjcl_random_generator);
7264

7365
test.each(PRIME)('isPrime/%#', (p) => {
7466
expect(isPrime(new sjcl.bn(p))).toBe(true);

0 commit comments

Comments
 (0)