Home | History | Annotate | Line # | Download | only in engines
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the OpenSSL license (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 /*
     11  1.1  christos  * This is the OSSLTEST engine. It provides deliberately crippled digest
     12  1.1  christos  * implementations for test purposes. It is highly insecure and must NOT be
     13  1.1  christos  * used for any purpose except testing
     14  1.1  christos  */
     15  1.1  christos 
     16  1.1  christos #include <stdio.h>
     17  1.1  christos #include <string.h>
     18  1.1  christos 
     19  1.1  christos #include <openssl/engine.h>
     20  1.1  christos #include <openssl/sha.h>
     21  1.1  christos #include <openssl/md5.h>
     22  1.1  christos #include <openssl/rsa.h>
     23  1.1  christos #include <openssl/evp.h>
     24  1.1  christos #include <openssl/modes.h>
     25  1.1  christos #include <openssl/aes.h>
     26  1.1  christos #include <openssl/rand.h>
     27  1.1  christos #include <openssl/crypto.h>
     28  1.1  christos 
     29  1.1  christos #include "e_ossltest_err.c"
     30  1.1  christos 
     31  1.1  christos /* Engine Id and Name */
     32  1.1  christos static const char *engine_ossltest_id = "ossltest";
     33  1.1  christos static const char *engine_ossltest_name = "OpenSSL Test engine support";
     34  1.1  christos 
     35  1.1  christos 
     36  1.1  christos /* Engine Lifetime functions */
     37  1.1  christos static int ossltest_destroy(ENGINE *e);
     38  1.1  christos static int ossltest_init(ENGINE *e);
     39  1.1  christos static int ossltest_finish(ENGINE *e);
     40  1.1  christos void ENGINE_load_ossltest(void);
     41  1.1  christos 
     42  1.1  christos 
     43  1.1  christos /* Set up digests */
     44  1.1  christos static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
     45  1.1  christos                           const int **nids, int nid);
     46  1.1  christos static const RAND_METHOD *ossltest_rand_method(void);
     47  1.1  christos 
     48  1.1  christos /* MD5 */
     49  1.1  christos static int digest_md5_init(EVP_MD_CTX *ctx);
     50  1.1  christos static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
     51  1.1  christos                              size_t count);
     52  1.1  christos static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md);
     53  1.1  christos 
     54  1.1  christos static EVP_MD *_hidden_md5_md = NULL;
     55  1.1  christos static const EVP_MD *digest_md5(void)
     56  1.1  christos {
     57  1.1  christos     if (_hidden_md5_md == NULL) {
     58  1.1  christos         EVP_MD *md;
     59  1.1  christos 
     60  1.1  christos         if ((md = EVP_MD_meth_new(NID_md5, NID_md5WithRSAEncryption)) == NULL
     61  1.1  christos             || !EVP_MD_meth_set_result_size(md, MD5_DIGEST_LENGTH)
     62  1.1  christos             || !EVP_MD_meth_set_input_blocksize(md, MD5_CBLOCK)
     63  1.1  christos             || !EVP_MD_meth_set_app_datasize(md,
     64  1.1  christos                                              sizeof(EVP_MD *) + sizeof(MD5_CTX))
     65  1.1  christos             || !EVP_MD_meth_set_flags(md, 0)
     66  1.1  christos             || !EVP_MD_meth_set_init(md, digest_md5_init)
     67  1.1  christos             || !EVP_MD_meth_set_update(md, digest_md5_update)
     68  1.1  christos             || !EVP_MD_meth_set_final(md, digest_md5_final)) {
     69  1.1  christos             EVP_MD_meth_free(md);
     70  1.1  christos             md = NULL;
     71  1.1  christos         }
     72  1.1  christos         _hidden_md5_md = md;
     73  1.1  christos     }
     74  1.1  christos     return _hidden_md5_md;
     75  1.1  christos }
     76  1.1  christos 
     77  1.1  christos /* SHA1 */
     78  1.1  christos static int digest_sha1_init(EVP_MD_CTX *ctx);
     79  1.1  christos static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
     80  1.1  christos                               size_t count);
     81  1.1  christos static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md);
     82  1.1  christos 
     83  1.1  christos static EVP_MD *_hidden_sha1_md = NULL;
     84  1.1  christos static const EVP_MD *digest_sha1(void)
     85  1.1  christos {
     86  1.1  christos     if (_hidden_sha1_md == NULL) {
     87  1.1  christos         EVP_MD *md;
     88  1.1  christos 
     89  1.1  christos         if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
     90  1.1  christos             || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
     91  1.1  christos             || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
     92  1.1  christos             || !EVP_MD_meth_set_app_datasize(md,
     93  1.1  christos                                              sizeof(EVP_MD *) + sizeof(SHA_CTX))
     94  1.1  christos             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
     95  1.1  christos             || !EVP_MD_meth_set_init(md, digest_sha1_init)
     96  1.1  christos             || !EVP_MD_meth_set_update(md, digest_sha1_update)
     97  1.1  christos             || !EVP_MD_meth_set_final(md, digest_sha1_final)) {
     98  1.1  christos             EVP_MD_meth_free(md);
     99  1.1  christos             md = NULL;
    100  1.1  christos         }
    101  1.1  christos         _hidden_sha1_md = md;
    102  1.1  christos     }
    103  1.1  christos     return _hidden_sha1_md;
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos /* SHA256 */
    107  1.1  christos static int digest_sha256_init(EVP_MD_CTX *ctx);
    108  1.1  christos static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
    109  1.1  christos                                 size_t count);
    110  1.1  christos static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md);
    111  1.1  christos 
    112  1.1  christos static EVP_MD *_hidden_sha256_md = NULL;
    113  1.1  christos static const EVP_MD *digest_sha256(void)
    114  1.1  christos {
    115  1.1  christos     if (_hidden_sha256_md == NULL) {
    116  1.1  christos         EVP_MD *md;
    117  1.1  christos 
    118  1.1  christos         if ((md = EVP_MD_meth_new(NID_sha256, NID_sha256WithRSAEncryption)) == NULL
    119  1.1  christos             || !EVP_MD_meth_set_result_size(md, SHA256_DIGEST_LENGTH)
    120  1.1  christos             || !EVP_MD_meth_set_input_blocksize(md, SHA256_CBLOCK)
    121  1.1  christos             || !EVP_MD_meth_set_app_datasize(md,
    122  1.1  christos                                              sizeof(EVP_MD *) + sizeof(SHA256_CTX))
    123  1.1  christos             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    124  1.1  christos             || !EVP_MD_meth_set_init(md, digest_sha256_init)
    125  1.1  christos             || !EVP_MD_meth_set_update(md, digest_sha256_update)
    126  1.1  christos             || !EVP_MD_meth_set_final(md, digest_sha256_final)) {
    127  1.1  christos             EVP_MD_meth_free(md);
    128  1.1  christos             md = NULL;
    129  1.1  christos         }
    130  1.1  christos         _hidden_sha256_md = md;
    131  1.1  christos     }
    132  1.1  christos     return _hidden_sha256_md;
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos /* SHA384/SHA512 */
    136  1.1  christos static int digest_sha384_init(EVP_MD_CTX *ctx);
    137  1.1  christos static int digest_sha512_init(EVP_MD_CTX *ctx);
    138  1.1  christos static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
    139  1.1  christos                                 size_t count);
    140  1.1  christos static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md);
    141  1.1  christos static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md);
    142  1.1  christos 
    143  1.1  christos static EVP_MD *_hidden_sha384_md = NULL;
    144  1.1  christos static const EVP_MD *digest_sha384(void)
    145  1.1  christos {
    146  1.1  christos     if (_hidden_sha384_md == NULL) {
    147  1.1  christos         EVP_MD *md;
    148  1.1  christos 
    149  1.1  christos         if ((md = EVP_MD_meth_new(NID_sha384, NID_sha384WithRSAEncryption)) == NULL
    150  1.1  christos             || !EVP_MD_meth_set_result_size(md, SHA384_DIGEST_LENGTH)
    151  1.1  christos             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
    152  1.1  christos             || !EVP_MD_meth_set_app_datasize(md,
    153  1.1  christos                                              sizeof(EVP_MD *) + sizeof(SHA512_CTX))
    154  1.1  christos             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    155  1.1  christos             || !EVP_MD_meth_set_init(md, digest_sha384_init)
    156  1.1  christos             || !EVP_MD_meth_set_update(md, digest_sha512_update)
    157  1.1  christos             || !EVP_MD_meth_set_final(md, digest_sha384_final)) {
    158  1.1  christos             EVP_MD_meth_free(md);
    159  1.1  christos             md = NULL;
    160  1.1  christos         }
    161  1.1  christos         _hidden_sha384_md = md;
    162  1.1  christos     }
    163  1.1  christos     return _hidden_sha384_md;
    164  1.1  christos }
    165  1.1  christos static EVP_MD *_hidden_sha512_md = NULL;
    166  1.1  christos static const EVP_MD *digest_sha512(void)
    167  1.1  christos {
    168  1.1  christos     if (_hidden_sha512_md == NULL) {
    169  1.1  christos         EVP_MD *md;
    170  1.1  christos 
    171  1.1  christos         if ((md = EVP_MD_meth_new(NID_sha512, NID_sha512WithRSAEncryption)) == NULL
    172  1.1  christos             || !EVP_MD_meth_set_result_size(md, SHA512_DIGEST_LENGTH)
    173  1.1  christos             || !EVP_MD_meth_set_input_blocksize(md, SHA512_CBLOCK)
    174  1.1  christos             || !EVP_MD_meth_set_app_datasize(md,
    175  1.1  christos                                              sizeof(EVP_MD *) + sizeof(SHA512_CTX))
    176  1.1  christos             || !EVP_MD_meth_set_flags(md, EVP_MD_FLAG_DIGALGID_ABSENT)
    177  1.1  christos             || !EVP_MD_meth_set_init(md, digest_sha512_init)
    178  1.1  christos             || !EVP_MD_meth_set_update(md, digest_sha512_update)
    179  1.1  christos             || !EVP_MD_meth_set_final(md, digest_sha512_final)) {
    180  1.1  christos             EVP_MD_meth_free(md);
    181  1.1  christos             md = NULL;
    182  1.1  christos         }
    183  1.1  christos         _hidden_sha512_md = md;
    184  1.1  christos     }
    185  1.1  christos     return _hidden_sha512_md;
    186  1.1  christos }
    187  1.1  christos static void destroy_digests(void)
    188  1.1  christos {
    189  1.1  christos     EVP_MD_meth_free(_hidden_md5_md);
    190  1.1  christos     _hidden_md5_md = NULL;
    191  1.1  christos     EVP_MD_meth_free(_hidden_sha1_md);
    192  1.1  christos     _hidden_sha1_md = NULL;
    193  1.1  christos     EVP_MD_meth_free(_hidden_sha256_md);
    194  1.1  christos     _hidden_sha256_md = NULL;
    195  1.1  christos     EVP_MD_meth_free(_hidden_sha384_md);
    196  1.1  christos     _hidden_sha384_md = NULL;
    197  1.1  christos     EVP_MD_meth_free(_hidden_sha512_md);
    198  1.1  christos     _hidden_sha512_md = NULL;
    199  1.1  christos }
    200  1.1  christos static int ossltest_digest_nids(const int **nids)
    201  1.1  christos {
    202  1.1  christos     static int digest_nids[6] = { 0, 0, 0, 0, 0, 0 };
    203  1.1  christos     static int pos = 0;
    204  1.1  christos     static int init = 0;
    205  1.1  christos 
    206  1.1  christos     if (!init) {
    207  1.1  christos         const EVP_MD *md;
    208  1.1  christos         if ((md = digest_md5()) != NULL)
    209  1.1  christos             digest_nids[pos++] = EVP_MD_type(md);
    210  1.1  christos         if ((md = digest_sha1()) != NULL)
    211  1.1  christos             digest_nids[pos++] = EVP_MD_type(md);
    212  1.1  christos         if ((md = digest_sha256()) != NULL)
    213  1.1  christos             digest_nids[pos++] = EVP_MD_type(md);
    214  1.1  christos         if ((md = digest_sha384()) != NULL)
    215  1.1  christos             digest_nids[pos++] = EVP_MD_type(md);
    216  1.1  christos         if ((md = digest_sha512()) != NULL)
    217  1.1  christos             digest_nids[pos++] = EVP_MD_type(md);
    218  1.1  christos         digest_nids[pos] = 0;
    219  1.1  christos         init = 1;
    220  1.1  christos     }
    221  1.1  christos     *nids = digest_nids;
    222  1.1  christos     return pos;
    223  1.1  christos }
    224  1.1  christos 
    225  1.1  christos /* Setup ciphers */
    226  1.1  christos static int ossltest_ciphers(ENGINE *, const EVP_CIPHER **,
    227  1.1  christos                             const int **, int);
    228  1.1  christos 
    229  1.1  christos static int ossltest_cipher_nids[] = {
    230  1.1  christos     NID_aes_128_cbc, NID_aes_128_gcm, 0
    231  1.1  christos };
    232  1.1  christos 
    233  1.1  christos /* AES128 */
    234  1.1  christos 
    235  1.1  christos int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    236  1.1  christos                              const unsigned char *iv, int enc);
    237  1.1  christos int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    238  1.1  christos                                const unsigned char *in, size_t inl);
    239  1.1  christos int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    240  1.1  christos                              const unsigned char *iv, int enc);
    241  1.1  christos int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    242  1.1  christos                                const unsigned char *in, size_t inl);
    243  1.1  christos static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    244  1.1  christos                                     void *ptr);
    245  1.1  christos 
    246  1.1  christos static EVP_CIPHER *_hidden_aes_128_cbc = NULL;
    247  1.1  christos static const EVP_CIPHER *ossltest_aes_128_cbc(void)
    248  1.1  christos {
    249  1.1  christos     if (_hidden_aes_128_cbc == NULL
    250  1.1  christos         && ((_hidden_aes_128_cbc = EVP_CIPHER_meth_new(NID_aes_128_cbc,
    251  1.1  christos                                                        16 /* block size */,
    252  1.1  christos                                                        16 /* key len */)) == NULL
    253  1.1  christos             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_cbc,16)
    254  1.1  christos             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_cbc,
    255  1.1  christos                                           EVP_CIPH_FLAG_DEFAULT_ASN1
    256  1.1  christos                                           | EVP_CIPH_CBC_MODE)
    257  1.1  christos             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_cbc,
    258  1.1  christos                                          ossltest_aes128_init_key)
    259  1.1  christos             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_cbc,
    260  1.1  christos                                               ossltest_aes128_cbc_cipher)
    261  1.1  christos             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_cbc,
    262  1.1  christos                                                   EVP_CIPHER_impl_ctx_size(EVP_aes_128_cbc())))) {
    263  1.1  christos         EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
    264  1.1  christos         _hidden_aes_128_cbc = NULL;
    265  1.1  christos     }
    266  1.1  christos     return _hidden_aes_128_cbc;
    267  1.1  christos }
    268  1.1  christos static EVP_CIPHER *_hidden_aes_128_gcm = NULL;
    269  1.1  christos 
    270  1.1  christos #define AES_GCM_FLAGS   (EVP_CIPH_FLAG_DEFAULT_ASN1 \
    271  1.1  christos                 | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
    272  1.1  christos                 | EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
    273  1.1  christos                 | EVP_CIPH_CUSTOM_COPY |EVP_CIPH_FLAG_AEAD_CIPHER \
    274  1.1  christos                 | EVP_CIPH_GCM_MODE)
    275  1.1  christos 
    276  1.1  christos static const EVP_CIPHER *ossltest_aes_128_gcm(void)
    277  1.1  christos {
    278  1.1  christos     if (_hidden_aes_128_gcm == NULL
    279  1.1  christos         && ((_hidden_aes_128_gcm = EVP_CIPHER_meth_new(NID_aes_128_gcm,
    280  1.1  christos                                                        1 /* block size */,
    281  1.1  christos                                                        16 /* key len */)) == NULL
    282  1.1  christos             || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_128_gcm,12)
    283  1.1  christos             || !EVP_CIPHER_meth_set_flags(_hidden_aes_128_gcm, AES_GCM_FLAGS)
    284  1.1  christos             || !EVP_CIPHER_meth_set_init(_hidden_aes_128_gcm,
    285  1.1  christos                                          ossltest_aes128_gcm_init_key)
    286  1.1  christos             || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_128_gcm,
    287  1.1  christos                                               ossltest_aes128_gcm_cipher)
    288  1.1  christos             || !EVP_CIPHER_meth_set_ctrl(_hidden_aes_128_gcm,
    289  1.1  christos                                               ossltest_aes128_gcm_ctrl)
    290  1.1  christos             || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_128_gcm,
    291  1.1  christos                               EVP_CIPHER_impl_ctx_size(EVP_aes_128_gcm())))) {
    292  1.1  christos         EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
    293  1.1  christos         _hidden_aes_128_gcm = NULL;
    294  1.1  christos     }
    295  1.1  christos     return _hidden_aes_128_gcm;
    296  1.1  christos }
    297  1.1  christos 
    298  1.1  christos static void destroy_ciphers(void)
    299  1.1  christos {
    300  1.1  christos     EVP_CIPHER_meth_free(_hidden_aes_128_cbc);
    301  1.1  christos     EVP_CIPHER_meth_free(_hidden_aes_128_gcm);
    302  1.1  christos     _hidden_aes_128_cbc = NULL;
    303  1.1  christos }
    304  1.1  christos 
    305  1.1  christos static int bind_ossltest(ENGINE *e)
    306  1.1  christos {
    307  1.1  christos     /* Ensure the ossltest error handling is set up */
    308  1.1  christos     ERR_load_OSSLTEST_strings();
    309  1.1  christos 
    310  1.1  christos     if (!ENGINE_set_id(e, engine_ossltest_id)
    311  1.1  christos         || !ENGINE_set_name(e, engine_ossltest_name)
    312  1.1  christos         || !ENGINE_set_digests(e, ossltest_digests)
    313  1.1  christos         || !ENGINE_set_ciphers(e, ossltest_ciphers)
    314  1.1  christos         || !ENGINE_set_RAND(e, ossltest_rand_method())
    315  1.1  christos         || !ENGINE_set_destroy_function(e, ossltest_destroy)
    316  1.1  christos         || !ENGINE_set_init_function(e, ossltest_init)
    317  1.1  christos         || !ENGINE_set_finish_function(e, ossltest_finish)) {
    318  1.1  christos         OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);
    319  1.1  christos         return 0;
    320  1.1  christos     }
    321  1.1  christos 
    322  1.1  christos     return 1;
    323  1.1  christos }
    324  1.1  christos 
    325  1.1  christos #ifndef OPENSSL_NO_DYNAMIC_ENGINE
    326  1.1  christos static int bind_helper(ENGINE *e, const char *id)
    327  1.1  christos {
    328  1.1  christos     if (id && (strcmp(id, engine_ossltest_id) != 0))
    329  1.1  christos         return 0;
    330  1.1  christos     if (!bind_ossltest(e))
    331  1.1  christos         return 0;
    332  1.1  christos     return 1;
    333  1.1  christos }
    334  1.1  christos 
    335  1.1  christos IMPLEMENT_DYNAMIC_CHECK_FN()
    336  1.1  christos     IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
    337  1.1  christos #endif
    338  1.1  christos 
    339  1.1  christos static ENGINE *engine_ossltest(void)
    340  1.1  christos {
    341  1.1  christos     ENGINE *ret = ENGINE_new();
    342  1.1  christos     if (ret == NULL)
    343  1.1  christos         return NULL;
    344  1.1  christos     if (!bind_ossltest(ret)) {
    345  1.1  christos         ENGINE_free(ret);
    346  1.1  christos         return NULL;
    347  1.1  christos     }
    348  1.1  christos     return ret;
    349  1.1  christos }
    350  1.1  christos 
    351  1.1  christos void ENGINE_load_ossltest(void)
    352  1.1  christos {
    353  1.1  christos     /* Copied from eng_[openssl|dyn].c */
    354  1.1  christos     ENGINE *toadd = engine_ossltest();
    355  1.1  christos     if (!toadd)
    356  1.1  christos         return;
    357  1.1  christos     ENGINE_add(toadd);
    358  1.1  christos     ENGINE_free(toadd);
    359  1.1  christos     ERR_clear_error();
    360  1.1  christos }
    361  1.1  christos 
    362  1.1  christos 
    363  1.1  christos static int ossltest_init(ENGINE *e)
    364  1.1  christos {
    365  1.1  christos     return 1;
    366  1.1  christos }
    367  1.1  christos 
    368  1.1  christos 
    369  1.1  christos static int ossltest_finish(ENGINE *e)
    370  1.1  christos {
    371  1.1  christos     return 1;
    372  1.1  christos }
    373  1.1  christos 
    374  1.1  christos 
    375  1.1  christos static int ossltest_destroy(ENGINE *e)
    376  1.1  christos {
    377  1.1  christos     destroy_digests();
    378  1.1  christos     destroy_ciphers();
    379  1.1  christos     ERR_unload_OSSLTEST_strings();
    380  1.1  christos     return 1;
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos static int ossltest_digests(ENGINE *e, const EVP_MD **digest,
    384  1.1  christos                           const int **nids, int nid)
    385  1.1  christos {
    386  1.1  christos     int ok = 1;
    387  1.1  christos     if (!digest) {
    388  1.1  christos         /* We are returning a list of supported nids */
    389  1.1  christos         return ossltest_digest_nids(nids);
    390  1.1  christos     }
    391  1.1  christos     /* We are being asked for a specific digest */
    392  1.1  christos     switch (nid) {
    393  1.1  christos     case NID_md5:
    394  1.1  christos         *digest = digest_md5();
    395  1.1  christos         break;
    396  1.1  christos     case NID_sha1:
    397  1.1  christos         *digest = digest_sha1();
    398  1.1  christos         break;
    399  1.1  christos     case NID_sha256:
    400  1.1  christos         *digest = digest_sha256();
    401  1.1  christos         break;
    402  1.1  christos     case NID_sha384:
    403  1.1  christos         *digest = digest_sha384();
    404  1.1  christos         break;
    405  1.1  christos     case NID_sha512:
    406  1.1  christos         *digest = digest_sha512();
    407  1.1  christos         break;
    408  1.1  christos     default:
    409  1.1  christos         ok = 0;
    410  1.1  christos         *digest = NULL;
    411  1.1  christos         break;
    412  1.1  christos     }
    413  1.1  christos     return ok;
    414  1.1  christos }
    415  1.1  christos 
    416  1.1  christos static int ossltest_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
    417  1.1  christos                           const int **nids, int nid)
    418  1.1  christos {
    419  1.1  christos     int ok = 1;
    420  1.1  christos     if (!cipher) {
    421  1.1  christos         /* We are returning a list of supported nids */
    422  1.1  christos         *nids = ossltest_cipher_nids;
    423  1.1  christos         return (sizeof(ossltest_cipher_nids) - 1)
    424  1.1  christos                / sizeof(ossltest_cipher_nids[0]);
    425  1.1  christos     }
    426  1.1  christos     /* We are being asked for a specific cipher */
    427  1.1  christos     switch (nid) {
    428  1.1  christos     case NID_aes_128_cbc:
    429  1.1  christos         *cipher = ossltest_aes_128_cbc();
    430  1.1  christos         break;
    431  1.1  christos     case NID_aes_128_gcm:
    432  1.1  christos         *cipher = ossltest_aes_128_gcm();
    433  1.1  christos         break;
    434  1.1  christos     default:
    435  1.1  christos         ok = 0;
    436  1.1  christos         *cipher = NULL;
    437  1.1  christos         break;
    438  1.1  christos     }
    439  1.1  christos     return ok;
    440  1.1  christos }
    441  1.1  christos 
    442  1.1  christos static void fill_known_data(unsigned char *md, unsigned int len)
    443  1.1  christos {
    444  1.1  christos     unsigned int i;
    445  1.1  christos 
    446  1.1  christos     for (i=0; i<len; i++) {
    447  1.1  christos         md[i] = (unsigned char)(i & 0xff);
    448  1.1  christos     }
    449  1.1  christos }
    450  1.1  christos 
    451  1.1  christos /*
    452  1.1  christos  * MD5 implementation. We go through the motions of doing MD5 by deferring to
    453  1.1  christos  * the standard implementation. Then we overwrite the result with a will defined
    454  1.1  christos  * value, so that all "MD5" digests using the test engine always end up with
    455  1.1  christos  * the same value.
    456  1.1  christos  */
    457  1.1  christos #undef data
    458  1.1  christos #define data(ctx) ((MD5_CTX *)EVP_MD_CTX_md_data(ctx))
    459  1.1  christos static int digest_md5_init(EVP_MD_CTX *ctx)
    460  1.1  christos {
    461  1.1  christos     return MD5_Init(data(ctx));
    462  1.1  christos }
    463  1.1  christos 
    464  1.1  christos static int digest_md5_update(EVP_MD_CTX *ctx, const void *data,
    465  1.1  christos                              size_t count)
    466  1.1  christos {
    467  1.1  christos     return MD5_Update(data(ctx), data, (size_t)count);
    468  1.1  christos }
    469  1.1  christos 
    470  1.1  christos static int digest_md5_final(EVP_MD_CTX *ctx, unsigned char *md)
    471  1.1  christos {
    472  1.1  christos     int ret;
    473  1.1  christos     ret = MD5_Final(md, data(ctx));
    474  1.1  christos 
    475  1.1  christos     if (ret > 0) {
    476  1.1  christos         fill_known_data(md, MD5_DIGEST_LENGTH);
    477  1.1  christos     }
    478  1.1  christos     return ret;
    479  1.1  christos }
    480  1.1  christos 
    481  1.1  christos /*
    482  1.1  christos  * SHA1 implementation.
    483  1.1  christos  */
    484  1.1  christos #undef data
    485  1.1  christos #define data(ctx) ((SHA_CTX *)EVP_MD_CTX_md_data(ctx))
    486  1.1  christos static int digest_sha1_init(EVP_MD_CTX *ctx)
    487  1.1  christos {
    488  1.1  christos     return SHA1_Init(data(ctx));
    489  1.1  christos }
    490  1.1  christos 
    491  1.1  christos static int digest_sha1_update(EVP_MD_CTX *ctx, const void *data,
    492  1.1  christos                               size_t count)
    493  1.1  christos {
    494  1.1  christos     return SHA1_Update(data(ctx), data, (size_t)count);
    495  1.1  christos }
    496  1.1  christos 
    497  1.1  christos static int digest_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
    498  1.1  christos {
    499  1.1  christos     int ret;
    500  1.1  christos     ret = SHA1_Final(md, data(ctx));
    501  1.1  christos 
    502  1.1  christos     if (ret > 0) {
    503  1.1  christos         fill_known_data(md, SHA_DIGEST_LENGTH);
    504  1.1  christos     }
    505  1.1  christos     return ret;
    506  1.1  christos }
    507  1.1  christos 
    508  1.1  christos /*
    509  1.1  christos  * SHA256 implementation.
    510  1.1  christos  */
    511  1.1  christos #undef data
    512  1.1  christos #define data(ctx) ((SHA256_CTX *)EVP_MD_CTX_md_data(ctx))
    513  1.1  christos static int digest_sha256_init(EVP_MD_CTX *ctx)
    514  1.1  christos {
    515  1.1  christos     return SHA256_Init(data(ctx));
    516  1.1  christos }
    517  1.1  christos 
    518  1.1  christos static int digest_sha256_update(EVP_MD_CTX *ctx, const void *data,
    519  1.1  christos                                 size_t count)
    520  1.1  christos {
    521  1.1  christos     return SHA256_Update(data(ctx), data, (size_t)count);
    522  1.1  christos }
    523  1.1  christos 
    524  1.1  christos static int digest_sha256_final(EVP_MD_CTX *ctx, unsigned char *md)
    525  1.1  christos {
    526  1.1  christos     int ret;
    527  1.1  christos     ret = SHA256_Final(md, data(ctx));
    528  1.1  christos 
    529  1.1  christos     if (ret > 0) {
    530  1.1  christos         fill_known_data(md, SHA256_DIGEST_LENGTH);
    531  1.1  christos     }
    532  1.1  christos     return ret;
    533  1.1  christos }
    534  1.1  christos 
    535  1.1  christos /*
    536  1.1  christos  * SHA384/512 implementation.
    537  1.1  christos  */
    538  1.1  christos #undef data
    539  1.1  christos #define data(ctx) ((SHA512_CTX *)EVP_MD_CTX_md_data(ctx))
    540  1.1  christos static int digest_sha384_init(EVP_MD_CTX *ctx)
    541  1.1  christos {
    542  1.1  christos     return SHA384_Init(data(ctx));
    543  1.1  christos }
    544  1.1  christos 
    545  1.1  christos static int digest_sha512_init(EVP_MD_CTX *ctx)
    546  1.1  christos {
    547  1.1  christos     return SHA512_Init(data(ctx));
    548  1.1  christos }
    549  1.1  christos 
    550  1.1  christos static int digest_sha512_update(EVP_MD_CTX *ctx, const void *data,
    551  1.1  christos                                 size_t count)
    552  1.1  christos {
    553  1.1  christos     return SHA512_Update(data(ctx), data, (size_t)count);
    554  1.1  christos }
    555  1.1  christos 
    556  1.1  christos static int digest_sha384_final(EVP_MD_CTX *ctx, unsigned char *md)
    557  1.1  christos {
    558  1.1  christos     int ret;
    559  1.1  christos     /* Actually uses SHA512_Final! */
    560  1.1  christos     ret = SHA512_Final(md, data(ctx));
    561  1.1  christos 
    562  1.1  christos     if (ret > 0) {
    563  1.1  christos         fill_known_data(md, SHA384_DIGEST_LENGTH);
    564  1.1  christos     }
    565  1.1  christos     return ret;
    566  1.1  christos }
    567  1.1  christos 
    568  1.1  christos static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
    569  1.1  christos {
    570  1.1  christos     int ret;
    571  1.1  christos     ret = SHA512_Final(md, data(ctx));
    572  1.1  christos 
    573  1.1  christos     if (ret > 0) {
    574  1.1  christos         fill_known_data(md, SHA512_DIGEST_LENGTH);
    575  1.1  christos     }
    576  1.1  christos     return ret;
    577  1.1  christos }
    578  1.1  christos 
    579  1.1  christos /*
    580  1.1  christos  * AES128 Implementation
    581  1.1  christos  */
    582  1.1  christos 
    583  1.1  christos int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    584  1.1  christos                              const unsigned char *iv, int enc)
    585  1.1  christos {
    586  1.1  christos     return EVP_CIPHER_meth_get_init(EVP_aes_128_cbc()) (ctx, key, iv, enc);
    587  1.1  christos }
    588  1.1  christos 
    589  1.1  christos int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    590  1.1  christos                                const unsigned char *in, size_t inl)
    591  1.1  christos {
    592  1.1  christos     unsigned char *tmpbuf;
    593  1.1  christos     int ret;
    594  1.1  christos 
    595  1.1  christos     tmpbuf = OPENSSL_malloc(inl);
    596  1.1  christos 
    597  1.1  christos     /* OPENSSL_malloc will return NULL if inl == 0 */
    598  1.1  christos     if (tmpbuf == NULL && inl > 0)
    599  1.1  christos         return -1;
    600  1.1  christos 
    601  1.1  christos     /* Remember what we were asked to encrypt */
    602  1.1  christos     if (tmpbuf != NULL)
    603  1.1  christos         memcpy(tmpbuf, in, inl);
    604  1.1  christos 
    605  1.1  christos     /* Go through the motions of encrypting it */
    606  1.1  christos     ret = EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_cbc())(ctx, out, in, inl);
    607  1.1  christos 
    608  1.1  christos     /* Throw it all away and just use the plaintext as the output */
    609  1.1  christos     if (tmpbuf != NULL)
    610  1.1  christos         memcpy(out, tmpbuf, inl);
    611  1.1  christos     OPENSSL_free(tmpbuf);
    612  1.1  christos 
    613  1.1  christos     return ret;
    614  1.1  christos }
    615  1.1  christos 
    616  1.1  christos int ossltest_aes128_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    617  1.1  christos                              const unsigned char *iv, int enc)
    618  1.1  christos {
    619  1.1  christos     return EVP_CIPHER_meth_get_init(EVP_aes_128_gcm()) (ctx, key, iv, enc);
    620  1.1  christos }
    621  1.1  christos 
    622  1.1  christos 
    623  1.1  christos int ossltest_aes128_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    624  1.1  christos                                const unsigned char *in, size_t inl)
    625  1.1  christos {
    626  1.1  christos     unsigned char *tmpbuf = OPENSSL_malloc(inl);
    627  1.1  christos 
    628  1.1  christos     /* OPENSSL_malloc will return NULL if inl == 0 */
    629  1.1  christos     if (tmpbuf == NULL && inl > 0)
    630  1.1  christos         return -1;
    631  1.1  christos 
    632  1.1  christos     /* Remember what we were asked to encrypt */
    633  1.1  christos     if (tmpbuf != NULL)
    634  1.1  christos         memcpy(tmpbuf, in, inl);
    635  1.1  christos 
    636  1.1  christos     /* Go through the motions of encrypting it */
    637  1.1  christos     EVP_CIPHER_meth_get_do_cipher(EVP_aes_128_gcm())(ctx, out, in, inl);
    638  1.1  christos 
    639  1.1  christos     /* Throw it all away and just use the plaintext as the output */
    640  1.1  christos     if (tmpbuf != NULL && out != NULL)
    641  1.1  christos         memcpy(out, tmpbuf, inl);
    642  1.1  christos     OPENSSL_free(tmpbuf);
    643  1.1  christos 
    644  1.1  christos     return inl;
    645  1.1  christos }
    646  1.1  christos 
    647  1.1  christos static int ossltest_aes128_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
    648  1.1  christos                                     void *ptr)
    649  1.1  christos {
    650  1.1  christos     /* Pass the ctrl down */
    651  1.1  christos     int ret = EVP_CIPHER_meth_get_ctrl(EVP_aes_128_gcm())(ctx, type, arg, ptr);
    652  1.1  christos 
    653  1.1  christos     if (ret <= 0)
    654  1.1  christos         return ret;
    655  1.1  christos 
    656  1.1  christos     switch(type) {
    657  1.1  christos     case EVP_CTRL_AEAD_GET_TAG:
    658  1.1  christos         /* Always give the same tag */
    659  1.1  christos         memset(ptr, 0, EVP_GCM_TLS_TAG_LEN);
    660  1.1  christos         break;
    661  1.1  christos 
    662  1.1  christos     default:
    663  1.1  christos         break;
    664  1.1  christos     }
    665  1.1  christos 
    666  1.1  christos     return 1;
    667  1.1  christos }
    668  1.1  christos 
    669  1.1  christos static int ossltest_rand_bytes(unsigned char *buf, int num)
    670  1.1  christos {
    671  1.1  christos     unsigned char val = 1;
    672  1.1  christos 
    673  1.1  christos     while (--num >= 0)
    674  1.1  christos         *buf++ = val++;
    675  1.1  christos     return 1;
    676  1.1  christos }
    677  1.1  christos 
    678  1.1  christos static int ossltest_rand_status(void)
    679  1.1  christos {
    680  1.1  christos     return 1;
    681  1.1  christos }
    682  1.1  christos 
    683  1.1  christos static const RAND_METHOD *ossltest_rand_method(void)
    684  1.1  christos {
    685  1.1  christos 
    686  1.1  christos     static RAND_METHOD osslt_rand_meth = {
    687  1.1  christos         NULL,
    688  1.1  christos         ossltest_rand_bytes,
    689  1.1  christos         NULL,
    690  1.1  christos         NULL,
    691  1.1  christos         ossltest_rand_bytes,
    692  1.1  christos         ossltest_rand_status
    693  1.1  christos     };
    694  1.1  christos 
    695  1.1  christos     return &osslt_rand_meth;
    696  1.1  christos }
    697