]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/ciphers/cipher_rc4.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / ciphers / cipher_rc4.c
1 /*
2 * Copyright 2019-2021 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_freectx_fn rc4_freectx;
25 static OSSL_FUNC_cipher_dupctx_fn rc4_dupctx;
26
27 static void rc4_freectx(void *vctx)
28 {
29 PROV_RC4_CTX *ctx = (PROV_RC4_CTX *)vctx;
30
31 ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
32 OPENSSL_clear_free(ctx, sizeof(*ctx));
33 }
34
35 static void *rc4_dupctx(void *ctx)
36 {
37 PROV_RC4_CTX *in = (PROV_RC4_CTX *)ctx;
38 PROV_RC4_CTX *ret;
39
40 if (!ossl_prov_is_running())
41 return NULL;
42
43 ret = OPENSSL_malloc(sizeof(*ret));
44 if (ret == NULL) {
45 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
46 return NULL;
47 }
48 *ret = *in;
49
50 return ret;
51 }
52
53 #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ) \
54 static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_get_params; \
55 static int alg##_##kbits##_get_params(OSSL_PARAM params[]) \
56 { \
57 return ossl_cipher_generic_get_params(params, 0, flags, \
58 kbits, blkbits, ivbits); \
59 } \
60 static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_newctx; \
61 static void * alg##_##kbits##_newctx(void *provctx) \
62 { \
63 PROV_##UCALG##_CTX *ctx; \
64 if (!ossl_prov_is_running()) \
65 return NULL; \
66 ctx = OPENSSL_zalloc(sizeof(*ctx)); \
67 if (ctx != NULL) { \
68 ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits, 0, flags, \
69 ossl_prov_cipher_hw_##alg(kbits), NULL); \
70 } \
71 return ctx; \
72 } \
73 const OSSL_DISPATCH ossl_##alg##kbits##_functions[] = { \
74 { OSSL_FUNC_CIPHER_NEWCTX, \
75 (void (*)(void)) alg##_##kbits##_newctx }, \
76 { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \
77 { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \
78 { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \
79 { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \
80 { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
81 { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \
82 { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \
83 { OSSL_FUNC_CIPHER_GET_PARAMS, \
84 (void (*)(void)) alg##_##kbits##_get_params }, \
85 { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
86 (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
87 { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
88 (void (*)(void))ossl_cipher_var_keylen_set_ctx_params }, \
89 { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
90 (void (*)(void))ossl_cipher_generic_gettable_params }, \
91 { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
92 (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
93 { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
94 (void (*)(void))ossl_cipher_var_keylen_settable_ctx_params }, \
95 { 0, NULL } \
96 };
97
98 /* ossl_rc440_functions */
99 IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 40, 8, 0, stream)
100 /* ossl_rc4128_functions */
101 IMPLEMENT_cipher(rc4, RC4, RC4_FLAGS, 128, 8, 0, stream)