| 1 | /* |
| 2 | * Copyright 2019 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 | #include "cipher_rc4_hmac_md5.h" |
| 13 | #include "prov/implementations.h" |
| 14 | #include "prov/providercommonerr.h" |
| 15 | |
| 16 | /* TODO(3.0) Figure out what flags are required */ |
| 17 | #define RC4_HMAC_MD5_FLAGS (EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH \ |
| 18 | | EVP_CIPH_FLAG_AEAD_CIPHER) |
| 19 | |
| 20 | #define RC4_HMAC_MD5_KEY_BITS (16 * 8) |
| 21 | #define RC4_HMAC_MD5_BLOCK_BITS (1 * 8) |
| 22 | #define RC4_HMAC_MD5_IV_BITS 0 |
| 23 | #define RC4_HMAC_MD5_MODE 0 |
| 24 | |
| 25 | #define GET_HW(ctx) ((PROV_CIPHER_HW_RC4_HMAC_MD5 *)ctx->base.hw) |
| 26 | |
| 27 | static OSSL_OP_cipher_newctx_fn rc4_hmac_md5_newctx; |
| 28 | static OSSL_OP_cipher_freectx_fn rc4_hmac_md5_freectx; |
| 29 | static OSSL_OP_cipher_get_ctx_params_fn rc4_hmac_md5_get_ctx_params; |
| 30 | static OSSL_OP_cipher_gettable_ctx_params_fn rc4_hmac_md5_gettable_ctx_params; |
| 31 | static OSSL_OP_cipher_set_ctx_params_fn rc4_hmac_md5_set_ctx_params; |
| 32 | static OSSL_OP_cipher_settable_ctx_params_fn rc4_hmac_md5_settable_ctx_params; |
| 33 | static OSSL_OP_cipher_get_params_fn rc4_hmac_md5_get_params; |
| 34 | #define rc4_hmac_md5_gettable_params cipher_generic_gettable_params |
| 35 | #define rc4_hmac_md5_einit cipher_generic_einit |
| 36 | #define rc4_hmac_md5_dinit cipher_generic_dinit |
| 37 | #define rc4_hmac_md5_update cipher_generic_stream_update |
| 38 | #define rc4_hmac_md5_final cipher_generic_stream_final |
| 39 | #define rc4_hmac_md5_cipher cipher_generic_cipher |
| 40 | |
| 41 | static void *rc4_hmac_md5_newctx(void *provctx) |
| 42 | { |
| 43 | PROV_RC4_HMAC_MD5_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 44 | |
| 45 | if (ctx != NULL) |
| 46 | cipher_generic_initkey(ctx, RC4_HMAC_MD5_KEY_BITS, |
| 47 | RC4_HMAC_MD5_BLOCK_BITS, |
| 48 | RC4_HMAC_MD5_IV_BITS, |
| 49 | RC4_HMAC_MD5_MODE, RC4_HMAC_MD5_FLAGS, |
| 50 | PROV_CIPHER_HW_rc4_hmac_md5(RC4_HMAC_MD5_KEY_BITS), |
| 51 | NULL); |
| 52 | return ctx; |
| 53 | } |
| 54 | |
| 55 | static void rc4_hmac_md5_freectx(void *vctx) |
| 56 | { |
| 57 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; |
| 58 | |
| 59 | OPENSSL_clear_free(ctx, sizeof(*ctx)); |
| 60 | } |
| 61 | |
| 62 | static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = { |
| 63 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
| 64 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
| 65 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL), |
| 66 | OSSL_PARAM_END |
| 67 | }; |
| 68 | const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(void) |
| 69 | { |
| 70 | return rc4_hmac_md5_known_gettable_ctx_params; |
| 71 | } |
| 72 | |
| 73 | static int rc4_hmac_md5_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
| 74 | { |
| 75 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; |
| 76 | OSSL_PARAM *p; |
| 77 | |
| 78 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN); |
| 79 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) { |
| 80 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN); |
| 85 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) { |
| 86 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
| 87 | return 0; |
| 88 | } |
| 89 | p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD); |
| 90 | if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) { |
| 91 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER); |
| 92 | return 0; |
| 93 | } |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static const OSSL_PARAM rc4_hmac_md5_known_settable_ctx_params[] = { |
| 98 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), |
| 99 | OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), |
| 100 | OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0), |
| 101 | OSSL_PARAM_END |
| 102 | }; |
| 103 | const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(void) |
| 104 | { |
| 105 | return rc4_hmac_md5_known_settable_ctx_params; |
| 106 | } |
| 107 | |
| 108 | static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
| 109 | { |
| 110 | PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx; |
| 111 | const OSSL_PARAM *p; |
| 112 | size_t sz; |
| 113 | |
| 114 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); |
| 115 | if (p != NULL) { |
| 116 | if (!OSSL_PARAM_get_size_t(p, &sz)) { |
| 117 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
| 118 | return 0; |
| 119 | } |
| 120 | if (ctx->base.keylen != sz) { |
| 121 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN); |
| 127 | if (p != NULL) { |
| 128 | if (!OSSL_PARAM_get_size_t(p, &sz)) { |
| 129 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
| 130 | return 0; |
| 131 | } |
| 132 | if (ctx->base.ivlen != sz) { |
| 133 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH); |
| 134 | return 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); |
| 139 | if (p != NULL) { |
| 140 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { |
| 141 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
| 142 | return 0; |
| 143 | } |
| 144 | sz = GET_HW(ctx)->tls_init(&ctx->base, p->data, p->data_size); |
| 145 | if (sz == 0) { |
| 146 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA); |
| 147 | return 0; |
| 148 | } |
| 149 | ctx->tls_aad_pad_sz = sz; |
| 150 | } |
| 151 | p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD); |
| 152 | if (p != NULL) { |
| 153 | if (p->data_type != OSSL_PARAM_OCTET_STRING) { |
| 154 | ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER); |
| 155 | return 0; |
| 156 | } |
| 157 | GET_HW(ctx)->init_mackey(&ctx->base, p->data, p->data_size); |
| 158 | } |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | static int rc4_hmac_md5_get_params(OSSL_PARAM params[]) |
| 163 | { |
| 164 | return cipher_generic_get_params(params, RC4_HMAC_MD5_MODE, |
| 165 | RC4_HMAC_MD5_FLAGS, |
| 166 | RC4_HMAC_MD5_KEY_BITS, |
| 167 | RC4_HMAC_MD5_BLOCK_BITS, |
| 168 | RC4_HMAC_MD5_IV_BITS); |
| 169 | } |
| 170 | |
| 171 | const OSSL_DISPATCH rc4_hmac_md5_functions[] = { |
| 172 | { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))rc4_hmac_md5_newctx }, |
| 173 | { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))rc4_hmac_md5_freectx }, |
| 174 | { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_hmac_md5_einit }, |
| 175 | { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_hmac_md5_dinit }, |
| 176 | { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))rc4_hmac_md5_update }, |
| 177 | { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))rc4_hmac_md5_final }, |
| 178 | { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))rc4_hmac_md5_cipher }, |
| 179 | { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))rc4_hmac_md5_get_params }, |
| 180 | { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, |
| 181 | (void (*)(void))rc4_hmac_md5_gettable_params }, |
| 182 | { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, |
| 183 | (void (*)(void))rc4_hmac_md5_get_ctx_params }, |
| 184 | { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, |
| 185 | (void (*)(void))rc4_hmac_md5_gettable_ctx_params }, |
| 186 | { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, |
| 187 | (void (*)(void))rc4_hmac_md5_set_ctx_params }, |
| 188 | { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, |
| 189 | (void (*)(void))rc4_hmac_md5_settable_ctx_params }, |
| 190 | { 0, NULL } |
| 191 | }; |
| 192 | |