xref: /onnv-gate/usr/src/common/crypto/rsa/rsa_impl.c (revision 676:fb4857bdbaea)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*676Sizick  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This file contains RSA helper routines common to
310Sstevel@tonic-gate  * the PKCS11 soft token code and the kernel RSA code.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include "rsa_impl.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #ifdef _KERNEL
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #else
400Sstevel@tonic-gate #include <strings.h>
410Sstevel@tonic-gate #include "softRandom.h"
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
45*676Sizick  * DER encoding T of the DigestInfo values for MD5, SHA1, and SHA2
460Sstevel@tonic-gate  * from PKCS#1 v2.1: RSA Cryptography Standard Section 9.2 Note 1
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * MD5:     (0x)30 20 30 0c 06 08 2a 86 48 86 f7 0d 02 05 05 00 04 10 || H
490Sstevel@tonic-gate  * SHA-1:   (0x)30 21 30 09 06 05 2b 0e 03 02 1a 05 00 04 14 || H
50*676Sizick  * SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 || H.
51*676Sizick  * SHA-384: (0x)30 41 30 0d 06 09 60 86 48 01 65 03 04 02 02 05 00 04 30 || H.
52*676Sizick  * SHA-512: (0x)30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00 04 40 || H.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * Where H is the digested output from MD5 or SHA1. We define the constant
550Sstevel@tonic-gate  * byte array (the prefix) here and use it rather than doing the DER
560Sstevel@tonic-gate  * encoding of the OID in a separate routine.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len] = {0x30, 0x20, 0x30, 0x0c,
590Sstevel@tonic-gate     0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00,
600Sstevel@tonic-gate     0x04, 0x10};
610Sstevel@tonic-gate 
620Sstevel@tonic-gate const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len] = {0x30, 0x21, 0x30,
630Sstevel@tonic-gate     0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14};
640Sstevel@tonic-gate 
65*676Sizick const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x31, 0x30, 0x0d,
66*676Sizick     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
67*676Sizick     0x00, 0x04, 0x20};
68*676Sizick 
69*676Sizick const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x41, 0x30, 0x0d,
70*676Sizick     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
71*676Sizick     0x00, 0x04, 0x30};
72*676Sizick 
73*676Sizick const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x51, 0x30, 0x0d,
74*676Sizick     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
75*676Sizick     0x00, 0x04, 0x40};
76*676Sizick 
770Sstevel@tonic-gate BIG_ERR_CODE
780Sstevel@tonic-gate RSA_key_init(RSAkey *key, int psize, int qsize)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	BIG_ERR_CODE err = BIG_OK;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /* EXPORT DELETE START */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	int plen, qlen, nlen;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	plen = (psize + 31) / 32;
870Sstevel@tonic-gate 	qlen = (qsize + 31) / 32;
880Sstevel@tonic-gate 	nlen = plen + qlen;
890Sstevel@tonic-gate 	key->size = psize + qsize;
900Sstevel@tonic-gate 	if ((err = big_init(&(key->p), plen)) != BIG_OK)
910Sstevel@tonic-gate 		return (err);
920Sstevel@tonic-gate 	if ((err = big_init(&(key->q), qlen)) != BIG_OK)
930Sstevel@tonic-gate 		goto ret1;
940Sstevel@tonic-gate 	if ((err = big_init(&(key->n), nlen)) != BIG_OK)
950Sstevel@tonic-gate 		goto ret2;
960Sstevel@tonic-gate 	if ((err = big_init(&(key->d), nlen)) != BIG_OK)
970Sstevel@tonic-gate 		goto ret3;
980Sstevel@tonic-gate 	if ((err = big_init(&(key->e), nlen)) != BIG_OK)
990Sstevel@tonic-gate 		goto ret4;
1000Sstevel@tonic-gate 	if ((err = big_init(&(key->dmodpminus1), plen)) != BIG_OK)
1010Sstevel@tonic-gate 		goto ret5;
1020Sstevel@tonic-gate 	if ((err = big_init(&(key->dmodqminus1), qlen)) != BIG_OK)
1030Sstevel@tonic-gate 		goto ret6;
1040Sstevel@tonic-gate 	if ((err = big_init(&(key->pinvmodq), qlen)) != BIG_OK)
1050Sstevel@tonic-gate 		goto ret7;
1060Sstevel@tonic-gate 	if ((err = big_init(&(key->p_rr), plen)) != BIG_OK)
1070Sstevel@tonic-gate 		goto ret8;
1080Sstevel@tonic-gate 	if ((err = big_init(&(key->q_rr), qlen)) != BIG_OK)
1090Sstevel@tonic-gate 		goto ret9;
1100Sstevel@tonic-gate 	if ((err = big_init(&(key->n_rr), nlen)) != BIG_OK)
1110Sstevel@tonic-gate 		goto ret10;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	return (BIG_OK);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate ret10:
1160Sstevel@tonic-gate 	big_finish(&(key->q_rr));
1170Sstevel@tonic-gate ret9:
1180Sstevel@tonic-gate 	big_finish(&(key->p_rr));
1190Sstevel@tonic-gate ret8:
1200Sstevel@tonic-gate 	big_finish(&(key->pinvmodq));
1210Sstevel@tonic-gate ret7:
1220Sstevel@tonic-gate 	big_finish(&(key->dmodqminus1));
1230Sstevel@tonic-gate ret6:
1240Sstevel@tonic-gate 	big_finish(&(key->dmodpminus1));
1250Sstevel@tonic-gate ret5:
1260Sstevel@tonic-gate 	big_finish(&(key->e));
1270Sstevel@tonic-gate ret4:
1280Sstevel@tonic-gate 	big_finish(&(key->d));
1290Sstevel@tonic-gate ret3:
1300Sstevel@tonic-gate 	big_finish(&(key->n));
1310Sstevel@tonic-gate ret2:
1320Sstevel@tonic-gate 	big_finish(&(key->q));
1330Sstevel@tonic-gate ret1:
1340Sstevel@tonic-gate 	big_finish(&(key->p));
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /* EXPORT DELETE END */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	return (err);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate void
1430Sstevel@tonic-gate RSA_key_finish(RSAkey *key)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* EXPORT DELETE START */
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	big_finish(&(key->n_rr));
1490Sstevel@tonic-gate 	big_finish(&(key->q_rr));
1500Sstevel@tonic-gate 	big_finish(&(key->p_rr));
1510Sstevel@tonic-gate 	big_finish(&(key->pinvmodq));
1520Sstevel@tonic-gate 	big_finish(&(key->dmodqminus1));
1530Sstevel@tonic-gate 	big_finish(&(key->dmodpminus1));
1540Sstevel@tonic-gate 	big_finish(&(key->e));
1550Sstevel@tonic-gate 	big_finish(&(key->d));
1560Sstevel@tonic-gate 	big_finish(&(key->n));
1570Sstevel@tonic-gate 	big_finish(&(key->q));
1580Sstevel@tonic-gate 	big_finish(&(key->p));
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /* EXPORT DELETE END */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate  * To create a block type "02" encryption block for RSA PKCS encryption
1670Sstevel@tonic-gate  * process.
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * The RSA PKCS Padding before encryption is in the following format:
1700Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
1710Sstevel@tonic-gate  * |0x0002| 8 bytes or more RN |0x00|       DATA                  |
1720Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate CK_RV
1760Sstevel@tonic-gate soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
1770Sstevel@tonic-gate     size_t datalen, uint8_t *padbuf, size_t padbuflen)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /* EXPORT DELETE START */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	size_t	padlen;
1830Sstevel@tonic-gate 	CK_RV	rv;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	padlen = padbuflen - datalen;
1860Sstevel@tonic-gate 	if (padlen < MIN_PKCS1_PADLEN) {
1870Sstevel@tonic-gate 		return (CKR_DATA_LEN_RANGE);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/* Pad with 0x0002+non-zero pseudorandom numbers+0x00. */
1910Sstevel@tonic-gate 	padbuf[0] = 0x00;
1920Sstevel@tonic-gate 	padbuf[1] = 0x02;
1930Sstevel@tonic-gate #ifdef _KERNEL
1940Sstevel@tonic-gate 	rv = knzero_random_generator(padbuf + 2, padbuflen - 3);
1950Sstevel@tonic-gate #else
1960Sstevel@tonic-gate 	rv = soft_nzero_random_generator(padbuf + 2, padbuflen - 3);
1970Sstevel@tonic-gate #endif
1980Sstevel@tonic-gate 	if (rv != CKR_OK) {
1990Sstevel@tonic-gate 		return (rv);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	padbuf[padlen - 1] = 0x00;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	bcopy(databuf, padbuf + padlen, datalen);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /* EXPORT DELETE END */
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	return (CKR_OK);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * The RSA PKCS Padding after decryption is in the following format:
2130Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
2140Sstevel@tonic-gate  * |0x0002| 8 bytes or more RN |0x00|       DATA                  |
2150Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * 'padbuf' points to the recovered message which is the modulus
2180Sstevel@tonic-gate  * length. As a result, 'plen' is changed to hold the actual data length.
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate CK_RV
2210Sstevel@tonic-gate soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /* EXPORT DELETE START */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	int	i;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/* Check to see if the recovered data is padded is 0x0002. */
2290Sstevel@tonic-gate 	if (padbuf[0] != 0x00 || padbuf[1] != 0x02) {
2300Sstevel@tonic-gate 		return (CKR_ENCRYPTED_DATA_INVALID);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	/* Remove all the random bits up to 0x00 (= NULL char) */
2340Sstevel@tonic-gate 	for (i = 2; (*plen - i) > 0; i++) {
2350Sstevel@tonic-gate 		if (padbuf[i] == 0x00) {
2360Sstevel@tonic-gate 			i++;
2370Sstevel@tonic-gate 			if (i < MIN_PKCS1_PADLEN) {
2380Sstevel@tonic-gate 				return (CKR_ENCRYPTED_DATA_INVALID);
2390Sstevel@tonic-gate 			}
2400Sstevel@tonic-gate 			*plen -= i;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 			return (CKR_OK);
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /* EXPORT DELETE END */
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	return (CKR_ENCRYPTED_DATA_INVALID);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * To create a block type "01" block for RSA PKCS signature process.
2530Sstevel@tonic-gate  *
2540Sstevel@tonic-gate  * The RSA PKCS Padding before Signing is in the following format:
2550Sstevel@tonic-gate  * +------+--------------+----+-----------------------------+
2560Sstevel@tonic-gate  * |0x0001| 0xFFFF.......|0x00|          DATA               |
2570Sstevel@tonic-gate  * +------+--------------+----+-----------------------------+
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate CK_RV
2600Sstevel@tonic-gate soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen, uint8_t *data,
2610Sstevel@tonic-gate     size_t mbit_l)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /* EXPORT DELETE START */
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	size_t	padlen;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	padlen = mbit_l - dataLen;
2690Sstevel@tonic-gate 	if (padlen < MIN_PKCS1_PADLEN) {
2700Sstevel@tonic-gate 		return (CKR_DATA_LEN_RANGE);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	padlen -= 3;
2740Sstevel@tonic-gate 	data[0] = 0x00;
2750Sstevel@tonic-gate 	data[1] = 0x01;
2760Sstevel@tonic-gate #ifdef _KERNEL
2770Sstevel@tonic-gate 	kmemset(data + 2, 0xFF, padlen);
2780Sstevel@tonic-gate #else
2790Sstevel@tonic-gate 	(void) memset(data + 2, 0xFF, padlen);
2800Sstevel@tonic-gate #endif
2810Sstevel@tonic-gate 	data[padlen + 2] = 0x00;
2820Sstevel@tonic-gate 	bcopy(pData, data + padlen + 3, dataLen);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /* EXPORT DELETE END */
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	return (CKR_OK);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate CK_RV
2910Sstevel@tonic-gate soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /* EXPORT DELETE START */
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	int i;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/* Check to see if the padding of recovered data starts with 0x0001. */
2990Sstevel@tonic-gate 	if ((data[0] != 0x00) || (data[1] != 0x01)) {
3000Sstevel@tonic-gate 		return (CKR_SIGNATURE_INVALID);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	/* Check to see if the recovered data is padded with 0xFFF...00. */
3030Sstevel@tonic-gate 	for (i = 2; i < *mbit_l; i++) {
3040Sstevel@tonic-gate 		if (data[i] == 0x00) {
3050Sstevel@tonic-gate 			i++;
3060Sstevel@tonic-gate 			if (i < MIN_PKCS1_PADLEN) {
3070Sstevel@tonic-gate 				return (CKR_SIGNATURE_INVALID);
3080Sstevel@tonic-gate 			}
3090Sstevel@tonic-gate 			*mbit_l -= i;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 			return (CKR_OK);
3120Sstevel@tonic-gate 		} else if (data[i] != 0xFF) {
3130Sstevel@tonic-gate 			return (CKR_SIGNATURE_INVALID);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /* EXPORT DELETE END */
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	return (CKR_SIGNATURE_INVALID);
3200Sstevel@tonic-gate }
321