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