xref: /freebsd/crypto/openssl/providers/implementations/ciphers/cipher_rc5.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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 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 <openssl/proverr.h>
19 #include "cipher_rc5.h"
20 #include "prov/implementations.h"
21 #include "prov/providercommon.h"
22 
23 #define RC5_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH
24 
25 static OSSL_FUNC_cipher_encrypt_init_fn rc5_einit;
26 static OSSL_FUNC_cipher_decrypt_init_fn rc5_dinit;
27 static OSSL_FUNC_cipher_freectx_fn rc5_freectx;
28 static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx;
29 OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params;
30 OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params;
31 static OSSL_FUNC_cipher_set_ctx_params_fn rc5_set_ctx_params;
32 
33 static void rc5_freectx(void *vctx)
34 {
35     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
36 
37     ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
38     OPENSSL_clear_free(ctx,  sizeof(*ctx));
39 }
40 
41 static void *rc5_dupctx(void *ctx)
42 {
43     PROV_RC5_CTX *in = (PROV_RC5_CTX *)ctx;
44     PROV_RC5_CTX *ret;
45 
46     if (!ossl_prov_is_running())
47         return NULL;
48 
49     ret = OPENSSL_malloc(sizeof(*ret));
50     if (ret == NULL) {
51         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
52         return NULL;
53     }
54     *ret = *in;
55 
56     return ret;
57 }
58 
59 static int rc5_einit(void *ctx, const unsigned char *key, size_t keylen,
60                           const unsigned char *iv, size_t ivlen,
61                           const OSSL_PARAM params[])
62 {
63     if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
64         return 0;
65     return rc5_set_ctx_params(ctx, params);
66 }
67 
68 static int rc5_dinit(void *ctx, const unsigned char *key, size_t keylen,
69                           const unsigned char *iv, size_t ivlen,
70                           const OSSL_PARAM params[])
71 {
72     if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
73         return 0;
74     return rc5_set_ctx_params(ctx, params);
75 }
76 
77 static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
78 {
79     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
80     const OSSL_PARAM *p;
81 
82     if (params == NULL)
83         return 1;
84 
85     if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params))
86         return 0;
87 
88     p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ROUNDS);
89     if (p != NULL) {
90         unsigned int rounds;
91 
92         if (!OSSL_PARAM_get_uint(p, &rounds)) {
93             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
94             return 0;
95         }
96         if (rounds != RC5_8_ROUNDS
97             && rounds != RC5_12_ROUNDS
98             && rounds != RC5_16_ROUNDS) {
99             ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_NUMBER_OF_ROUNDS);
100             return 0;
101         }
102         ctx->rounds = rounds;
103     }
104     return 1;
105 }
106 
107 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(rc5)
108     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
109 CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(rc5)
110 
111 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_START(rc5)
112     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
113     OSSL_PARAM_uint(OSSL_CIPHER_PARAM_ROUNDS, NULL),
114 CIPHER_DEFAULT_SETTABLE_CTX_PARAMS_END(rc5)
115 
116 
117 static int rc5_get_ctx_params(void *vctx, OSSL_PARAM params[])
118 {
119     PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx;
120     OSSL_PARAM *p;
121 
122     if (!ossl_cipher_generic_get_ctx_params(vctx, params))
123         return 0;
124     p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ROUNDS);
125     if (p != NULL && !OSSL_PARAM_set_uint(p, ctx->rounds)) {
126         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
127         return 0;
128     }
129     return 1;
130 }
131 
132 #define IMPLEMENT_cipher(alg, UCALG, lcmode, UCMODE, flags, kbits,             \
133                          blkbits, ivbits, typ)                                 \
134 static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lcmode##_get_params;   \
135 static int alg##_##kbits##_##lcmode##_get_params(OSSL_PARAM params[])          \
136 {                                                                              \
137     return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE,    \
138                                           flags, kbits, blkbits, ivbits);      \
139 }                                                                              \
140 static OSSL_FUNC_cipher_newctx_fn alg##_##kbits##_##lcmode##_newctx;           \
141 static void * alg##_##kbits##_##lcmode##_newctx(void *provctx)                 \
142 {                                                                              \
143      PROV_##UCALG##_CTX *ctx;                                                  \
144      if (!ossl_prov_is_running())                                              \
145         return NULL;                                                           \
146      ctx = OPENSSL_zalloc(sizeof(*ctx));                                       \
147      if (ctx != NULL) {                                                        \
148          ossl_cipher_generic_initkey(ctx, kbits, blkbits, ivbits,              \
149                                      EVP_CIPH_##UCMODE##_MODE, flags,          \
150                                      ossl_prov_cipher_hw_##alg##_##lcmode(kbits),\
151                                 NULL);                                         \
152          ctx->rounds = RC5_12_ROUNDS;                                          \
153      }                                                                         \
154      return ctx;                                                               \
155 }                                                                              \
156 const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = {                \
157     { OSSL_FUNC_CIPHER_NEWCTX,                                                 \
158       (void (*)(void)) alg##_##kbits##_##lcmode##_newctx },                    \
159     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx },              \
160     { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx },                \
161     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc5_einit },              \
162     { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc5_dinit },              \
163     { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\
164     { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final },  \
165     { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher },   \
166     { OSSL_FUNC_CIPHER_GET_PARAMS,                                             \
167       (void (*)(void)) alg##_##kbits##_##lcmode##_get_params },                \
168     { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,                                        \
169       (void (*)(void))ossl_cipher_generic_gettable_params },                   \
170     { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,                                         \
171       (void (*)(void))rc5_get_ctx_params },                                    \
172     { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,                                    \
173       (void (*)(void))rc5_gettable_ctx_params },                               \
174     { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,                                         \
175       (void (*)(void))rc5_set_ctx_params },                                    \
176     { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,                                    \
177      (void (*)(void))rc5_settable_ctx_params },                                \
178     { 0, NULL }                                                                \
179 };
180 
181 /* ossl_rc5128ecb_functions */
182 IMPLEMENT_cipher(rc5, RC5, ecb, ECB, RC5_FLAGS, 128, 64, 0, block)
183 /* ossl_rc5128cbc_functions */
184 IMPLEMENT_cipher(rc5, RC5, cbc, CBC, RC5_FLAGS, 128, 64, 64, block)
185 /* ossl_rc5128ofb64_functions */
186 IMPLEMENT_cipher(rc5, RC5, ofb64, OFB, RC5_FLAGS, 128, 8, 64, stream)
187 /* ossl_rc5128cfb64_functions */
188 IMPLEMENT_cipher(rc5, RC5, cfb64,  CFB, RC5_FLAGS, 128, 8, 64, stream)
189