diff options
Diffstat (limited to 'src/lib/libcrypto/engine/hw_cryptodev.c')
| -rw-r--r-- | src/lib/libcrypto/engine/hw_cryptodev.c | 707 |
1 files changed, 312 insertions, 395 deletions
diff --git a/src/lib/libcrypto/engine/hw_cryptodev.c b/src/lib/libcrypto/engine/hw_cryptodev.c index 5a715aca4f..bc953872dd 100644 --- a/src/lib/libcrypto/engine/hw_cryptodev.c +++ b/src/lib/libcrypto/engine/hw_cryptodev.c | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2002-2004 Theo de Raadt | ||
| 2 | * Copyright (c) 2002 Bob Beck <beck@openbsd.org> | 3 | * Copyright (c) 2002 Bob Beck <beck@openbsd.org> |
| 3 | * Copyright (c) 2002 Theo de Raadt | ||
| 4 | * Copyright (c) 2002 Markus Friedl | 4 | * Copyright (c) 2002 Markus Friedl |
| 5 | * All rights reserved. | 5 | * All rights reserved. |
| 6 | * | 6 | * |
| @@ -29,16 +29,14 @@ | |||
| 29 | #include <openssl/objects.h> | 29 | #include <openssl/objects.h> |
| 30 | #include <openssl/engine.h> | 30 | #include <openssl/engine.h> |
| 31 | #include <openssl/evp.h> | 31 | #include <openssl/evp.h> |
| 32 | #include <openssl/bn.h> | ||
| 33 | 32 | ||
| 34 | #if (defined(__unix__) || defined(unix)) && !defined(USG) && \ | 33 | #if (defined(__unix__) || defined(unix)) && !defined(USG) |
| 35 | (defined(OpenBSD) || defined(__FreeBSD__)) | ||
| 36 | #include <sys/param.h> | 34 | #include <sys/param.h> |
| 37 | # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041) | 35 | # if (OpenBSD >= 200112) || ((__FreeBSD_version >= 470101 && __FreeBSD_version < 500000) || __FreeBSD_version >= 500041) |
| 38 | # define HAVE_CRYPTODEV | 36 | # define HAVE_CRYPTODEV |
| 39 | # endif | 37 | # endif |
| 40 | # if (OpenBSD >= 200110) | 38 | # if (OpenBSD >= 200110) |
| 41 | # define HAVE_SYSLOG_R | 39 | # define HAVE_SYSLOG_R |
| 42 | # endif | 40 | # endif |
| 43 | #endif | 41 | #endif |
| 44 | 42 | ||
| @@ -51,35 +49,43 @@ ENGINE_load_cryptodev(void) | |||
| 51 | return; | 49 | return; |
| 52 | } | 50 | } |
| 53 | 51 | ||
| 54 | #else | 52 | #else |
| 55 | 53 | ||
| 56 | #include <sys/types.h> | 54 | #include <sys/types.h> |
| 57 | #include <crypto/cryptodev.h> | 55 | #include <crypto/cryptodev.h> |
| 58 | #include <crypto/dh/dh.h> | ||
| 59 | #include <crypto/dsa/dsa.h> | ||
| 60 | #include <crypto/err/err.h> | ||
| 61 | #include <crypto/rsa/rsa.h> | ||
| 62 | #include <sys/ioctl.h> | 56 | #include <sys/ioctl.h> |
| 57 | |||
| 63 | #include <errno.h> | 58 | #include <errno.h> |
| 64 | #include <stdio.h> | ||
| 65 | #include <unistd.h> | ||
| 66 | #include <fcntl.h> | 59 | #include <fcntl.h> |
| 60 | #include <limits.h> | ||
| 67 | #include <stdarg.h> | 61 | #include <stdarg.h> |
| 68 | #include <syslog.h> | 62 | #include <stdio.h> |
| 69 | #include <errno.h> | ||
| 70 | #include <string.h> | 63 | #include <string.h> |
| 64 | #include <syslog.h> | ||
| 65 | #include <unistd.h> | ||
| 66 | |||
| 67 | #if defined(__i386__) || defined(__amd64__) | ||
| 68 | #include <sys/sysctl.h> | ||
| 69 | #include <machine/cpu.h> | ||
| 70 | #include <machine/specialreg.h> | ||
| 71 | |||
| 72 | #include <ssl/aes.h> | ||
| 73 | |||
| 74 | static int check_viac3aes(void); | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #define CRYPTO_VIAC3_MAX 3 | ||
| 71 | 78 | ||
| 72 | struct dev_crypto_state { | 79 | struct dev_crypto_state { |
| 73 | struct session_op d_sess; | 80 | struct session_op d_sess; |
| 74 | int d_fd; | 81 | int d_fd; |
| 82 | }; | ||
| 75 | 83 | ||
| 76 | #ifdef USE_CRYPTODEV_DIGESTS | 84 | struct dev_crypto_cipher { |
| 77 | char dummy_mac_key[HASH_MAX_LEN]; | 85 | int c_id; |
| 78 | 86 | int c_nid; | |
| 79 | unsigned char digest_res[HASH_MAX_LEN]; | 87 | int c_ivmax; |
| 80 | char *mac_data; | 88 | int c_keylen; |
| 81 | int mac_len; | ||
| 82 | #endif | ||
| 83 | }; | 89 | }; |
| 84 | 90 | ||
| 85 | static u_int32_t cryptodev_asymfeat = 0; | 91 | static u_int32_t cryptodev_asymfeat = 0; |
| @@ -87,10 +93,9 @@ static u_int32_t cryptodev_asymfeat = 0; | |||
| 87 | static int get_asym_dev_crypto(void); | 93 | static int get_asym_dev_crypto(void); |
| 88 | static int open_dev_crypto(void); | 94 | static int open_dev_crypto(void); |
| 89 | static int get_dev_crypto(void); | 95 | static int get_dev_crypto(void); |
| 96 | static struct dev_crypto_cipher *cipher_nid_to_cryptodev(int nid); | ||
| 90 | static int get_cryptodev_ciphers(const int **cnids); | 97 | static int get_cryptodev_ciphers(const int **cnids); |
| 91 | #ifdef USE_CRYPTODEV_DIGESTS | 98 | /*static int get_cryptodev_digests(const int **cnids);*/ |
| 92 | static int get_cryptodev_digests(const int **cnids); | ||
| 93 | #endif | ||
| 94 | static int cryptodev_usable_ciphers(const int **nids); | 99 | static int cryptodev_usable_ciphers(const int **nids); |
| 95 | static int cryptodev_usable_digests(const int **nids); | 100 | static int cryptodev_usable_digests(const int **nids); |
| 96 | static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 101 | static int cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
| @@ -112,7 +117,8 @@ static int cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, | |||
| 112 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | 117 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); |
| 113 | static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, | 118 | static int cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, |
| 114 | RSA *rsa, BN_CTX *ctx); | 119 | RSA *rsa, BN_CTX *ctx); |
| 115 | static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); | 120 | static int cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, |
| 121 | BN_CTX *ctx); | ||
| 116 | static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, | 122 | static int cryptodev_dsa_bn_mod_exp(DSA *dsa, BIGNUM *r, BIGNUM *a, |
| 117 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); | 123 | const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx); |
| 118 | static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, | 124 | static int cryptodev_dsa_dsa_mod_exp(DSA *dsa, BIGNUM *t1, BIGNUM *g, |
| @@ -128,20 +134,14 @@ static int cryptodev_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a, | |||
| 128 | static int cryptodev_dh_compute_key(unsigned char *key, | 134 | static int cryptodev_dh_compute_key(unsigned char *key, |
| 129 | const BIGNUM *pub_key, DH *dh); | 135 | const BIGNUM *pub_key, DH *dh); |
| 130 | static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, | 136 | static int cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, |
| 131 | void (*f)(void)); | 137 | void (*f)()); |
| 132 | void ENGINE_load_cryptodev(void); | 138 | void ENGINE_load_cryptodev(void); |
| 133 | 139 | ||
| 134 | static const ENGINE_CMD_DEFN cryptodev_defns[] = { | 140 | static const ENGINE_CMD_DEFN cryptodev_defns[] = { |
| 135 | { 0, NULL, NULL, 0 } | 141 | { 0, NULL, NULL, 0 } |
| 136 | }; | 142 | }; |
| 137 | 143 | ||
| 138 | static struct { | 144 | static struct dev_crypto_cipher ciphers[] = { |
| 139 | int id; | ||
| 140 | int nid; | ||
| 141 | int ivmax; | ||
| 142 | int keylen; | ||
| 143 | } ciphers[] = { | ||
| 144 | { CRYPTO_ARC4, NID_rc4, 0, 16, }, | ||
| 145 | { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, }, | 145 | { CRYPTO_DES_CBC, NID_des_cbc, 8, 8, }, |
| 146 | { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, }, | 146 | { CRYPTO_3DES_CBC, NID_des_ede3_cbc, 8, 24, }, |
| 147 | { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, }, | 147 | { CRYPTO_AES_CBC, NID_aes_128_cbc, 16, 16, }, |
| @@ -149,29 +149,26 @@ static struct { | |||
| 149 | { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, }, | 149 | { CRYPTO_AES_CBC, NID_aes_256_cbc, 16, 32, }, |
| 150 | { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, }, | 150 | { CRYPTO_BLF_CBC, NID_bf_cbc, 8, 16, }, |
| 151 | { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, }, | 151 | { CRYPTO_CAST_CBC, NID_cast5_cbc, 8, 16, }, |
| 152 | { CRYPTO_SKIPJACK_CBC, NID_undef, 0, 0, }, | ||
| 153 | { 0, NID_undef, 0, 0, }, | 152 | { 0, NID_undef, 0, 0, }, |
| 154 | }; | 153 | }; |
| 155 | 154 | ||
| 156 | #ifdef USE_CRYPTODEV_DIGESTS | 155 | #if 0 /* UNUSED */ |
| 157 | static struct { | 156 | static struct { |
| 158 | int id; | 157 | int id; |
| 159 | int nid; | 158 | int nid; |
| 160 | int keylen; | ||
| 161 | } digests[] = { | 159 | } digests[] = { |
| 162 | { CRYPTO_MD5_HMAC, NID_hmacWithMD5, 16}, | 160 | { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, }, |
| 163 | { CRYPTO_SHA1_HMAC, NID_hmacWithSHA1, 20}, | 161 | { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, }, |
| 164 | { CRYPTO_RIPEMD160_HMAC, NID_ripemd160, 16/*?*/}, | 162 | { CRYPTO_MD5_KPDK, NID_undef, }, |
| 165 | { CRYPTO_MD5_KPDK, NID_undef, 0}, | 163 | { CRYPTO_SHA1_KPDK, NID_undef, }, |
| 166 | { CRYPTO_SHA1_KPDK, NID_undef, 0}, | 164 | { CRYPTO_MD5, NID_md5, }, |
| 167 | { CRYPTO_MD5, NID_md5, 16}, | 165 | { CRYPTO_SHA1, NID_undef, }, |
| 168 | { CRYPTO_SHA1, NID_sha1, 20}, | 166 | { 0, NID_undef, }, |
| 169 | { 0, NID_undef, 0}, | ||
| 170 | }; | 167 | }; |
| 171 | #endif | 168 | #endif |
| 172 | 169 | ||
| 173 | /* | 170 | /* |
| 174 | * Return a fd if /dev/crypto seems usable, 0 otherwise. | 171 | * Return a fd if /dev/crypto seems usable, -1 otherwise. |
| 175 | */ | 172 | */ |
| 176 | static int | 173 | static int |
| 177 | open_dev_crypto(void) | 174 | open_dev_crypto(void) |
| @@ -198,28 +195,19 @@ get_dev_crypto(void) | |||
| 198 | 195 | ||
| 199 | if ((fd = open_dev_crypto()) == -1) | 196 | if ((fd = open_dev_crypto()) == -1) |
| 200 | return (-1); | 197 | return (-1); |
| 201 | #ifndef CRIOGET_NOT_NEEDED | 198 | if (ioctl(fd, CRIOGET, &retfd) == -1) { |
| 202 | if (ioctl(fd, CRIOGET, &retfd) == -1) | 199 | close(fd); |
| 203 | return (-1); | 200 | return (-1); |
| 201 | } | ||
| 204 | 202 | ||
| 205 | /* close on exec */ | 203 | /* close on exec */ |
| 206 | if (fcntl(retfd, F_SETFD, 1) == -1) { | 204 | if (fcntl(retfd, F_SETFD, 1) == -1) { |
| 207 | close(retfd); | 205 | close(retfd); |
| 208 | return (-1); | 206 | return (-1); |
| 209 | } | 207 | } |
| 210 | #else | ||
| 211 | retfd = fd; | ||
| 212 | #endif | ||
| 213 | return (retfd); | 208 | return (retfd); |
| 214 | } | 209 | } |
| 215 | 210 | ||
| 216 | static void put_dev_crypto(int fd) | ||
| 217 | { | ||
| 218 | #ifndef CRIOGET_NOT_NEEDED | ||
| 219 | close(fd); | ||
| 220 | #endif | ||
| 221 | } | ||
| 222 | |||
| 223 | /* Caching version for asym operations */ | 211 | /* Caching version for asym operations */ |
| 224 | static int | 212 | static int |
| 225 | get_asym_dev_crypto(void) | 213 | get_asym_dev_crypto(void) |
| @@ -231,6 +219,18 @@ get_asym_dev_crypto(void) | |||
| 231 | return fd; | 219 | return fd; |
| 232 | } | 220 | } |
| 233 | 221 | ||
| 222 | /* convert libcrypto nids to cryptodev */ | ||
| 223 | static struct dev_crypto_cipher * | ||
| 224 | cipher_nid_to_cryptodev(int nid) | ||
| 225 | { | ||
| 226 | int i; | ||
| 227 | |||
| 228 | for (i = 0; ciphers[i].c_id; i++) | ||
| 229 | if (ciphers[i].c_nid == nid) | ||
| 230 | return (&ciphers[i]); | ||
| 231 | return (NULL); | ||
| 232 | } | ||
| 233 | |||
| 234 | /* | 234 | /* |
| 235 | * Find out what ciphers /dev/crypto will let us have a session for. | 235 | * Find out what ciphers /dev/crypto will let us have a session for. |
| 236 | * XXX note, that some of these openssl doesn't deal with yet! | 236 | * XXX note, that some of these openssl doesn't deal with yet! |
| @@ -240,7 +240,7 @@ get_asym_dev_crypto(void) | |||
| 240 | static int | 240 | static int |
| 241 | get_cryptodev_ciphers(const int **cnids) | 241 | get_cryptodev_ciphers(const int **cnids) |
| 242 | { | 242 | { |
| 243 | static int nids[CRYPTO_ALGORITHM_MAX]; | 243 | static int nids[CRYPTO_ALGORITHM_MAX + CRYPTO_VIAC3_MAX + 1]; |
| 244 | struct session_op sess; | 244 | struct session_op sess; |
| 245 | int fd, i, count = 0; | 245 | int fd, i, count = 0; |
| 246 | 246 | ||
| @@ -249,19 +249,46 @@ get_cryptodev_ciphers(const int **cnids) | |||
| 249 | return (0); | 249 | return (0); |
| 250 | } | 250 | } |
| 251 | memset(&sess, 0, sizeof(sess)); | 251 | memset(&sess, 0, sizeof(sess)); |
| 252 | sess.key = (caddr_t)"123456789abcdefghijklmno"; | 252 | sess.key = (caddr_t)"123456781234567812345678"; |
| 253 | 253 | ||
| 254 | for (i = 0; ciphers[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { | 254 | for (i = 0; ciphers[i].c_id && count <= CRYPTO_ALGORITHM_MAX; i++) { |
| 255 | if (ciphers[i].nid == NID_undef) | 255 | if (ciphers[i].c_nid == NID_undef) |
| 256 | continue; | 256 | continue; |
| 257 | sess.cipher = ciphers[i].id; | 257 | sess.cipher = ciphers[i].c_id; |
| 258 | sess.keylen = ciphers[i].keylen; | 258 | sess.keylen = ciphers[i].c_keylen; |
| 259 | sess.mac = 0; | 259 | sess.mac = 0; |
| 260 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | 260 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && |
| 261 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | 261 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) |
| 262 | nids[count++] = ciphers[i].nid; | 262 | nids[count++] = ciphers[i].c_nid; |
| 263 | } | 263 | } |
| 264 | put_dev_crypto(fd); | 264 | close(fd); |
| 265 | |||
| 266 | #if defined(__i386__) || defined(__amd64__) | ||
| 267 | /* | ||
| 268 | * Always check for the VIA C3 AES instructions; | ||
| 269 | * even if /dev/crypto is disabled. | ||
| 270 | */ | ||
| 271 | if (check_viac3aes() >= 1) { | ||
| 272 | int have_NID_aes_128_cbc = 0; | ||
| 273 | int have_NID_aes_192_cbc = 0; | ||
| 274 | int have_NID_aes_256_cbc = 0; | ||
| 275 | |||
| 276 | for (i = 0; i < count; i++) { | ||
| 277 | if (nids[i] == NID_aes_128_cbc) | ||
| 278 | have_NID_aes_128_cbc = 1; | ||
| 279 | if (nids[i] == NID_aes_192_cbc) | ||
| 280 | have_NID_aes_192_cbc = 1; | ||
| 281 | if (nids[i] == NID_aes_256_cbc) | ||
| 282 | have_NID_aes_256_cbc = 1; | ||
| 283 | } | ||
| 284 | if (!have_NID_aes_128_cbc) | ||
| 285 | nids[count++] = NID_aes_128_cbc; | ||
| 286 | if (!have_NID_aes_192_cbc) | ||
| 287 | nids[count++] = NID_aes_192_cbc; | ||
| 288 | if (!have_NID_aes_256_cbc) | ||
| 289 | nids[count++] = NID_aes_256_cbc; | ||
| 290 | } | ||
| 291 | #endif | ||
| 265 | 292 | ||
| 266 | if (count > 0) | 293 | if (count > 0) |
| 267 | *cnids = nids; | 294 | *cnids = nids; |
| @@ -270,13 +297,13 @@ get_cryptodev_ciphers(const int **cnids) | |||
| 270 | return (count); | 297 | return (count); |
| 271 | } | 298 | } |
| 272 | 299 | ||
| 273 | #ifdef USE_CRYPTODEV_DIGESTS | ||
| 274 | /* | 300 | /* |
| 275 | * Find out what digests /dev/crypto will let us have a session for. | 301 | * Find out what digests /dev/crypto will let us have a session for. |
| 276 | * XXX note, that some of these openssl doesn't deal with yet! | 302 | * XXX note, that some of these openssl doesn't deal with yet! |
| 277 | * returning them here is harmless, as long as we return NULL | 303 | * returning them here is harmless, as long as we return NULL |
| 278 | * when asked for a handler in the cryptodev_engine_digests routine | 304 | * when asked for a handler in the cryptodev_engine_digests routine |
| 279 | */ | 305 | */ |
| 306 | #if 0 /* UNUSED */ | ||
| 280 | static int | 307 | static int |
| 281 | get_cryptodev_digests(const int **cnids) | 308 | get_cryptodev_digests(const int **cnids) |
| 282 | { | 309 | { |
| @@ -289,18 +316,16 @@ get_cryptodev_digests(const int **cnids) | |||
| 289 | return (0); | 316 | return (0); |
| 290 | } | 317 | } |
| 291 | memset(&sess, 0, sizeof(sess)); | 318 | memset(&sess, 0, sizeof(sess)); |
| 292 | sess.mackey = (caddr_t)"123456789abcdefghijklmno"; | ||
| 293 | for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { | 319 | for (i = 0; digests[i].id && count < CRYPTO_ALGORITHM_MAX; i++) { |
| 294 | if (digests[i].nid == NID_undef) | 320 | if (digests[i].nid == NID_undef) |
| 295 | continue; | 321 | continue; |
| 296 | sess.mac = digests[i].id; | 322 | sess.mac = digests[i].id; |
| 297 | sess.mackeylen = digests[i].keylen; | ||
| 298 | sess.cipher = 0; | 323 | sess.cipher = 0; |
| 299 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && | 324 | if (ioctl(fd, CIOCGSESSION, &sess) != -1 && |
| 300 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) | 325 | ioctl(fd, CIOCFSESSION, &sess.ses) != -1) |
| 301 | nids[count++] = digests[i].nid; | 326 | nids[count++] = digests[i].nid; |
| 302 | } | 327 | } |
| 303 | put_dev_crypto(fd); | 328 | close(fd); |
| 304 | 329 | ||
| 305 | if (count > 0) | 330 | if (count > 0) |
| 306 | *cnids = nids; | 331 | *cnids = nids; |
| @@ -308,7 +333,7 @@ get_cryptodev_digests(const int **cnids) | |||
| 308 | *cnids = NULL; | 333 | *cnids = NULL; |
| 309 | return (count); | 334 | return (count); |
| 310 | } | 335 | } |
| 311 | #endif /* 0 */ | 336 | #endif |
| 312 | 337 | ||
| 313 | /* | 338 | /* |
| 314 | * Find the useable ciphers|digests from dev/crypto - this is the first | 339 | * Find the useable ciphers|digests from dev/crypto - this is the first |
| @@ -340,9 +365,6 @@ cryptodev_usable_ciphers(const int **nids) | |||
| 340 | static int | 365 | static int |
| 341 | cryptodev_usable_digests(const int **nids) | 366 | cryptodev_usable_digests(const int **nids) |
| 342 | { | 367 | { |
| 343 | #ifdef USE_CRYPTODEV_DIGESTS | ||
| 344 | return (get_cryptodev_digests(nids)); | ||
| 345 | #else | ||
| 346 | /* | 368 | /* |
| 347 | * XXXX just disable all digests for now, because it sucks. | 369 | * XXXX just disable all digests for now, because it sucks. |
| 348 | * we need a better way to decide this - i.e. I may not | 370 | * we need a better way to decide this - i.e. I may not |
| @@ -357,7 +379,6 @@ cryptodev_usable_digests(const int **nids) | |||
| 357 | */ | 379 | */ |
| 358 | *nids = NULL; | 380 | *nids = NULL; |
| 359 | return (0); | 381 | return (0); |
| 360 | #endif | ||
| 361 | } | 382 | } |
| 362 | 383 | ||
| 363 | static int | 384 | static int |
| @@ -367,7 +388,7 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
| 367 | struct crypt_op cryp; | 388 | struct crypt_op cryp; |
| 368 | struct dev_crypto_state *state = ctx->cipher_data; | 389 | struct dev_crypto_state *state = ctx->cipher_data; |
| 369 | struct session_op *sess = &state->d_sess; | 390 | struct session_op *sess = &state->d_sess; |
| 370 | const void *iiv; | 391 | void *iiv; |
| 371 | unsigned char save_iv[EVP_MAX_IV_LENGTH]; | 392 | unsigned char save_iv[EVP_MAX_IV_LENGTH]; |
| 372 | 393 | ||
| 373 | if (state->d_fd < 0) | 394 | if (state->d_fd < 0) |
| @@ -391,7 +412,7 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
| 391 | if (ctx->cipher->iv_len) { | 412 | if (ctx->cipher->iv_len) { |
| 392 | cryp.iv = (caddr_t) ctx->iv; | 413 | cryp.iv = (caddr_t) ctx->iv; |
| 393 | if (!ctx->encrypt) { | 414 | if (!ctx->encrypt) { |
| 394 | iiv = in + inl - ctx->cipher->iv_len; | 415 | iiv = (void *) in + inl - ctx->cipher->iv_len; |
| 395 | memcpy(save_iv, iiv, ctx->cipher->iv_len); | 416 | memcpy(save_iv, iiv, ctx->cipher->iv_len); |
| 396 | } | 417 | } |
| 397 | } else | 418 | } else |
| @@ -406,7 +427,7 @@ cryptodev_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
| 406 | 427 | ||
| 407 | if (ctx->cipher->iv_len) { | 428 | if (ctx->cipher->iv_len) { |
| 408 | if (ctx->encrypt) | 429 | if (ctx->encrypt) |
| 409 | iiv = out + inl - ctx->cipher->iv_len; | 430 | iiv = (void *) out + inl - ctx->cipher->iv_len; |
| 410 | else | 431 | else |
| 411 | iiv = save_iv; | 432 | iiv = save_iv; |
| 412 | memcpy(ctx->iv, iiv, ctx->cipher->iv_len); | 433 | memcpy(ctx->iv, iiv, ctx->cipher->iv_len); |
| @@ -420,32 +441,28 @@ cryptodev_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | |||
| 420 | { | 441 | { |
| 421 | struct dev_crypto_state *state = ctx->cipher_data; | 442 | struct dev_crypto_state *state = ctx->cipher_data; |
| 422 | struct session_op *sess = &state->d_sess; | 443 | struct session_op *sess = &state->d_sess; |
| 423 | int cipher = -1, i; | 444 | struct dev_crypto_cipher *cipher; |
| 424 | |||
| 425 | for (i = 0; ciphers[i].id; i++) | ||
| 426 | if (ctx->cipher->nid == ciphers[i].nid && | ||
| 427 | ctx->cipher->iv_len <= ciphers[i].ivmax && | ||
| 428 | ctx->key_len == ciphers[i].keylen) { | ||
| 429 | cipher = ciphers[i].id; | ||
| 430 | break; | ||
| 431 | } | ||
| 432 | 445 | ||
| 433 | if (!ciphers[i].id) { | 446 | if ((cipher = cipher_nid_to_cryptodev(ctx->cipher->nid)) == NULL) |
| 434 | state->d_fd = -1; | 447 | return (0); |
| 448 | |||
| 449 | if (ctx->cipher->iv_len > cipher->c_ivmax) | ||
| 450 | return (0); | ||
| 451 | |||
| 452 | if (ctx->key_len != cipher->c_keylen) | ||
| 435 | return (0); | 453 | return (0); |
| 436 | } | ||
| 437 | 454 | ||
| 438 | memset(sess, 0, sizeof(struct session_op)); | 455 | memset(sess, 0, sizeof(struct session_op)); |
| 439 | 456 | ||
| 440 | if ((state->d_fd = get_dev_crypto()) < 0) | 457 | if ((state->d_fd = get_dev_crypto()) < 0) |
| 441 | return (0); | 458 | return (0); |
| 442 | 459 | ||
| 443 | sess->key = (caddr_t)key; | 460 | sess->key = (unsigned char *)key; |
| 444 | sess->keylen = ctx->key_len; | 461 | sess->keylen = ctx->key_len; |
| 445 | sess->cipher = cipher; | 462 | sess->cipher = cipher->c_id; |
| 446 | 463 | ||
| 447 | if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { | 464 | if (ioctl(state->d_fd, CIOCGSESSION, sess) == -1) { |
| 448 | put_dev_crypto(state->d_fd); | 465 | close(state->d_fd); |
| 449 | state->d_fd = -1; | 466 | state->d_fd = -1; |
| 450 | return (0); | 467 | return (0); |
| 451 | } | 468 | } |
| @@ -482,7 +499,7 @@ cryptodev_cleanup(EVP_CIPHER_CTX *ctx) | |||
| 482 | } else { | 499 | } else { |
| 483 | ret = 1; | 500 | ret = 1; |
| 484 | } | 501 | } |
| 485 | put_dev_crypto(state->d_fd); | 502 | close(state->d_fd); |
| 486 | state->d_fd = -1; | 503 | state->d_fd = -1; |
| 487 | 504 | ||
| 488 | return (ret); | 505 | return (ret); |
| @@ -493,20 +510,6 @@ cryptodev_cleanup(EVP_CIPHER_CTX *ctx) | |||
| 493 | * gets called when libcrypto requests a cipher NID. | 510 | * gets called when libcrypto requests a cipher NID. |
| 494 | */ | 511 | */ |
| 495 | 512 | ||
| 496 | /* RC4 */ | ||
| 497 | const EVP_CIPHER cryptodev_rc4 = { | ||
| 498 | NID_rc4, | ||
| 499 | 1, 16, 0, | ||
| 500 | EVP_CIPH_VARIABLE_LENGTH, | ||
| 501 | cryptodev_init_key, | ||
| 502 | cryptodev_cipher, | ||
| 503 | cryptodev_cleanup, | ||
| 504 | sizeof(struct dev_crypto_state), | ||
| 505 | NULL, | ||
| 506 | NULL, | ||
| 507 | NULL | ||
| 508 | }; | ||
| 509 | |||
| 510 | /* DES CBC EVP */ | 513 | /* DES CBC EVP */ |
| 511 | const EVP_CIPHER cryptodev_des_cbc = { | 514 | const EVP_CIPHER cryptodev_des_cbc = { |
| 512 | NID_des_cbc, | 515 | NID_des_cbc, |
| @@ -561,7 +564,7 @@ const EVP_CIPHER cryptodev_cast_cbc = { | |||
| 561 | NULL | 564 | NULL |
| 562 | }; | 565 | }; |
| 563 | 566 | ||
| 564 | const EVP_CIPHER cryptodev_aes_cbc = { | 567 | EVP_CIPHER cryptodev_aes_128_cbc = { |
| 565 | NID_aes_128_cbc, | 568 | NID_aes_128_cbc, |
| 566 | 16, 16, 16, | 569 | 16, 16, 16, |
| 567 | EVP_CIPH_CBC_MODE, | 570 | EVP_CIPH_CBC_MODE, |
| @@ -574,7 +577,7 @@ const EVP_CIPHER cryptodev_aes_cbc = { | |||
| 574 | NULL | 577 | NULL |
| 575 | }; | 578 | }; |
| 576 | 579 | ||
| 577 | const EVP_CIPHER cryptodev_aes_192_cbc = { | 580 | EVP_CIPHER cryptodev_aes_192_cbc = { |
| 578 | NID_aes_192_cbc, | 581 | NID_aes_192_cbc, |
| 579 | 16, 24, 16, | 582 | 16, 24, 16, |
| 580 | EVP_CIPH_CBC_MODE, | 583 | EVP_CIPH_CBC_MODE, |
| @@ -587,7 +590,7 @@ const EVP_CIPHER cryptodev_aes_192_cbc = { | |||
| 587 | NULL | 590 | NULL |
| 588 | }; | 591 | }; |
| 589 | 592 | ||
| 590 | const EVP_CIPHER cryptodev_aes_256_cbc = { | 593 | EVP_CIPHER cryptodev_aes_256_cbc = { |
| 591 | NID_aes_256_cbc, | 594 | NID_aes_256_cbc, |
| 592 | 16, 32, 16, | 595 | 16, 32, 16, |
| 593 | EVP_CIPH_CBC_MODE, | 596 | EVP_CIPH_CBC_MODE, |
| @@ -600,300 +603,240 @@ const EVP_CIPHER cryptodev_aes_256_cbc = { | |||
| 600 | NULL | 603 | NULL |
| 601 | }; | 604 | }; |
| 602 | 605 | ||
| 603 | /* | 606 | #if defined(__i386__) || defined(__amd64__) |
| 604 | * Registered by the ENGINE when used to find out how to deal with | ||
| 605 | * a particular NID in the ENGINE. this says what we'll do at the | ||
| 606 | * top level - note, that list is restricted by what we answer with | ||
| 607 | */ | ||
| 608 | static int | ||
| 609 | cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
| 610 | const int **nids, int nid) | ||
| 611 | { | ||
| 612 | if (!cipher) | ||
| 613 | return (cryptodev_usable_ciphers(nids)); | ||
| 614 | |||
| 615 | switch (nid) { | ||
| 616 | case NID_rc4: | ||
| 617 | *cipher = &cryptodev_rc4; | ||
| 618 | break; | ||
| 619 | case NID_des_ede3_cbc: | ||
| 620 | *cipher = &cryptodev_3des_cbc; | ||
| 621 | break; | ||
| 622 | case NID_des_cbc: | ||
| 623 | *cipher = &cryptodev_des_cbc; | ||
| 624 | break; | ||
| 625 | case NID_bf_cbc: | ||
| 626 | *cipher = &cryptodev_bf_cbc; | ||
| 627 | break; | ||
| 628 | case NID_cast5_cbc: | ||
| 629 | *cipher = &cryptodev_cast_cbc; | ||
| 630 | break; | ||
| 631 | case NID_aes_128_cbc: | ||
| 632 | *cipher = &cryptodev_aes_cbc; | ||
| 633 | break; | ||
| 634 | case NID_aes_192_cbc: | ||
| 635 | *cipher = &cryptodev_aes_192_cbc; | ||
| 636 | break; | ||
| 637 | case NID_aes_256_cbc: | ||
| 638 | *cipher = &cryptodev_aes_256_cbc; | ||
| 639 | break; | ||
| 640 | default: | ||
| 641 | *cipher = NULL; | ||
| 642 | break; | ||
| 643 | } | ||
| 644 | return (*cipher != NULL); | ||
| 645 | } | ||
| 646 | |||
| 647 | |||
| 648 | #ifdef USE_CRYPTODEV_DIGESTS | ||
| 649 | 607 | ||
| 650 | /* convert digest type to cryptodev */ | 608 | static inline void |
| 651 | static int | 609 | viac3_xcrypt_cbc(int *cw, const void *src, void *dst, void *key, int rep, |
| 652 | digest_nid_to_cryptodev(int nid) | 610 | void *iv) |
| 653 | { | 611 | { |
| 654 | int i; | 612 | #ifdef notdef |
| 613 | printf("cw %p[%x %x %x %x] src %p dst %p key %p rep %x iv %p\n", | ||
| 614 | cw, cw[0], cw[1], cw[2], cw[3], | ||
| 615 | src, dst, key, rep, iv); | ||
| 616 | #endif | ||
| 617 | #if defined(__i386__) | ||
| 655 | 618 | ||
| 656 | for (i = 0; digests[i].id; i++) | 619 | /* |
| 657 | if (digests[i].nid == nid) | 620 | * Clear bit 30 of EFLAGS. |
| 658 | return (digests[i].id); | 621 | */ |
| 659 | return (0); | 622 | __asm __volatile("pushfl; popfl"); |
| 660 | } | ||
| 661 | 623 | ||
| 624 | /* | ||
| 625 | * Cannot simply place key into "b" register, since the compiler | ||
| 626 | * -pic mode uses that register; so instead we must dance a little. | ||
| 627 | */ | ||
| 628 | __asm __volatile("pushl %%ebx; movl %0, %%ebx; rep xcryptcbc; popl %%ebx" : | ||
| 629 | : "m" (key), "a" (iv), "c" (rep), "d" (cw), "S" (src), "D" (dst) | ||
| 630 | : "memory", "cc"); | ||
| 631 | #else | ||
| 662 | 632 | ||
| 663 | static int | 633 | /* |
| 664 | digest_key_length(int nid) | 634 | * Clear bit 30 of EFLAGS. |
| 665 | { | 635 | */ |
| 666 | int i; | 636 | __asm __volatile("pushfq; popfq"); |
| 637 | __asm __volatile("rep xcryptcbc" : | ||
| 638 | : "b" (key), "a" (iv), "c" (rep), "d" (cw), "S" (src), "D" (dst) | ||
| 639 | : "memory", "cc"); | ||
| 640 | #endif | ||
| 667 | 641 | ||
| 668 | for (i = 0; digests[i].id; i++) | ||
| 669 | if (digests[i].nid == nid) | ||
| 670 | return digests[i].keylen; | ||
| 671 | return (0); | ||
| 672 | } | 642 | } |
| 673 | 643 | ||
| 644 | #define ISUNALIGNED(x) ((long)(x)) & 15 | ||
| 645 | #define DOALIGN(v) ((void *)(((long)(v) + 15) & ~15)) | ||
| 674 | 646 | ||
| 675 | static int cryptodev_digest_init(EVP_MD_CTX *ctx) | 647 | static int |
| 648 | xcrypt_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | ||
| 649 | const unsigned char *in, size_t inl) | ||
| 676 | { | 650 | { |
| 677 | struct dev_crypto_state *state = ctx->md_data; | 651 | unsigned char *save_iv_store[EVP_MAX_IV_LENGTH + 15]; |
| 678 | struct session_op *sess = &state->d_sess; | 652 | unsigned char *save_iv = DOALIGN(save_iv_store); |
| 679 | int digest; | 653 | unsigned char *ivs_store[EVP_MAX_IV_LENGTH + 15]; |
| 654 | unsigned char *ivs = DOALIGN(ivs_store); | ||
| 655 | void *iiv, *iv = NULL, *ivp = NULL; | ||
| 656 | const void *usein = in; | ||
| 657 | void *useout = out, *spare; | ||
| 658 | int cws[4 + 3], *cw = DOALIGN(cws); | ||
| 680 | 659 | ||
| 681 | if ((digest = digest_nid_to_cryptodev(ctx->digest->type)) == NID_undef){ | 660 | if (!inl) |
| 682 | printf("cryptodev_digest_init: Can't get digest \n"); | 661 | return (1); |
| 662 | if ((inl % ctx->cipher->block_size) != 0) | ||
| 683 | return (0); | 663 | return (0); |
| 684 | } | 664 | if (inl > UINT_MAX) |
| 685 | |||
| 686 | memset(state, 0, sizeof(struct dev_crypto_state)); | ||
| 687 | |||
| 688 | if ((state->d_fd = get_dev_crypto()) < 0) { | ||
| 689 | printf("cryptodev_digest_init: Can't get Dev \n"); | ||
| 690 | return (0); | 665 | return (0); |
| 691 | } | ||
| 692 | 666 | ||
| 693 | sess->mackey = state->dummy_mac_key; | 667 | if (ISUNALIGNED(in) || ISUNALIGNED(out)) { |
| 694 | sess->mackeylen = digest_key_length(ctx->digest->type); | 668 | spare = malloc(inl); |
| 695 | sess->mac = digest; | 669 | if (spare == NULL) |
| 670 | return (0); | ||
| 696 | 671 | ||
| 697 | if (ioctl(state->d_fd, CIOCGSESSION, sess) < 0) { | 672 | if (ISUNALIGNED(in)) { |
| 698 | put_dev_crypto(state->d_fd); | 673 | bcopy(in, spare, inl); |
| 699 | state->d_fd = -1; | 674 | usein = spare; |
| 700 | printf("cryptodev_digest_init: Open session failed\n"); | 675 | } |
| 701 | return (0); | 676 | if (ISUNALIGNED(out)) |
| 677 | useout = spare; | ||
| 702 | } | 678 | } |
| 703 | 679 | ||
| 704 | return (1); | 680 | cw[0] = C3_CRYPT_CWLO_ALG_AES | C3_CRYPT_CWLO_KEYGEN_SW | |
| 705 | } | 681 | C3_CRYPT_CWLO_NORMAL; |
| 706 | 682 | cw[0] |= ctx->encrypt ? C3_CRYPT_CWLO_ENCRYPT : C3_CRYPT_CWLO_DECRYPT; | |
| 707 | static int cryptodev_digest_update(EVP_MD_CTX *ctx, const void *data, | 683 | cw[1] = cw[2] = cw[3] = 0; |
| 708 | size_t count) | ||
| 709 | { | ||
| 710 | struct crypt_op cryp; | ||
| 711 | struct dev_crypto_state *state = ctx->md_data; | ||
| 712 | struct session_op *sess = &state->d_sess; | ||
| 713 | 684 | ||
| 714 | if (!data || state->d_fd < 0) { | 685 | switch (ctx->key_len * 8) { |
| 715 | printf("cryptodev_digest_update: illegal inputs \n"); | 686 | case 128: |
| 716 | return (0); | 687 | cw[0] |= C3_CRYPT_CWLO_KEY128; |
| 688 | break; | ||
| 689 | case 192: | ||
| 690 | cw[0] |= C3_CRYPT_CWLO_KEY192; | ||
| 691 | break; | ||
| 692 | case 256: | ||
| 693 | cw[0] |= C3_CRYPT_CWLO_KEY256; | ||
| 694 | break; | ||
| 717 | } | 695 | } |
| 718 | 696 | ||
| 719 | if (!count) { | 697 | if (ctx->cipher->iv_len) { |
| 720 | return (0); | 698 | iv = (caddr_t) ctx->iv; |
| 699 | if (!ctx->encrypt) { | ||
| 700 | iiv = (void *) in + inl - ctx->cipher->iv_len; | ||
| 701 | memcpy(save_iv, iiv, ctx->cipher->iv_len); | ||
| 702 | } | ||
| 721 | } | 703 | } |
| 722 | 704 | ||
| 723 | if (!(ctx->flags & EVP_MD_CTX_FLAG_ONESHOT)) { | 705 | ivp = iv; |
| 724 | /* if application doesn't support one buffer */ | 706 | if (ISUNALIGNED(iv)) { |
| 725 | state->mac_data = OPENSSL_realloc(state->mac_data, state->mac_len + count); | 707 | bcopy(iv, ivs, ctx->cipher->iv_len); |
| 708 | ivp = ivs; | ||
| 709 | } | ||
| 726 | 710 | ||
| 727 | if (!state->mac_data) { | 711 | viac3_xcrypt_cbc(cw, usein, useout, ctx->cipher_data, inl / 16, ivp); |
| 728 | printf("cryptodev_digest_update: realloc failed\n"); | ||
| 729 | return (0); | ||
| 730 | } | ||
| 731 | 712 | ||
| 732 | memcpy(state->mac_data + state->mac_len, data, count); | 713 | if (ISUNALIGNED(in) || ISUNALIGNED(out)) { |
| 733 | state->mac_len += count; | 714 | if (ISUNALIGNED(out)) |
| 734 | 715 | bcopy(spare, out, inl); | |
| 735 | return (1); | 716 | free(spare); |
| 736 | } | 717 | } |
| 737 | 718 | ||
| 738 | memset(&cryp, 0, sizeof(cryp)); | 719 | if (ivp == ivs) |
| 720 | bcopy(ivp, iv, ctx->cipher->iv_len); | ||
| 739 | 721 | ||
| 740 | cryp.ses = sess->ses; | 722 | if (ctx->cipher->iv_len) { |
| 741 | cryp.flags = 0; | 723 | if (ctx->encrypt) |
| 742 | cryp.len = count; | 724 | iiv = (void *) out + inl - ctx->cipher->iv_len; |
| 743 | cryp.src = (caddr_t) data; | 725 | else |
| 744 | cryp.dst = NULL; | 726 | iiv = save_iv; |
| 745 | cryp.mac = (caddr_t) state->digest_res; | 727 | memcpy(ctx->iv, iiv, ctx->cipher->iv_len); |
| 746 | if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) { | ||
| 747 | printf("cryptodev_digest_update: digest failed\n"); | ||
| 748 | return (0); | ||
| 749 | } | 728 | } |
| 750 | return (1); | 729 | return (1); |
| 751 | } | 730 | } |
| 752 | 731 | ||
| 753 | 732 | static int | |
| 754 | static int cryptodev_digest_final(EVP_MD_CTX *ctx, unsigned char *md) | 733 | xcrypt_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 734 | const unsigned char *iv, int enc) | ||
| 755 | { | 735 | { |
| 756 | struct crypt_op cryp; | 736 | AES_KEY *k = ctx->cipher_data; |
| 757 | struct dev_crypto_state *state = ctx->md_data; | 737 | #ifndef AES_ASM |
| 758 | struct session_op *sess = &state->d_sess; | 738 | int i; |
| 759 | 739 | #endif | |
| 760 | int ret = 1; | ||
| 761 | |||
| 762 | if (!md || state->d_fd < 0) { | ||
| 763 | printf("cryptodev_digest_final: illegal input\n"); | ||
| 764 | return(0); | ||
| 765 | } | ||
| 766 | |||
| 767 | if (! (ctx->flags & EVP_MD_CTX_FLAG_ONESHOT) ) { | ||
| 768 | /* if application doesn't support one buffer */ | ||
| 769 | memset(&cryp, 0, sizeof(cryp)); | ||
| 770 | cryp.ses = sess->ses; | ||
| 771 | cryp.flags = 0; | ||
| 772 | cryp.len = state->mac_len; | ||
| 773 | cryp.src = state->mac_data; | ||
| 774 | cryp.dst = NULL; | ||
| 775 | cryp.mac = (caddr_t)md; | ||
| 776 | if (ioctl(state->d_fd, CIOCCRYPT, &cryp) < 0) { | ||
| 777 | printf("cryptodev_digest_final: digest failed\n"); | ||
| 778 | return (0); | ||
| 779 | } | ||
| 780 | 740 | ||
| 781 | return 1; | 741 | bzero(k, sizeof *k); |
| 782 | } | 742 | if (enc) |
| 743 | AES_set_encrypt_key(key, ctx->key_len * 8, k); | ||
| 744 | else | ||
| 745 | AES_set_decrypt_key(key, ctx->key_len * 8, k); | ||
| 783 | 746 | ||
| 784 | memcpy(md, state->digest_res, ctx->digest->md_size); | 747 | #ifndef AES_ASM |
| 748 | /* | ||
| 749 | * XXX Damn OpenSSL byte swaps the expanded key!! | ||
| 750 | * | ||
| 751 | * XXX But only if we're using the C implementation of AES | ||
| 752 | */ | ||
| 753 | for (i = 0; i < 4 * (AES_MAXNR + 1); i++) | ||
| 754 | k->rd_key[i] = htonl(k->rd_key[i]); | ||
| 755 | #endif | ||
| 785 | 756 | ||
| 786 | return (ret); | 757 | return (1); |
| 787 | } | 758 | } |
| 788 | 759 | ||
| 789 | 760 | static int | |
| 790 | static int cryptodev_digest_cleanup(EVP_MD_CTX *ctx) | 761 | xcrypt_cleanup(EVP_CIPHER_CTX *ctx) |
| 791 | { | 762 | { |
| 792 | int ret = 1; | 763 | bzero(ctx->cipher_data, ctx->cipher->ctx_size); |
| 793 | struct dev_crypto_state *state = ctx->md_data; | 764 | return (1); |
| 794 | struct session_op *sess = &state->d_sess; | 765 | } |
| 795 | 766 | ||
| 796 | if (state == NULL) | 767 | static int |
| 797 | return 0; | 768 | check_viac3aes(void) |
| 769 | { | ||
| 770 | int mib[2] = { CTL_MACHDEP, CPU_XCRYPT }, value; | ||
| 771 | size_t size = sizeof(value); | ||
| 798 | 772 | ||
| 799 | if (state->d_fd < 0) { | 773 | if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, |
| 800 | printf("cryptodev_digest_cleanup: illegal input\n"); | 774 | NULL, 0) < 0) |
| 775 | return (0); | ||
| 776 | if (value == 0) | ||
| 801 | return (0); | 777 | return (0); |
| 802 | } | ||
| 803 | |||
| 804 | if (state->mac_data) { | ||
| 805 | OPENSSL_free(state->mac_data); | ||
| 806 | state->mac_data = NULL; | ||
| 807 | state->mac_len = 0; | ||
| 808 | } | ||
| 809 | 778 | ||
| 810 | if (ioctl(state->d_fd, CIOCFSESSION, &sess->ses) < 0) { | 779 | if (value & C3_HAS_AES) { |
| 811 | printf("cryptodev_digest_cleanup: failed to close session\n"); | 780 | cryptodev_aes_128_cbc.init = xcrypt_init_key; |
| 812 | ret = 0; | 781 | cryptodev_aes_128_cbc.do_cipher = xcrypt_cipher; |
| 813 | } else { | 782 | cryptodev_aes_128_cbc.cleanup = xcrypt_cleanup; |
| 814 | ret = 1; | 783 | cryptodev_aes_128_cbc.ctx_size = sizeof(AES_KEY); |
| 784 | |||
| 785 | cryptodev_aes_192_cbc.init = xcrypt_init_key; | ||
| 786 | cryptodev_aes_192_cbc.do_cipher = xcrypt_cipher; | ||
| 787 | cryptodev_aes_192_cbc.cleanup = xcrypt_cleanup; | ||
| 788 | cryptodev_aes_192_cbc.ctx_size = sizeof(AES_KEY); | ||
| 789 | |||
| 790 | cryptodev_aes_256_cbc.init = xcrypt_init_key; | ||
| 791 | cryptodev_aes_256_cbc.do_cipher = xcrypt_cipher; | ||
| 792 | cryptodev_aes_256_cbc.cleanup = xcrypt_cleanup; | ||
| 793 | cryptodev_aes_256_cbc.ctx_size = sizeof(AES_KEY); | ||
| 815 | } | 794 | } |
| 816 | put_dev_crypto(state->d_fd); | 795 | return (value); |
| 817 | state->d_fd = -1; | ||
| 818 | |||
| 819 | return (ret); | ||
| 820 | } | 796 | } |
| 797 | #endif /* __i386__ || __amd64__ */ | ||
| 821 | 798 | ||
| 822 | static int cryptodev_digest_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from) | 799 | /* |
| 800 | * Registered by the ENGINE when used to find out how to deal with | ||
| 801 | * a particular NID in the ENGINE. this says what we'll do at the | ||
| 802 | * top level - note, that list is restricted by what we answer with | ||
| 803 | */ | ||
| 804 | static int | ||
| 805 | cryptodev_engine_ciphers(ENGINE *e, const EVP_CIPHER **cipher, | ||
| 806 | const int **nids, int nid) | ||
| 823 | { | 807 | { |
| 824 | struct dev_crypto_state *fstate = from->md_data; | 808 | if (!cipher) |
| 825 | struct dev_crypto_state *dstate = to->md_data; | 809 | return (cryptodev_usable_ciphers(nids)); |
| 826 | struct session_op *sess; | ||
| 827 | int digest; | ||
| 828 | |||
| 829 | if (dstate == NULL || fstate == NULL) | ||
| 830 | return 1; | ||
| 831 | |||
| 832 | memcpy(dstate, fstate, sizeof(struct dev_crypto_state)); | ||
| 833 | |||
| 834 | sess = &dstate->d_sess; | ||
| 835 | |||
| 836 | digest = digest_nid_to_cryptodev(to->digest->type); | ||
| 837 | |||
| 838 | sess->mackey = dstate->dummy_mac_key; | ||
| 839 | sess->mackeylen = digest_key_length(to->digest->type); | ||
| 840 | sess->mac = digest; | ||
| 841 | |||
| 842 | dstate->d_fd = get_dev_crypto(); | ||
| 843 | |||
| 844 | if (ioctl(dstate->d_fd, CIOCGSESSION, sess) < 0) { | ||
| 845 | put_dev_crypto(dstate->d_fd); | ||
| 846 | dstate->d_fd = -1; | ||
| 847 | printf("cryptodev_digest_init: Open session failed\n"); | ||
| 848 | return (0); | ||
| 849 | } | ||
| 850 | 810 | ||
| 851 | if (fstate->mac_len != 0) { | 811 | switch (nid) { |
| 852 | if (fstate->mac_data != NULL) | 812 | case NID_des_ede3_cbc: |
| 853 | { | 813 | *cipher = &cryptodev_3des_cbc; |
| 854 | dstate->mac_data = OPENSSL_malloc(fstate->mac_len); | 814 | break; |
| 855 | memcpy(dstate->mac_data, fstate->mac_data, fstate->mac_len); | 815 | case NID_des_cbc: |
| 856 | dstate->mac_len = fstate->mac_len; | 816 | *cipher = &cryptodev_des_cbc; |
| 857 | } | 817 | break; |
| 818 | case NID_bf_cbc: | ||
| 819 | *cipher = &cryptodev_bf_cbc; | ||
| 820 | break; | ||
| 821 | case NID_cast5_cbc: | ||
| 822 | *cipher = &cryptodev_cast_cbc; | ||
| 823 | break; | ||
| 824 | case NID_aes_128_cbc: | ||
| 825 | *cipher = &cryptodev_aes_128_cbc; | ||
| 826 | break; | ||
| 827 | case NID_aes_192_cbc: | ||
| 828 | *cipher = &cryptodev_aes_192_cbc; | ||
| 829 | break; | ||
| 830 | case NID_aes_256_cbc: | ||
| 831 | *cipher = &cryptodev_aes_256_cbc; | ||
| 832 | break; | ||
| 833 | default: | ||
| 834 | *cipher = NULL; | ||
| 835 | break; | ||
| 858 | } | 836 | } |
| 859 | 837 | return (*cipher != NULL); | |
| 860 | return 1; | ||
| 861 | } | 838 | } |
| 862 | 839 | ||
| 863 | |||
| 864 | const EVP_MD cryptodev_sha1 = { | ||
| 865 | NID_sha1, | ||
| 866 | NID_undef, | ||
| 867 | SHA_DIGEST_LENGTH, | ||
| 868 | EVP_MD_FLAG_ONESHOT, | ||
| 869 | cryptodev_digest_init, | ||
| 870 | cryptodev_digest_update, | ||
| 871 | cryptodev_digest_final, | ||
| 872 | cryptodev_digest_copy, | ||
| 873 | cryptodev_digest_cleanup, | ||
| 874 | EVP_PKEY_NULL_method, | ||
| 875 | SHA_CBLOCK, | ||
| 876 | sizeof(struct dev_crypto_state), | ||
| 877 | }; | ||
| 878 | |||
| 879 | const EVP_MD cryptodev_md5 = { | ||
| 880 | NID_md5, | ||
| 881 | NID_undef, | ||
| 882 | 16 /* MD5_DIGEST_LENGTH */, | ||
| 883 | EVP_MD_FLAG_ONESHOT, | ||
| 884 | cryptodev_digest_init, | ||
| 885 | cryptodev_digest_update, | ||
| 886 | cryptodev_digest_final, | ||
| 887 | cryptodev_digest_copy, | ||
| 888 | cryptodev_digest_cleanup, | ||
| 889 | EVP_PKEY_NULL_method, | ||
| 890 | 64 /* MD5_CBLOCK */, | ||
| 891 | sizeof(struct dev_crypto_state), | ||
| 892 | }; | ||
| 893 | |||
| 894 | #endif /* USE_CRYPTODEV_DIGESTS */ | ||
| 895 | |||
| 896 | |||
| 897 | static int | 840 | static int |
| 898 | cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | 841 | cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, |
| 899 | const int **nids, int nid) | 842 | const int **nids, int nid) |
| @@ -902,15 +845,10 @@ cryptodev_engine_digests(ENGINE *e, const EVP_MD **digest, | |||
| 902 | return (cryptodev_usable_digests(nids)); | 845 | return (cryptodev_usable_digests(nids)); |
| 903 | 846 | ||
| 904 | switch (nid) { | 847 | switch (nid) { |
| 905 | #ifdef USE_CRYPTODEV_DIGESTS | ||
| 906 | case NID_md5: | 848 | case NID_md5: |
| 907 | *digest = &cryptodev_md5; | 849 | *digest = NULL; /* need to make a clean md5 critter */ |
| 908 | break; | 850 | break; |
| 909 | case NID_sha1: | ||
| 910 | *digest = &cryptodev_sha1; | ||
| 911 | break; | ||
| 912 | default: | 851 | default: |
| 913 | #endif /* USE_CRYPTODEV_DIGESTS */ | ||
| 914 | *digest = NULL; | 852 | *digest = NULL; |
| 915 | break; | 853 | break; |
| 916 | } | 854 | } |
| @@ -938,9 +876,8 @@ bn2crparam(const BIGNUM *a, struct crparam *crp) | |||
| 938 | b = malloc(bytes); | 876 | b = malloc(bytes); |
| 939 | if (b == NULL) | 877 | if (b == NULL) |
| 940 | return (1); | 878 | return (1); |
| 941 | memset(b, 0, bytes); | ||
| 942 | 879 | ||
| 943 | crp->crp_p = (caddr_t) b; | 880 | crp->crp_p = b; |
| 944 | crp->crp_nbits = bits; | 881 | crp->crp_nbits = bits; |
| 945 | 882 | ||
| 946 | for (i = 0, j = 0; i < a->top; i++) { | 883 | for (i = 0, j = 0; i < a->top; i++) { |
| @@ -983,7 +920,7 @@ zapparams(struct crypt_kop *kop) | |||
| 983 | { | 920 | { |
| 984 | int i; | 921 | int i; |
| 985 | 922 | ||
| 986 | for (i = 0; i < kop->crk_iparams + kop->crk_oparams; i++) { | 923 | for (i = 0; i <= kop->crk_iparams + kop->crk_oparams; i++) { |
| 987 | if (kop->crk_param[i].crp_p) | 924 | if (kop->crk_param[i].crp_p) |
| 988 | free(kop->crk_param[i].crp_p); | 925 | free(kop->crk_param[i].crp_p); |
| 989 | kop->crk_param[i].crp_p = NULL; | 926 | kop->crk_param[i].crp_p = NULL; |
| @@ -1048,31 +985,20 @@ cryptodev_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |||
| 1048 | goto err; | 985 | goto err; |
| 1049 | kop.crk_iparams = 3; | 986 | kop.crk_iparams = 3; |
| 1050 | 987 | ||
| 1051 | if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL)) { | 988 | if (cryptodev_asym(&kop, BN_num_bytes(m), r, 0, NULL) == -1) { |
| 1052 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
| 1053 | printf("OCF asym process failed, Running in software\n"); | ||
| 1054 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); | ||
| 1055 | |||
| 1056 | } else if (ECANCELED == kop.crk_status) { | ||
| 1057 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | 989 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); |
| 1058 | printf("OCF hardware operation cancelled. Running in Software\n"); | ||
| 1059 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); | 990 | ret = meth->bn_mod_exp(r, a, p, m, ctx, in_mont); |
| 1060 | } | 991 | } |
| 1061 | /* else cryptodev operation worked ok ==> ret = 1*/ | ||
| 1062 | |||
| 1063 | err: | 992 | err: |
| 1064 | zapparams(&kop); | 993 | zapparams(&kop); |
| 1065 | return (ret); | 994 | return (ret); |
| 1066 | } | 995 | } |
| 1067 | 996 | ||
| 1068 | static int | 997 | static int |
| 1069 | cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) | 998 | cryptodev_rsa_nocrt_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, |
| 999 | BN_CTX *ctx) | ||
| 1070 | { | 1000 | { |
| 1071 | int r; | 1001 | return (RSA_PKCS1_SSLeay()->rsa_mod_exp)(r0, I, rsa, ctx); |
| 1072 | ctx = BN_CTX_new(); | ||
| 1073 | r = cryptodev_bn_mod_exp(r0, I, rsa->d, rsa->n, ctx, NULL); | ||
| 1074 | BN_CTX_free(ctx); | ||
| 1075 | return (r); | ||
| 1076 | } | 1002 | } |
| 1077 | 1003 | ||
| 1078 | static int | 1004 | static int |
| @@ -1103,18 +1029,10 @@ cryptodev_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) | |||
| 1103 | goto err; | 1029 | goto err; |
| 1104 | kop.crk_iparams = 6; | 1030 | kop.crk_iparams = 6; |
| 1105 | 1031 | ||
| 1106 | if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL)) { | 1032 | if (cryptodev_asym(&kop, BN_num_bytes(rsa->n), r0, 0, NULL) == -1) { |
| 1107 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | ||
| 1108 | printf("OCF asym process failed, running in Software\n"); | ||
| 1109 | ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx); | ||
| 1110 | |||
| 1111 | } else if (ECANCELED == kop.crk_status) { | ||
| 1112 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); | 1033 | const RSA_METHOD *meth = RSA_PKCS1_SSLeay(); |
| 1113 | printf("OCF hardware operation cancelled. Running in Software\n"); | ||
| 1114 | ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx); | 1034 | ret = (*meth->rsa_mod_exp)(r0, I, rsa, ctx); |
| 1115 | } | 1035 | } |
| 1116 | /* else cryptodev operation worked ok ==> ret = 1*/ | ||
| 1117 | |||
| 1118 | err: | 1036 | err: |
| 1119 | zapparams(&kop); | 1037 | zapparams(&kop); |
| 1120 | return (ret); | 1038 | return (ret); |
| @@ -1250,8 +1168,7 @@ cryptodev_dsa_verify(const unsigned char *dgst, int dlen, | |||
| 1250 | kop.crk_iparams = 7; | 1168 | kop.crk_iparams = 7; |
| 1251 | 1169 | ||
| 1252 | if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) { | 1170 | if (cryptodev_asym(&kop, 0, NULL, 0, NULL) == 0) { |
| 1253 | /*OCF success value is 0, if not zero, change dsaret to fail*/ | 1171 | dsaret = kop.crk_status; |
| 1254 | if(0 != kop.crk_status) dsaret = 0; | ||
| 1255 | } else { | 1172 | } else { |
| 1256 | const DSA_METHOD *meth = DSA_OpenSSL(); | 1173 | const DSA_METHOD *meth = DSA_OpenSSL(); |
| 1257 | 1174 | ||
| @@ -1311,7 +1228,7 @@ cryptodev_dh_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
| 1311 | goto err; | 1228 | goto err; |
| 1312 | kop.crk_iparams = 3; | 1229 | kop.crk_iparams = 3; |
| 1313 | 1230 | ||
| 1314 | kop.crk_param[3].crp_p = (caddr_t) key; | 1231 | kop.crk_param[3].crp_p = key; |
| 1315 | kop.crk_param[3].crp_nbits = keylen * 8; | 1232 | kop.crk_param[3].crp_nbits = keylen * 8; |
| 1316 | kop.crk_oparams = 1; | 1233 | kop.crk_oparams = 1; |
| 1317 | 1234 | ||
| @@ -1342,7 +1259,7 @@ static DH_METHOD cryptodev_dh = { | |||
| 1342 | * but I expect we'll want some options soon. | 1259 | * but I expect we'll want some options soon. |
| 1343 | */ | 1260 | */ |
| 1344 | static int | 1261 | static int |
| 1345 | cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) | 1262 | cryptodev_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()) |
| 1346 | { | 1263 | { |
| 1347 | #ifdef HAVE_SYSLOG_R | 1264 | #ifdef HAVE_SYSLOG_R |
| 1348 | struct syslog_data sd = SYSLOG_DATA_INIT; | 1265 | struct syslog_data sd = SYSLOG_DATA_INIT; |
| @@ -1378,11 +1295,11 @@ ENGINE_load_cryptodev(void) | |||
| 1378 | * find out what asymmetric crypto algorithms we support | 1295 | * find out what asymmetric crypto algorithms we support |
| 1379 | */ | 1296 | */ |
| 1380 | if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) { | 1297 | if (ioctl(fd, CIOCASYMFEAT, &cryptodev_asymfeat) == -1) { |
| 1381 | put_dev_crypto(fd); | 1298 | close(fd); |
| 1382 | ENGINE_free(engine); | 1299 | ENGINE_free(engine); |
| 1383 | return; | 1300 | return; |
| 1384 | } | 1301 | } |
| 1385 | put_dev_crypto(fd); | 1302 | close(fd); |
| 1386 | 1303 | ||
| 1387 | if (!ENGINE_set_id(engine, "cryptodev") || | 1304 | if (!ENGINE_set_id(engine, "cryptodev") || |
| 1388 | !ENGINE_set_name(engine, "BSD cryptodev engine") || | 1305 | !ENGINE_set_name(engine, "BSD cryptodev engine") || |
