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