xref: /onnv-gate/usr/src/common/crypto/rsa/rsa_impl.h (revision 12573:fb4ef506980f)
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  */
21*12573SDina.Nimeh@Sun.COM 
220Sstevel@tonic-gate /*
23*12573SDina.Nimeh@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
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 #ifdef _KERNEL
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/sunddi.h>
440Sstevel@tonic-gate #include <sys/crypto/common.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	CK_BYTE			uchar_t
470Sstevel@tonic-gate #define	CK_ULONG		ulong_t
480Sstevel@tonic-gate #define	CK_RV			int
49*12573SDina.Nimeh@Sun.COM 
500Sstevel@tonic-gate #define	CKR_OK			CRYPTO_SUCCESS
51*12573SDina.Nimeh@Sun.COM #define	CKR_ARGUMENTS_BAD	CRYPTO_ARGUMENTS_BAD
520Sstevel@tonic-gate #define	CKR_DATA_LEN_RANGE	CRYPTO_DATA_LEN_RANGE
53*12573SDina.Nimeh@Sun.COM #define	CKR_DEVICE_ERROR	CRYPTO_DEVICE_ERROR
54*12573SDina.Nimeh@Sun.COM #define	CKR_GENERAL_ERROR	CRYPTO_GENERAL_ERROR
55*12573SDina.Nimeh@Sun.COM #define	CKR_HOST_MEMORY		CRYPTO_HOST_MEMORY
56*12573SDina.Nimeh@Sun.COM #define	CKR_KEY_SIZE_RANGE	CRYPTO_KEY_SIZE_RANGE
57*12573SDina.Nimeh@Sun.COM 
58*12573SDina.Nimeh@Sun.COM int random_get_bytes(uint8_t *ran_out, size_t ran_len);
59*12573SDina.Nimeh@Sun.COM int random_get_pseudo_bytes(uint8_t *ran_out, size_t ran_len);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #else
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <security/cryptoki.h>
640Sstevel@tonic-gate #include <security/pkcs11t.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #endif	/* _KERNEL */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #define	MD5_DER_PREFIX_Len	18
690Sstevel@tonic-gate #define	SHA1_DER_PREFIX_Len	15
70872Sizick #define	SHA1_DER_PREFIX_OID_Len	13
71676Sizick #define	SHA2_DER_PREFIX_Len	19
729536SDina.Nimeh@Sun.COM #define	DEFAULT_PUB_EXPO_Len	3
730Sstevel@tonic-gate 
740Sstevel@tonic-gate extern const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len];
750Sstevel@tonic-gate extern const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len];
76872Sizick extern const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len];
77676Sizick extern const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len];
78676Sizick extern const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len];
79676Sizick extern const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len];
809536SDina.Nimeh@Sun.COM extern const CK_BYTE DEFAULT_PUB_EXPO[DEFAULT_PUB_EXPO_Len];
810Sstevel@tonic-gate 
82*12573SDina.Nimeh@Sun.COM 
83*12573SDina.Nimeh@Sun.COM /* RSA key using BIGNUM representations */
840Sstevel@tonic-gate typedef struct {
850Sstevel@tonic-gate 	int 	size;		/* key size in bits */
860Sstevel@tonic-gate 	BIGNUM	p;		/* p */
870Sstevel@tonic-gate 	BIGNUM	q;		/* q */
880Sstevel@tonic-gate 	BIGNUM	n;		/* n = p * q (the modulus) */
890Sstevel@tonic-gate 	BIGNUM	d;		/* private exponent */
900Sstevel@tonic-gate 	BIGNUM	e;		/* public exponent */
91*12573SDina.Nimeh@Sun.COM 	BIGNUM	dmodpminus1;	/* d mod (p - 1) (exponent 1) */
92*12573SDina.Nimeh@Sun.COM 	BIGNUM	dmodqminus1;	/* d mod (q - 1) (exponent 2) */
93*12573SDina.Nimeh@Sun.COM 	BIGNUM	pinvmodq;	/* p^(-1) mod q (the coefficient) */
940Sstevel@tonic-gate 	BIGNUM	p_rr;		/* 2^(2*(32*p->len)) mod p */
950Sstevel@tonic-gate 	BIGNUM	q_rr;		/* 2^(2*(32*q->len)) mod q */
960Sstevel@tonic-gate 	BIGNUM	n_rr;		/* 2^(2*(32*n->len)) mod n */
970Sstevel@tonic-gate } RSAkey;
980Sstevel@tonic-gate 
99*12573SDina.Nimeh@Sun.COM /* RSA key using byte string representations, useful for parameter lists */
100*12573SDina.Nimeh@Sun.COM typedef struct {
101*12573SDina.Nimeh@Sun.COM 	uint32_t modulus_bits;	/* size */
102*12573SDina.Nimeh@Sun.COM 	uchar_t	*modulus;	/* n */
103*12573SDina.Nimeh@Sun.COM 	uint32_t privexpo_bytes;
104*12573SDina.Nimeh@Sun.COM 	uchar_t	*privexpo;	/* d */
105*12573SDina.Nimeh@Sun.COM 	uint32_t pubexpo_bytes;
106*12573SDina.Nimeh@Sun.COM 	uchar_t	*pubexpo;	/* e */
107*12573SDina.Nimeh@Sun.COM 	uint32_t prime1_bytes;
108*12573SDina.Nimeh@Sun.COM 	uchar_t	*prime1;	/* p */
109*12573SDina.Nimeh@Sun.COM 	uint32_t prime2_bytes;
110*12573SDina.Nimeh@Sun.COM 	uchar_t	*prime2;	/* q */
111*12573SDina.Nimeh@Sun.COM 	uint32_t expo1_bytes;
112*12573SDina.Nimeh@Sun.COM 	uchar_t	*expo1;		/* = d mod (p - 1) */
113*12573SDina.Nimeh@Sun.COM 	uint32_t expo2_bytes;
114*12573SDina.Nimeh@Sun.COM 	uchar_t	*expo2;		/* = d mod (q - 1) */
115*12573SDina.Nimeh@Sun.COM 	uint32_t coeff_bytes;	/* = q bytes, .... or = p bytes */
116*12573SDina.Nimeh@Sun.COM 	uchar_t *coeff;		/* = p^(-1) mod q, or = q^(-1) mod p */
117*12573SDina.Nimeh@Sun.COM 	int (*rfunc)(void *, size_t);	/* random function */
118*12573SDina.Nimeh@Sun.COM } RSAbytekey;
1190Sstevel@tonic-gate 
120*12573SDina.Nimeh@Sun.COM 
121*12573SDina.Nimeh@Sun.COM CK_RV rsa_genkey_pair(RSAbytekey *bkey);
122*12573SDina.Nimeh@Sun.COM 
123*12573SDina.Nimeh@Sun.COM CK_RV rsa_encrypt(RSAbytekey *bkey,
124*12573SDina.Nimeh@Sun.COM     uchar_t *msg, uint32_t msglen, uchar_t *encrmsg);
1250Sstevel@tonic-gate 
126*12573SDina.Nimeh@Sun.COM CK_RV rsa_decrypt(RSAbytekey *bkey,
127*12573SDina.Nimeh@Sun.COM     uchar_t *encrmsg, uint32_t encrmsglen, uchar_t *msg);
128*12573SDina.Nimeh@Sun.COM 
129*12573SDina.Nimeh@Sun.COM #define	rsa_sign(key, msg, len, sig)	rsa_decrypt((key), (msg), (len), (sig))
130*12573SDina.Nimeh@Sun.COM #define	rsa_verify(key, msg, len, sig)	rsa_encrypt((key), (msg), (len), (sig))
1310Sstevel@tonic-gate 
13210500SHai-May.Chao@Sun.COM /*
13310500SHai-May.Chao@Sun.COM  * The following definitions and declarations are only used by RSA FIPS POST
13410500SHai-May.Chao@Sun.COM  */
13510500SHai-May.Chao@Sun.COM #ifdef _RSA_FIPS_POST
13610500SHai-May.Chao@Sun.COM 
13710500SHai-May.Chao@Sun.COM /* RSA FIPS Declarations */
13810500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PUBLIC_EXPONENT_LENGTH		  3 /*   24-bits */
13910500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIVATE_VERSION_LENGTH		  1 /*    8-bits */
14010500SHai-May.Chao@Sun.COM #define	FIPS_RSA_MESSAGE_LENGTH			128 /* 1024-bits */
14110500SHai-May.Chao@Sun.COM #define	FIPS_RSA_COEFFICIENT_LENGTH		 64 /*  512-bits */
14210500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIME0_LENGTH			 64 /*  512-bits */
14310500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIME1_LENGTH			 64 /*  512-bits */
14410500SHai-May.Chao@Sun.COM #define	FIPS_RSA_EXPONENT0_LENGTH		 64 /*  512-bits */
14510500SHai-May.Chao@Sun.COM #define	FIPS_RSA_EXPONENT1_LENGTH		 64 /*  512-bits */
14610500SHai-May.Chao@Sun.COM #define	FIPS_RSA_PRIVATE_EXPONENT_LENGTH	128 /* 1024-bits */
14710500SHai-May.Chao@Sun.COM #define	FIPS_RSA_ENCRYPT_LENGTH			128 /* 1024-bits */
14810500SHai-May.Chao@Sun.COM #define	FIPS_RSA_DECRYPT_LENGTH			128 /* 1024-bits */
14910500SHai-May.Chao@Sun.COM #define	FIPS_RSA_SIGNATURE_LENGTH		128 /* 1024-bits */
15010500SHai-May.Chao@Sun.COM #define	FIPS_RSA_MODULUS_LENGTH			128 /* 1024-bits */
15110500SHai-May.Chao@Sun.COM #define	MAX_KEY_ATTR_BUFLEN			1024
15210500SHai-May.Chao@Sun.COM 
15310500SHai-May.Chao@Sun.COM typedef struct RSAPrivateKey_s {
15410500SHai-May.Chao@Sun.COM 	uint8_t		*version;
15510500SHai-May.Chao@Sun.COM 	int		version_len;
156*12573SDina.Nimeh@Sun.COM 	RSAbytekey	bkey;
15710500SHai-May.Chao@Sun.COM } RSAPrivateKey_t;
15810500SHai-May.Chao@Sun.COM 
15910500SHai-May.Chao@Sun.COM /* RSA FIPS functions */
16010500SHai-May.Chao@Sun.COM extern int fips_rsa_post(void);
161*12573SDina.Nimeh@Sun.COM extern int fips_rsa_encrypt(RSAPrivateKey_t *, uint8_t *, int, uint8_t *);
162*12573SDina.Nimeh@Sun.COM extern int fips_rsa_decrypt(RSAPrivateKey_t *, uint8_t *, int, uint8_t *);
16310500SHai-May.Chao@Sun.COM 
16410500SHai-May.Chao@Sun.COM #endif /* _RSA_FIPS_POST */
16510500SHai-May.Chao@Sun.COM 
1660Sstevel@tonic-gate #ifdef	__cplusplus
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate #endif
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate #endif /* _RSA_IMPL_H */
171