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