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