Source file
src/crypto/rsa/pkcs1v15.go
1
2
3
4
5 package rsa
6
7 import (
8 "crypto"
9 "crypto/internal/boring"
10 "crypto/internal/randutil"
11 "crypto/subtle"
12 "errors"
13 "io"
14 )
15
16
17
18
19
20 type PKCS1v15DecryptOptions struct {
21
22
23
24
25 SessionKeyLen int
26 }
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 func EncryptPKCS1v15(random io.Reader, pub *PublicKey, msg []byte) ([]byte, error) {
42 randutil.MaybeReadByte(random)
43
44 if err := checkPub(pub); err != nil {
45 return nil, err
46 }
47 k := pub.Size()
48 if len(msg) > k-11 {
49 return nil, ErrMessageTooLong
50 }
51
52 if boring.Enabled && random == boring.RandReader {
53 bkey, err := boringPublicKey(pub)
54 if err != nil {
55 return nil, err
56 }
57 return boring.EncryptRSAPKCS1(bkey, msg)
58 }
59 boring.UnreachableExceptTests()
60
61
62 em := make([]byte, k)
63 em[1] = 2
64 ps, mm := em[2:len(em)-len(msg)-1], em[len(em)-len(msg):]
65 err := nonZeroRandomBytes(ps, random)
66 if err != nil {
67 return nil, err
68 }
69 em[len(em)-len(msg)-1] = 0
70 copy(mm, msg)
71
72 if boring.Enabled {
73 var bkey *boring.PublicKeyRSA
74 bkey, err = boringPublicKey(pub)
75 if err != nil {
76 return nil, err
77 }
78 return boring.EncryptRSANoPadding(bkey, em)
79 }
80
81 return encrypt(pub, em)
82 }
83
84
85
86
87
88
89
90
91
92 func DecryptPKCS1v15(random io.Reader, priv *PrivateKey, ciphertext []byte) ([]byte, error) {
93 if err := checkPub(&priv.PublicKey); err != nil {
94 return nil, err
95 }
96
97 if boring.Enabled {
98 bkey, err := boringPrivateKey(priv)
99 if err != nil {
100 return nil, err
101 }
102 out, err := boring.DecryptRSAPKCS1(bkey, ciphertext)
103 if err != nil {
104 return nil, ErrDecryption
105 }
106 return out, nil
107 }
108
109 valid, out, index, err := decryptPKCS1v15(priv, ciphertext)
110 if err != nil {
111 return nil, err
112 }
113 if valid == 0 {
114 return nil, ErrDecryption
115 }
116 return out[index:], nil
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 func DecryptPKCS1v15SessionKey(random io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) error {
154 if err := checkPub(&priv.PublicKey); err != nil {
155 return err
156 }
157 k := priv.Size()
158 if k-(len(key)+3+8) < 0 {
159 return ErrDecryption
160 }
161
162 valid, em, index, err := decryptPKCS1v15(priv, ciphertext)
163 if err != nil {
164 return err
165 }
166
167 if len(em) != k {
168
169
170 return ErrDecryption
171 }
172
173 valid &= subtle.ConstantTimeEq(int32(len(em)-index), int32(len(key)))
174 subtle.ConstantTimeCopy(valid, key, em[len(em)-len(key):])
175 return nil
176 }
177
178
179
180
181
182
183
184 func decryptPKCS1v15(priv *PrivateKey, ciphertext []byte) (valid int, em []byte, index int, err error) {
185 k := priv.Size()
186 if k < 11 {
187 err = ErrDecryption
188 return
189 }
190
191 if boring.Enabled {
192 var bkey *boring.PrivateKeyRSA
193 bkey, err = boringPrivateKey(priv)
194 if err != nil {
195 return
196 }
197 em, err = boring.DecryptRSANoPadding(bkey, ciphertext)
198 if err != nil {
199 return
200 }
201 } else {
202 em, err = decrypt(priv, ciphertext, noCheck)
203 if err != nil {
204 return
205 }
206 }
207
208 firstByteIsZero := subtle.ConstantTimeByteEq(em[0], 0)
209 secondByteIsTwo := subtle.ConstantTimeByteEq(em[1], 2)
210
211
212
213
214
215 lookingForIndex := 1
216
217 for i := 2; i < len(em); i++ {
218 equals0 := subtle.ConstantTimeByteEq(em[i], 0)
219 index = subtle.ConstantTimeSelect(lookingForIndex&equals0, i, index)
220 lookingForIndex = subtle.ConstantTimeSelect(equals0, 0, lookingForIndex)
221 }
222
223
224
225 validPS := subtle.ConstantTimeLessOrEq(2+8, index)
226
227 valid = firstByteIsZero & secondByteIsTwo & (^lookingForIndex & 1) & validPS
228 index = subtle.ConstantTimeSelect(valid, index+1, 0)
229 return valid, em, index, nil
230 }
231
232
233 func nonZeroRandomBytes(s []byte, random io.Reader) (err error) {
234 _, err = io.ReadFull(random, s)
235 if err != nil {
236 return
237 }
238
239 for i := 0; i < len(s); i++ {
240 for s[i] == 0 {
241 _, err = io.ReadFull(random, s[i:i+1])
242 if err != nil {
243 return
244 }
245
246
247 s[i] ^= 0x42
248 }
249 }
250
251 return
252 }
253
254
255
256
257
258
259
260
261
262
263
264 var hashPrefixes = map[crypto.Hash][]byte{
265 crypto.MD5: {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
266 crypto.SHA1: {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14},
267 crypto.SHA224: {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
268 crypto.SHA256: {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
269 crypto.SHA384: {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
270 crypto.SHA512: {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
271 crypto.MD5SHA1: {},
272 crypto.RIPEMD160: {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31, 0x04, 0x14},
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287 func SignPKCS1v15(random io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
288 hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
289 if err != nil {
290 return nil, err
291 }
292
293 tLen := len(prefix) + hashLen
294 k := priv.Size()
295 if k < tLen+11 {
296 return nil, ErrMessageTooLong
297 }
298
299 if boring.Enabled {
300 bkey, err := boringPrivateKey(priv)
301 if err != nil {
302 return nil, err
303 }
304 return boring.SignRSAPKCS1v15(bkey, hash, hashed)
305 }
306
307
308 em := make([]byte, k)
309 em[1] = 1
310 for i := 2; i < k-tLen-1; i++ {
311 em[i] = 0xff
312 }
313 copy(em[k-tLen:k-hashLen], prefix)
314 copy(em[k-hashLen:k], hashed)
315
316 return decrypt(priv, em, withCheck)
317 }
318
319
320
321
322
323
324 func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
325 if boring.Enabled {
326 bkey, err := boringPublicKey(pub)
327 if err != nil {
328 return err
329 }
330 if err := boring.VerifyRSAPKCS1v15(bkey, hash, hashed, sig); err != nil {
331 return ErrVerification
332 }
333 return nil
334 }
335
336 hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
337 if err != nil {
338 return err
339 }
340
341 tLen := len(prefix) + hashLen
342 k := pub.Size()
343 if k < tLen+11 {
344 return ErrVerification
345 }
346
347
348
349
350 if k != len(sig) {
351 return ErrVerification
352 }
353
354 em, err := encrypt(pub, sig)
355 if err != nil {
356 return ErrVerification
357 }
358
359
360 ok := subtle.ConstantTimeByteEq(em[0], 0)
361 ok &= subtle.ConstantTimeByteEq(em[1], 1)
362 ok &= subtle.ConstantTimeCompare(em[k-hashLen:k], hashed)
363 ok &= subtle.ConstantTimeCompare(em[k-tLen:k-hashLen], prefix)
364 ok &= subtle.ConstantTimeByteEq(em[k-tLen-1], 0)
365
366 for i := 2; i < k-tLen-1; i++ {
367 ok &= subtle.ConstantTimeByteEq(em[i], 0xff)
368 }
369
370 if ok != 1 {
371 return ErrVerification
372 }
373
374 return nil
375 }
376
377 func pkcs1v15HashInfo(hash crypto.Hash, inLen int) (hashLen int, prefix []byte, err error) {
378
379
380 if hash == 0 {
381 return inLen, nil, nil
382 }
383
384 hashLen = hash.Size()
385 if inLen != hashLen {
386 return 0, nil, errors.New("crypto/rsa: input must be hashed message")
387 }
388 prefix, ok := hashPrefixes[hash]
389 if !ok {
390 return 0, nil, errors.New("crypto/rsa: unsupported hash function")
391 }
392 return
393 }
394
View as plain text