1 1.1 christos /* 2 1.1 christos * Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos /* We need to use some deprecated APIs */ 11 1.1 christos #define OPENSSL_SUPPRESS_DEPRECATED 12 1.1 christos 13 1.1 christos #include "../e_os.h" 14 1.1 christos #include <string.h> 15 1.1 christos #include <sys/types.h> 16 1.1 christos #include <sys/stat.h> 17 1.1 christos #include <fcntl.h> 18 1.1 christos #include <sys/ioctl.h> 19 1.1 christos #include <unistd.h> 20 1.1 christos #include <assert.h> 21 1.1 christos 22 1.1 christos #include <openssl/conf.h> 23 1.1 christos #include <openssl/evp.h> 24 1.1 christos #include <openssl/err.h> 25 1.1 christos #include <openssl/engine.h> 26 1.1 christos #include <openssl/objects.h> 27 1.1 christos #include "crypto/cryptodev.h" 28 1.1 christos 29 1.1 christos /* #define ENGINE_DEVCRYPTO_DEBUG */ 30 1.1 christos 31 1.1 christos #if CRYPTO_ALGORITHM_MIN < CRYPTO_ALGORITHM_MAX 32 1.1 christos # define CHECK_BSD_STYLE_MACROS 33 1.1 christos #endif 34 1.1 christos 35 1.1 christos #define engine_devcrypto_id "devcrypto" 36 1.1 christos 37 1.1 christos /* 38 1.1 christos * Use session2_op on FreeBSD which permits requesting specific 39 1.1 christos * drivers or classes of drivers at session creation time. 40 1.1 christos */ 41 1.1 christos #ifdef CIOCGSESSION2 42 1.1 christos typedef struct session2_op session_op_t; 43 1.1 christos #else 44 1.1 christos typedef struct session_op session_op_t; 45 1.1 christos #endif 46 1.1 christos 47 1.1 christos /* 48 1.1 christos * ONE global file descriptor for all sessions. This allows operations 49 1.1 christos * such as digest session data copying (see digest_copy()), but is also 50 1.1 christos * saner... why re-open /dev/crypto for every session? 51 1.1 christos */ 52 1.1 christos static int cfd = -1; 53 1.1 christos #define DEVCRYPTO_REQUIRE_ACCELERATED 0 /* require confirmation of acceleration */ 54 1.1 christos #define DEVCRYPTO_USE_SOFTWARE 1 /* allow software drivers */ 55 1.1 christos #define DEVCRYPTO_REJECT_SOFTWARE 2 /* only disallow confirmed software drivers */ 56 1.1 christos 57 1.1 christos #define DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS DEVCRYPTO_REJECT_SOFTWARE 58 1.1 christos static int use_softdrivers = DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS; 59 1.1 christos 60 1.1 christos /* 61 1.1 christos * cipher/digest status & acceleration definitions 62 1.1 christos * Make sure the defaults are set to 0 63 1.1 christos */ 64 1.1 christos struct driver_info_st { 65 1.1 christos enum devcrypto_status_t { 66 1.1 christos DEVCRYPTO_STATUS_FAILURE = -3, /* unusable for other reason */ 67 1.1 christos DEVCRYPTO_STATUS_NO_CIOCCPHASH = -2, /* hash state copy not supported */ 68 1.1 christos DEVCRYPTO_STATUS_NO_CIOCGSESSION = -1, /* session open failed */ 69 1.1 christos DEVCRYPTO_STATUS_UNKNOWN = 0, /* not tested yet */ 70 1.1 christos DEVCRYPTO_STATUS_USABLE = 1 /* algo can be used */ 71 1.1 christos } status; 72 1.1 christos 73 1.1 christos enum devcrypto_accelerated_t { 74 1.1 christos DEVCRYPTO_NOT_ACCELERATED = -1, /* software implemented */ 75 1.1 christos DEVCRYPTO_ACCELERATION_UNKNOWN = 0, /* acceleration support unknown */ 76 1.1 christos DEVCRYPTO_ACCELERATED = 1 /* hardware accelerated */ 77 1.1 christos } accelerated; 78 1.1 christos 79 1.1 christos char *driver_name; 80 1.1 christos }; 81 1.1 christos 82 1.1 christos #ifdef OPENSSL_NO_DYNAMIC_ENGINE 83 1.1 christos void engine_load_devcrypto_int(void); 84 1.1 christos #endif 85 1.1 christos 86 1.1 christos static int clean_devcrypto_session(session_op_t *sess) { 87 1.1 christos if (ioctl(cfd, CIOCFSESSION, &sess->ses) < 0) { 88 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 89 1.1 christos return 0; 90 1.1 christos } 91 1.1 christos memset(sess, 0, sizeof(*sess)); 92 1.1 christos return 1; 93 1.1 christos } 94 1.1 christos 95 1.1 christos /****************************************************************************** 96 1.1 christos * 97 1.1 christos * Ciphers 98 1.1 christos * 99 1.1 christos * Because they all do the same basic operation, we have only one set of 100 1.1 christos * method functions for them all to share, and a mapping table between 101 1.1 christos * NIDs and cryptodev IDs, with all the necessary size data. 102 1.1 christos * 103 1.1 christos *****/ 104 1.1 christos 105 1.1 christos struct cipher_ctx { 106 1.1 christos session_op_t sess; 107 1.1 christos int op; /* COP_ENCRYPT or COP_DECRYPT */ 108 1.1 christos unsigned long mode; /* EVP_CIPH_*_MODE */ 109 1.1 christos 110 1.1 christos /* to handle ctr mode being a stream cipher */ 111 1.1 christos unsigned char partial[EVP_MAX_BLOCK_LENGTH]; 112 1.1 christos unsigned int blocksize, num; 113 1.1 christos }; 114 1.1 christos 115 1.1 christos static const struct cipher_data_st { 116 1.1 christos int nid; 117 1.1 christos int blocksize; 118 1.1 christos int keylen; 119 1.1 christos int ivlen; 120 1.1 christos int flags; 121 1.1 christos int devcryptoid; 122 1.1 christos } cipher_data[] = { 123 1.1 christos #ifndef OPENSSL_NO_DES 124 1.1 christos { NID_des_cbc, 8, 8, 8, EVP_CIPH_CBC_MODE, CRYPTO_DES_CBC }, 125 1.1 christos { NID_des_ede3_cbc, 8, 24, 8, EVP_CIPH_CBC_MODE, CRYPTO_3DES_CBC }, 126 1.1 christos #endif 127 1.1 christos #ifndef OPENSSL_NO_BF 128 1.1 christos { NID_bf_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_BLF_CBC }, 129 1.1 christos #endif 130 1.1 christos #ifndef OPENSSL_NO_CAST 131 1.1 christos { NID_cast5_cbc, 8, 16, 8, EVP_CIPH_CBC_MODE, CRYPTO_CAST_CBC }, 132 1.1 christos #endif 133 1.1 christos { NID_aes_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC }, 134 1.1 christos { NID_aes_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC }, 135 1.1 christos { NID_aes_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, CRYPTO_AES_CBC }, 136 1.1 christos #ifndef OPENSSL_NO_RC4 137 1.1 christos { NID_rc4, 1, 16, 0, EVP_CIPH_STREAM_CIPHER, CRYPTO_ARC4 }, 138 1.1 christos #endif 139 1.1 christos #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_CTR) 140 1.1 christos { NID_aes_128_ctr, 16, 128 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR }, 141 1.1 christos { NID_aes_192_ctr, 16, 192 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR }, 142 1.1 christos { NID_aes_256_ctr, 16, 256 / 8, 16, EVP_CIPH_CTR_MODE, CRYPTO_AES_CTR }, 143 1.1 christos #endif 144 1.1 christos #if 0 /* Not yet supported */ 145 1.1 christos { NID_aes_128_xts, 16, 128 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS }, 146 1.1 christos { NID_aes_256_xts, 16, 256 / 8 * 2, 16, EVP_CIPH_XTS_MODE, CRYPTO_AES_XTS }, 147 1.1 christos #endif 148 1.1 christos #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_AES_ECB) 149 1.1 christos { NID_aes_128_ecb, 16, 128 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB }, 150 1.1 christos { NID_aes_192_ecb, 16, 192 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB }, 151 1.1 christos { NID_aes_256_ecb, 16, 256 / 8, 0, EVP_CIPH_ECB_MODE, CRYPTO_AES_ECB }, 152 1.1 christos #endif 153 1.1 christos #if 0 /* Not yet supported */ 154 1.1 christos { NID_aes_128_gcm, 16, 128 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM }, 155 1.1 christos { NID_aes_192_gcm, 16, 192 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM }, 156 1.1 christos { NID_aes_256_gcm, 16, 256 / 8, 16, EVP_CIPH_GCM_MODE, CRYPTO_AES_GCM }, 157 1.1 christos #endif 158 1.1 christos #ifndef OPENSSL_NO_CAMELLIA 159 1.1 christos { NID_camellia_128_cbc, 16, 128 / 8, 16, EVP_CIPH_CBC_MODE, 160 1.1 christos CRYPTO_CAMELLIA_CBC }, 161 1.1 christos { NID_camellia_192_cbc, 16, 192 / 8, 16, EVP_CIPH_CBC_MODE, 162 1.1 christos CRYPTO_CAMELLIA_CBC }, 163 1.1 christos { NID_camellia_256_cbc, 16, 256 / 8, 16, EVP_CIPH_CBC_MODE, 164 1.1 christos CRYPTO_CAMELLIA_CBC }, 165 1.1 christos #endif 166 1.1 christos }; 167 1.1 christos 168 1.1 christos static size_t find_cipher_data_index(int nid) 169 1.1 christos { 170 1.1 christos size_t i; 171 1.1 christos 172 1.1 christos for (i = 0; i < OSSL_NELEM(cipher_data); i++) 173 1.1 christos if (nid == cipher_data[i].nid) 174 1.1 christos return i; 175 1.1 christos return (size_t)-1; 176 1.1 christos } 177 1.1 christos 178 1.1 christos static size_t get_cipher_data_index(int nid) 179 1.1 christos { 180 1.1 christos size_t i = find_cipher_data_index(nid); 181 1.1 christos 182 1.1 christos if (i != (size_t)-1) 183 1.1 christos return i; 184 1.1 christos 185 1.1 christos /* 186 1.1 christos * Code further down must make sure that only NIDs in the table above 187 1.1 christos * are used. If any other NID reaches this function, there's a grave 188 1.1 christos * coding error further down. 189 1.1 christos */ 190 1.1 christos assert("Code that never should be reached" == NULL); 191 1.1 christos return -1; 192 1.1 christos } 193 1.1 christos 194 1.1 christos static const struct cipher_data_st *get_cipher_data(int nid) 195 1.1 christos { 196 1.1 christos return &cipher_data[get_cipher_data_index(nid)]; 197 1.1 christos } 198 1.1 christos 199 1.1 christos /* 200 1.1 christos * Following are the three necessary functions to map OpenSSL functionality 201 1.1 christos * with cryptodev. 202 1.1 christos */ 203 1.1 christos 204 1.1 christos static int cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, 205 1.1 christos const unsigned char *iv, int enc) 206 1.1 christos { 207 1.1 christos struct cipher_ctx *cipher_ctx = 208 1.1 christos (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); 209 1.1 christos const struct cipher_data_st *cipher_d = 210 1.1 christos get_cipher_data(EVP_CIPHER_CTX_get_nid(ctx)); 211 1.1 christos int ret; 212 1.1 christos 213 1.1 christos /* cleanup a previous session */ 214 1.1 christos if (cipher_ctx->sess.ses != 0 && 215 1.1 christos clean_devcrypto_session(&cipher_ctx->sess) == 0) 216 1.1 christos return 0; 217 1.1 christos 218 1.1 christos cipher_ctx->sess.cipher = cipher_d->devcryptoid; 219 1.1 christos cipher_ctx->sess.keylen = cipher_d->keylen; 220 1.1 christos cipher_ctx->sess.key = (void *)key; 221 1.1 christos cipher_ctx->op = enc ? COP_ENCRYPT : COP_DECRYPT; 222 1.1 christos cipher_ctx->mode = cipher_d->flags & EVP_CIPH_MODE; 223 1.1 christos cipher_ctx->blocksize = cipher_d->blocksize; 224 1.1 christos #ifdef CIOCGSESSION2 225 1.1 christos cipher_ctx->sess.crid = (use_softdrivers == DEVCRYPTO_USE_SOFTWARE) ? 226 1.1 christos CRYPTO_FLAG_SOFTWARE | CRYPTO_FLAG_HARDWARE : 227 1.1 christos CRYPTO_FLAG_HARDWARE; 228 1.1 christos ret = ioctl(cfd, CIOCGSESSION2, &cipher_ctx->sess); 229 1.1 christos #else 230 1.1 christos ret = ioctl(cfd, CIOCGSESSION, &cipher_ctx->sess); 231 1.1 christos #endif 232 1.1 christos if (ret < 0) { 233 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 234 1.1 christos return 0; 235 1.1 christos } 236 1.1 christos 237 1.1 christos return 1; 238 1.1 christos } 239 1.1 christos 240 1.1 christos static int cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 241 1.1 christos const unsigned char *in, size_t inl) 242 1.1 christos { 243 1.1 christos struct cipher_ctx *cipher_ctx = 244 1.1 christos (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); 245 1.1 christos struct crypt_op cryp; 246 1.1 christos unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx); 247 1.1 christos #if !defined(COP_FLAG_WRITE_IV) 248 1.1 christos unsigned char saved_iv[EVP_MAX_IV_LENGTH]; 249 1.1 christos const unsigned char *ivptr; 250 1.1 christos size_t nblocks, ivlen; 251 1.1 christos #endif 252 1.1 christos 253 1.1 christos memset(&cryp, 0, sizeof(cryp)); 254 1.1 christos cryp.ses = cipher_ctx->sess.ses; 255 1.1 christos cryp.len = inl; 256 1.1 christos cryp.src = (void *)in; 257 1.1 christos cryp.dst = (void *)out; 258 1.1 christos cryp.iv = (void *)iv; 259 1.1 christos cryp.op = cipher_ctx->op; 260 1.1 christos #if !defined(COP_FLAG_WRITE_IV) 261 1.1 christos cryp.flags = 0; 262 1.1 christos 263 1.1 christos ivlen = EVP_CIPHER_CTX_get_iv_length(ctx); 264 1.1 christos if (ivlen > 0) 265 1.1 christos switch (cipher_ctx->mode) { 266 1.1 christos case EVP_CIPH_CBC_MODE: 267 1.1 christos assert(inl >= ivlen); 268 1.1 christos if (!EVP_CIPHER_CTX_is_encrypting(ctx)) { 269 1.1 christos ivptr = in + inl - ivlen; 270 1.1 christos memcpy(saved_iv, ivptr, ivlen); 271 1.1 christos } 272 1.1 christos break; 273 1.1 christos 274 1.1 christos case EVP_CIPH_CTR_MODE: 275 1.1 christos break; 276 1.1 christos 277 1.1 christos default: /* should not happen */ 278 1.1 christos return 0; 279 1.1 christos } 280 1.1 christos #else 281 1.1 christos cryp.flags = COP_FLAG_WRITE_IV; 282 1.1 christos #endif 283 1.1 christos 284 1.1 christos if (ioctl(cfd, CIOCCRYPT, &cryp) < 0) { 285 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 286 1.1 christos return 0; 287 1.1 christos } 288 1.1 christos 289 1.1 christos #if !defined(COP_FLAG_WRITE_IV) 290 1.1 christos if (ivlen > 0) 291 1.1 christos switch (cipher_ctx->mode) { 292 1.1 christos case EVP_CIPH_CBC_MODE: 293 1.1 christos assert(inl >= ivlen); 294 1.1 christos if (EVP_CIPHER_CTX_is_encrypting(ctx)) 295 1.1 christos ivptr = out + inl - ivlen; 296 1.1 christos else 297 1.1 christos ivptr = saved_iv; 298 1.1 christos 299 1.1 christos memcpy(iv, ivptr, ivlen); 300 1.1 christos break; 301 1.1 christos 302 1.1 christos case EVP_CIPH_CTR_MODE: 303 1.1 christos nblocks = (inl + cipher_ctx->blocksize - 1) 304 1.1 christos / cipher_ctx->blocksize; 305 1.1 christos do { 306 1.1 christos ivlen--; 307 1.1 christos nblocks += iv[ivlen]; 308 1.1 christos iv[ivlen] = (uint8_t) nblocks; 309 1.1 christos nblocks >>= 8; 310 1.1 christos } while (ivlen); 311 1.1 christos break; 312 1.1 christos 313 1.1 christos default: /* should not happen */ 314 1.1 christos return 0; 315 1.1 christos } 316 1.1 christos #endif 317 1.1 christos 318 1.1 christos return 1; 319 1.1 christos } 320 1.1 christos 321 1.1 christos static int ctr_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 322 1.1 christos const unsigned char *in, size_t inl) 323 1.1 christos { 324 1.1 christos struct cipher_ctx *cipher_ctx = 325 1.1 christos (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); 326 1.1 christos size_t nblocks, len; 327 1.1 christos 328 1.1 christos /* initial partial block */ 329 1.1 christos while (cipher_ctx->num && inl) { 330 1.1 christos (*out++) = *(in++) ^ cipher_ctx->partial[cipher_ctx->num]; 331 1.1 christos --inl; 332 1.1 christos cipher_ctx->num = (cipher_ctx->num + 1) % cipher_ctx->blocksize; 333 1.1 christos } 334 1.1 christos 335 1.1 christos /* full blocks */ 336 1.1 christos if (inl > (unsigned int) cipher_ctx->blocksize) { 337 1.1 christos nblocks = inl/cipher_ctx->blocksize; 338 1.1 christos len = nblocks * cipher_ctx->blocksize; 339 1.1 christos if (cipher_do_cipher(ctx, out, in, len) < 1) 340 1.1 christos return 0; 341 1.1 christos inl -= len; 342 1.1 christos out += len; 343 1.1 christos in += len; 344 1.1 christos } 345 1.1 christos 346 1.1 christos /* final partial block */ 347 1.1 christos if (inl) { 348 1.1 christos memset(cipher_ctx->partial, 0, cipher_ctx->blocksize); 349 1.1 christos if (cipher_do_cipher(ctx, cipher_ctx->partial, cipher_ctx->partial, 350 1.1 christos cipher_ctx->blocksize) < 1) 351 1.1 christos return 0; 352 1.1 christos while (inl--) { 353 1.1 christos out[cipher_ctx->num] = in[cipher_ctx->num] 354 1.1 christos ^ cipher_ctx->partial[cipher_ctx->num]; 355 1.1 christos cipher_ctx->num++; 356 1.1 christos } 357 1.1 christos } 358 1.1 christos 359 1.1 christos return 1; 360 1.1 christos } 361 1.1 christos 362 1.1 christos static int cipher_ctrl(EVP_CIPHER_CTX *ctx, int type, int p1, void* p2) 363 1.1 christos { 364 1.1 christos struct cipher_ctx *cipher_ctx = 365 1.1 christos (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); 366 1.1 christos EVP_CIPHER_CTX *to_ctx = (EVP_CIPHER_CTX *)p2; 367 1.1 christos struct cipher_ctx *to_cipher_ctx; 368 1.1 christos 369 1.1 christos switch (type) { 370 1.1 christos 371 1.1 christos case EVP_CTRL_COPY: 372 1.1 christos if (cipher_ctx == NULL) 373 1.1 christos return 1; 374 1.1 christos /* when copying the context, a new session needs to be initialized */ 375 1.1 christos to_cipher_ctx = 376 1.1 christos (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(to_ctx); 377 1.1 christos memset(&to_cipher_ctx->sess, 0, sizeof(to_cipher_ctx->sess)); 378 1.1 christos return cipher_init(to_ctx, (void *)cipher_ctx->sess.key, EVP_CIPHER_CTX_iv(ctx), 379 1.1 christos (cipher_ctx->op == COP_ENCRYPT)); 380 1.1 christos 381 1.1 christos case EVP_CTRL_INIT: 382 1.1 christos memset(&cipher_ctx->sess, 0, sizeof(cipher_ctx->sess)); 383 1.1 christos return 1; 384 1.1 christos 385 1.1 christos default: 386 1.1 christos break; 387 1.1 christos } 388 1.1 christos 389 1.1 christos return -1; 390 1.1 christos } 391 1.1 christos 392 1.1 christos static int cipher_cleanup(EVP_CIPHER_CTX *ctx) 393 1.1 christos { 394 1.1 christos struct cipher_ctx *cipher_ctx = 395 1.1 christos (struct cipher_ctx *)EVP_CIPHER_CTX_get_cipher_data(ctx); 396 1.1 christos 397 1.1 christos return clean_devcrypto_session(&cipher_ctx->sess); 398 1.1 christos } 399 1.1 christos 400 1.1 christos /* 401 1.1 christos * Keep tables of known nids, associated methods, selected ciphers, and driver 402 1.1 christos * info. 403 1.1 christos * Note that known_cipher_nids[] isn't necessarily indexed the same way as 404 1.1 christos * cipher_data[] above, which the other tables are. 405 1.1 christos */ 406 1.1 christos static int known_cipher_nids[OSSL_NELEM(cipher_data)]; 407 1.1 christos static int known_cipher_nids_amount = -1; /* -1 indicates not yet initialised */ 408 1.1 christos static EVP_CIPHER *known_cipher_methods[OSSL_NELEM(cipher_data)] = { NULL, }; 409 1.1 christos static int selected_ciphers[OSSL_NELEM(cipher_data)]; 410 1.1 christos static struct driver_info_st cipher_driver_info[OSSL_NELEM(cipher_data)]; 411 1.1 christos 412 1.1 christos 413 1.1 christos static int devcrypto_test_cipher(size_t cipher_data_index) 414 1.1 christos { 415 1.1 christos return (cipher_driver_info[cipher_data_index].status == DEVCRYPTO_STATUS_USABLE 416 1.1 christos && selected_ciphers[cipher_data_index] == 1 417 1.1 christos && (cipher_driver_info[cipher_data_index].accelerated 418 1.1 christos == DEVCRYPTO_ACCELERATED 419 1.1 christos || use_softdrivers == DEVCRYPTO_USE_SOFTWARE 420 1.1 christos || (cipher_driver_info[cipher_data_index].accelerated 421 1.1 christos != DEVCRYPTO_NOT_ACCELERATED 422 1.1 christos && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE))); 423 1.1 christos } 424 1.1 christos 425 1.1 christos static void prepare_cipher_methods(void) 426 1.1 christos { 427 1.1 christos size_t i; 428 1.1 christos session_op_t sess; 429 1.1 christos unsigned long cipher_mode; 430 1.1 christos #ifdef CIOCGSESSION2 431 1.1 christos struct crypt_find_op fop; 432 1.1 christos enum devcrypto_accelerated_t accelerated; 433 1.1 christos #elif defined(CIOCGSESSINFO) 434 1.1 christos struct session_info_op siop; 435 1.1 christos #endif 436 1.1 christos 437 1.1 christos memset(&cipher_driver_info, 0, sizeof(cipher_driver_info)); 438 1.1 christos 439 1.1 christos memset(&sess, 0, sizeof(sess)); 440 1.1 christos sess.key = (void *)"01234567890123456789012345678901234567890123456789"; 441 1.1 christos 442 1.1 christos for (i = 0, known_cipher_nids_amount = 0; 443 1.1 christos i < OSSL_NELEM(cipher_data); i++) { 444 1.1 christos 445 1.1 christos selected_ciphers[i] = 1; 446 1.1 christos /* 447 1.1 christos * Check that the cipher is usable 448 1.1 christos */ 449 1.1 christos sess.cipher = cipher_data[i].devcryptoid; 450 1.1 christos sess.keylen = cipher_data[i].keylen; 451 1.1 christos #ifdef CIOCGSESSION2 452 1.1 christos /* 453 1.1 christos * When using CIOCGSESSION2, first try to allocate a hardware 454 1.1 christos * ("accelerated") session. If that fails, fall back to 455 1.1 christos * allocating a software session. 456 1.1 christos */ 457 1.1 christos sess.crid = CRYPTO_FLAG_HARDWARE; 458 1.1 christos if (ioctl(cfd, CIOCGSESSION2, &sess) == 0) { 459 1.1 christos accelerated = DEVCRYPTO_ACCELERATED; 460 1.1 christos } else { 461 1.1 christos sess.crid = CRYPTO_FLAG_SOFTWARE; 462 1.1 christos if (ioctl(cfd, CIOCGSESSION2, &sess) < 0) { 463 1.1 christos cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION; 464 1.1 christos continue; 465 1.1 christos } 466 1.1 christos accelerated = DEVCRYPTO_NOT_ACCELERATED; 467 1.1 christos } 468 1.1 christos #else 469 1.1 christos if (ioctl(cfd, CIOCGSESSION, &sess) < 0) { 470 1.1 christos cipher_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION; 471 1.1 christos continue; 472 1.1 christos } 473 1.1 christos #endif 474 1.1 christos 475 1.1 christos cipher_mode = cipher_data[i].flags & EVP_CIPH_MODE; 476 1.1 christos 477 1.1 christos if ((known_cipher_methods[i] = 478 1.1 christos EVP_CIPHER_meth_new(cipher_data[i].nid, 479 1.1 christos cipher_mode == EVP_CIPH_CTR_MODE ? 1 : 480 1.1 christos cipher_data[i].blocksize, 481 1.1 christos cipher_data[i].keylen)) == NULL 482 1.1 christos || !EVP_CIPHER_meth_set_iv_length(known_cipher_methods[i], 483 1.1 christos cipher_data[i].ivlen) 484 1.1 christos || !EVP_CIPHER_meth_set_flags(known_cipher_methods[i], 485 1.1 christos cipher_data[i].flags 486 1.1 christos | EVP_CIPH_CUSTOM_COPY 487 1.1 christos | EVP_CIPH_CTRL_INIT 488 1.1 christos | EVP_CIPH_FLAG_DEFAULT_ASN1) 489 1.1 christos || !EVP_CIPHER_meth_set_init(known_cipher_methods[i], cipher_init) 490 1.1 christos || !EVP_CIPHER_meth_set_do_cipher(known_cipher_methods[i], 491 1.1 christos cipher_mode == EVP_CIPH_CTR_MODE ? 492 1.1 christos ctr_do_cipher : 493 1.1 christos cipher_do_cipher) 494 1.1 christos || !EVP_CIPHER_meth_set_ctrl(known_cipher_methods[i], cipher_ctrl) 495 1.1 christos || !EVP_CIPHER_meth_set_cleanup(known_cipher_methods[i], 496 1.1 christos cipher_cleanup) 497 1.1 christos || !EVP_CIPHER_meth_set_impl_ctx_size(known_cipher_methods[i], 498 1.1 christos sizeof(struct cipher_ctx))) { 499 1.1 christos cipher_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE; 500 1.1 christos EVP_CIPHER_meth_free(known_cipher_methods[i]); 501 1.1 christos known_cipher_methods[i] = NULL; 502 1.1 christos } else { 503 1.1 christos cipher_driver_info[i].status = DEVCRYPTO_STATUS_USABLE; 504 1.1 christos #ifdef CIOCGSESSION2 505 1.1 christos cipher_driver_info[i].accelerated = accelerated; 506 1.1 christos fop.crid = sess.crid; 507 1.1 christos if (ioctl(cfd, CIOCFINDDEV, &fop) == 0) { 508 1.1 christos cipher_driver_info[i].driver_name = 509 1.1 christos OPENSSL_strndup(fop.name, sizeof(fop.name)); 510 1.1 christos } 511 1.1 christos #elif defined(CIOCGSESSINFO) 512 1.1 christos siop.ses = sess.ses; 513 1.1 christos if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) { 514 1.1 christos cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN; 515 1.1 christos } else { 516 1.1 christos cipher_driver_info[i].driver_name = 517 1.1 christos OPENSSL_strndup(siop.cipher_info.cra_driver_name, 518 1.1 christos CRYPTODEV_MAX_ALG_NAME); 519 1.1 christos if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)) 520 1.1 christos cipher_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED; 521 1.1 christos else 522 1.1 christos cipher_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED; 523 1.1 christos } 524 1.1 christos #endif /* CIOCGSESSINFO */ 525 1.1 christos } 526 1.1 christos ioctl(cfd, CIOCFSESSION, &sess.ses); 527 1.1 christos if (devcrypto_test_cipher(i)) { 528 1.1 christos known_cipher_nids[known_cipher_nids_amount++] = 529 1.1 christos cipher_data[i].nid; 530 1.1 christos } 531 1.1 christos } 532 1.1 christos } 533 1.1 christos 534 1.1 christos static void rebuild_known_cipher_nids(ENGINE *e) 535 1.1 christos { 536 1.1 christos size_t i; 537 1.1 christos 538 1.1 christos for (i = 0, known_cipher_nids_amount = 0; i < OSSL_NELEM(cipher_data); i++) { 539 1.1 christos if (devcrypto_test_cipher(i)) 540 1.1 christos known_cipher_nids[known_cipher_nids_amount++] = cipher_data[i].nid; 541 1.1 christos } 542 1.1 christos ENGINE_unregister_ciphers(e); 543 1.1 christos ENGINE_register_ciphers(e); 544 1.1 christos } 545 1.1 christos 546 1.1 christos static const EVP_CIPHER *get_cipher_method(int nid) 547 1.1 christos { 548 1.1 christos size_t i = get_cipher_data_index(nid); 549 1.1 christos 550 1.1 christos if (i == (size_t)-1) 551 1.1 christos return NULL; 552 1.1 christos return known_cipher_methods[i]; 553 1.1 christos } 554 1.1 christos 555 1.1 christos static int get_cipher_nids(const int **nids) 556 1.1 christos { 557 1.1 christos *nids = known_cipher_nids; 558 1.1 christos return known_cipher_nids_amount; 559 1.1 christos } 560 1.1 christos 561 1.1 christos static void destroy_cipher_method(int nid) 562 1.1 christos { 563 1.1 christos size_t i = get_cipher_data_index(nid); 564 1.1 christos 565 1.1 christos EVP_CIPHER_meth_free(known_cipher_methods[i]); 566 1.1 christos known_cipher_methods[i] = NULL; 567 1.1 christos } 568 1.1 christos 569 1.1 christos static void destroy_all_cipher_methods(void) 570 1.1 christos { 571 1.1 christos size_t i; 572 1.1 christos 573 1.1 christos for (i = 0; i < OSSL_NELEM(cipher_data); i++) { 574 1.1 christos destroy_cipher_method(cipher_data[i].nid); 575 1.1 christos OPENSSL_free(cipher_driver_info[i].driver_name); 576 1.1 christos cipher_driver_info[i].driver_name = NULL; 577 1.1 christos } 578 1.1 christos } 579 1.1 christos 580 1.1 christos static int devcrypto_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 581 1.1 christos const int **nids, int nid) 582 1.1 christos { 583 1.1 christos if (cipher == NULL) 584 1.1 christos return get_cipher_nids(nids); 585 1.1 christos 586 1.1 christos *cipher = get_cipher_method(nid); 587 1.1 christos 588 1.1 christos return *cipher != NULL; 589 1.1 christos } 590 1.1 christos 591 1.1 christos static void devcrypto_select_all_ciphers(int *cipher_list) 592 1.1 christos { 593 1.1 christos size_t i; 594 1.1 christos 595 1.1 christos for (i = 0; i < OSSL_NELEM(cipher_data); i++) 596 1.1 christos cipher_list[i] = 1; 597 1.1 christos } 598 1.1 christos 599 1.1 christos static int cryptodev_select_cipher_cb(const char *str, int len, void *usr) 600 1.1 christos { 601 1.1 christos int *cipher_list = (int *)usr; 602 1.1 christos char *name; 603 1.1 christos const EVP_CIPHER *EVP; 604 1.1 christos size_t i; 605 1.1 christos 606 1.1 christos if (len == 0) 607 1.1 christos return 1; 608 1.1 christos if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL) 609 1.1 christos return 0; 610 1.1 christos EVP = EVP_get_cipherbyname(name); 611 1.1 christos if (EVP == NULL) 612 1.1 christos fprintf(stderr, "devcrypto: unknown cipher %s\n", name); 613 1.1 christos else if ((i = find_cipher_data_index(EVP_CIPHER_get_nid(EVP))) != (size_t)-1) 614 1.1 christos cipher_list[i] = 1; 615 1.1 christos else 616 1.1 christos fprintf(stderr, "devcrypto: cipher %s not available\n", name); 617 1.1 christos OPENSSL_free(name); 618 1.1 christos return 1; 619 1.1 christos } 620 1.1 christos 621 1.1 christos static void dump_cipher_info(void) 622 1.1 christos { 623 1.1 christos size_t i; 624 1.1 christos const char *name; 625 1.1 christos 626 1.1 christos fprintf (stderr, "Information about ciphers supported by the /dev/crypto" 627 1.1 christos " engine:\n"); 628 1.1 christos #ifndef CIOCGSESSINFO 629 1.1 christos fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n"); 630 1.1 christos #endif 631 1.1 christos for (i = 0; i < OSSL_NELEM(cipher_data); i++) { 632 1.1 christos name = OBJ_nid2sn(cipher_data[i].nid); 633 1.1 christos fprintf (stderr, "Cipher %s, NID=%d, /dev/crypto info: id=%d, ", 634 1.1 christos name ? name : "unknown", cipher_data[i].nid, 635 1.1 christos cipher_data[i].devcryptoid); 636 1.1 christos if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION ) { 637 1.1 christos fprintf (stderr, "CIOCGSESSION (session open call) failed\n"); 638 1.1 christos continue; 639 1.1 christos } 640 1.1 christos fprintf (stderr, "driver=%s ", cipher_driver_info[i].driver_name ? 641 1.1 christos cipher_driver_info[i].driver_name : "unknown"); 642 1.1 christos if (cipher_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED) 643 1.1 christos fprintf(stderr, "(hw accelerated)"); 644 1.1 christos else if (cipher_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED) 645 1.1 christos fprintf(stderr, "(software)"); 646 1.1 christos else 647 1.1 christos fprintf(stderr, "(acceleration status unknown)"); 648 1.1 christos if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE) 649 1.1 christos fprintf (stderr, ". Cipher setup failed"); 650 1.1 christos fprintf(stderr, "\n"); 651 1.1 christos } 652 1.1 christos fprintf(stderr, "\n"); 653 1.1 christos } 654 1.1 christos 655 1.1 christos /* 656 1.1 christos * We only support digests if the cryptodev implementation supports multiple 657 1.1 christos * data updates and session copying. Otherwise, we would be forced to maintain 658 1.1 christos * a cache, which is perilous if there's a lot of data coming in (if someone 659 1.1 christos * wants to checksum an OpenSSL tarball, for example). 660 1.1 christos */ 661 1.1 christos #if defined(CIOCCPHASH) && defined(COP_FLAG_UPDATE) && defined(COP_FLAG_FINAL) 662 1.1 christos #define IMPLEMENT_DIGEST 663 1.1 christos 664 1.1 christos /****************************************************************************** 665 1.1 christos * 666 1.1 christos * Digests 667 1.1 christos * 668 1.1 christos * Because they all do the same basic operation, we have only one set of 669 1.1 christos * method functions for them all to share, and a mapping table between 670 1.1 christos * NIDs and cryptodev IDs, with all the necessary size data. 671 1.1 christos * 672 1.1 christos *****/ 673 1.1 christos 674 1.1 christos struct digest_ctx { 675 1.1 christos session_op_t sess; 676 1.1 christos /* This signals that the init function was called, not that it succeeded. */ 677 1.1 christos int init_called; 678 1.1 christos unsigned char digest_res[HASH_MAX_LEN]; 679 1.1 christos }; 680 1.1 christos 681 1.1 christos static const struct digest_data_st { 682 1.1 christos int nid; 683 1.1 christos int blocksize; 684 1.1 christos int digestlen; 685 1.1 christos int devcryptoid; 686 1.1 christos } digest_data[] = { 687 1.1 christos #ifndef OPENSSL_NO_MD5 688 1.1 christos { NID_md5, /* MD5_CBLOCK */ 64, 16, CRYPTO_MD5 }, 689 1.1 christos #endif 690 1.1 christos { NID_sha1, SHA_CBLOCK, 20, CRYPTO_SHA1 }, 691 1.1 christos #ifndef OPENSSL_NO_RMD160 692 1.1 christos # if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_RIPEMD160) 693 1.1 christos { NID_ripemd160, /* RIPEMD160_CBLOCK */ 64, 20, CRYPTO_RIPEMD160 }, 694 1.1 christos # endif 695 1.1 christos #endif 696 1.1 christos #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_224) 697 1.1 christos { NID_sha224, SHA256_CBLOCK, 224 / 8, CRYPTO_SHA2_224 }, 698 1.1 christos #endif 699 1.1 christos #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_256) 700 1.1 christos { NID_sha256, SHA256_CBLOCK, 256 / 8, CRYPTO_SHA2_256 }, 701 1.1 christos #endif 702 1.1 christos #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_384) 703 1.1 christos { NID_sha384, SHA512_CBLOCK, 384 / 8, CRYPTO_SHA2_384 }, 704 1.1 christos #endif 705 1.1 christos #if !defined(CHECK_BSD_STYLE_MACROS) || defined(CRYPTO_SHA2_512) 706 1.1 christos { NID_sha512, SHA512_CBLOCK, 512 / 8, CRYPTO_SHA2_512 }, 707 1.1 christos #endif 708 1.1 christos }; 709 1.1 christos 710 1.1 christos static size_t find_digest_data_index(int nid) 711 1.1 christos { 712 1.1 christos size_t i; 713 1.1 christos 714 1.1 christos for (i = 0; i < OSSL_NELEM(digest_data); i++) 715 1.1 christos if (nid == digest_data[i].nid) 716 1.1 christos return i; 717 1.1 christos return (size_t)-1; 718 1.1 christos } 719 1.1 christos 720 1.1 christos static size_t get_digest_data_index(int nid) 721 1.1 christos { 722 1.1 christos size_t i = find_digest_data_index(nid); 723 1.1 christos 724 1.1 christos if (i != (size_t)-1) 725 1.1 christos return i; 726 1.1 christos 727 1.1 christos /* 728 1.1 christos * Code further down must make sure that only NIDs in the table above 729 1.1 christos * are used. If any other NID reaches this function, there's a grave 730 1.1 christos * coding error further down. 731 1.1 christos */ 732 1.1 christos assert("Code that never should be reached" == NULL); 733 1.1 christos return -1; 734 1.1 christos } 735 1.1 christos 736 1.1 christos static const struct digest_data_st *get_digest_data(int nid) 737 1.1 christos { 738 1.1 christos return &digest_data[get_digest_data_index(nid)]; 739 1.1 christos } 740 1.1 christos 741 1.1 christos /* 742 1.1 christos * Following are the five necessary functions to map OpenSSL functionality 743 1.1 christos * with cryptodev: init, update, final, cleanup, and copy. 744 1.1 christos */ 745 1.1 christos 746 1.1 christos static int digest_init(EVP_MD_CTX *ctx) 747 1.1 christos { 748 1.1 christos struct digest_ctx *digest_ctx = 749 1.1 christos (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx); 750 1.1 christos const struct digest_data_st *digest_d = 751 1.1 christos get_digest_data(EVP_MD_CTX_get_type(ctx)); 752 1.1 christos 753 1.1 christos digest_ctx->init_called = 1; 754 1.1 christos 755 1.1 christos memset(&digest_ctx->sess, 0, sizeof(digest_ctx->sess)); 756 1.1 christos digest_ctx->sess.mac = digest_d->devcryptoid; 757 1.1 christos if (ioctl(cfd, CIOCGSESSION, &digest_ctx->sess) < 0) { 758 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 759 1.1 christos return 0; 760 1.1 christos } 761 1.1 christos return 1; 762 1.1 christos } 763 1.1 christos 764 1.1 christos static int digest_op(struct digest_ctx *ctx, const void *src, size_t srclen, 765 1.1 christos void *res, unsigned int flags) 766 1.1 christos { 767 1.1 christos struct crypt_op cryp; 768 1.1 christos 769 1.1 christos memset(&cryp, 0, sizeof(cryp)); 770 1.1 christos cryp.ses = ctx->sess.ses; 771 1.1 christos cryp.len = srclen; 772 1.1 christos cryp.src = (void *)src; 773 1.1 christos cryp.dst = NULL; 774 1.1 christos cryp.mac = res; 775 1.1 christos cryp.flags = flags; 776 1.1 christos return ioctl(cfd, CIOCCRYPT, &cryp); 777 1.1 christos } 778 1.1 christos 779 1.1 christos static int digest_update(EVP_MD_CTX *ctx, const void *data, size_t count) 780 1.1 christos { 781 1.1 christos struct digest_ctx *digest_ctx = 782 1.1 christos (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx); 783 1.1 christos 784 1.1 christos if (count == 0) 785 1.1 christos return 1; 786 1.1 christos 787 1.1 christos if (digest_ctx == NULL) 788 1.1 christos return 0; 789 1.1 christos 790 1.1 christos if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) { 791 1.1 christos if (digest_op(digest_ctx, data, count, digest_ctx->digest_res, 0) >= 0) 792 1.1 christos return 1; 793 1.1 christos } else if (digest_op(digest_ctx, data, count, NULL, COP_FLAG_UPDATE) >= 0) { 794 1.1 christos return 1; 795 1.1 christos } 796 1.1 christos 797 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 798 1.1 christos return 0; 799 1.1 christos } 800 1.1 christos 801 1.1 christos static int digest_final(EVP_MD_CTX *ctx, unsigned char *md) 802 1.1 christos { 803 1.1 christos struct digest_ctx *digest_ctx = 804 1.1 christos (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx); 805 1.1 christos 806 1.1 christos if (md == NULL || digest_ctx == NULL) 807 1.1 christos return 0; 808 1.1 christos 809 1.1 christos if (EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT)) { 810 1.1 christos memcpy(md, digest_ctx->digest_res, EVP_MD_CTX_get_size(ctx)); 811 1.1 christos } else if (digest_op(digest_ctx, NULL, 0, md, COP_FLAG_FINAL) < 0) { 812 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 813 1.1 christos return 0; 814 1.1 christos } 815 1.1 christos 816 1.1 christos return 1; 817 1.1 christos } 818 1.1 christos 819 1.1 christos static int digest_copy(EVP_MD_CTX *to, const EVP_MD_CTX *from) 820 1.1 christos { 821 1.1 christos struct digest_ctx *digest_from = 822 1.1 christos (struct digest_ctx *)EVP_MD_CTX_get0_md_data(from); 823 1.1 christos struct digest_ctx *digest_to = 824 1.1 christos (struct digest_ctx *)EVP_MD_CTX_get0_md_data(to); 825 1.1 christos struct cphash_op cphash; 826 1.1 christos 827 1.1 christos if (digest_from == NULL || digest_from->init_called != 1) 828 1.1 christos return 1; 829 1.1 christos 830 1.1 christos if (!digest_init(to)) { 831 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 832 1.1 christos return 0; 833 1.1 christos } 834 1.1 christos 835 1.1 christos cphash.src_ses = digest_from->sess.ses; 836 1.1 christos cphash.dst_ses = digest_to->sess.ses; 837 1.1 christos if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) { 838 1.1 christos ERR_raise_data(ERR_LIB_SYS, errno, "calling ioctl()"); 839 1.1 christos return 0; 840 1.1 christos } 841 1.1 christos return 1; 842 1.1 christos } 843 1.1 christos 844 1.1 christos static int digest_cleanup(EVP_MD_CTX *ctx) 845 1.1 christos { 846 1.1 christos struct digest_ctx *digest_ctx = 847 1.1 christos (struct digest_ctx *)EVP_MD_CTX_get0_md_data(ctx); 848 1.1 christos 849 1.1 christos if (digest_ctx == NULL) 850 1.1 christos return 1; 851 1.1 christos 852 1.1 christos return clean_devcrypto_session(&digest_ctx->sess); 853 1.1 christos } 854 1.1 christos 855 1.1 christos /* 856 1.1 christos * Keep tables of known nids, associated methods, selected digests, and 857 1.1 christos * driver info. 858 1.1 christos * Note that known_digest_nids[] isn't necessarily indexed the same way as 859 1.1 christos * digest_data[] above, which the other tables are. 860 1.1 christos */ 861 1.1 christos static int known_digest_nids[OSSL_NELEM(digest_data)]; 862 1.1 christos static int known_digest_nids_amount = -1; /* -1 indicates not yet initialised */ 863 1.1 christos static EVP_MD *known_digest_methods[OSSL_NELEM(digest_data)] = { NULL, }; 864 1.1 christos static int selected_digests[OSSL_NELEM(digest_data)]; 865 1.1 christos static struct driver_info_st digest_driver_info[OSSL_NELEM(digest_data)]; 866 1.1 christos 867 1.1 christos static int devcrypto_test_digest(size_t digest_data_index) 868 1.1 christos { 869 1.1 christos return (digest_driver_info[digest_data_index].status == DEVCRYPTO_STATUS_USABLE 870 1.1 christos && selected_digests[digest_data_index] == 1 871 1.1 christos && (digest_driver_info[digest_data_index].accelerated 872 1.1 christos == DEVCRYPTO_ACCELERATED 873 1.1 christos || use_softdrivers == DEVCRYPTO_USE_SOFTWARE 874 1.1 christos || (digest_driver_info[digest_data_index].accelerated 875 1.1 christos != DEVCRYPTO_NOT_ACCELERATED 876 1.1 christos && use_softdrivers == DEVCRYPTO_REJECT_SOFTWARE))); 877 1.1 christos } 878 1.1 christos 879 1.1 christos static void rebuild_known_digest_nids(ENGINE *e) 880 1.1 christos { 881 1.1 christos size_t i; 882 1.1 christos 883 1.1 christos for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); i++) { 884 1.1 christos if (devcrypto_test_digest(i)) 885 1.1 christos known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid; 886 1.1 christos } 887 1.1 christos ENGINE_unregister_digests(e); 888 1.1 christos ENGINE_register_digests(e); 889 1.1 christos } 890 1.1 christos 891 1.1 christos static void prepare_digest_methods(void) 892 1.1 christos { 893 1.1 christos size_t i; 894 1.1 christos session_op_t sess1, sess2; 895 1.1 christos #ifdef CIOCGSESSINFO 896 1.1 christos struct session_info_op siop; 897 1.1 christos #endif 898 1.1 christos struct cphash_op cphash; 899 1.1 christos 900 1.1 christos memset(&digest_driver_info, 0, sizeof(digest_driver_info)); 901 1.1 christos 902 1.1 christos memset(&sess1, 0, sizeof(sess1)); 903 1.1 christos memset(&sess2, 0, sizeof(sess2)); 904 1.1 christos 905 1.1 christos for (i = 0, known_digest_nids_amount = 0; i < OSSL_NELEM(digest_data); 906 1.1 christos i++) { 907 1.1 christos 908 1.1 christos selected_digests[i] = 1; 909 1.1 christos 910 1.1 christos /* 911 1.1 christos * Check that the digest is usable 912 1.1 christos */ 913 1.1 christos sess1.mac = digest_data[i].devcryptoid; 914 1.1 christos sess2.ses = 0; 915 1.1 christos if (ioctl(cfd, CIOCGSESSION, &sess1) < 0) { 916 1.1 christos digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCGSESSION; 917 1.1 christos goto finish; 918 1.1 christos } 919 1.1 christos 920 1.1 christos #ifdef CIOCGSESSINFO 921 1.1 christos /* gather hardware acceleration info from the driver */ 922 1.1 christos siop.ses = sess1.ses; 923 1.1 christos if (ioctl(cfd, CIOCGSESSINFO, &siop) < 0) { 924 1.1 christos digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATION_UNKNOWN; 925 1.1 christos } else { 926 1.1 christos digest_driver_info[i].driver_name = 927 1.1 christos OPENSSL_strndup(siop.hash_info.cra_driver_name, 928 1.1 christos CRYPTODEV_MAX_ALG_NAME); 929 1.1 christos if (siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY) 930 1.1 christos digest_driver_info[i].accelerated = DEVCRYPTO_ACCELERATED; 931 1.1 christos else 932 1.1 christos digest_driver_info[i].accelerated = DEVCRYPTO_NOT_ACCELERATED; 933 1.1 christos } 934 1.1 christos #endif 935 1.1 christos 936 1.1 christos /* digest must be capable of hash state copy */ 937 1.1 christos sess2.mac = sess1.mac; 938 1.1 christos if (ioctl(cfd, CIOCGSESSION, &sess2) < 0) { 939 1.1 christos digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE; 940 1.1 christos goto finish; 941 1.1 christos } 942 1.1 christos cphash.src_ses = sess1.ses; 943 1.1 christos cphash.dst_ses = sess2.ses; 944 1.1 christos if (ioctl(cfd, CIOCCPHASH, &cphash) < 0) { 945 1.1 christos digest_driver_info[i].status = DEVCRYPTO_STATUS_NO_CIOCCPHASH; 946 1.1 christos goto finish; 947 1.1 christos } 948 1.1 christos if ((known_digest_methods[i] = EVP_MD_meth_new(digest_data[i].nid, 949 1.1 christos NID_undef)) == NULL 950 1.1 christos || !EVP_MD_meth_set_input_blocksize(known_digest_methods[i], 951 1.1 christos digest_data[i].blocksize) 952 1.1 christos || !EVP_MD_meth_set_result_size(known_digest_methods[i], 953 1.1 christos digest_data[i].digestlen) 954 1.1 christos || !EVP_MD_meth_set_init(known_digest_methods[i], digest_init) 955 1.1 christos || !EVP_MD_meth_set_update(known_digest_methods[i], digest_update) 956 1.1 christos || !EVP_MD_meth_set_final(known_digest_methods[i], digest_final) 957 1.1 christos || !EVP_MD_meth_set_copy(known_digest_methods[i], digest_copy) 958 1.1 christos || !EVP_MD_meth_set_cleanup(known_digest_methods[i], digest_cleanup) 959 1.1 christos || !EVP_MD_meth_set_app_datasize(known_digest_methods[i], 960 1.1 christos sizeof(struct digest_ctx))) { 961 1.1 christos digest_driver_info[i].status = DEVCRYPTO_STATUS_FAILURE; 962 1.1 christos EVP_MD_meth_free(known_digest_methods[i]); 963 1.1 christos known_digest_methods[i] = NULL; 964 1.1 christos goto finish; 965 1.1 christos } 966 1.1 christos digest_driver_info[i].status = DEVCRYPTO_STATUS_USABLE; 967 1.1 christos finish: 968 1.1 christos ioctl(cfd, CIOCFSESSION, &sess1.ses); 969 1.1 christos if (sess2.ses != 0) 970 1.1 christos ioctl(cfd, CIOCFSESSION, &sess2.ses); 971 1.1 christos if (devcrypto_test_digest(i)) 972 1.1 christos known_digest_nids[known_digest_nids_amount++] = digest_data[i].nid; 973 1.1 christos } 974 1.1 christos } 975 1.1 christos 976 1.1 christos static const EVP_MD *get_digest_method(int nid) 977 1.1 christos { 978 1.1 christos size_t i = get_digest_data_index(nid); 979 1.1 christos 980 1.1 christos if (i == (size_t)-1) 981 1.1 christos return NULL; 982 1.1 christos return known_digest_methods[i]; 983 1.1 christos } 984 1.1 christos 985 1.1 christos static int get_digest_nids(const int **nids) 986 1.1 christos { 987 1.1 christos *nids = known_digest_nids; 988 1.1 christos return known_digest_nids_amount; 989 1.1 christos } 990 1.1 christos 991 1.1 christos static void destroy_digest_method(int nid) 992 1.1 christos { 993 1.1 christos size_t i = get_digest_data_index(nid); 994 1.1 christos 995 1.1 christos EVP_MD_meth_free(known_digest_methods[i]); 996 1.1 christos known_digest_methods[i] = NULL; 997 1.1 christos } 998 1.1 christos 999 1.1 christos static void destroy_all_digest_methods(void) 1000 1.1 christos { 1001 1.1 christos size_t i; 1002 1.1 christos 1003 1.1 christos for (i = 0; i < OSSL_NELEM(digest_data); i++) { 1004 1.1 christos destroy_digest_method(digest_data[i].nid); 1005 1.1 christos OPENSSL_free(digest_driver_info[i].driver_name); 1006 1.1 christos digest_driver_info[i].driver_name = NULL; 1007 1.1 christos } 1008 1.1 christos } 1009 1.1 christos 1010 1.1 christos static int devcrypto_digests(ENGINE *e, const EVP_MD **digest, 1011 1.1 christos const int **nids, int nid) 1012 1.1 christos { 1013 1.1 christos if (digest == NULL) 1014 1.1 christos return get_digest_nids(nids); 1015 1.1 christos 1016 1.1 christos *digest = get_digest_method(nid); 1017 1.1 christos 1018 1.1 christos return *digest != NULL; 1019 1.1 christos } 1020 1.1 christos 1021 1.1 christos static void devcrypto_select_all_digests(int *digest_list) 1022 1.1 christos { 1023 1.1 christos size_t i; 1024 1.1 christos 1025 1.1 christos for (i = 0; i < OSSL_NELEM(digest_data); i++) 1026 1.1 christos digest_list[i] = 1; 1027 1.1 christos } 1028 1.1 christos 1029 1.1 christos static int cryptodev_select_digest_cb(const char *str, int len, void *usr) 1030 1.1 christos { 1031 1.1 christos int *digest_list = (int *)usr; 1032 1.1 christos char *name; 1033 1.1 christos const EVP_MD *EVP; 1034 1.1 christos size_t i; 1035 1.1 christos 1036 1.1 christos if (len == 0) 1037 1.1 christos return 1; 1038 1.1 christos if (usr == NULL || (name = OPENSSL_strndup(str, len)) == NULL) 1039 1.1 christos return 0; 1040 1.1 christos EVP = EVP_get_digestbyname(name); 1041 1.1 christos if (EVP == NULL) 1042 1.1 christos fprintf(stderr, "devcrypto: unknown digest %s\n", name); 1043 1.1 christos else if ((i = find_digest_data_index(EVP_MD_get_type(EVP))) != (size_t)-1) 1044 1.1 christos digest_list[i] = 1; 1045 1.1 christos else 1046 1.1 christos fprintf(stderr, "devcrypto: digest %s not available\n", name); 1047 1.1 christos OPENSSL_free(name); 1048 1.1 christos return 1; 1049 1.1 christos } 1050 1.1 christos 1051 1.1 christos static void dump_digest_info(void) 1052 1.1 christos { 1053 1.1 christos size_t i; 1054 1.1 christos const char *name; 1055 1.1 christos 1056 1.1 christos fprintf (stderr, "Information about digests supported by the /dev/crypto" 1057 1.1 christos " engine:\n"); 1058 1.1 christos #ifndef CIOCGSESSINFO 1059 1.1 christos fprintf(stderr, "CIOCGSESSINFO (session info call) unavailable\n"); 1060 1.1 christos #endif 1061 1.1 christos 1062 1.1 christos for (i = 0; i < OSSL_NELEM(digest_data); i++) { 1063 1.1 christos name = OBJ_nid2sn(digest_data[i].nid); 1064 1.1 christos fprintf (stderr, "Digest %s, NID=%d, /dev/crypto info: id=%d, driver=%s", 1065 1.1 christos name ? name : "unknown", digest_data[i].nid, 1066 1.1 christos digest_data[i].devcryptoid, 1067 1.1 christos digest_driver_info[i].driver_name ? digest_driver_info[i].driver_name : "unknown"); 1068 1.1 christos if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCGSESSION) { 1069 1.1 christos fprintf (stderr, ". CIOCGSESSION (session open) failed\n"); 1070 1.1 christos continue; 1071 1.1 christos } 1072 1.1 christos if (digest_driver_info[i].accelerated == DEVCRYPTO_ACCELERATED) 1073 1.1 christos fprintf(stderr, " (hw accelerated)"); 1074 1.1 christos else if (digest_driver_info[i].accelerated == DEVCRYPTO_NOT_ACCELERATED) 1075 1.1 christos fprintf(stderr, " (software)"); 1076 1.1 christos else 1077 1.1 christos fprintf(stderr, " (acceleration status unknown)"); 1078 1.1 christos if (cipher_driver_info[i].status == DEVCRYPTO_STATUS_FAILURE) 1079 1.1 christos fprintf (stderr, ". Cipher setup failed\n"); 1080 1.1 christos else if (digest_driver_info[i].status == DEVCRYPTO_STATUS_NO_CIOCCPHASH) 1081 1.1 christos fprintf(stderr, ", CIOCCPHASH failed\n"); 1082 1.1 christos else 1083 1.1 christos fprintf(stderr, ", CIOCCPHASH capable\n"); 1084 1.1 christos } 1085 1.1 christos fprintf(stderr, "\n"); 1086 1.1 christos } 1087 1.1 christos 1088 1.1 christos #endif 1089 1.1 christos 1090 1.1 christos /****************************************************************************** 1091 1.1 christos * 1092 1.1 christos * CONTROL COMMANDS 1093 1.1 christos * 1094 1.1 christos *****/ 1095 1.1 christos 1096 1.1 christos #define DEVCRYPTO_CMD_USE_SOFTDRIVERS ENGINE_CMD_BASE 1097 1.1 christos #define DEVCRYPTO_CMD_CIPHERS (ENGINE_CMD_BASE + 1) 1098 1.1 christos #define DEVCRYPTO_CMD_DIGESTS (ENGINE_CMD_BASE + 2) 1099 1.1 christos #define DEVCRYPTO_CMD_DUMP_INFO (ENGINE_CMD_BASE + 3) 1100 1.1 christos 1101 1.1 christos static const ENGINE_CMD_DEFN devcrypto_cmds[] = { 1102 1.1 christos #if defined(CIOCGSESSINFO) || defined(CIOCGSESSION2) 1103 1.1 christos {DEVCRYPTO_CMD_USE_SOFTDRIVERS, 1104 1.1 christos "USE_SOFTDRIVERS", 1105 1.1 christos "specifies whether to use software (not accelerated) drivers (" 1106 1.1 christos OPENSSL_MSTR(DEVCRYPTO_REQUIRE_ACCELERATED) "=use only accelerated drivers, " 1107 1.1 christos OPENSSL_MSTR(DEVCRYPTO_USE_SOFTWARE) "=allow all drivers, " 1108 1.1 christos OPENSSL_MSTR(DEVCRYPTO_REJECT_SOFTWARE) 1109 1.1 christos "=use if acceleration can't be determined) [default=" 1110 1.1 christos OPENSSL_MSTR(DEVCRYPTO_DEFAULT_USE_SOFTDRIVERS) "]", 1111 1.1 christos ENGINE_CMD_FLAG_NUMERIC}, 1112 1.1 christos #endif 1113 1.1 christos 1114 1.1 christos {DEVCRYPTO_CMD_CIPHERS, 1115 1.1 christos "CIPHERS", 1116 1.1 christos "either ALL, NONE, or a comma-separated list of ciphers to enable [default=ALL]", 1117 1.1 christos ENGINE_CMD_FLAG_STRING}, 1118 1.1 christos 1119 1.1 christos #ifdef IMPLEMENT_DIGEST 1120 1.1 christos {DEVCRYPTO_CMD_DIGESTS, 1121 1.1 christos "DIGESTS", 1122 1.1 christos "either ALL, NONE, or a comma-separated list of digests to enable [default=ALL]", 1123 1.1 christos ENGINE_CMD_FLAG_STRING}, 1124 1.1 christos #endif 1125 1.1 christos 1126 1.1 christos {DEVCRYPTO_CMD_DUMP_INFO, 1127 1.1 christos "DUMP_INFO", 1128 1.1 christos "dump info about each algorithm to stderr; use 'openssl engine -pre DUMP_INFO devcrypto'", 1129 1.1 christos ENGINE_CMD_FLAG_NO_INPUT}, 1130 1.1 christos 1131 1.1 christos {0, NULL, NULL, 0} 1132 1.1 christos }; 1133 1.1 christos 1134 1.1 christos static int devcrypto_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) 1135 1.1 christos { 1136 1.1 christos int *new_list; 1137 1.1 christos switch (cmd) { 1138 1.1 christos #if defined(CIOCGSESSINFO) || defined(CIOCGSESSION2) 1139 1.1 christos case DEVCRYPTO_CMD_USE_SOFTDRIVERS: 1140 1.1 christos switch (i) { 1141 1.1 christos case DEVCRYPTO_REQUIRE_ACCELERATED: 1142 1.1 christos case DEVCRYPTO_USE_SOFTWARE: 1143 1.1 christos case DEVCRYPTO_REJECT_SOFTWARE: 1144 1.1 christos break; 1145 1.1 christos default: 1146 1.1 christos fprintf(stderr, "devcrypto: invalid value (%ld) for USE_SOFTDRIVERS\n", i); 1147 1.1 christos return 0; 1148 1.1 christos } 1149 1.1 christos if (use_softdrivers == i) 1150 1.1 christos return 1; 1151 1.1 christos use_softdrivers = i; 1152 1.1 christos #ifdef IMPLEMENT_DIGEST 1153 1.1 christos rebuild_known_digest_nids(e); 1154 1.1 christos #endif 1155 1.1 christos rebuild_known_cipher_nids(e); 1156 1.1 christos return 1; 1157 1.1 christos #endif /* CIOCGSESSINFO || CIOCGSESSION2 */ 1158 1.1 christos 1159 1.1 christos case DEVCRYPTO_CMD_CIPHERS: 1160 1.1 christos if (p == NULL) 1161 1.1 christos return 1; 1162 1.1 christos if (OPENSSL_strcasecmp((const char *)p, "ALL") == 0) { 1163 1.1 christos devcrypto_select_all_ciphers(selected_ciphers); 1164 1.1 christos } else if (OPENSSL_strcasecmp((const char*)p, "NONE") == 0) { 1165 1.1 christos memset(selected_ciphers, 0, sizeof(selected_ciphers)); 1166 1.1 christos } else { 1167 1.1 christos new_list=OPENSSL_zalloc(sizeof(selected_ciphers)); 1168 1.1 christos if (!CONF_parse_list(p, ',', 1, cryptodev_select_cipher_cb, new_list)) { 1169 1.1 christos OPENSSL_free(new_list); 1170 1.1 christos return 0; 1171 1.1 christos } 1172 1.1 christos memcpy(selected_ciphers, new_list, sizeof(selected_ciphers)); 1173 1.1 christos OPENSSL_free(new_list); 1174 1.1 christos } 1175 1.1 christos rebuild_known_cipher_nids(e); 1176 1.1 christos return 1; 1177 1.1 christos 1178 1.1 christos #ifdef IMPLEMENT_DIGEST 1179 1.1 christos case DEVCRYPTO_CMD_DIGESTS: 1180 1.1 christos if (p == NULL) 1181 1.1 christos return 1; 1182 1.1 christos if (OPENSSL_strcasecmp((const char *)p, "ALL") == 0) { 1183 1.1 christos devcrypto_select_all_digests(selected_digests); 1184 1.1 christos } else if (OPENSSL_strcasecmp((const char*)p, "NONE") == 0) { 1185 1.1 christos memset(selected_digests, 0, sizeof(selected_digests)); 1186 1.1 christos } else { 1187 1.1 christos new_list=OPENSSL_zalloc(sizeof(selected_digests)); 1188 1.1 christos if (!CONF_parse_list(p, ',', 1, cryptodev_select_digest_cb, new_list)) { 1189 1.1 christos OPENSSL_free(new_list); 1190 1.1 christos return 0; 1191 1.1 christos } 1192 1.1 christos memcpy(selected_digests, new_list, sizeof(selected_digests)); 1193 1.1 christos OPENSSL_free(new_list); 1194 1.1 christos } 1195 1.1 christos rebuild_known_digest_nids(e); 1196 1.1 christos return 1; 1197 1.1 christos #endif /* IMPLEMENT_DIGEST */ 1198 1.1 christos 1199 1.1 christos case DEVCRYPTO_CMD_DUMP_INFO: 1200 1.1 christos dump_cipher_info(); 1201 1.1 christos #ifdef IMPLEMENT_DIGEST 1202 1.1 christos dump_digest_info(); 1203 1.1 christos #endif 1204 1.1 christos return 1; 1205 1.1 christos 1206 1.1 christos default: 1207 1.1 christos break; 1208 1.1 christos } 1209 1.1 christos return 0; 1210 1.1 christos } 1211 1.1 christos 1212 1.1 christos /****************************************************************************** 1213 1.1 christos * 1214 1.1 christos * LOAD / UNLOAD 1215 1.1 christos * 1216 1.1 christos *****/ 1217 1.1 christos 1218 1.1 christos /* 1219 1.1 christos * Opens /dev/crypto 1220 1.1 christos */ 1221 1.1 christos static int open_devcrypto(void) 1222 1.1 christos { 1223 1.1 christos int fd; 1224 1.1 christos 1225 1.1 christos if (cfd >= 0) 1226 1.1 christos return 1; 1227 1.1 christos 1228 1.1 christos if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) { 1229 1.1 christos #ifndef ENGINE_DEVCRYPTO_DEBUG 1230 1.1 christos if (errno != ENOENT) 1231 1.1 christos #endif 1232 1.1 christos fprintf(stderr, "Could not open /dev/crypto: %s\n", strerror(errno)); 1233 1.1 christos return 0; 1234 1.1 christos } 1235 1.1 christos 1236 1.1 christos #ifdef CRIOGET 1237 1.1 christos if (ioctl(fd, CRIOGET, &cfd) < 0) { 1238 1.1 christos fprintf(stderr, "Could not create crypto fd: %s\n", strerror(errno)); 1239 1.1 christos close(fd); 1240 1.1 christos cfd = -1; 1241 1.1 christos return 0; 1242 1.1 christos } 1243 1.1 christos close(fd); 1244 1.1 christos #else 1245 1.1 christos cfd = fd; 1246 1.1 christos #endif 1247 1.1 christos 1248 1.1 christos return 1; 1249 1.1 christos } 1250 1.1 christos 1251 1.1 christos static int close_devcrypto(void) 1252 1.1 christos { 1253 1.1 christos int ret; 1254 1.1 christos 1255 1.1 christos if (cfd < 0) 1256 1.1 christos return 1; 1257 1.1 christos ret = close(cfd); 1258 1.1 christos cfd = -1; 1259 1.1 christos if (ret != 0) { 1260 1.1 christos fprintf(stderr, "Error closing /dev/crypto: %s\n", strerror(errno)); 1261 1.1 christos return 0; 1262 1.1 christos } 1263 1.1 christos return 1; 1264 1.1 christos } 1265 1.1 christos 1266 1.1 christos static int devcrypto_unload(ENGINE *e) 1267 1.1 christos { 1268 1.1 christos destroy_all_cipher_methods(); 1269 1.1 christos #ifdef IMPLEMENT_DIGEST 1270 1.1 christos destroy_all_digest_methods(); 1271 1.1 christos #endif 1272 1.1 christos 1273 1.1 christos close_devcrypto(); 1274 1.1 christos 1275 1.1 christos return 1; 1276 1.1 christos } 1277 1.1 christos 1278 1.1 christos static int bind_devcrypto(ENGINE *e) { 1279 1.1 christos 1280 1.1 christos if (!ENGINE_set_id(e, engine_devcrypto_id) 1281 1.1 christos || !ENGINE_set_name(e, "/dev/crypto engine") 1282 1.1 christos || !ENGINE_set_destroy_function(e, devcrypto_unload) 1283 1.1 christos || !ENGINE_set_cmd_defns(e, devcrypto_cmds) 1284 1.1 christos || !ENGINE_set_ctrl_function(e, devcrypto_ctrl)) 1285 1.1 christos return 0; 1286 1.1 christos 1287 1.1 christos prepare_cipher_methods(); 1288 1.1 christos #ifdef IMPLEMENT_DIGEST 1289 1.1 christos prepare_digest_methods(); 1290 1.1 christos #endif 1291 1.1 christos 1292 1.1 christos return (ENGINE_set_ciphers(e, devcrypto_ciphers) 1293 1.1 christos #ifdef IMPLEMENT_DIGEST 1294 1.1 christos && ENGINE_set_digests(e, devcrypto_digests) 1295 1.1 christos #endif 1296 1.1 christos /* 1297 1.1 christos * Asymmetric ciphers aren't well supported with /dev/crypto. Among the BSD 1298 1.1 christos * implementations, it seems to only exist in FreeBSD, and regarding the 1299 1.1 christos * parameters in its crypt_kop, the manual crypto(4) has this to say: 1300 1.1 christos * 1301 1.1 christos * The semantics of these arguments are currently undocumented. 1302 1.1 christos * 1303 1.1 christos * Reading through the FreeBSD source code doesn't give much more than 1304 1.1 christos * their CRK_MOD_EXP implementation for ubsec. 1305 1.1 christos * 1306 1.1 christos * It doesn't look much better with cryptodev-linux. They have the crypt_kop 1307 1.1 christos * structure as well as the command (CRK_*) in cryptodev.h, but no support 1308 1.1 christos * seems to be implemented at all for the moment. 1309 1.1 christos * 1310 1.1 christos * At the time of writing, it seems impossible to write proper support for 1311 1.1 christos * FreeBSD's asym features without some very deep knowledge and access to 1312 1.1 christos * specific kernel modules. 1313 1.1 christos * 1314 1.1 christos * /Richard Levitte, 2017-05-11 1315 1.1 christos */ 1316 1.1 christos #if 0 1317 1.1 christos && ENGINE_set_RSA(e, devcrypto_rsa) 1318 1.1 christos # ifndef OPENSSL_NO_DSA 1319 1.1 christos && ENGINE_set_DSA(e, devcrypto_dsa) 1320 1.1 christos # endif 1321 1.1 christos # ifndef OPENSSL_NO_DH 1322 1.1 christos && ENGINE_set_DH(e, devcrypto_dh) 1323 1.1 christos # endif 1324 1.1 christos # ifndef OPENSSL_NO_EC 1325 1.1 christos && ENGINE_set_EC(e, devcrypto_ec) 1326 1.1 christos # endif 1327 1.1 christos #endif 1328 1.1 christos ); 1329 1.1 christos } 1330 1.1 christos 1331 1.1 christos #ifdef OPENSSL_NO_DYNAMIC_ENGINE 1332 1.1 christos /* 1333 1.1 christos * In case this engine is built into libcrypto, then it doesn't offer any 1334 1.1 christos * ability to be dynamically loadable. 1335 1.1 christos */ 1336 1.1 christos void engine_load_devcrypto_int(void) 1337 1.1 christos { 1338 1.1 christos ENGINE *e = NULL; 1339 1.1 christos 1340 1.1 christos if (!open_devcrypto()) 1341 1.1 christos return; 1342 1.1 christos 1343 1.1 christos if ((e = ENGINE_new()) == NULL 1344 1.1 christos || !bind_devcrypto(e)) { 1345 1.1 christos close_devcrypto(); 1346 1.1 christos ENGINE_free(e); 1347 1.1 christos return; 1348 1.1 christos } 1349 1.1 christos 1350 1.1 christos ERR_set_mark(); 1351 1.1 christos ENGINE_add(e); 1352 1.1 christos /* 1353 1.1 christos * If the "add" worked, it gets a structural reference. So either way, we 1354 1.1 christos * release our just-created reference. 1355 1.1 christos */ 1356 1.1 christos ENGINE_free(e); /* Loose our local reference */ 1357 1.1 christos /* 1358 1.1 christos * If the "add" didn't work, it was probably a conflict because it was 1359 1.1 christos * already added (eg. someone calling ENGINE_load_blah then calling 1360 1.1 christos * ENGINE_load_builtin_engines() perhaps). 1361 1.1 christos */ 1362 1.1 christos ERR_pop_to_mark(); 1363 1.1 christos } 1364 1.1 christos 1365 1.1 christos #else 1366 1.1 christos 1367 1.1 christos static int bind_helper(ENGINE *e, const char *id) 1368 1.1 christos { 1369 1.1 christos if ((id && (strcmp(id, engine_devcrypto_id) != 0)) 1370 1.1 christos || !open_devcrypto()) 1371 1.1 christos return 0; 1372 1.1 christos if (!bind_devcrypto(e)) { 1373 1.1 christos close_devcrypto(); 1374 1.1 christos return 0; 1375 1.1 christos } 1376 1.1 christos return 1; 1377 1.1 christos } 1378 1.1 christos 1379 1.1 christos IMPLEMENT_DYNAMIC_CHECK_FN() 1380 1.1 christos IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) 1381 1.1 christos 1382 1.1 christos #endif 1383