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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _RSA_IMPL_H
28 #define	_RSA_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/types.h>
37 #include <bignum.h>
38 
39 #define	MIN_RSA_KEYLENGTH_IN_BYTES	32
40 #define	MAX_RSA_KEYLENGTH_IN_BYTES	512
41 #define	RSA_MIN_KEY_LEN	256	/* RSA min key length in bits */
42 #define	RSA_MAX_KEY_LEN	4096	/* RSA max key length in bits */
43 
44 #define	MIN_PKCS1_PADLEN	11
45 
46 #ifdef _KERNEL
47 
48 #include <sys/sunddi.h>
49 #include <sys/crypto/common.h>
50 
51 #define	CK_BYTE			uchar_t
52 #define	CK_ULONG		ulong_t
53 #define	CK_RV			int
54 #define	CKR_OK			CRYPTO_SUCCESS
55 #define	CKR_HOST_MEMORY		CRYPTO_HOST_MEMORY
56 #define	CKR_DATA_LEN_RANGE	CRYPTO_DATA_LEN_RANGE
57 #define	CKR_ENCRYPTED_DATA_INVALID	CRYPTO_ENCRYPTED_DATA_INVALID
58 #define	CKR_SIGNATURE_INVALID	CRYPTO_SIGNATURE_INVALID
59 #define	CKR_FUNCTION_FAILED	CRYPTO_NOT_SUPPORTED
60 
61 #else
62 
63 #include <security/cryptoki.h>
64 #include <security/pkcs11t.h>
65 
66 #endif	/* _KERNEL */
67 
68 #define	MD5_DER_PREFIX_Len	18
69 #define	SHA1_DER_PREFIX_Len	15
70 #define	SHA1_DER_PREFIX_OID_Len	13
71 #define	SHA2_DER_PREFIX_Len	19
72 
73 extern const CK_BYTE MD5_DER_PREFIX[MD5_DER_PREFIX_Len];
74 extern const CK_BYTE SHA1_DER_PREFIX[SHA1_DER_PREFIX_Len];
75 extern const CK_BYTE SHA1_DER_PREFIX_OID[SHA1_DER_PREFIX_OID_Len];
76 extern const CK_BYTE SHA256_DER_PREFIX[SHA2_DER_PREFIX_Len];
77 extern const CK_BYTE SHA384_DER_PREFIX[SHA2_DER_PREFIX_Len];
78 extern const CK_BYTE SHA512_DER_PREFIX[SHA2_DER_PREFIX_Len];
79 
80 typedef struct {
81 	int 	size;		/* key size in bits */
82 	BIGNUM	p;		/* p */
83 	BIGNUM	q;		/* q */
84 	BIGNUM	n;		/* n = p * q (the modulus) */
85 	BIGNUM	d;		/* private exponent */
86 	BIGNUM	e;		/* public exponent */
87 	BIGNUM	dmodpminus1;	/* d mod (p - 1) */
88 	BIGNUM	dmodqminus1;	/* d mod (q - 1) */
89 	BIGNUM	pinvmodq;	/* p^(-1) mod q */
90 	BIGNUM	p_rr;		/* 2^(2*(32*p->len)) mod p */
91 	BIGNUM	q_rr;		/* 2^(2*(32*q->len)) mod q */
92 	BIGNUM	n_rr;		/* 2^(2*(32*n->len)) mod n */
93 } RSAkey;
94 
95 
96 BIG_ERR_CODE RSA_key_init(RSAkey *key, int psize, int qsize);
97 void RSA_key_finish(RSAkey *key);
98 
99 CK_RV soft_encrypt_rsa_pkcs_encode(uint8_t *databuf,
100     size_t datalen, uint8_t *padbuf, size_t padbuflen);
101 CK_RV soft_decrypt_rsa_pkcs_decode(uint8_t *padbuf, int *plen);
102 
103 CK_RV soft_sign_rsa_pkcs_encode(uint8_t *pData, size_t dataLen,
104     uint8_t *data, size_t mbit_l);
105 CK_RV soft_verify_rsa_pkcs_decode(uint8_t *data, int *mbit_l);
106 
107 #ifdef _KERNEL
108 int knzero_random_generator(uint8_t *ran_out, size_t ran_len);
109 void kmemset(uint8_t *buf, char pattern, size_t len);
110 #endif
111 
112 #ifdef	__cplusplus
113 }
114 #endif
115 
116 #endif /* _RSA_IMPL_H */
117