xref: /illumos-gate/usr/src/common/crypto/rsa/rsa_impl.c (revision 20d58091d9e165708ac7724f90a965464972f87f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5b60f2a0bSfr41279  * Common Development and Distribution License (the "License").
6b60f2a0bSfr41279  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227b79d846SDina K Nimeh  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains RSA helper routines common to
287c478bd9Sstevel@tonic-gate  * the PKCS11 soft token code and the kernel RSA code.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include "rsa_impl.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef _KERNEL
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #else
377c478bd9Sstevel@tonic-gate #include <strings.h>
387b79d846SDina K Nimeh #include <cryptoutil.h>
397c478bd9Sstevel@tonic-gate #include "softRandom.h"
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
43f66d273dSizick  * DER encoding T of the DigestInfo values for MD5, SHA1, and SHA2
447c478bd9Sstevel@tonic-gate  * from PKCS#1 v2.1: RSA Cryptography Standard Section 9.2 Note 1
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * MD5:     (0x)30 20 30 0c 06 08 2a 86 48 86 f7 0d 02 05 05 00 04 10 || H
477c478bd9Sstevel@tonic-gate  * SHA-1:   (0x)30 21 30 09 06 05 2b 0e 03 02 1a 05 00 04 14 || H
48f66d273dSizick  * SHA-256: (0x)30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 || H.
49f66d273dSizick  * SHA-384: (0x)30 41 30 0d 06 09 60 86 48 01 65 03 04 02 02 05 00 04 30 || H.
50f66d273dSizick  * SHA-512: (0x)30 51 30 0d 06 09 60 86 48 01 65 03 04 02 03 05 00 04 40 || H.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Where H is the digested output from MD5 or SHA1. We define the constant
537c478bd9Sstevel@tonic-gate  * byte array (the prefix) here and use it rather than doing the DER
547c478bd9Sstevel@tonic-gate  * encoding of the OID in a separate routine.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len] = {0x30, 0x20, 0x30, 0x0c,
577c478bd9Sstevel@tonic-gate     0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00,
587c478bd9Sstevel@tonic-gate     0x04, 0x10};
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len] = {0x30, 0x21, 0x30,
617c478bd9Sstevel@tonic-gate     0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14};
627c478bd9Sstevel@tonic-gate 
6360722cc8Sizick const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len] = {0x30, 0x1f, 0x30,
6460722cc8Sizick     0x07, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x04, 0x14};
6560722cc8Sizick 
66f66d273dSizick const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x31, 0x30, 0x0d,
67f66d273dSizick     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
68f66d273dSizick     0x00, 0x04, 0x20};
69f66d273dSizick 
70f66d273dSizick const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x41, 0x30, 0x0d,
71f66d273dSizick     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
72f66d273dSizick     0x00, 0x04, 0x30};
73f66d273dSizick 
74f66d273dSizick const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len] = {0x30, 0x51, 0x30, 0x0d,
75f66d273dSizick     0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
76f66d273dSizick     0x00, 0x04, 0x40};
77f66d273dSizick 
78*20d58091SDina K Nimeh const CK_BYTE DEFAULT_PUB_EXPO[DEFAULT_PUB_EXPO_Len] = { 0x01, 0x00, 0x01 };
79b60f2a0bSfr41279 
80b60f2a0bSfr41279 /* psize and qsize are in bits */
817c478bd9Sstevel@tonic-gate BIG_ERR_CODE
827c478bd9Sstevel@tonic-gate RSA_key_init(RSAkey *key, int psize, int qsize)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	BIG_ERR_CODE err = BIG_OK;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	int plen, qlen, nlen;
897c478bd9Sstevel@tonic-gate 
90b60f2a0bSfr41279 	plen = BITLEN2BIGNUMLEN(psize);
91b60f2a0bSfr41279 	qlen = BITLEN2BIGNUMLEN(qsize);
927c478bd9Sstevel@tonic-gate 	nlen = plen + qlen;
937c478bd9Sstevel@tonic-gate 	key->size = psize + qsize;
947c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->p), plen)) != BIG_OK)
957c478bd9Sstevel@tonic-gate 		return (err);
967c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->q), qlen)) != BIG_OK)
977c478bd9Sstevel@tonic-gate 		goto ret1;
987c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->n), nlen)) != BIG_OK)
997c478bd9Sstevel@tonic-gate 		goto ret2;
1007c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->d), nlen)) != BIG_OK)
1017c478bd9Sstevel@tonic-gate 		goto ret3;
1027c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->e), nlen)) != BIG_OK)
1037c478bd9Sstevel@tonic-gate 		goto ret4;
1047c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->dmodpminus1), plen)) != BIG_OK)
1057c478bd9Sstevel@tonic-gate 		goto ret5;
1067c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->dmodqminus1), qlen)) != BIG_OK)
1077c478bd9Sstevel@tonic-gate 		goto ret6;
1087c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->pinvmodq), qlen)) != BIG_OK)
1097c478bd9Sstevel@tonic-gate 		goto ret7;
1107c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->p_rr), plen)) != BIG_OK)
1117c478bd9Sstevel@tonic-gate 		goto ret8;
1127c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->q_rr), qlen)) != BIG_OK)
1137c478bd9Sstevel@tonic-gate 		goto ret9;
1147c478bd9Sstevel@tonic-gate 	if ((err = big_init(&(key->n_rr), nlen)) != BIG_OK)
1157c478bd9Sstevel@tonic-gate 		goto ret10;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	return (BIG_OK);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate ret10:
1207c478bd9Sstevel@tonic-gate 	big_finish(&(key->q_rr));
1217c478bd9Sstevel@tonic-gate ret9:
1227c478bd9Sstevel@tonic-gate 	big_finish(&(key->p_rr));
1237c478bd9Sstevel@tonic-gate ret8:
1247c478bd9Sstevel@tonic-gate 	big_finish(&(key->pinvmodq));
1257c478bd9Sstevel@tonic-gate ret7:
1267c478bd9Sstevel@tonic-gate 	big_finish(&(key->dmodqminus1));
1277c478bd9Sstevel@tonic-gate ret6:
1287c478bd9Sstevel@tonic-gate 	big_finish(&(key->dmodpminus1));
1297c478bd9Sstevel@tonic-gate ret5:
1307c478bd9Sstevel@tonic-gate 	big_finish(&(key->e));
1317c478bd9Sstevel@tonic-gate ret4:
1327c478bd9Sstevel@tonic-gate 	big_finish(&(key->d));
1337c478bd9Sstevel@tonic-gate ret3:
1347c478bd9Sstevel@tonic-gate 	big_finish(&(key->n));
1357c478bd9Sstevel@tonic-gate ret2:
1367c478bd9Sstevel@tonic-gate 	big_finish(&(key->q));
1377c478bd9Sstevel@tonic-gate ret1:
1387c478bd9Sstevel@tonic-gate 	big_finish(&(key->p));
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	return (err);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate void
1477c478bd9Sstevel@tonic-gate RSA_key_finish(RSAkey *key)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	big_finish(&(key->n_rr));
1537c478bd9Sstevel@tonic-gate 	big_finish(&(key->q_rr));
1547c478bd9Sstevel@tonic-gate 	big_finish(&(key->p_rr));
1557c478bd9Sstevel@tonic-gate 	big_finish(&(key->pinvmodq));
1567c478bd9Sstevel@tonic-gate 	big_finish(&(key->dmodqminus1));
1577c478bd9Sstevel@tonic-gate 	big_finish(&(key->dmodpminus1));
1587c478bd9Sstevel@tonic-gate 	big_finish(&(key->e));
1597c478bd9Sstevel@tonic-gate 	big_finish(&(key->d));
1607c478bd9Sstevel@tonic-gate 	big_finish(&(key->n));
1617c478bd9Sstevel@tonic-gate 	big_finish(&(key->q));
1627c478bd9Sstevel@tonic-gate 	big_finish(&(key->p));
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * To create a block type "02" encryption block for RSA PKCS encryption
1717c478bd9Sstevel@tonic-gate  * process.
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * The RSA PKCS Padding before encryption is in the following format:
1747c478bd9Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
1757c478bd9Sstevel@tonic-gate  * |0x0002| 8 bytes or more RN |0x00|       DATA                  |
1767c478bd9Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate CK_RV
1807c478bd9Sstevel@tonic-gate soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
1817c478bd9Sstevel@tonic-gate     size_t datalen, uint8_t *padbuf, size_t padbuflen)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	size_t	padlen;
1877c478bd9Sstevel@tonic-gate 	CK_RV	rv;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	padlen = padbuflen - datalen;
1907c478bd9Sstevel@tonic-gate 	if (padlen < MIN_PKCS1_PADLEN) {
1917c478bd9Sstevel@tonic-gate 		return (CKR_DATA_LEN_RANGE);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/* Pad with 0x0002+non-zero pseudorandom numbers+0x00. */
1957c478bd9Sstevel@tonic-gate 	padbuf[0] = 0x00;
1967c478bd9Sstevel@tonic-gate 	padbuf[1] = 0x02;
1977c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1987c478bd9Sstevel@tonic-gate 	rv = knzero_random_generator(padbuf + 2, padbuflen - 3);
1997c478bd9Sstevel@tonic-gate #else
2007b79d846SDina K Nimeh 	rv = CKR_OK;
2017b79d846SDina K Nimeh 	if (pkcs11_get_nzero_urandom(padbuf + 2, padbuflen - 3) < 0)
2027b79d846SDina K Nimeh 		rv = CKR_DEVICE_ERROR;
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK) {
2057c478bd9Sstevel@tonic-gate 		return (rv);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	padbuf[padlen - 1] = 0x00;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	bcopy(databuf, padbuf + padlen, datalen);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (CKR_OK);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * The RSA PKCS Padding after decryption is in the following format:
2197c478bd9Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
2207c478bd9Sstevel@tonic-gate  * |0x0002| 8 bytes or more RN |0x00|       DATA                  |
2217c478bd9Sstevel@tonic-gate  * +------+--------------------+----+-----------------------------+
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * 'padbuf' points to the recovered message which is the modulus
2247c478bd9Sstevel@tonic-gate  * length. As a result, 'plen' is changed to hold the actual data length.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate CK_RV
2277c478bd9Sstevel@tonic-gate soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	int	i;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* Check to see if the recovered data is padded is 0x0002. */
2357c478bd9Sstevel@tonic-gate 	if (padbuf[0] != 0x00 || padbuf[1] != 0x02) {
2367c478bd9Sstevel@tonic-gate 		return (CKR_ENCRYPTED_DATA_INVALID);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* Remove all the random bits up to 0x00 (= NULL char) */
2407c478bd9Sstevel@tonic-gate 	for (i = 2; (*plen - i) > 0; i++) {
2417c478bd9Sstevel@tonic-gate 		if (padbuf[i] == 0x00) {
2427c478bd9Sstevel@tonic-gate 			i++;
2437c478bd9Sstevel@tonic-gate 			if (i < MIN_PKCS1_PADLEN) {
2447c478bd9Sstevel@tonic-gate 				return (CKR_ENCRYPTED_DATA_INVALID);
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 			*plen -= i;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 			return (CKR_OK);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	return (CKR_ENCRYPTED_DATA_INVALID);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * To create a block type "01" block for RSA PKCS signature process.
2597c478bd9Sstevel@tonic-gate  *
2607c478bd9Sstevel@tonic-gate  * The RSA PKCS Padding before Signing is in the following format:
2617c478bd9Sstevel@tonic-gate  * +------+--------------+----+-----------------------------+
2627c478bd9Sstevel@tonic-gate  * |0x0001| 0xFFFF.......|0x00|          DATA               |
2637c478bd9Sstevel@tonic-gate  * +------+--------------+----+-----------------------------+
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate CK_RV
2667c478bd9Sstevel@tonic-gate soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen, uint8_t *data,
2677c478bd9Sstevel@tonic-gate     size_t mbit_l)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	size_t	padlen;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	padlen = mbit_l - dataLen;
2757c478bd9Sstevel@tonic-gate 	if (padlen < MIN_PKCS1_PADLEN) {
2767c478bd9Sstevel@tonic-gate 		return (CKR_DATA_LEN_RANGE);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	padlen -= 3;
2807c478bd9Sstevel@tonic-gate 	data[0] = 0x00;
2817c478bd9Sstevel@tonic-gate 	data[1] = 0x01;
2827c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2837c478bd9Sstevel@tonic-gate 	kmemset(data + 2, 0xFF, padlen);
2847c478bd9Sstevel@tonic-gate #else
2857c478bd9Sstevel@tonic-gate 	(void) memset(data + 2, 0xFF, padlen);
2867c478bd9Sstevel@tonic-gate #endif
2877c478bd9Sstevel@tonic-gate 	data[padlen + 2] = 0x00;
2887c478bd9Sstevel@tonic-gate 	bcopy(pData, data + padlen + 3, dataLen);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	return (CKR_OK);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate CK_RV
2977c478bd9Sstevel@tonic-gate soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	int i;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* Check to see if the padding of recovered data starts with 0x0001. */
3057c478bd9Sstevel@tonic-gate 	if ((data[0] != 0x00) || (data[1] != 0x01)) {
3067c478bd9Sstevel@tonic-gate 		return (CKR_SIGNATURE_INVALID);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	/* Check to see if the recovered data is padded with 0xFFF...00. */
3097c478bd9Sstevel@tonic-gate 	for (i = 2; i < *mbit_l; i++) {
3107c478bd9Sstevel@tonic-gate 		if (data[i] == 0x00) {
3117c478bd9Sstevel@tonic-gate 			i++;
3127c478bd9Sstevel@tonic-gate 			if (i < MIN_PKCS1_PADLEN) {
3137c478bd9Sstevel@tonic-gate 				return (CKR_SIGNATURE_INVALID);
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 			*mbit_l -= i;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			return (CKR_OK);
3187c478bd9Sstevel@tonic-gate 		} else if (data[i] != 0xFF) {
3197c478bd9Sstevel@tonic-gate 			return (CKR_SIGNATURE_INVALID);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	return (CKR_SIGNATURE_INVALID);
3267c478bd9Sstevel@tonic-gate }
327