xref: /titanic_51/usr/src/common/crypto/rsa/rsa_impl.c (revision 613a2f6ba31e891e3d947a356daf5e563d43c1ce)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file contains RSA helper routines common to
28  * the PKCS11 soft token code and the kernel RSA code.
29  */
30 
31 #include <sys/types.h>
32 #include "rsa_impl.h"
33 
34 #ifdef _KERNEL
35 #include <sys/param.h>
36 #else
37 #include <strings.h>
38 #include <cryptoutil.h>
39 #include "softRandom.h"
40 #endif
41 
42 /*
43  * DER encoding T of the DigestInfo values for MD5, SHA1, and SHA2
44  * from PKCS#1 v2.1: RSA Cryptography Standard Section 9.2 Note 1
45  *
46  * MD5:     (0x)30 20 30 0c 06 08 2a 86 48 86 f7 0d 02 05 05 00 04 10 || H
47  * SHA-1:   (0x)30 21 30 09 06 05 2b 0e 03 02 1a 05 00 04 14 || H
48  * SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 || H.
49  * SHA-384: (0x)30 41 30 0d 06 09 60 86 48 01 65 03 04 02 02 05 00 04 30 || H.
50  * SHA-512: (0x)30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00 04 40 || H.
51  *
52  * Where H is the digested output from MD5 or SHA1. We define the constant
53  * byte array (the prefix) here and use it rather than doing the DER
54  * encoding of the OID in a separate routine.
55  */
56 const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len] = {0x30, 0x20, 0x30, 0x0c,
57     0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00,
58     0x04, 0x10};
59 
60 const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len] = {0x30, 0x21, 0x30,
61     0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14};
62 
63 const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len] = {0x30, 0x1f, 0x30,
64     0x07, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x04, 0x14};
65 
66 const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x31, 0x30, 0x0d,
67     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
68     0x00, 0x04, 0x20};
69 
70 const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x41, 0x30, 0x0d,
71     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
72     0x00, 0x04, 0x30};
73 
74 const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x51, 0x30, 0x0d,
75     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
76     0x00, 0x04, 0x40};
77 
78 const CK_BYTE DEFAULT_PUB_EXPO[DEFAULT_PUB_EXPO_Len] = { 0x01, 0x00, 0x01 };
79 
80 /* psize and qsize are in bits */
81 BIG_ERR_CODE
82 RSA_key_init(RSAkey *key, int psize, int qsize)
83 {
84 	BIG_ERR_CODE err = BIG_OK;
85 
86 /* EXPORT DELETE START */
87 
88 	int plen, qlen, nlen;
89 
90 	plen = BITLEN2BIGNUMLEN(psize);
91 	qlen = BITLEN2BIGNUMLEN(qsize);
92 	nlen = plen + qlen;
93 	key->size = psize + qsize;
94 	if ((err = big_init(&(key->p), plen)) != BIG_OK)
95 		return (err);
96 	if ((err = big_init(&(key->q), qlen)) != BIG_OK)
97 		goto ret1;
98 	if ((err = big_init(&(key->n), nlen)) != BIG_OK)
99 		goto ret2;
100 	if ((err = big_init(&(key->d), nlen)) != BIG_OK)
101 		goto ret3;
102 	if ((err = big_init(&(key->e), nlen)) != BIG_OK)
103 		goto ret4;
104 	if ((err = big_init(&(key->dmodpminus1), plen)) != BIG_OK)
105 		goto ret5;
106 	if ((err = big_init(&(key->dmodqminus1), qlen)) != BIG_OK)
107 		goto ret6;
108 	if ((err = big_init(&(key->pinvmodq), qlen)) != BIG_OK)
109 		goto ret7;
110 	if ((err = big_init(&(key->p_rr), plen)) != BIG_OK)
111 		goto ret8;
112 	if ((err = big_init(&(key->q_rr), qlen)) != BIG_OK)
113 		goto ret9;
114 	if ((err = big_init(&(key->n_rr), nlen)) != BIG_OK)
115 		goto ret10;
116 
117 	return (BIG_OK);
118 
119 ret10:
120 	big_finish(&(key->q_rr));
121 ret9:
122 	big_finish(&(key->p_rr));
123 ret8:
124 	big_finish(&(key->pinvmodq));
125 ret7:
126 	big_finish(&(key->dmodqminus1));
127 ret6:
128 	big_finish(&(key->dmodpminus1));
129 ret5:
130 	big_finish(&(key->e));
131 ret4:
132 	big_finish(&(key->d));
133 ret3:
134 	big_finish(&(key->n));
135 ret2:
136 	big_finish(&(key->q));
137 ret1:
138 	big_finish(&(key->p));
139 
140 /* EXPORT DELETE END */
141 
142 	return (err);
143 }
144 
145 
146 void
147 RSA_key_finish(RSAkey *key)
148 {
149 
150 /* EXPORT DELETE START */
151 
152 	big_finish(&(key->n_rr));
153 	big_finish(&(key->q_rr));
154 	big_finish(&(key->p_rr));
155 	big_finish(&(key->pinvmodq));
156 	big_finish(&(key->dmodqminus1));
157 	big_finish(&(key->dmodpminus1));
158 	big_finish(&(key->e));
159 	big_finish(&(key->d));
160 	big_finish(&(key->n));
161 	big_finish(&(key->q));
162 	big_finish(&(key->p));
163 
164 /* EXPORT DELETE END */
165 
166 }
167 
168 
169 /*
170  * To create a block type "02" encryption block for RSA PKCS encryption
171  * process.
172  *
173  * The RSA PKCS Padding before encryption is in the following format:
174  * +------+--------------------+----+-----------------------------+
175  * |0x0002| 8 bytes or more RN |0x00|       DATA                  |
176  * +------+--------------------+----+-----------------------------+
177  *
178  */
179 CK_RV
180 soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
181     size_t datalen, uint8_t *padbuf, size_t padbuflen)
182 {
183 
184 /* EXPORT DELETE START */
185 
186 	size_t	padlen;
187 	CK_RV	rv;
188 
189 	padlen = padbuflen - datalen;
190 	if (padlen < MIN_PKCS1_PADLEN) {
191 		return (CKR_DATA_LEN_RANGE);
192 	}
193 
194 	/* Pad with 0x0002+non-zero pseudorandom numbers+0x00. */
195 	padbuf[0] = 0x00;
196 	padbuf[1] = 0x02;
197 #ifdef _KERNEL
198 	rv = knzero_random_generator(padbuf + 2, padbuflen - 3);
199 #else
200 	rv = CKR_OK;
201 	if (pkcs11_get_nzero_urandom(padbuf + 2, padbuflen - 3) < 0)
202 		rv = CKR_DEVICE_ERROR;
203 #endif
204 	if (rv != CKR_OK) {
205 		return (rv);
206 	}
207 	padbuf[padlen - 1] = 0x00;
208 
209 	bcopy(databuf, padbuf + padlen, datalen);
210 
211 /* EXPORT DELETE END */
212 
213 	return (CKR_OK);
214 }
215 
216 
217 /*
218  * The RSA PKCS Padding after decryption is in the following format:
219  * +------+--------------------+----+-----------------------------+
220  * |0x0002| 8 bytes or more RN |0x00|       DATA                  |
221  * +------+--------------------+----+-----------------------------+
222  *
223  * 'padbuf' points to the recovered message which is the modulus
224  * length. As a result, 'plen' is changed to hold the actual data length.
225  */
226 CK_RV
227 soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen)
228 {
229 
230 /* EXPORT DELETE START */
231 
232 	int	i;
233 
234 	/* Check to see if the recovered data is padded is 0x0002. */
235 	if (padbuf[0] != 0x00 || padbuf[1] != 0x02) {
236 		return (CKR_ENCRYPTED_DATA_INVALID);
237 	}
238 
239 	/* Remove all the random bits up to 0x00 (= NULL char) */
240 	for (i = 2; (*plen - i) > 0; i++) {
241 		if (padbuf[i] == 0x00) {
242 			i++;
243 			if (i < MIN_PKCS1_PADLEN) {
244 				return (CKR_ENCRYPTED_DATA_INVALID);
245 			}
246 			*plen -= i;
247 
248 			return (CKR_OK);
249 		}
250 	}
251 
252 /* EXPORT DELETE END */
253 
254 	return (CKR_ENCRYPTED_DATA_INVALID);
255 }
256 
257 /*
258  * To create a block type "01" block for RSA PKCS signature process.
259  *
260  * The RSA PKCS Padding before Signing is in the following format:
261  * +------+--------------+----+-----------------------------+
262  * |0x0001| 0xFFFF.......|0x00|          DATA               |
263  * +------+--------------+----+-----------------------------+
264  */
265 CK_RV
266 soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen, uint8_t *data,
267     size_t mbit_l)
268 {
269 
270 /* EXPORT DELETE START */
271 
272 	size_t	padlen;
273 
274 	padlen = mbit_l - dataLen;
275 	if (padlen < MIN_PKCS1_PADLEN) {
276 		return (CKR_DATA_LEN_RANGE);
277 	}
278 
279 	padlen -= 3;
280 	data[0] = 0x00;
281 	data[1] = 0x01;
282 #ifdef _KERNEL
283 	kmemset(data + 2, 0xFF, padlen);
284 #else
285 	(void) memset(data + 2, 0xFF, padlen);
286 #endif
287 	data[padlen + 2] = 0x00;
288 	bcopy(pData, data + padlen + 3, dataLen);
289 
290 /* EXPORT DELETE END */
291 
292 	return (CKR_OK);
293 }
294 
295 
296 CK_RV
297 soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l)
298 {
299 
300 /* EXPORT DELETE START */
301 
302 	int i;
303 
304 	/* Check to see if the padding of recovered data starts with 0x0001. */
305 	if ((data[0] != 0x00) || (data[1] != 0x01)) {
306 		return (CKR_SIGNATURE_INVALID);
307 	}
308 	/* Check to see if the recovered data is padded with 0xFFF...00. */
309 	for (i = 2; i < *mbit_l; i++) {
310 		if (data[i] == 0x00) {
311 			i++;
312 			if (i < MIN_PKCS1_PADLEN) {
313 				return (CKR_SIGNATURE_INVALID);
314 			}
315 			*mbit_l -= i;
316 
317 			return (CKR_OK);
318 		} else if (data[i] != 0xFF) {
319 			return (CKR_SIGNATURE_INVALID);
320 		}
321 	}
322 
323 /* EXPORT DELETE END */
324 
325 	return (CKR_SIGNATURE_INVALID);
326 }
327