@@ -46,6 +46,7 @@ import (
46
46
"crypto/internal/boring/bbig"
47
47
"crypto/internal/fips140/bigmod"
48
48
"crypto/internal/fips140/rsa"
49
+ "crypto/internal/fips140only"
49
50
"crypto/internal/randutil"
50
51
"crypto/rand"
51
52
"crypto/subtle"
@@ -278,32 +279,8 @@ func GenerateKey(random io.Reader, bits int) (*PrivateKey, error) {
278
279
if err := checkKeySize (bits ); err != nil {
279
280
return nil , err
280
281
}
281
- return GenerateMultiPrimeKey (random , 2 , bits )
282
- }
283
-
284
- // GenerateMultiPrimeKey generates a multi-prime RSA keypair of the given bit
285
- // size and the given random source.
286
- //
287
- // Table 1 in "[On the Security of Multi-prime RSA]" suggests maximum numbers of
288
- // primes for a given bit size.
289
- //
290
- // Although the public keys are compatible (actually, indistinguishable) from
291
- // the 2-prime case, the private keys are not. Thus it may not be possible to
292
- // export multi-prime private keys in certain formats or to subsequently import
293
- // them into other code.
294
- //
295
- // This package does not implement CRT optimizations for multi-prime RSA, so the
296
- // keys with more than two primes will have worse performance.
297
- //
298
- // Deprecated: The use of this function with a number of primes different from
299
- // two is not recommended for the above security, compatibility, and performance
300
- // reasons. Use [GenerateKey] instead.
301
- //
302
- // [On the Security of Multi-prime RSA]: http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf
303
- func GenerateMultiPrimeKey (random io.Reader , nprimes int , bits int ) (* PrivateKey , error ) {
304
- randutil .MaybeReadByte (random )
305
282
306
- if boring .Enabled && random == boring .RandReader && nprimes == 2 &&
283
+ if boring .Enabled && random == boring .RandReader &&
307
284
(bits == 2048 || bits == 3072 || bits == 4096 ) {
308
285
bN , bE , bD , bP , bQ , bDp , bDq , bQinv , err := boring .GenerateKeyRSA (bits )
309
286
if err != nil {
@@ -339,6 +316,68 @@ func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (*PrivateKey
339
316
return key , nil
340
317
}
341
318
319
+ if fips140only .Enabled && bits < 2048 {
320
+ return nil , errors .New ("crypto/rsa: use of keys smaller than 2048 bits is not allowed in FIPS 140-only mode" )
321
+ }
322
+ if fips140only .Enabled && bits > 16384 {
323
+ return nil , errors .New ("crypto/rsa: use of keys larger than 16384 bits is not allowed in FIPS 140-only mode" )
324
+ }
325
+
326
+ k , err := rsa .GenerateKey (random , bits )
327
+ if err != nil {
328
+ return nil , err
329
+ }
330
+ N , e , d , p , q , dP , dQ , qInv := k .Export ()
331
+ key := & PrivateKey {
332
+ PublicKey : PublicKey {
333
+ N : new (big.Int ).SetBytes (N ),
334
+ E : e ,
335
+ },
336
+ D : new (big.Int ).SetBytes (d ),
337
+ Primes : []* big.Int {
338
+ new (big.Int ).SetBytes (p ),
339
+ new (big.Int ).SetBytes (q ),
340
+ },
341
+ Precomputed : PrecomputedValues {
342
+ fips : k ,
343
+ Dp : new (big.Int ).SetBytes (dP ),
344
+ Dq : new (big.Int ).SetBytes (dQ ),
345
+ Qinv : new (big.Int ).SetBytes (qInv ),
346
+ CRTValues : make ([]CRTValue , 0 ), // non-nil, to match Precompute
347
+ },
348
+ }
349
+ return key , nil
350
+ }
351
+
352
+ // GenerateMultiPrimeKey generates a multi-prime RSA keypair of the given bit
353
+ // size and the given random source.
354
+ //
355
+ // Table 1 in "[On the Security of Multi-prime RSA]" suggests maximum numbers of
356
+ // primes for a given bit size.
357
+ //
358
+ // Although the public keys are compatible (actually, indistinguishable) from
359
+ // the 2-prime case, the private keys are not. Thus it may not be possible to
360
+ // export multi-prime private keys in certain formats or to subsequently import
361
+ // them into other code.
362
+ //
363
+ // This package does not implement CRT optimizations for multi-prime RSA, so the
364
+ // keys with more than two primes will have worse performance.
365
+ //
366
+ // Deprecated: The use of this function with a number of primes different from
367
+ // two is not recommended for the above security, compatibility, and performance
368
+ // reasons. Use [GenerateKey] instead.
369
+ //
370
+ // [On the Security of Multi-prime RSA]: http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf
371
+ func GenerateMultiPrimeKey (random io.Reader , nprimes int , bits int ) (* PrivateKey , error ) {
372
+ if nprimes == 2 {
373
+ return GenerateKey (random , bits )
374
+ }
375
+ if fips140only .Enabled {
376
+ return nil , errors .New ("crypto/rsa: multi-prime RSA is not allowed in FIPS 140-only mode" )
377
+ }
378
+
379
+ randutil .MaybeReadByte (random )
380
+
342
381
priv := new (PrivateKey )
343
382
priv .E = 65537
344
383
0 commit comments