28 #include <string.h>
29 #include <strings.h>
30 #include <sys/types.h>
31 #include <security/cryptoki.h>
32 #include <cryptoutil.h>
33 #include "softGlobal.h"
34 #include "softSession.h"
35 #include "softObject.h"
36 #include "softOps.h"
37 #include "softRSA.h"
38 #include "softMAC.h"
39 #include "softCrypt.h"
40
41 CK_RV
42 soft_rsa_encrypt(soft_object_t *key, CK_BYTE_PTR in, uint32_t in_len,
43 CK_BYTE_PTR out, int realpublic)
44 {
45
46 CK_RV rv = CKR_OK;
47
48 /* EXPORT DELETE START */
49
50 uchar_t expo[MAX_KEY_ATTR_BUFLEN];
51 uchar_t modulus[MAX_KEY_ATTR_BUFLEN];
52 uint32_t expo_len = sizeof (expo);
53 uint32_t modulus_len = sizeof (modulus);
54 RSAbytekey k;
55
56 if (realpublic) {
57 rv = soft_get_public_value(key, CKA_PUBLIC_EXPONENT, expo,
58 &expo_len);
59 if (rv != CKR_OK) {
60 goto clean1;
61 }
62 } else {
63 rv = soft_get_private_value(key, CKA_PRIVATE_EXPONENT, expo,
64 &expo_len);
65 if (rv != CKR_OK) {
66 goto clean1;
67 }
68 }
69
70 rv = soft_get_public_value(key, CKA_MODULUS, modulus, &modulus_len);
71 if (rv != CKR_OK) {
72 goto clean1;
73 }
74
75 k.modulus = modulus;
76 k.modulus_bits = CRYPTO_BYTES2BITS(modulus_len);
77 k.pubexpo = expo;
78 k.pubexpo_bytes = expo_len;
79 k.rfunc = NULL;
80
81 rv = rsa_encrypt(&k, in, in_len, out);
82
83 clean1:
84
85 /* EXPORT DELETE END */
86
87 return (rv);
88 }
89
90
91 CK_RV
92 soft_rsa_decrypt(soft_object_t *key, CK_BYTE_PTR in, uint32_t in_len,
93 CK_BYTE_PTR out)
94 {
95
96 CK_RV rv = CKR_OK;
97
98 /* EXPORT DELETE START */
99
100 uchar_t modulus[MAX_KEY_ATTR_BUFLEN];
101 uchar_t prime1[MAX_KEY_ATTR_BUFLEN];
102 uchar_t prime2[MAX_KEY_ATTR_BUFLEN];
103 uchar_t expo1[MAX_KEY_ATTR_BUFLEN];
104 uchar_t expo2[MAX_KEY_ATTR_BUFLEN];
105 uchar_t coef[MAX_KEY_ATTR_BUFLEN];
106 uint32_t modulus_len = sizeof (modulus);
107 uint32_t prime1_len = sizeof (prime1);
108 uint32_t prime2_len = sizeof (prime2);
109 uint32_t expo1_len = sizeof (expo1);
110 uint32_t expo2_len = sizeof (expo2);
111 uint32_t coef_len = sizeof (coef);
112 RSAbytekey k;
113
114 rv = soft_get_private_value(key, CKA_MODULUS, modulus, &modulus_len);
115 if (rv != CKR_OK) {
116 goto clean1;
117 }
118
119 rv = soft_get_private_value(key, CKA_PRIME_1, prime1, &prime1_len);
167 }
168
169 k.modulus = modulus;
170 k.modulus_bits = CRYPTO_BYTES2BITS(modulus_len);
171 k.prime1 = prime1;
172 k.prime1_bytes = prime1_len;
173 k.prime2 = prime2;
174 k.prime2_bytes = prime2_len;
175 k.expo1 = expo1;
176 k.expo1_bytes = expo1_len;
177 k.expo2 = expo2;
178 k.expo2_bytes = expo2_len;
179 k.coeff = coef;
180 k.coeff_bytes = coef_len;
181 k.rfunc = NULL;
182
183 rv = rsa_decrypt(&k, in, in_len, out);
184
185 clean1:
186
187 /* EXPORT DELETE END */
188
189 return (rv);
190 }
191
192 /*
193 * Allocate a RSA context for the active encryption or decryption operation.
194 * This function is called without the session lock held.
195 */
196 CK_RV
197 soft_rsa_crypt_init_common(soft_session_t *session_p,
198 CK_MECHANISM_PTR pMechanism, soft_object_t *key_p,
199 boolean_t encrypt)
200 {
201
202 soft_rsa_ctx_t *rsa_ctx;
203 soft_object_t *tmp_key = NULL;
204 CK_RV rv;
205
206 rsa_ctx = calloc(1, sizeof (soft_rsa_ctx_t));
207 if (rsa_ctx == NULL) {
208 return (CKR_HOST_MEMORY);
|
28 #include <string.h>
29 #include <strings.h>
30 #include <sys/types.h>
31 #include <security/cryptoki.h>
32 #include <cryptoutil.h>
33 #include "softGlobal.h"
34 #include "softSession.h"
35 #include "softObject.h"
36 #include "softOps.h"
37 #include "softRSA.h"
38 #include "softMAC.h"
39 #include "softCrypt.h"
40
41 CK_RV
42 soft_rsa_encrypt(soft_object_t *key, CK_BYTE_PTR in, uint32_t in_len,
43 CK_BYTE_PTR out, int realpublic)
44 {
45
46 CK_RV rv = CKR_OK;
47
48 uchar_t expo[MAX_KEY_ATTR_BUFLEN];
49 uchar_t modulus[MAX_KEY_ATTR_BUFLEN];
50 uint32_t expo_len = sizeof (expo);
51 uint32_t modulus_len = sizeof (modulus);
52 RSAbytekey k;
53
54 if (realpublic) {
55 rv = soft_get_public_value(key, CKA_PUBLIC_EXPONENT, expo,
56 &expo_len);
57 if (rv != CKR_OK) {
58 goto clean1;
59 }
60 } else {
61 rv = soft_get_private_value(key, CKA_PRIVATE_EXPONENT, expo,
62 &expo_len);
63 if (rv != CKR_OK) {
64 goto clean1;
65 }
66 }
67
68 rv = soft_get_public_value(key, CKA_MODULUS, modulus, &modulus_len);
69 if (rv != CKR_OK) {
70 goto clean1;
71 }
72
73 k.modulus = modulus;
74 k.modulus_bits = CRYPTO_BYTES2BITS(modulus_len);
75 k.pubexpo = expo;
76 k.pubexpo_bytes = expo_len;
77 k.rfunc = NULL;
78
79 rv = rsa_encrypt(&k, in, in_len, out);
80
81 clean1:
82
83 return (rv);
84 }
85
86
87 CK_RV
88 soft_rsa_decrypt(soft_object_t *key, CK_BYTE_PTR in, uint32_t in_len,
89 CK_BYTE_PTR out)
90 {
91
92 CK_RV rv = CKR_OK;
93
94 uchar_t modulus[MAX_KEY_ATTR_BUFLEN];
95 uchar_t prime1[MAX_KEY_ATTR_BUFLEN];
96 uchar_t prime2[MAX_KEY_ATTR_BUFLEN];
97 uchar_t expo1[MAX_KEY_ATTR_BUFLEN];
98 uchar_t expo2[MAX_KEY_ATTR_BUFLEN];
99 uchar_t coef[MAX_KEY_ATTR_BUFLEN];
100 uint32_t modulus_len = sizeof (modulus);
101 uint32_t prime1_len = sizeof (prime1);
102 uint32_t prime2_len = sizeof (prime2);
103 uint32_t expo1_len = sizeof (expo1);
104 uint32_t expo2_len = sizeof (expo2);
105 uint32_t coef_len = sizeof (coef);
106 RSAbytekey k;
107
108 rv = soft_get_private_value(key, CKA_MODULUS, modulus, &modulus_len);
109 if (rv != CKR_OK) {
110 goto clean1;
111 }
112
113 rv = soft_get_private_value(key, CKA_PRIME_1, prime1, &prime1_len);
161 }
162
163 k.modulus = modulus;
164 k.modulus_bits = CRYPTO_BYTES2BITS(modulus_len);
165 k.prime1 = prime1;
166 k.prime1_bytes = prime1_len;
167 k.prime2 = prime2;
168 k.prime2_bytes = prime2_len;
169 k.expo1 = expo1;
170 k.expo1_bytes = expo1_len;
171 k.expo2 = expo2;
172 k.expo2_bytes = expo2_len;
173 k.coeff = coef;
174 k.coeff_bytes = coef_len;
175 k.rfunc = NULL;
176
177 rv = rsa_decrypt(&k, in, in_len, out);
178
179 clean1:
180
181 return (rv);
182 }
183
184 /*
185 * Allocate a RSA context for the active encryption or decryption operation.
186 * This function is called without the session lock held.
187 */
188 CK_RV
189 soft_rsa_crypt_init_common(soft_session_t *session_p,
190 CK_MECHANISM_PTR pMechanism, soft_object_t *key_p,
191 boolean_t encrypt)
192 {
193
194 soft_rsa_ctx_t *rsa_ctx;
195 soft_object_t *tmp_key = NULL;
196 CK_RV rv;
197
198 rsa_ctx = calloc(1, sizeof (soft_rsa_ctx_t));
199 if (rsa_ctx == NULL) {
200 return (CKR_HOST_MEMORY);
|