1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery /* Dispatch functions for RC4 ciphers */
11*b077aed3SPierre Pronchery
12*b077aed3SPierre Pronchery /*
13*b077aed3SPierre Pronchery * RC4 low level APIs are deprecated for public use, but still ok for internal
14*b077aed3SPierre Pronchery * use.
15*b077aed3SPierre Pronchery */
16*b077aed3SPierre Pronchery #include "internal/deprecated.h"
17*b077aed3SPierre Pronchery
18*b077aed3SPierre Pronchery #include "cipher_rc4.h"
19*b077aed3SPierre Pronchery #include "prov/implementations.h"
20*b077aed3SPierre Pronchery #include "prov/providercommon.h"
21*b077aed3SPierre Pronchery
22*b077aed3SPierre Pronchery #define RC4_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
23*b077aed3SPierre Pronchery
24*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_encrypt_init_fn rc4_einit;
25*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_decrypt_init_fn rc4_dinit;
26*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn rc4_freectx;
27*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_dupctx_fn rc4_dupctx;
28*b077aed3SPierre Pronchery
rc4_freectx(void * vctx)29*b077aed3SPierre Pronchery static void rc4_freectx(void *vctx)
30*b077aed3SPierre Pronchery {
31*b077aed3SPierre Pronchery PROV_RC4_CTX *ctx = (PROV_RC4_CTX *)vctx;
32*b077aed3SPierre Pronchery
33*b077aed3SPierre Pronchery ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
34*b077aed3SPierre Pronchery OPENSSL_clear_free(ctx, sizeof(*ctx));
35*b077aed3SPierre Pronchery }
36*b077aed3SPierre Pronchery
rc4_dupctx(void * ctx)37*b077aed3SPierre Pronchery static void *rc4_dupctx(void *ctx)
38*b077aed3SPierre Pronchery {
39*b077aed3SPierre Pronchery PROV_RC4_CTX *in = (PROV_RC4_CTX *)ctx;
40*b077aed3SPierre Pronchery PROV_RC4_CTX *ret;
41*b077aed3SPierre Pronchery
42*b077aed3SPierre Pronchery if (!ossl_prov_is_running())
43*b077aed3SPierre Pronchery return NULL;
44*b077aed3SPierre Pronchery
45*b077aed3SPierre Pronchery ret = OPENSSL_malloc(sizeof(*ret));
46*b077aed3SPierre Pronchery if (ret == NULL) {
47*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
48*b077aed3SPierre Pronchery return NULL;
49*b077aed3SPierre Pronchery }
50*b077aed3SPierre Pronchery *ret = *in;
51*b077aed3SPierre Pronchery
52*b077aed3SPierre Pronchery return ret;
53*b077aed3SPierre Pronchery }
54*b077aed3SPierre Pronchery
rc4_einit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])55*b077aed3SPierre Pronchery static int rc4_einit(void *ctx, const unsigned char *key, size_t keylen,
56*b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen,
57*b077aed3SPierre Pronchery const OSSL_PARAM params[])
58*b077aed3SPierre Pronchery {
59*b077aed3SPierre Pronchery if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
60*b077aed3SPierre Pronchery return 0;
61*b077aed3SPierre Pronchery return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
62*b077aed3SPierre Pronchery }
63*b077aed3SPierre Pronchery
rc4_dinit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])64*b077aed3SPierre Pronchery static int rc4_dinit(void *ctx, const unsigned char *key, size_t keylen,
65*b077aed3SPierre Pronchery const unsigned char *iv, size_t ivlen,
66*b077aed3SPierre Pronchery const OSSL_PARAM params[])
67*b077aed3SPierre Pronchery {
68*b077aed3SPierre Pronchery if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
69*b077aed3SPierre Pronchery return 0;
70*b077aed3SPierre Pronchery return ossl_cipher_var_keylen_set_ctx_params(ctx, params);
71*b077aed3SPierre Pronchery }
72*b077aed3SPierre Pronchery
73*b077aed3SPierre Pronchery #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ) \
74*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_get_params; \
75*b077aed3SPierre Pronchery static int alg##_##kbits##_get_params(OSSL_PARAM params[]) \
76*b077aed3SPierre Pronchery { \
77*b077aed3SPierre Pronchery return ossl_cipher_generic_get_params(params, 0, flags, \
78*b077aed3SPierre Pronchery kbits, blkbits, ivbits); \
79*b077aed3SPierre Pronchery } \
80*b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_newctx; \
81*b077aed3SPierre Pronchery static void * alg##_##kbits##_newctx(void *provctx) \
82*b077aed3SPierre Pronchery { \
83*b077aed3SPierre Pronchery PROV_##UCALG##_CTX *ctx; \
84*b077aed3SPierre Pronchery if (!ossl_prov_is_running()) \
85*b077aed3SPierre Pronchery return NULL; \
86*b077aed3SPierre Pronchery ctx = OPENSSL_zalloc(sizeof(*ctx)); \
87*b077aed3SPierre Pronchery if (ctx != NULL) { \
88*b077aed3SPierre Pronchery ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, 0, flags, \
89*b077aed3SPierre Pronchery ossl_prov_cipher_hw_##alg(kbits), NULL); \
90*b077aed3SPierre Pronchery } \
91*b077aed3SPierre Pronchery return ctx; \
92*b077aed3SPierre Pronchery } \
93*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_##alg##kbits##_functions[] = { \
94*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_NEWCTX, \
95*b077aed3SPierre Pronchery (void (*)(void)) alg##_##kbits##_newctx }, \
96*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
97*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
98*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_einit }, \
99*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_dinit }, \
100*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
101*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
102*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
103*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_PARAMS, \
104*b077aed3SPierre Pronchery (void (*)(void)) alg##_##kbits##_get_params }, \
105*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
106*b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
107*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
108*b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \
109*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
110*b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_generic_gettable_params }, \
111*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
112*b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
113*b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
114*b077aed3SPierre Pronchery (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \
115*b077aed3SPierre Pronchery { 0, NULL } \
116*b077aed3SPierre Pronchery };
117*b077aed3SPierre Pronchery
118*b077aed3SPierre Pronchery /* ossl_rc440_functions */
119*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 40, 8, 0, stream)
120*b077aed3SPierre Pronchery /* ossl_rc4128_functions */
121*b077aed3SPierre Pronchery IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 128, 8, 0, stream)
122