xref: /titanic_50/usr/src/common/crypto/rsa/rsa_impl.h (revision dd52495f0d9ba8ff6d84921ec0500be837896554)
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 
95 BIG_ERR_CODE RSA_key_init(RSAkey *key, int psize, int qsize);
96 void RSA_key_finish(RSAkey *key);
97 
98 CK_RV soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
99     size_t datalen, uint8_t *padbuf, size_t padbuflen);
100 CK_RV soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen);
101 
102 CK_RV soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen,
103     uint8_t *data, size_t mbit_l);
104 CK_RV soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l);
105 
106 #ifdef _KERNEL
107 int knzero_random_generator(uint8_t *ran_out, size_t ran_len);
108 void kmemset(uint8_t *buf, char pattern, size_t len);
109 #endif
110 
111 #ifdef	__cplusplus
112 }
113 #endif
114 
115 #endif /* _RSA_IMPL_H */
116