Home | History | Annotate | Line # | Download | only in ciphers
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-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 /* Dispatch functions for RC4 ciphers */
     11      1.1  christos 
     12      1.1  christos /*
     13      1.1  christos  * RC4 low level APIs are deprecated for public use, but still ok for internal
     14      1.1  christos  * use.
     15      1.1  christos  */
     16      1.1  christos #include "internal/deprecated.h"
     17      1.1  christos 
     18      1.1  christos #include "cipher_rc4.h"
     19      1.1  christos #include "prov/implementations.h"
     20      1.1  christos #include "prov/providercommon.h"
     21      1.1  christos 
     22      1.1  christos #define RC4_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
     23      1.1  christos 
     24      1.1  christos static OSSL_FUNC_cipher_encrypt_init_fn rc4_einit;
     25      1.1  christos static OSSL_FUNC_cipher_decrypt_init_fn rc4_dinit;
     26      1.1  christos static OSSL_FUNC_cipher_freectx_fn rc4_freectx;
     27      1.1  christos static OSSL_FUNC_cipher_dupctx_fn rc4_dupctx;
     28      1.1  christos 
     29      1.1  christos static void rc4_freectx(void *vctx)
     30      1.1  christos {
     31      1.1  christos     PROV_RC4_CTX *ctx = (PROV_RC4_CTX *)vctx;
     32      1.1  christos 
     33      1.1  christos     ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     34  1.1.1.2  christos     OPENSSL_clear_free(ctx, sizeof(*ctx));
     35      1.1  christos }
     36      1.1  christos 
     37      1.1  christos static void *rc4_dupctx(void *ctx)
     38      1.1  christos {
     39      1.1  christos     PROV_RC4_CTX *in = (PROV_RC4_CTX *)ctx;
     40      1.1  christos     PROV_RC4_CTX *ret;
     41      1.1  christos 
     42      1.1  christos     if (!ossl_prov_is_running())
     43      1.1  christos         return NULL;
     44      1.1  christos 
     45      1.1  christos     ret = OPENSSL_malloc(sizeof(*ret));
     46      1.1  christos     if (ret == NULL)
     47      1.1  christos         return NULL;
     48      1.1  christos     *ret = *in;
     49      1.1  christos 
     50      1.1  christos     return ret;
     51      1.1  christos }
     52      1.1  christos 
     53      1.1  christos static int rc4_einit(void *ctx, const unsigned char *key, size_t keylen,
     54  1.1.1.2  christos     const unsigned char *iv, size_t ivlen,
     55  1.1.1.2  christos     const OSSL_PARAM params[])
     56      1.1  christos {
     57      1.1  christos     if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
     58      1.1  christos         return 0;
     59      1.1  christos     return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
     60      1.1  christos }
     61      1.1  christos 
     62      1.1  christos static int rc4_dinit(void *ctx, const unsigned char *key, size_t keylen,
     63  1.1.1.2  christos     const unsigned char *iv, size_t ivlen,
     64  1.1.1.2  christos     const OSSL_PARAM params[])
     65      1.1  christos {
     66      1.1  christos     if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
     67      1.1  christos         return 0;
     68      1.1  christos     return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
     69      1.1  christos }
     70      1.1  christos 
     71  1.1.1.2  christos #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ)                 \
     72  1.1.1.2  christos     static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_get_params;                    \
     73  1.1.1.2  christos     static int alg##_##kbits##_get_params(OSSL_PARAM params[])                           \
     74  1.1.1.2  christos     {                                                                                    \
     75  1.1.1.2  christos         return ossl_cipher_generic_get_params(params, 0, flags,                          \
     76  1.1.1.2  christos             kbits, blkbits, ivbits);                                                     \
     77  1.1.1.2  christos     }                                                                                    \
     78  1.1.1.2  christos     static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_newctx;                            \
     79  1.1.1.2  christos     static void *alg##_##kbits##_newctx(void *provctx)                                   \
     80  1.1.1.2  christos     {                                                                                    \
     81  1.1.1.2  christos         PROV_##UCALG##_CTX *ctx;                                                         \
     82  1.1.1.2  christos         if (!ossl_prov_is_running())                                                     \
     83  1.1.1.2  christos             return NULL;                                                                 \
     84  1.1.1.2  christos         ctx = OPENSSL_zalloc(sizeof(*ctx));                                              \
     85  1.1.1.2  christos         if (ctx != NULL) {                                                               \
     86  1.1.1.2  christos             ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, 0, flags,           \
     87  1.1.1.2  christos                 ossl_prov_cipher_hw_##alg(kbits), NULL);                                 \
     88  1.1.1.2  christos         }                                                                                \
     89  1.1.1.2  christos         return ctx;                                                                      \
     90  1.1.1.2  christos     }                                                                                    \
     91  1.1.1.2  christos     const OSSL_DISPATCH ossl_##alg##kbits##_functions[] = {                              \
     92  1.1.1.2  christos         { OSSL_FUNC_CIPHER_NEWCTX,                                                       \
     93  1.1.1.2  christos             (void (*)(void))alg##_##kbits##_newctx },                                    \
     94  1.1.1.2  christos         { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_freectx },                     \
     95  1.1.1.2  christos         { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))alg##_dupctx },                       \
     96  1.1.1.2  christos         { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_einit },                    \
     97  1.1.1.2  christos         { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_dinit },                    \
     98  1.1.1.2  christos         { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update }, \
     99  1.1.1.2  christos         { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final },   \
    100  1.1.1.2  christos         { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },         \
    101  1.1.1.2  christos         { OSSL_FUNC_CIPHER_GET_PARAMS,                                                   \
    102  1.1.1.2  christos             (void (*)(void))alg##_##kbits##_get_params },                                \
    103  1.1.1.2  christos         { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                               \
    104  1.1.1.2  christos             (void (*)(void))ossl_cipher_generic_get_ctx_params },                        \
    105  1.1.1.2  christos         { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                               \
    106  1.1.1.2  christos             (void (*)(void))ossl_cipher_var_keylen_set_ctx_params },                     \
    107  1.1.1.2  christos         { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                              \
    108  1.1.1.2  christos             (void (*)(void))ossl_cipher_generic_gettable_params },                       \
    109  1.1.1.2  christos         { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                          \
    110  1.1.1.2  christos             (void (*)(void))ossl_cipher_generic_gettable_ctx_params },                   \
    111  1.1.1.2  christos         { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                          \
    112  1.1.1.2  christos             (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params },                \
    113  1.1.1.2  christos         OSSL_DISPATCH_END                                                                \
    114  1.1.1.2  christos     };
    115      1.1  christos 
    116      1.1  christos /* ossl_rc440_functions */
    117      1.1  christos IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 40, 8, 0, stream)
    118      1.1  christos /* ossl_rc4128_functions */
    119      1.1  christos IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 128, 8, 0, stream)
    120