xref: /titanic_44/usr/src/common/crypto/rsa/rsa_impl.h (revision 023e71de9e5670cebc23dd51162833661d3d2d3b)
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 #ifndef _RSA_IMPL_H
27 #define	_RSA_IMPL_H
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/types.h>
34 #include <bignum.h>
35 
36 #define	MIN_RSA_KEYLENGTH_IN_BYTES	32
37 #define	MAX_RSA_KEYLENGTH_IN_BYTES	512
38 #define	RSA_MIN_KEY_LEN	256	/* RSA min key length in bits */
39 #define	RSA_MAX_KEY_LEN	4096	/* RSA max key length in bits */
40 
41 #define	MIN_PKCS1_PADLEN	11
42 
43 #ifdef _KERNEL
44 
45 #include <sys/sunddi.h>
46 #include <sys/crypto/common.h>
47 
48 #define	CK_BYTE			uchar_t
49 #define	CK_ULONG		ulong_t
50 #define	CK_RV			int
51 #define	CKR_OK			CRYPTO_SUCCESS
52 #define	CKR_HOST_MEMORY		CRYPTO_HOST_MEMORY
53 #define	CKR_DATA_LEN_RANGE	CRYPTO_DATA_LEN_RANGE
54 #define	CKR_ENCRYPTED_DATA_INVALID	CRYPTO_ENCRYPTED_DATA_INVALID
55 #define	CKR_SIGNATURE_INVALID	CRYPTO_SIGNATURE_INVALID
56 #define	CKR_FUNCTION_FAILED	CRYPTO_NOT_SUPPORTED
57 
58 #else
59 
60 #include <security/cryptoki.h>
61 #include <security/pkcs11t.h>
62 
63 #endif	/* _KERNEL */
64 
65 #define	MD5_DER_PREFIX_Len	18
66 #define	SHA1_DER_PREFIX_Len	15
67 #define	SHA1_DER_PREFIX_OID_Len	13
68 #define	SHA2_DER_PREFIX_Len	19
69 #define	DEFAULT_PUB_EXPO_Len	3
70 
71 extern const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len];
72 extern const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len];
73 extern const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len];
74 extern const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len];
75 extern const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len];
76 extern const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len];
77 extern const CK_BYTE DEFAULT_PUB_EXPO[DEFAULT_PUB_EXPO_Len];
78 
79 typedef struct {
80 	int 	size;		/* key size in bits */
81 	BIGNUM	p;		/* p */
82 	BIGNUM	q;		/* q */
83 	BIGNUM	n;		/* n = p * q (the modulus) */
84 	BIGNUM	d;		/* private exponent */
85 	BIGNUM	e;		/* public exponent */
86 	BIGNUM	dmodpminus1;	/* d mod (p - 1) */
87 	BIGNUM	dmodqminus1;	/* d mod (q - 1) */
88 	BIGNUM	pinvmodq;	/* p^(-1) mod q */
89 	BIGNUM	p_rr;		/* 2^(2*(32*p->len)) mod p */
90 	BIGNUM	q_rr;		/* 2^(2*(32*q->len)) mod q */
91 	BIGNUM	n_rr;		/* 2^(2*(32*n->len)) mod n */
92 } RSAkey;
93 
94 BIG_ERR_CODE RSA_key_init(RSAkey *key, int psize, int qsize);
95 void RSA_key_finish(RSAkey *key);
96 
97 CK_RV soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
98     size_t datalen, uint8_t *padbuf, size_t padbuflen);
99 CK_RV soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen);
100 
101 CK_RV soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen,
102     uint8_t *data, size_t mbit_l);
103 CK_RV soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l);
104 
105 #ifdef _KERNEL
106 int knzero_random_generator(uint8_t *ran_out, size_t ran_len);
107 void kmemset(uint8_t *buf, char pattern, size_t len);
108 #endif
109 
110 /*
111  * The following definitions and declarations are only used by RSA FIPS POST
112  */
113 #ifdef _RSA_FIPS_POST
114 
115 /* RSA FIPS Declarations */
116 #define	FIPS_RSA_PUBLIC_EXPONENT_LENGTH		  3 /*   24-bits */
117 #define	FIPS_RSA_PRIVATE_VERSION_LENGTH		  1 /*    8-bits */
118 #define	FIPS_RSA_MESSAGE_LENGTH			128 /* 1024-bits */
119 #define	FIPS_RSA_COEFFICIENT_LENGTH		 64 /*  512-bits */
120 #define	FIPS_RSA_PRIME0_LENGTH			 64 /*  512-bits */
121 #define	FIPS_RSA_PRIME1_LENGTH			 64 /*  512-bits */
122 #define	FIPS_RSA_EXPONENT0_LENGTH		 64 /*  512-bits */
123 #define	FIPS_RSA_EXPONENT1_LENGTH		 64 /*  512-bits */
124 #define	FIPS_RSA_PRIVATE_EXPONENT_LENGTH	128 /* 1024-bits */
125 #define	FIPS_RSA_ENCRYPT_LENGTH			128 /* 1024-bits */
126 #define	FIPS_RSA_DECRYPT_LENGTH			128 /* 1024-bits */
127 #define	FIPS_RSA_SIGNATURE_LENGTH		128 /* 1024-bits */
128 #define	FIPS_RSA_MODULUS_LENGTH			128 /* 1024-bits */
129 #define	MAX_KEY_ATTR_BUFLEN			1024
130 
131 typedef struct RSAPrivateKey_s {
132 	uint8_t		*version;
133 	int		version_len;
134 	uint8_t		*modulus;
135 	int		modulus_len;
136 	uint8_t		*public_expo;
137 	int		public_expo_len;
138 	uint8_t		*private_expo;
139 	int		private_expo_len;
140 	uint8_t		*prime1;
141 	int		prime1_len;
142 	uint8_t		*prime2;
143 	int		prime2_len;
144 	uint8_t		*exponent1;
145 	int		exponent1_len;
146 	uint8_t		*exponent2;
147 	int		exponent2_len;
148 	uint8_t		*coef;
149 	int		coef_len;
150 } RSAPrivateKey_t;
151 
152 /* RSA FIPS functions */
153 extern int fips_rsa_post(void);
154 extern int fips_rsa_encrypt(uint8_t *, int, uint8_t *,
155 	int, uint8_t *, int, uint8_t *);
156 extern int fips_rsa_decrypt(RSAPrivateKey_t *, uint8_t *,
157 	int, uint8_t *);
158 extern int fips_rsa_sign(RSAPrivateKey_t *, uint8_t *,
159 	uint32_t, uint8_t *);
160 extern int fips_rsa_verify(RSAPrivateKey_t *, uint8_t *, uint32_t,
161 	uint8_t *);
162 
163 #endif /* _RSA_FIPS_POST */
164 
165 #ifdef	__cplusplus
166 }
167 #endif
168 
169 #endif /* _RSA_IMPL_H */
170