xref: /onnv-gate/usr/src/common/crypto/rsa/rsa_impl.h (revision 10500:a10fbcfc2f21)
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
59536SDina.Nimeh@Sun.COM  * Common Development and Distribution License (the "License").
69536SDina.Nimeh@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229536SDina.Nimeh@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #ifndef _RSA_IMPL_H
270Sstevel@tonic-gate #define	_RSA_IMPL_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #ifdef __cplusplus
300Sstevel@tonic-gate extern "C" {
310Sstevel@tonic-gate #endif
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <bignum.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	MIN_RSA_KEYLENGTH_IN_BYTES	32
370Sstevel@tonic-gate #define	MAX_RSA_KEYLENGTH_IN_BYTES	512
380Sstevel@tonic-gate #define	RSA_MIN_KEY_LEN	256	/* RSA min key length in bits */
390Sstevel@tonic-gate #define	RSA_MAX_KEY_LEN	4096	/* RSA max key length in bits */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	MIN_PKCS1_PADLEN	11
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #ifdef _KERNEL
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/sunddi.h>
460Sstevel@tonic-gate #include <sys/crypto/common.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	CK_BYTE			uchar_t
490Sstevel@tonic-gate #define	CK_ULONG		ulong_t
500Sstevel@tonic-gate #define	CK_RV			int
510Sstevel@tonic-gate #define	CKR_OK			CRYPTO_SUCCESS
520Sstevel@tonic-gate #define	CKR_HOST_MEMORY		CRYPTO_HOST_MEMORY
530Sstevel@tonic-gate #define	CKR_DATA_LEN_RANGE	CRYPTO_DATA_LEN_RANGE
540Sstevel@tonic-gate #define	CKR_ENCRYPTED_DATA_INVALID	CRYPTO_ENCRYPTED_DATA_INVALID
550Sstevel@tonic-gate #define	CKR_SIGNATURE_INVALID	CRYPTO_SIGNATURE_INVALID
560Sstevel@tonic-gate #define	CKR_FUNCTION_FAILED	CRYPTO_NOT_SUPPORTED
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #else
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <security/cryptoki.h>
610Sstevel@tonic-gate #include <security/pkcs11t.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #endif	/* _KERNEL */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	MD5_DER_PREFIX_Len	18
660Sstevel@tonic-gate #define	SHA1_DER_PREFIX_Len	15
67872Sizick #define	SHA1_DER_PREFIX_OID_Len	13
68676Sizick #define	SHA2_DER_PREFIX_Len	19
699536SDina.Nimeh@Sun.COM #define	DEFAULT_PUB_EXPO_Len	3
700Sstevel@tonic-gate 
710Sstevel@tonic-gate extern const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len];
720Sstevel@tonic-gate extern const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len];
73872Sizick extern const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len];
74676Sizick extern const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len];
75676Sizick extern const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len];
76676Sizick extern const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len];
779536SDina.Nimeh@Sun.COM extern const CK_BYTE DEFAULT_PUB_EXPO[DEFAULT_PUB_EXPO_Len];
780Sstevel@tonic-gate 
790Sstevel@tonic-gate typedef struct {
800Sstevel@tonic-gate 	int 	size;		/* key size in bits */
810Sstevel@tonic-gate 	BIGNUM	p;		/* p */
820Sstevel@tonic-gate 	BIGNUM	q;		/* q */
830Sstevel@tonic-gate 	BIGNUM	n;		/* n = p * q (the modulus) */
840Sstevel@tonic-gate 	BIGNUM	d;		/* private exponent */
850Sstevel@tonic-gate 	BIGNUM	e;		/* public exponent */
860Sstevel@tonic-gate 	BIGNUM	dmodpminus1;	/* d mod (p - 1) */
870Sstevel@tonic-gate 	BIGNUM	dmodqminus1;	/* d mod (q - 1) */
880Sstevel@tonic-gate 	BIGNUM	pinvmodq;	/* p^(-1) mod q */
890Sstevel@tonic-gate 	BIGNUM	p_rr;		/* 2^(2*(32*p->len)) mod p */
900Sstevel@tonic-gate 	BIGNUM	q_rr;		/* 2^(2*(32*q->len)) mod q */
910Sstevel@tonic-gate 	BIGNUM	n_rr;		/* 2^(2*(32*n->len)) mod n */
920Sstevel@tonic-gate } RSAkey;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate BIG_ERR_CODE RSA_key_init(RSAkey *key, int psize, int qsize);
950Sstevel@tonic-gate void RSA_key_finish(RSAkey *key);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate CK_RV soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
980Sstevel@tonic-gate     size_t datalen, uint8_t *padbuf, size_t padbuflen);
990Sstevel@tonic-gate CK_RV soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate CK_RV soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen,
1020Sstevel@tonic-gate     uint8_t *data, size_t mbit_l);
1030Sstevel@tonic-gate CK_RV soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #ifdef _KERNEL
1060Sstevel@tonic-gate int knzero_random_generator(uint8_t *ran_out, size_t ran_len);
1070Sstevel@tonic-gate void kmemset(uint8_t *buf, char pattern, size_t len);
1080Sstevel@tonic-gate #endif
1090Sstevel@tonic-gate 
110*10500SHai-May.Chao@Sun.COM /*
111*10500SHai-May.Chao@Sun.COM  * The following definitions and declarations are only used by RSA FIPS POST
112*10500SHai-May.Chao@Sun.COM  */
113*10500SHai-May.Chao@Sun.COM #ifdef _RSA_FIPS_POST
114*10500SHai-May.Chao@Sun.COM 
115*10500SHai-May.Chao@Sun.COM /* RSA FIPS Declarations */
116*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PUBLIC_EXPONENT_LENGTH		  3 /*   24-bits */
117*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIVATE_VERSION_LENGTH		  1 /*    8-bits */
118*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_MESSAGE_LENGTH			128 /* 1024-bits */
119*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_COEFFICIENT_LENGTH		 64 /*  512-bits */
120*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIME0_LENGTH			 64 /*  512-bits */
121*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIME1_LENGTH			 64 /*  512-bits */
122*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_EXPONENT0_LENGTH		 64 /*  512-bits */
123*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_EXPONENT1_LENGTH		 64 /*  512-bits */
124*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIVATE_EXPONENT_LENGTH	128 /* 1024-bits */
125*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_ENCRYPT_LENGTH			128 /* 1024-bits */
126*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_DECRYPT_LENGTH			128 /* 1024-bits */
127*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_SIGNATURE_LENGTH		128 /* 1024-bits */
128*10500SHai-May.Chao@Sun.COM #define	FIPS_RSA_MODULUS_LENGTH			128 /* 1024-bits */
129*10500SHai-May.Chao@Sun.COM #define	MAX_KEY_ATTR_BUFLEN			1024
130*10500SHai-May.Chao@Sun.COM 
131*10500SHai-May.Chao@Sun.COM typedef struct RSAPrivateKey_s {
132*10500SHai-May.Chao@Sun.COM 	uint8_t		*version;
133*10500SHai-May.Chao@Sun.COM 	int		version_len;
134*10500SHai-May.Chao@Sun.COM 	uint8_t		*modulus;
135*10500SHai-May.Chao@Sun.COM 	int		modulus_len;
136*10500SHai-May.Chao@Sun.COM 	uint8_t		*public_expo;
137*10500SHai-May.Chao@Sun.COM 	int		public_expo_len;
138*10500SHai-May.Chao@Sun.COM 	uint8_t		*private_expo;
139*10500SHai-May.Chao@Sun.COM 	int		private_expo_len;
140*10500SHai-May.Chao@Sun.COM 	uint8_t		*prime1;
141*10500SHai-May.Chao@Sun.COM 	int		prime1_len;
142*10500SHai-May.Chao@Sun.COM 	uint8_t		*prime2;
143*10500SHai-May.Chao@Sun.COM 	int		prime2_len;
144*10500SHai-May.Chao@Sun.COM 	uint8_t		*exponent1;
145*10500SHai-May.Chao@Sun.COM 	int		exponent1_len;
146*10500SHai-May.Chao@Sun.COM 	uint8_t		*exponent2;
147*10500SHai-May.Chao@Sun.COM 	int		exponent2_len;
148*10500SHai-May.Chao@Sun.COM 	uint8_t		*coef;
149*10500SHai-May.Chao@Sun.COM 	int		coef_len;
150*10500SHai-May.Chao@Sun.COM } RSAPrivateKey_t;
151*10500SHai-May.Chao@Sun.COM 
152*10500SHai-May.Chao@Sun.COM /* RSA FIPS functions */
153*10500SHai-May.Chao@Sun.COM extern int fips_rsa_post(void);
154*10500SHai-May.Chao@Sun.COM extern int fips_rsa_encrypt(uint8_t *, int, uint8_t *,
155*10500SHai-May.Chao@Sun.COM 	int, uint8_t *, int, uint8_t *);
156*10500SHai-May.Chao@Sun.COM extern int fips_rsa_decrypt(RSAPrivateKey_t *, uint8_t *,
157*10500SHai-May.Chao@Sun.COM 	int, uint8_t *);
158*10500SHai-May.Chao@Sun.COM extern int fips_rsa_sign(RSAPrivateKey_t *, uint8_t *,
159*10500SHai-May.Chao@Sun.COM 	uint32_t, uint8_t *);
160*10500SHai-May.Chao@Sun.COM extern int fips_rsa_verify(RSAPrivateKey_t *, uint8_t *, uint32_t,
161*10500SHai-May.Chao@Sun.COM 	uint8_t *);
162*10500SHai-May.Chao@Sun.COM 
163*10500SHai-May.Chao@Sun.COM #endif /* _RSA_FIPS_POST */
164*10500SHai-May.Chao@Sun.COM 
1650Sstevel@tonic-gate #ifdef	__cplusplus
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate #endif /* _RSA_IMPL_H */
170