1
2
3
4
5 package rsa
6
7 import (
8 "crypto/internal/fips140"
9 "crypto/internal/fips140/bigmod"
10 "crypto/internal/fips140/drbg"
11 "errors"
12 "io"
13 )
14
15
16
17
18
19
20
21
22
23
24 func GenerateKey(rand io.Reader, bits int) (*PrivateKey, error) {
25 if bits < 32 {
26 return nil, errors.New("rsa: key too small")
27 }
28 fips140.RecordApproved()
29 if bits < 2048 || bits%2 == 1 {
30 fips140.RecordNonApproved()
31 }
32
33 for {
34 p, err := randomPrime(rand, (bits+1)/2)
35 if err != nil {
36 return nil, err
37 }
38 q, err := randomPrime(rand, bits/2)
39 if err != nil {
40 return nil, err
41 }
42
43 P, err := bigmod.NewModulus(p)
44 if err != nil {
45 return nil, err
46 }
47 Q, err := bigmod.NewModulus(q)
48 if err != nil {
49 return nil, err
50 }
51
52 if Q.Nat().ExpandFor(P).Equal(P.Nat()) == 1 {
53 return nil, errors.New("rsa: generated p == q, random source is broken")
54 }
55
56 N, err := bigmod.NewModulusProduct(p, q)
57 if err != nil {
58 return nil, err
59 }
60 if N.BitLen() != bits {
61 return nil, errors.New("rsa: internal error: modulus size incorrect")
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76 λ, err := totient(P, Q)
77 if err == errDivisorTooLarge {
78
79 continue
80 }
81 if err != nil {
82 return nil, err
83 }
84
85 e := bigmod.NewNat().SetUint(65537)
86 d, ok := bigmod.NewNat().InverseVarTime(e, λ)
87 if !ok {
88
89
90
91
92
93
94
95 continue
96 }
97
98 if e.ExpandFor(λ).Mul(d, λ).IsOne() == 0 {
99 return nil, errors.New("rsa: internal error: e*d != 1 mod λ(N)")
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 k, err := newPrivateKey(N, 65537, d, P, Q)
116 if err != nil {
117 return nil, err
118 }
119
120 if k.fipsApproved {
121 fips140.PCT("RSA sign and verify PCT", func() error {
122 hash := []byte{
123 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
124 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
125 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
126 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
127 }
128 sig, err := signPKCS1v15(k, "SHA-256", hash)
129 if err != nil {
130 return err
131 }
132 return verifyPKCS1v15(k.PublicKey(), "SHA-256", hash, sig)
133 })
134 }
135
136 return k, nil
137 }
138 }
139
140
141 var errDivisorTooLarge = errors.New("divisor too large")
142
143
144
145
146
147
148
149 func totient(p, q *bigmod.Modulus) (*bigmod.Modulus, error) {
150 a, b := p.Nat().SubOne(p), q.Nat().SubOne(q)
151 if a.IsOdd() == 1 || b.IsOdd() == 1 {
152 return nil, errors.New("rsa: internal error: p and q must be 7 mod 8")
153 }
154
155
156
157
158
159
160
161
162 b = b.ShiftRightByOne()
163 if b.IsOdd() == 0 {
164 return nil, errors.New("rsa: internal error: p and q must be 7 mod 8")
165 }
166
167 gcd, err := bigmod.NewNat().GCDVarTime(a, b)
168 if err != nil {
169 return nil, err
170 }
171 if gcd.IsOdd() == 0 {
172 return nil, errors.New("rsa: internal error: gcd(a, b) is even")
173 }
174
175
176
177
178
179
180
181 if gcd.BitLenVarTime()+1 > 32 {
182 return nil, errDivisorTooLarge
183 }
184 if gcd.IsZero() == 1 || gcd.Bits()[0] == 0 {
185 return nil, errors.New("rsa: internal error: gcd(a, b) is zero")
186 }
187 if rem := b.DivShortVarTime(gcd.Bits()[0]); rem != 0 {
188 return nil, errors.New("rsa: internal error: b is not divisible by gcd(a, b)")
189 }
190
191 return bigmod.NewModulusProduct(a.Bytes(p), b.Bytes(q))
192 }
193
194
195
196 func randomPrime(rand io.Reader, bits int) ([]byte, error) {
197 if bits < 16 {
198 return nil, errors.New("rsa: prime size must be at least 16 bits")
199 }
200
201 b := make([]byte, (bits+7)/8)
202 for {
203 if err := drbg.ReadWithReader(rand, b); err != nil {
204 return nil, err
205 }
206
207
208
209 excess := len(b)*8 - bits
210 b[0] &= 0b1111_1111 >> excess
211
212
213
214
215
216 if excess < 7 {
217 b[0] |= 0b1100_0000 >> excess
218 } else {
219 b[0] |= 0b0000_0001
220 b[1] |= 0b1000_0000
221 }
222
223
224
225
226 b[len(b)-1] |= 0b0000_0111
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 if isPrime(b) {
245 return b, nil
246 }
247 }
248 }
249
250
251
252
253
254
255
256
257 func isPrime(w []byte) bool {
258 mr, err := millerRabinSetup(w)
259 if err != nil {
260
261 return false
262 }
263
264
265 for i := 0; i < len(primes); i += 3 {
266 p1, p2, p3 := primes[i], primes[i+1], primes[i+2]
267 r := mr.w.Nat().DivShortVarTime(p1 * p2 * p3)
268 if r%p1 == 0 || r%p2 == 0 || r%p3 == 0 {
269 return false
270 }
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 bits := mr.w.BitLen()
287 var iterations int
288 switch {
289 case bits >= 3747:
290 iterations = 3
291 case bits >= 1345:
292 iterations = 4
293 case bits >= 476:
294 iterations = 5
295 case bits >= 400:
296 iterations = 6
297 case bits >= 347:
298 iterations = 7
299 case bits >= 308:
300 iterations = 8
301 case bits >= 55:
302 iterations = 27
303 default:
304 iterations = 34
305 }
306
307 b := make([]byte, (bits+7)/8)
308 for {
309 drbg.Read(b)
310 excess := len(b)*8 - bits
311 b[0] &= 0b1111_1111 >> excess
312 result, err := millerRabinIteration(mr, b)
313 if err != nil {
314
315 continue
316 }
317 if result == millerRabinCOMPOSITE {
318 return false
319 }
320 iterations--
321 if iterations == 0 {
322 return true
323 }
324 }
325 }
326
327
328
329
330
331
332
333
334
335
336 var primes = []uint{
337 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
338 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
339 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
340 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
341 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
342 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467,
343 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
344 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
345 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
346 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877,
347 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
348 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069,
349 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181,
350 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283,
351 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
352 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487,
353 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583,
354 1597, 1601, 1607, 1609, 1613, 1619,
355 }
356
357 type millerRabin struct {
358 w *bigmod.Modulus
359 m []byte
360 }
361
362
363
364
365
366 func millerRabinSetup(w []byte) (*millerRabin, error) {
367 mr := &millerRabin{}
368
369
370 if len(w) == 0 || w[len(w)-1]&0b11 != 0b11 {
371 return nil, errors.New("candidate is not 3 mod 4")
372 }
373 wm, err := bigmod.NewModulus(w)
374 if err != nil {
375 return nil, err
376 }
377 mr.w = wm
378
379
380
381 m := mr.w.Nat().SubOne(mr.w).ShiftRightByOne()
382
383
384
385 mr.m = m.Bytes(mr.w)
386 for mr.m[0] == 0 {
387 mr.m = mr.m[1:]
388 }
389
390 return mr, nil
391 }
392
393 const millerRabinCOMPOSITE = false
394 const millerRabinPOSSIBLYPRIME = true
395
396 func millerRabinIteration(mr *millerRabin, bb []byte) (bool, error) {
397
398 if len(bb) != (mr.w.BitLen()+7)/8 {
399 return false, errors.New("incorrect length")
400 }
401 b := bigmod.NewNat()
402 if _, err := b.SetBytes(bb, mr.w); err != nil {
403 return false, err
404 }
405 if b.IsZero() == 1 || b.IsOne() == 1 || b.IsMinusOne(mr.w) == 1 {
406 return false, errors.New("out-of-range candidate")
407 }
408
409
410
411
412
413
414
415 z := bigmod.NewNat().Exp(b, mr.m, mr.w)
416 if z.IsOne() == 1 || z.IsMinusOne(mr.w) == 1 {
417 return millerRabinPOSSIBLYPRIME, nil
418 }
419
420 return millerRabinCOMPOSITE, nil
421 }
422
View as plain text