diff options
Diffstat (limited to 'src/lib/libcrypto/engine/hw_cryptodev.c')
-rw-r--r-- | src/lib/libcrypto/engine/hw_cryptodev.c | 122 |
1 files changed, 60 insertions, 62 deletions
diff --git a/src/lib/libcrypto/engine/hw_cryptodev.c b/src/lib/libcrypto/engine/hw_cryptodev.c index 40af97ac24..b502d12b6d 100644 --- a/src/lib/libcrypto/engine/hw_cryptodev.c +++ b/src/lib/libcrypto/engine/hw_cryptodev.c | |||
@@ -12,9 +12,6 @@ | |||
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the author nor the names of contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | 15 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
@@ -71,14 +68,19 @@ struct dev_crypto_state { | |||
71 | int d_fd; | 68 | int d_fd; |
72 | }; | 69 | }; |
73 | 70 | ||
71 | struct dev_crypto_cipher { | ||
72 | int c_id; | ||
73 | int c_nid; | ||
74 | int c_ivmax; | ||
75 | int c_keylen; | ||
76 | }; | ||
77 | |||
74 | static u_int32_t cryptodev_asymfeat = 0; | 78 | static u_int32_t cryptodev_asymfeat = 0; |
75 | 79 | ||
76 | static int get_asym_dev_crypto(void); | 80 | static int get_asym_dev_crypto(void); |
77 | static int open_dev_crypto(void); | 81 | static int open_dev_crypto(void); |
78 | static int get_dev_crypto(void); | 82 | static int get_dev_crypto(void); |
79 | static int cryptodev_max_iv(int cipher); | 83 | static struct dev_crypto_cipher *cipher_nid_to_cryptodev(int nid); |
80 | static int cryptodev_key_length_valid(int cipher, int len); | ||
81 | static int cipher_nid_to_cryptodev(int nid); | ||
82 | static int get_cryptodev_ciphers(const int **cnids); | 84 | static int get_cryptodev_ciphers(const int **cnids); |
83 | static int get_cryptodev_digests(const int **cnids); | 85 | static int get_cryptodev_digests(const int **cnids); |
84 | static int cryptodev_usable_ciphers(const int **nids); | 86 | static int cryptodev_usable_ciphers(const int **nids); |
@@ -125,15 +127,12 @@ static const ENGINE_CMD_DEFN cryptodev_defns[] = { | |||
125 | { 0, NULL, NULL, 0 } | 127 | { 0, NULL, NULL, 0 } |
126 | }; | 128 | }; |
127 | 129 | ||
128 | static struct { | 130 | static struct dev_crypto_cipher ciphers[] = { |
129 | int id; | ||
130 | int nid; | ||
131 | int ivmax; | ||
132 | int keylen; | ||
133 | } ciphers[] = { | ||
134 | { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, }, | 131 | { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, }, |
135 | { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, }, | 132 | { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, }, |
136 | { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, }, | 133 | { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, }, |
134 | { CRYPTO_AES_CBC, NID_aes_192_cbc, 16, 24, }, | ||
135 | { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, }, | ||
137 | { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, }, | 136 | { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, }, |
138 | { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, }, | 137 | { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, }, |
139 | { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, }, | 138 | { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, }, |
@@ -203,48 +202,16 @@ get_asym_dev_crypto(void) | |||
203 | return fd; | 202 | return fd; |
204 | } | 203 | } |
205 | 204 | ||
206 | /* | ||
207 | * XXXX this needs to be set for each alg - and determined from | ||
208 | * a running card. | ||
209 | */ | ||
210 | static int | ||
211 | cryptodev_max_iv(int cipher) | ||
212 | { | ||
213 | int i; | ||
214 | |||
215 | for (i = 0; ciphers[i].id; i++) | ||
216 | if (ciphers[i].id == cipher) | ||
217 | return (ciphers[i].ivmax); | ||
218 | return (0); | ||
219 | } | ||
220 | |||
221 | /* | ||
222 | * XXXX this needs to be set for each alg - and determined from | ||
223 | * a running card. For now, fake it out - but most of these | ||
224 | * for real devices should return 1 for the supported key | ||
225 | * sizes the device can handle. | ||
226 | */ | ||
227 | static int | ||
228 | cryptodev_key_length_valid(int cipher, int len) | ||
229 | { | ||
230 | int i; | ||
231 | |||
232 | for (i = 0; ciphers[i].id; i++) | ||
233 | if (ciphers[i].id == cipher) | ||
234 | return (ciphers[i].keylen == len); | ||
235 | return (0); | ||
236 | } | ||
237 | |||
238 | /* convert libcrypto nids to cryptodev */ | 205 | /* convert libcrypto nids to cryptodev */ |
239 | static int | 206 | static struct dev_crypto_cipher * |
240 | cipher_nid_to_cryptodev(int nid) | 207 | cipher_nid_to_cryptodev(int nid) |
241 | { | 208 | { |
242 | int i; | 209 | int i; |
243 | 210 | ||
244 | for (i = 0; ciphers[i].id; i++) | 211 | for (i = 0; ciphers[i].c_id; i++) |
245 | if (ciphers[i].nid == nid) | 212 | if (ciphers[i].c_nid == nid) |
246 | return (ciphers[i].id); | 213 | return (&ciphers[i]); |
247 | return (0); | 214 | return (NULL); |
248 | } | 215 | } |
249 | 216 | ||
250 | /* | 217 | /* |
@@ -267,15 +234,15 @@ get_cryptodev_ciphers(const int **cnids) | |||
267 | memset(&sess, 0, sizeof(sess)); | 234 | memset(&sess, 0, sizeof(sess)); |
268 | sess.key = (caddr_t)"123456781234567812345678"; | 235 | sess.key = (caddr_t)"123456781234567812345678"; |
269 | 236 | ||
270 | for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { | 237 | for (i = 0; ciphers[i].c_id && count < CRYPTO_ALGORITHM_MAX; i++) { |
271 | if (ciphers[i].nid == NID_undef) | 238 | if (ciphers[i].c_nid == NID_undef) |
272 | continue; | 239 | continue; |
273 | sess.cipher = ciphers[i].id; | 240 | sess.cipher = ciphers[i].c_id; |
274 | sess.keylen = ciphers[i].keylen; | 241 | sess.keylen = ciphers[i].c_keylen; |
275 | sess.mac = 0; | 242 | sess.mac = 0; |
276 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | 243 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && |
277 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | 244 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) |
278 | nids[count++] = ciphers[i].nid; | 245 | nids[count++] = ciphers[i].c_nid; |
279 | } | 246 | } |
280 | close(fd); | 247 | close(fd); |
281 | 248 | ||
@@ -428,15 +395,15 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
428 | { | 395 | { |
429 | struct dev_crypto_state *state = ctx->cipher_data; | 396 | struct dev_crypto_state *state = ctx->cipher_data; |
430 | struct session_op *sess = &state->d_sess; | 397 | struct session_op *sess = &state->d_sess; |
431 | int cipher; | 398 | struct dev_crypto_cipher *cipher; |
432 | 399 | ||
433 | if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NID_undef) | 400 | if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NULL) |
434 | return (0); | 401 | return (0); |
435 | 402 | ||
436 | if (ctx->cipher->iv_len > cryptodev_max_iv(cipher)) | 403 | if (ctx->cipher->iv_len > cipher->c_ivmax) |
437 | return (0); | 404 | return (0); |
438 | 405 | ||
439 | if (!cryptodev_key_length_valid(cipher, ctx->key_len)) | 406 | if (ctx->key_len != cipher->c_keylen) |
440 | return (0); | 407 | return (0); |
441 | 408 | ||
442 | memset(sess, 0, sizeof(struct session_op)); | 409 | memset(sess, 0, sizeof(struct session_op)); |
@@ -446,7 +413,7 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
446 | 413 | ||
447 | sess->key = (unsigned char *)key; | 414 | sess->key = (unsigned char *)key; |
448 | sess->keylen = ctx->key_len; | 415 | sess->keylen = ctx->key_len; |
449 | sess->cipher = cipher; | 416 | sess->cipher = cipher->c_id; |
450 | 417 | ||
451 | if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { | 418 | if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { |
452 | close(state->d_fd); | 419 | close(state->d_fd); |
@@ -551,7 +518,7 @@ const EVP_CIPHER cryptodev_cast_cbc = { | |||
551 | NULL | 518 | NULL |
552 | }; | 519 | }; |
553 | 520 | ||
554 | const EVP_CIPHER cryptodev_aes_cbc = { | 521 | const EVP_CIPHER cryptodev_aes_128_cbc = { |
555 | NID_aes_128_cbc, | 522 | NID_aes_128_cbc, |
556 | 16, 16, 16, | 523 | 16, 16, 16, |
557 | EVP_CIPH_CBC_MODE, | 524 | EVP_CIPH_CBC_MODE, |
@@ -564,6 +531,32 @@ const EVP_CIPHER cryptodev_aes_cbc = { | |||
564 | NULL | 531 | NULL |
565 | }; | 532 | }; |
566 | 533 | ||
534 | const EVP_CIPHER cryptodev_aes_192_cbc = { | ||
535 | NID_aes_192_cbc, | ||
536 | 16, 24, 16, | ||
537 | EVP_CIPH_CBC_MODE, | ||
538 | cryptodev_init_key, | ||
539 | cryptodev_cipher, | ||
540 | cryptodev_cleanup, | ||
541 | sizeof(struct dev_crypto_state), | ||
542 | EVP_CIPHER_set_asn1_iv, | ||
543 | EVP_CIPHER_get_asn1_iv, | ||
544 | NULL | ||
545 | }; | ||
546 | |||
547 | const EVP_CIPHER cryptodev_aes_256_cbc = { | ||
548 | NID_aes_256_cbc, | ||
549 | 16, 32, 16, | ||
550 | EVP_CIPH_CBC_MODE, | ||
551 | cryptodev_init_key, | ||
552 | cryptodev_cipher, | ||
553 | cryptodev_cleanup, | ||
554 | sizeof(struct dev_crypto_state), | ||
555 | EVP_CIPHER_set_asn1_iv, | ||
556 | EVP_CIPHER_get_asn1_iv, | ||
557 | NULL | ||
558 | }; | ||
559 | |||
567 | /* | 560 | /* |
568 | * Registered by the ENGINE when used to find out how to deal with | 561 | * Registered by the ENGINE when used to find out how to deal with |
569 | * a particular NID in the ENGINE. this says what we'll do at the | 562 | * a particular NID in the ENGINE. this says what we'll do at the |
@@ -590,7 +583,13 @@ cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | |||
590 | *cipher = &cryptodev_cast_cbc; | 583 | *cipher = &cryptodev_cast_cbc; |
591 | break; | 584 | break; |
592 | case NID_aes_128_cbc: | 585 | case NID_aes_128_cbc: |
593 | *cipher = &cryptodev_aes_cbc; | 586 | *cipher = &cryptodev_aes_128_cbc; |
587 | break; | ||
588 | case NID_aes_192_cbc: | ||
589 | *cipher = &cryptodev_aes_192_cbc; | ||
590 | break; | ||
591 | case NID_aes_256_cbc: | ||
592 | *cipher = &cryptodev_aes_256_cbc; | ||
594 | break; | 593 | break; |
595 | default: | 594 | default: |
596 | *cipher = NULL; | 595 | *cipher = NULL; |
@@ -874,7 +873,6 @@ cryptodev_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) | |||
874 | goto err; | 873 | goto err; |
875 | } | 874 | } |
876 | 875 | ||
877 | printf("bar\n"); | ||
878 | memset(&kop, 0, sizeof kop); | 876 | memset(&kop, 0, sizeof kop); |
879 | kop.crk_op = CRK_DSA_SIGN; | 877 | kop.crk_op = CRK_DSA_SIGN; |
880 | 878 | ||