1b077aed3SPierre Pronchery /*
2*e0c4386eSCy Schubert * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery */
9b077aed3SPierre Pronchery
10b077aed3SPierre Pronchery /* Dispatch functions for RC4_HMAC_MD5 cipher */
11b077aed3SPierre Pronchery
12b077aed3SPierre Pronchery /*
13b077aed3SPierre Pronchery * MD5 and RC4 low level APIs are deprecated for public use, but still ok for
14b077aed3SPierre Pronchery * internal use.
15b077aed3SPierre Pronchery */
16b077aed3SPierre Pronchery #include "internal/deprecated.h"
17b077aed3SPierre Pronchery
18b077aed3SPierre Pronchery #include <openssl/proverr.h>
19b077aed3SPierre Pronchery #include "cipher_rc4_hmac_md5.h"
20b077aed3SPierre Pronchery #include "prov/implementations.h"
21b077aed3SPierre Pronchery #include "prov/providercommon.h"
22b077aed3SPierre Pronchery
23b077aed3SPierre Pronchery #define RC4_HMAC_MD5_FLAGS (PROV_CIPHER_FLAG_VARIABLE_LENGTH \
24b077aed3SPierre Pronchery | PROV_CIPHER_FLAG_AEAD)
25b077aed3SPierre Pronchery
26b077aed3SPierre Pronchery #define RC4_HMAC_MD5_KEY_BITS (16 * 8)
27b077aed3SPierre Pronchery #define RC4_HMAC_MD5_BLOCK_BITS (1 * 8)
28b077aed3SPierre Pronchery #define RC4_HMAC_MD5_IV_BITS 0
29b077aed3SPierre Pronchery #define RC4_HMAC_MD5_MODE 0
30b077aed3SPierre Pronchery
31b077aed3SPierre Pronchery #define GET_HW(ctx) ((PROV_CIPHER_HW_RC4_HMAC_MD5 *)ctx->base.hw)
32b077aed3SPierre Pronchery
33b077aed3SPierre Pronchery static OSSL_FUNC_cipher_encrypt_init_fn rc4_hmac_md5_einit;
34b077aed3SPierre Pronchery static OSSL_FUNC_cipher_decrypt_init_fn rc4_hmac_md5_dinit;
35b077aed3SPierre Pronchery static OSSL_FUNC_cipher_newctx_fn rc4_hmac_md5_newctx;
36b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn rc4_hmac_md5_freectx;
37*e0c4386eSCy Schubert static OSSL_FUNC_cipher_dupctx_fn rc4_hmac_md5_dupctx;
38b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_ctx_params_fn rc4_hmac_md5_get_ctx_params;
39b077aed3SPierre Pronchery static OSSL_FUNC_cipher_gettable_ctx_params_fn rc4_hmac_md5_gettable_ctx_params;
40b077aed3SPierre Pronchery static OSSL_FUNC_cipher_set_ctx_params_fn rc4_hmac_md5_set_ctx_params;
41b077aed3SPierre Pronchery static OSSL_FUNC_cipher_settable_ctx_params_fn rc4_hmac_md5_settable_ctx_params;
42b077aed3SPierre Pronchery static OSSL_FUNC_cipher_get_params_fn rc4_hmac_md5_get_params;
43b077aed3SPierre Pronchery #define rc4_hmac_md5_gettable_params ossl_cipher_generic_gettable_params
44b077aed3SPierre Pronchery #define rc4_hmac_md5_update ossl_cipher_generic_stream_update
45b077aed3SPierre Pronchery #define rc4_hmac_md5_final ossl_cipher_generic_stream_final
46b077aed3SPierre Pronchery #define rc4_hmac_md5_cipher ossl_cipher_generic_cipher
47b077aed3SPierre Pronchery
rc4_hmac_md5_newctx(void * provctx)48b077aed3SPierre Pronchery static void *rc4_hmac_md5_newctx(void *provctx)
49b077aed3SPierre Pronchery {
50b077aed3SPierre Pronchery PROV_RC4_HMAC_MD5_CTX *ctx;
51b077aed3SPierre Pronchery
52b077aed3SPierre Pronchery if (!ossl_prov_is_running())
53b077aed3SPierre Pronchery return NULL;
54b077aed3SPierre Pronchery
55b077aed3SPierre Pronchery ctx = OPENSSL_zalloc(sizeof(*ctx));
56b077aed3SPierre Pronchery if (ctx != NULL)
57b077aed3SPierre Pronchery ossl_cipher_generic_initkey(ctx, RC4_HMAC_MD5_KEY_BITS,
58b077aed3SPierre Pronchery RC4_HMAC_MD5_BLOCK_BITS,
59b077aed3SPierre Pronchery RC4_HMAC_MD5_IV_BITS,
60b077aed3SPierre Pronchery RC4_HMAC_MD5_MODE, RC4_HMAC_MD5_FLAGS,
61b077aed3SPierre Pronchery ossl_prov_cipher_hw_rc4_hmac_md5(
62b077aed3SPierre Pronchery RC4_HMAC_MD5_KEY_BITS
63b077aed3SPierre Pronchery ), NULL);
64b077aed3SPierre Pronchery return ctx;
65b077aed3SPierre Pronchery }
66b077aed3SPierre Pronchery
rc4_hmac_md5_freectx(void * vctx)67b077aed3SPierre Pronchery static void rc4_hmac_md5_freectx(void *vctx)
68b077aed3SPierre Pronchery {
69b077aed3SPierre Pronchery PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
70b077aed3SPierre Pronchery
71b077aed3SPierre Pronchery ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
72b077aed3SPierre Pronchery OPENSSL_clear_free(ctx, sizeof(*ctx));
73b077aed3SPierre Pronchery }
74b077aed3SPierre Pronchery
rc4_hmac_md5_dupctx(void * vctx)75*e0c4386eSCy Schubert static void *rc4_hmac_md5_dupctx(void *vctx)
76*e0c4386eSCy Schubert {
77*e0c4386eSCy Schubert PROV_RC4_HMAC_MD5_CTX *ctx = vctx;
78*e0c4386eSCy Schubert
79*e0c4386eSCy Schubert if (ctx == NULL)
80*e0c4386eSCy Schubert return NULL;
81*e0c4386eSCy Schubert return OPENSSL_memdup(ctx, sizeof(*ctx));
82*e0c4386eSCy Schubert }
83*e0c4386eSCy Schubert
rc4_hmac_md5_einit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])84b077aed3SPierre Pronchery static int rc4_hmac_md5_einit(void *ctx, const unsigned char *key,
85b077aed3SPierre Pronchery size_t keylen, const unsigned char *iv,
86b077aed3SPierre Pronchery size_t ivlen, const OSSL_PARAM params[])
87b077aed3SPierre Pronchery {
88b077aed3SPierre Pronchery if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
89b077aed3SPierre Pronchery return 0;
90b077aed3SPierre Pronchery return rc4_hmac_md5_set_ctx_params(ctx, params);
91b077aed3SPierre Pronchery }
92b077aed3SPierre Pronchery
rc4_hmac_md5_dinit(void * ctx,const unsigned char * key,size_t keylen,const unsigned char * iv,size_t ivlen,const OSSL_PARAM params[])93b077aed3SPierre Pronchery static int rc4_hmac_md5_dinit(void *ctx, const unsigned char *key,
94b077aed3SPierre Pronchery size_t keylen, const unsigned char *iv,
95b077aed3SPierre Pronchery size_t ivlen, const OSSL_PARAM params[])
96b077aed3SPierre Pronchery {
97b077aed3SPierre Pronchery if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL))
98b077aed3SPierre Pronchery return 0;
99b077aed3SPierre Pronchery return rc4_hmac_md5_set_ctx_params(ctx, params);
100b077aed3SPierre Pronchery }
101b077aed3SPierre Pronchery
102b077aed3SPierre Pronchery static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = {
103b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
104b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
105b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
106b077aed3SPierre Pronchery OSSL_PARAM_END
107b077aed3SPierre Pronchery };
rc4_hmac_md5_gettable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)108b077aed3SPierre Pronchery const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(ossl_unused void *cctx,
109b077aed3SPierre Pronchery ossl_unused void *provctx)
110b077aed3SPierre Pronchery {
111b077aed3SPierre Pronchery return rc4_hmac_md5_known_gettable_ctx_params;
112b077aed3SPierre Pronchery }
113b077aed3SPierre Pronchery
rc4_hmac_md5_get_ctx_params(void * vctx,OSSL_PARAM params[])114b077aed3SPierre Pronchery static int rc4_hmac_md5_get_ctx_params(void *vctx, OSSL_PARAM params[])
115b077aed3SPierre Pronchery {
116b077aed3SPierre Pronchery PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
117b077aed3SPierre Pronchery OSSL_PARAM *p;
118b077aed3SPierre Pronchery
119b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
120b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) {
121b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
122b077aed3SPierre Pronchery return 0;
123b077aed3SPierre Pronchery }
124b077aed3SPierre Pronchery
125b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
126b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) {
127b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
128b077aed3SPierre Pronchery return 0;
129b077aed3SPierre Pronchery }
130b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
131b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
132b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
133b077aed3SPierre Pronchery return 0;
134b077aed3SPierre Pronchery }
135b077aed3SPierre Pronchery return 1;
136b077aed3SPierre Pronchery }
137b077aed3SPierre Pronchery
138b077aed3SPierre Pronchery static const OSSL_PARAM rc4_hmac_md5_known_settable_ctx_params[] = {
139b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
140b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
141b077aed3SPierre Pronchery OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0),
142b077aed3SPierre Pronchery OSSL_PARAM_END
143b077aed3SPierre Pronchery };
rc4_hmac_md5_settable_ctx_params(ossl_unused void * cctx,ossl_unused void * provctx)144b077aed3SPierre Pronchery const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(ossl_unused void *cctx,
145b077aed3SPierre Pronchery ossl_unused void *provctx)
146b077aed3SPierre Pronchery {
147b077aed3SPierre Pronchery return rc4_hmac_md5_known_settable_ctx_params;
148b077aed3SPierre Pronchery }
149b077aed3SPierre Pronchery
rc4_hmac_md5_set_ctx_params(void * vctx,const OSSL_PARAM params[])150b077aed3SPierre Pronchery static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
151b077aed3SPierre Pronchery {
152b077aed3SPierre Pronchery PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
153b077aed3SPierre Pronchery const OSSL_PARAM *p;
154b077aed3SPierre Pronchery size_t sz;
155b077aed3SPierre Pronchery
156b077aed3SPierre Pronchery if (params == NULL)
157b077aed3SPierre Pronchery return 1;
158b077aed3SPierre Pronchery
159b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
160b077aed3SPierre Pronchery if (p != NULL) {
161b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &sz)) {
162b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
163b077aed3SPierre Pronchery return 0;
164b077aed3SPierre Pronchery }
165b077aed3SPierre Pronchery if (ctx->base.keylen != sz) {
166b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
167b077aed3SPierre Pronchery return 0;
168b077aed3SPierre Pronchery }
169b077aed3SPierre Pronchery }
170b077aed3SPierre Pronchery
171b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
172b077aed3SPierre Pronchery if (p != NULL) {
173b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &sz)) {
174b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
175b077aed3SPierre Pronchery return 0;
176b077aed3SPierre Pronchery }
177b077aed3SPierre Pronchery if (ctx->base.ivlen != sz) {
178b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
179b077aed3SPierre Pronchery return 0;
180b077aed3SPierre Pronchery }
181b077aed3SPierre Pronchery }
182b077aed3SPierre Pronchery
183b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
184b077aed3SPierre Pronchery if (p != NULL) {
185b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
186b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
187b077aed3SPierre Pronchery return 0;
188b077aed3SPierre Pronchery }
189b077aed3SPierre Pronchery sz = GET_HW(ctx)->tls_init(&ctx->base, p->data, p->data_size);
190b077aed3SPierre Pronchery if (sz == 0) {
191b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
192b077aed3SPierre Pronchery return 0;
193b077aed3SPierre Pronchery }
194b077aed3SPierre Pronchery ctx->tls_aad_pad_sz = sz;
195b077aed3SPierre Pronchery }
196b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_MAC_KEY);
197b077aed3SPierre Pronchery if (p != NULL) {
198b077aed3SPierre Pronchery if (p->data_type != OSSL_PARAM_OCTET_STRING) {
199b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
200b077aed3SPierre Pronchery return 0;
201b077aed3SPierre Pronchery }
202b077aed3SPierre Pronchery GET_HW(ctx)->init_mackey(&ctx->base, p->data, p->data_size);
203b077aed3SPierre Pronchery }
204b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_VERSION);
205b077aed3SPierre Pronchery if (p != NULL) {
206b077aed3SPierre Pronchery if (!OSSL_PARAM_get_uint(p, &ctx->base.tlsversion)) {
207b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
208b077aed3SPierre Pronchery return 0;
209b077aed3SPierre Pronchery }
210b077aed3SPierre Pronchery }
211b077aed3SPierre Pronchery
212b077aed3SPierre Pronchery return 1;
213b077aed3SPierre Pronchery }
214b077aed3SPierre Pronchery
rc4_hmac_md5_get_params(OSSL_PARAM params[])215b077aed3SPierre Pronchery static int rc4_hmac_md5_get_params(OSSL_PARAM params[])
216b077aed3SPierre Pronchery {
217b077aed3SPierre Pronchery return ossl_cipher_generic_get_params(params, RC4_HMAC_MD5_MODE,
218b077aed3SPierre Pronchery RC4_HMAC_MD5_FLAGS,
219b077aed3SPierre Pronchery RC4_HMAC_MD5_KEY_BITS,
220b077aed3SPierre Pronchery RC4_HMAC_MD5_BLOCK_BITS,
221b077aed3SPierre Pronchery RC4_HMAC_MD5_IV_BITS);
222b077aed3SPierre Pronchery }
223b077aed3SPierre Pronchery
224b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_rc4_hmac_ossl_md5_functions[] = {
225b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))rc4_hmac_md5_newctx },
226b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))rc4_hmac_md5_freectx },
227*e0c4386eSCy Schubert { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))rc4_hmac_md5_dupctx },
228b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_hmac_md5_einit },
229b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_hmac_md5_dinit },
230b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))rc4_hmac_md5_update },
231b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))rc4_hmac_md5_final },
232b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))rc4_hmac_md5_cipher },
233b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))rc4_hmac_md5_get_params },
234b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
235b077aed3SPierre Pronchery (void (*)(void))rc4_hmac_md5_gettable_params },
236b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
237b077aed3SPierre Pronchery (void (*)(void))rc4_hmac_md5_get_ctx_params },
238b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
239b077aed3SPierre Pronchery (void (*)(void))rc4_hmac_md5_gettable_ctx_params },
240b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
241b077aed3SPierre Pronchery (void (*)(void))rc4_hmac_md5_set_ctx_params },
242b077aed3SPierre Pronchery { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
243b077aed3SPierre Pronchery (void (*)(void))rc4_hmac_md5_settable_ctx_params },
244b077aed3SPierre Pronchery { 0, NULL }
245b077aed3SPierre Pronchery };
246