Fix RC4-MD5 based ciphersuites
[openssl.git] / providers / implementations / ciphers / cipher_rc5.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 RC5 cipher modes ecb, cbc, ofb, cfb */
11
12 /*
13  * RC5 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_rc5.h"
19 #include "prov/implementations.h"
20 #include "prov/providercommon.h"
21 #include "prov/providercommonerr.h"
22
23 static OSSL_FUNC_cipher_freectx_fn rc5_freectx;
24 static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx;
25 OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
26 OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
27
28 static void rc5_freectx(void *vctx)
29 {
30     PROV_RC5_CTX *ctx = (PROV_RC5_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 *rc5_dupctx(void *ctx)
37 {
38     PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
39     PROV_RC5_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 static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
55 {
56     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
57     const OSSL_PARAM *p;
58
59     if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
60         return 0;
61
62     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
63     if (p != NULL) {
64         unsigned int rounds;
65
66         if (!OSSL_PARAM_get_uint(p, &rounds)) {
67             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
68             return 0;
69         }
70         if (rounds != RC5_8_ROUNDS
71             && rounds != RC5_12_ROUNDS
72             && rounds != RC5_16_ROUNDS) {
73             ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
74             return 0;
75         }
76         ctx->rounds = rounds;
77     }
78     return 1;
79 }
80
81 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
82     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
83 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
84
85 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
86     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
87     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
88 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
89
90
91 static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
92 {
93     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
94     OSSL_PARAM *p;
95
96     if (!ossl_cipher_generic_get_ctx_params(vctx, params))
97         return 0;
98     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
99     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
100         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
101         return 0;
102     }
103     return 1;
104 }
105
106 #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,             \
107                          blkbits, ivbits, typ)                                 \
108 static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;   \
109 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
110 {                                                                              \
111     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
112                                           flags, kbits, blkbits, ivbits);      \
113 }                                                                              \
114 static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;           \
115 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
116 {                                                                              \
117      PROV_##UCALG##_CTX *ctx;                                                  \
118      if (!ossl_prov_is_running())                                              \
119         return NULL;                                                           \
120      ctx = OPENSSL_zalloc(sizeof(*ctx));                                       \
121      if (ctx != NULL) {                                                        \
122          ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits,              \
123                                      EVP_CIPH_##UCMODE##_MODE, flags,          \
124                                      ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
125                                 NULL);                                         \
126          ctx->rounds = RC5_12_ROUNDS;                                          \
127      }                                                                         \
128      return ctx;                                                               \
129 }                                                                              \
130 const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = {                \
131     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
132       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
133     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
134     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
135     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\
136     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\
137     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
138     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final },  \
139     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },   \
140     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
141       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
142     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
143       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
144     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
145       (void (*)(void))rc5_get_ctx_params },                                    \
146     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
147       (void (*)(void))rc5_gettable_ctx_params },                               \
148     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
149       (void (*)(void))rc5_set_ctx_params },                                    \
150     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
151      (void (*)(void))rc5_settable_ctx_params },                                \
152     { 0, NULL }                                                                \
153 };
154
155 /* ossl_rc5128ecb_functions */
156 IMPLEMENT_cipher(rc5, RC5, ecb, ECB, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 0, block)
157 /* ossl_rc5128cbc_functions */
158 IMPLEMENT_cipher(rc5, RC5, cbc, CBC, EVP_CIPH_VARIABLE_LENGTH, 128, 64, 64, block)
159 /* ossl_rc5128ofb64_functions */
160 IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)
161 /* ossl_rc5128cfb64_functions */
162 IMPLEMENT_cipher(rc5, RC5, cfb64,  CFB, EVP_CIPH_VARIABLE_LENGTH, 128, 8, 64, stream)