2
0

cipher_rc4_hmac_md5.c 8.9 KB

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