1
2
3
4
5 package rsa
6
7
8
9
10 import (
11 "bytes"
12 "crypto/internal/constanttime"
13 "crypto/internal/fips140"
14 "crypto/internal/fips140/drbg"
15 "crypto/internal/fips140/subtle"
16 "errors"
17 "hash"
18 "io"
19 )
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 func incCounter(c *[4]byte) {
36 if c[3]++; c[3] != 0 {
37 return
38 }
39 if c[2]++; c[2] != 0 {
40 return
41 }
42 if c[1]++; c[1] != 0 {
43 return
44 }
45 c[0]++
46 }
47
48
49
50 func mgf1XOR(out []byte, hash hash.Hash, seed []byte) {
51 var counter [4]byte
52 var digest []byte
53
54 done := 0
55 for done < len(out) {
56 hash.Reset()
57 hash.Write(seed)
58 hash.Write(counter[0:4])
59 digest = hash.Sum(digest[:0])
60
61 for i := 0; i < len(digest) && done < len(out); i++ {
62 out[done] ^= digest[i]
63 done++
64 }
65 incCounter(&counter)
66 }
67 }
68
69 func emsaPSSEncode(mHash []byte, emBits int, salt []byte, hash hash.Hash) ([]byte, error) {
70
71
72 hLen := hash.Size()
73 sLen := len(salt)
74 emLen := (emBits + 7) / 8
75
76
77
78
79
80
81
82 if len(mHash) != hLen {
83 return nil, errors.New("crypto/rsa: input must be hashed with given hash")
84 }
85
86
87
88 if emLen < hLen+sLen+2 {
89 return nil, ErrMessageTooLong
90 }
91
92 em := make([]byte, emLen)
93 psLen := emLen - sLen - hLen - 2
94 db := em[:psLen+1+sLen]
95 h := em[psLen+1+sLen : emLen-1]
96
97
98
99
100
101
102
103
104
105
106
107
108 var prefix [8]byte
109
110 hash.Reset()
111 hash.Write(prefix[:])
112 hash.Write(mHash)
113 hash.Write(salt)
114
115 h = hash.Sum(h[:0])
116
117
118
119
120
121
122
123 db[psLen] = 0x01
124 copy(db[psLen+1:], salt)
125
126
127
128
129
130 mgf1XOR(db, hash, h)
131
132
133
134
135 db[0] &= 0xff >> (8*emLen - emBits)
136
137
138 em[emLen-1] = 0xbc
139
140
141 return em, nil
142 }
143
144 const pssSaltLengthAutodetect = -1
145
146 func emsaPSSVerify(mHash, em []byte, emBits, sLen int, hash hash.Hash) error {
147
148
149 hLen := hash.Size()
150 emLen := (emBits + 7) / 8
151 if emLen != len(em) {
152 return errors.New("rsa: internal error: inconsistent length")
153 }
154
155
156
157
158
159
160 if hLen != len(mHash) {
161 return ErrVerification
162 }
163
164
165 if emLen < hLen+sLen+2 {
166 return ErrVerification
167 }
168
169
170
171 if em[emLen-1] != 0xbc {
172 return ErrVerification
173 }
174
175
176
177 db := em[:emLen-hLen-1]
178 h := em[emLen-hLen-1 : emLen-1]
179
180
181
182
183 var bitMask byte = 0xff >> (8*emLen - emBits)
184 if em[0] & ^bitMask != 0 {
185 return ErrVerification
186 }
187
188
189
190
191 mgf1XOR(db, hash, h)
192
193
194
195 db[0] &= bitMask
196
197
198 if sLen == pssSaltLengthAutodetect {
199 psLen := bytes.IndexByte(db, 0x01)
200 if psLen < 0 {
201 return ErrVerification
202 }
203 sLen = len(db) - psLen - 1
204 }
205
206
207
208 if sLen > hLen {
209 fips140.RecordNonApproved()
210 }
211
212
213
214
215
216 psLen := emLen - hLen - sLen - 2
217 for _, e := range db[:psLen] {
218 if e != 0x00 {
219 return ErrVerification
220 }
221 }
222 if db[psLen] != 0x01 {
223 return ErrVerification
224 }
225
226
227 salt := db[len(db)-sLen:]
228
229
230
231
232
233
234
235 hash.Reset()
236 var prefix [8]byte
237 hash.Write(prefix[:])
238 hash.Write(mHash)
239 hash.Write(salt)
240
241 h0 := hash.Sum(nil)
242
243
244 if !bytes.Equal(h0, h) {
245 return ErrVerification
246 }
247 return nil
248 }
249
250
251
252 func PSSMaxSaltLength(pub *PublicKey, hash hash.Hash) (int, error) {
253 saltLength := (pub.N.BitLen()-1+7)/8 - 2 - hash.Size()
254 if saltLength < 0 {
255 return 0, ErrMessageTooLong
256 }
257
258
259 if fips140.Enabled && saltLength > hash.Size() {
260 return hash.Size(), nil
261 }
262 return saltLength, nil
263 }
264
265
266 func SignPSS(rand io.Reader, priv *PrivateKey, hash hash.Hash, hashed []byte, saltLength int) ([]byte, error) {
267 fipsSelfTest()
268 fips140.RecordApproved()
269 checkApprovedHash(hash)
270
271
272
273
274
275
276
277 if saltLength < 0 {
278 return nil, errors.New("crypto/rsa: salt length cannot be negative")
279 }
280
281
282 if saltLength > hash.Size() {
283 fips140.RecordNonApproved()
284 }
285 salt := make([]byte, saltLength)
286 if err := drbg.ReadWithReader(rand, salt); err != nil {
287 return nil, err
288 }
289
290 emBits := priv.pub.N.BitLen() - 1
291 em, err := emsaPSSEncode(hashed, emBits, salt, hash)
292 if err != nil {
293 return nil, err
294 }
295
296
297
298
299
300
301
302
303 if emLen, k := len(em), priv.pub.Size(); emLen < k {
304 emNew := make([]byte, k)
305 copy(emNew[k-emLen:], em)
306 em = emNew
307 }
308
309 return decrypt(priv, em, withCheck)
310 }
311
312
313 func VerifyPSS(pub *PublicKey, hash hash.Hash, digest []byte, sig []byte) error {
314 return verifyPSS(pub, hash, digest, sig, pssSaltLengthAutodetect)
315 }
316
317
318 func VerifyPSSWithSaltLength(pub *PublicKey, hash hash.Hash, digest []byte, sig []byte, saltLength int) error {
319 if saltLength < 0 {
320 return errors.New("crypto/rsa: salt length cannot be negative")
321 }
322 return verifyPSS(pub, hash, digest, sig, saltLength)
323 }
324
325 func verifyPSS(pub *PublicKey, hash hash.Hash, digest []byte, sig []byte, saltLength int) error {
326 fipsSelfTest()
327 fips140.RecordApproved()
328 checkApprovedHash(hash)
329 if fipsApproved, err := checkPublicKey(pub); err != nil {
330 return err
331 } else if !fipsApproved {
332 fips140.RecordNonApproved()
333 }
334
335 if len(sig) != pub.Size() {
336 return ErrVerification
337 }
338
339 emBits := pub.N.BitLen() - 1
340 emLen := (emBits + 7) / 8
341 em, err := encrypt(pub, sig)
342 if err != nil {
343 return ErrVerification
344 }
345
346
347
348
349
350
351 for len(em) > emLen && len(em) > 0 {
352 if em[0] != 0 {
353 return ErrVerification
354 }
355 em = em[1:]
356 }
357
358 return emsaPSSVerify(digest, em, emBits, saltLength, hash)
359 }
360
361 func checkApprovedHash(hash hash.Hash) {
362 if !fips140.ApprovedHash(hash) {
363 fips140.RecordNonApproved()
364 }
365 }
366
367
368 func EncryptOAEP(hash, mgfHash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) ([]byte, error) {
369
370
371
372
373
374
375 fipsSelfTest()
376 fips140.RecordApproved()
377 checkApprovedHash(hash)
378 checkApprovedHash(mgfHash)
379 if fipsApproved, err := checkPublicKey(pub); err != nil {
380 return nil, err
381 } else if !fipsApproved {
382 fips140.RecordNonApproved()
383 }
384 k := pub.Size()
385 if len(msg) > k-2*hash.Size()-2 {
386 return nil, ErrMessageTooLong
387 }
388
389 hash.Reset()
390 hash.Write(label)
391 lHash := hash.Sum(nil)
392
393 em := make([]byte, k)
394 seed := em[1 : 1+hash.Size()]
395 db := em[1+hash.Size():]
396
397 copy(db[0:hash.Size()], lHash)
398 db[len(db)-len(msg)-1] = 1
399 copy(db[len(db)-len(msg):], msg)
400
401 if err := drbg.ReadWithReader(random, seed); err != nil {
402 return nil, err
403 }
404
405 mgf1XOR(db, mgfHash, seed)
406 mgf1XOR(seed, mgfHash, db)
407
408 return encrypt(pub, em)
409 }
410
411
412 func DecryptOAEP(hash, mgfHash hash.Hash, priv *PrivateKey, ciphertext []byte, label []byte) ([]byte, error) {
413 fipsSelfTest()
414 fips140.RecordApproved()
415 checkApprovedHash(hash)
416 checkApprovedHash(mgfHash)
417
418 k := priv.pub.Size()
419 if len(ciphertext) > k ||
420 k < hash.Size()*2+2 {
421 return nil, ErrDecryption
422 }
423
424 em, err := decrypt(priv, ciphertext, noCheck)
425 if err != nil {
426 return nil, err
427 }
428
429 hash.Reset()
430 hash.Write(label)
431 lHash := hash.Sum(nil)
432
433 firstByteIsZero := constanttime.ByteEq(em[0], 0)
434
435 seed := em[1 : hash.Size()+1]
436 db := em[hash.Size()+1:]
437
438 mgf1XOR(seed, mgfHash, db)
439 mgf1XOR(db, mgfHash, seed)
440
441 lHash2 := db[0:hash.Size()]
442
443
444
445
446
447 lHash2Good := subtle.ConstantTimeCompare(lHash, lHash2)
448
449
450
451
452
453
454 var lookingForIndex, index, invalid int
455 lookingForIndex = 1
456 rest := db[hash.Size():]
457
458 for i := 0; i < len(rest); i++ {
459 equals0 := constanttime.ByteEq(rest[i], 0)
460 equals1 := constanttime.ByteEq(rest[i], 1)
461 index = constanttime.Select(lookingForIndex&equals1, i, index)
462 lookingForIndex = constanttime.Select(equals1, 0, lookingForIndex)
463 invalid = constanttime.Select(lookingForIndex&^equals0, 1, invalid)
464 }
465
466 if firstByteIsZero&lHash2Good&^invalid&^lookingForIndex != 1 {
467 return nil, ErrDecryption
468 }
469
470 return rest[index+1:], nil
471 }
472
View as plain text