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