cipher_rc4_hmac_md5.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /* 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_get_ctx_params_fn rc4_hmac_md5_get_ctx_params;
  31. static OSSL_FUNC_cipher_gettable_ctx_params_fn rc4_hmac_md5_gettable_ctx_params;
  32. static OSSL_FUNC_cipher_set_ctx_params_fn rc4_hmac_md5_set_ctx_params;
  33. static OSSL_FUNC_cipher_settable_ctx_params_fn rc4_hmac_md5_settable_ctx_params;
  34. static OSSL_FUNC_cipher_get_params_fn rc4_hmac_md5_get_params;
  35. #define rc4_hmac_md5_gettable_params ossl_cipher_generic_gettable_params
  36. #define rc4_hmac_md5_update ossl_cipher_generic_stream_update
  37. #define rc4_hmac_md5_final ossl_cipher_generic_stream_final
  38. #define rc4_hmac_md5_cipher ossl_cipher_generic_cipher
  39. static void *rc4_hmac_md5_newctx(void *provctx)
  40. {
  41. PROV_RC4_HMAC_MD5_CTX *ctx;
  42. if (!ossl_prov_is_running())
  43. return NULL;
  44. ctx = OPENSSL_zalloc(sizeof(*ctx));
  45. if (ctx != NULL)
  46. ossl_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. ossl_prov_cipher_hw_rc4_hmac_md5(
  51. RC4_HMAC_MD5_KEY_BITS
  52. ), NULL);
  53. return ctx;
  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. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  59. OPENSSL_clear_free(ctx, sizeof(*ctx));
  60. }
  61. static int rc4_hmac_md5_einit(void *ctx, const unsigned char *key,
  62. size_t keylen, const unsigned char *iv,
  63. size_t ivlen, const OSSL_PARAM params[])
  64. {
  65. if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL))
  66. return 0;
  67. return rc4_hmac_md5_set_ctx_params(ctx, params);
  68. }
  69. static int rc4_hmac_md5_dinit(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_dinit(ctx, key, keylen, iv, ivlen, NULL))
  74. return 0;
  75. return rc4_hmac_md5_set_ctx_params(ctx, params);
  76. }
  77. static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = {
  78. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  79. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  80. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
  81. OSSL_PARAM_END
  82. };
  83. const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(ossl_unused void *cctx,
  84. ossl_unused void *provctx)
  85. {
  86. return rc4_hmac_md5_known_gettable_ctx_params;
  87. }
  88. static int rc4_hmac_md5_get_ctx_params(void *vctx, OSSL_PARAM params[])
  89. {
  90. PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
  91. OSSL_PARAM *p;
  92. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  93. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.keylen)) {
  94. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  95. return 0;
  96. }
  97. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  98. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->base.ivlen)) {
  99. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  100. return 0;
  101. }
  102. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
  103. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
  104. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  105. return 0;
  106. }
  107. return 1;
  108. }
  109. static const OSSL_PARAM rc4_hmac_md5_known_settable_ctx_params[] = {
  110. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  111. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  112. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0),
  113. OSSL_PARAM_END
  114. };
  115. const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(ossl_unused void *cctx,
  116. ossl_unused void *provctx)
  117. {
  118. return rc4_hmac_md5_known_settable_ctx_params;
  119. }
  120. static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  121. {
  122. PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
  123. const OSSL_PARAM *p;
  124. size_t sz;
  125. if (params == NULL)
  126. return 1;
  127. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  128. if (p != NULL) {
  129. if (!OSSL_PARAM_get_size_t(p, &sz)) {
  130. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  131. return 0;
  132. }
  133. if (ctx->base.keylen != sz) {
  134. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  135. return 0;
  136. }
  137. }
  138. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
  139. if (p != NULL) {
  140. if (!OSSL_PARAM_get_size_t(p, &sz)) {
  141. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  142. return 0;
  143. }
  144. if (ctx->base.ivlen != sz) {
  145. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  146. return 0;
  147. }
  148. }
  149. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
  150. if (p != NULL) {
  151. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  152. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  153. return 0;
  154. }
  155. sz = GET_HW(ctx)->tls_init(&ctx->base, p->data, p->data_size);
  156. if (sz == 0) {
  157. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
  158. return 0;
  159. }
  160. ctx->tls_aad_pad_sz = sz;
  161. }
  162. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_MAC_KEY);
  163. if (p != NULL) {
  164. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  165. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  166. return 0;
  167. }
  168. GET_HW(ctx)->init_mackey(&ctx->base, p->data, p->data_size);
  169. }
  170. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_VERSION);
  171. if (p != NULL) {
  172. if (!OSSL_PARAM_get_uint(p, &ctx->base.tlsversion)) {
  173. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  174. return 0;
  175. }
  176. }
  177. return 1;
  178. }
  179. static int rc4_hmac_md5_get_params(OSSL_PARAM params[])
  180. {
  181. return ossl_cipher_generic_get_params(params, RC4_HMAC_MD5_MODE,
  182. RC4_HMAC_MD5_FLAGS,
  183. RC4_HMAC_MD5_KEY_BITS,
  184. RC4_HMAC_MD5_BLOCK_BITS,
  185. RC4_HMAC_MD5_IV_BITS);
  186. }
  187. const OSSL_DISPATCH ossl_rc4_hmac_ossl_md5_functions[] = {
  188. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))rc4_hmac_md5_newctx },
  189. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))rc4_hmac_md5_freectx },
  190. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_hmac_md5_einit },
  191. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_hmac_md5_dinit },
  192. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))rc4_hmac_md5_update },
  193. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))rc4_hmac_md5_final },
  194. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))rc4_hmac_md5_cipher },
  195. { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))rc4_hmac_md5_get_params },
  196. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
  197. (void (*)(void))rc4_hmac_md5_gettable_params },
  198. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
  199. (void (*)(void))rc4_hmac_md5_get_ctx_params },
  200. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
  201. (void (*)(void))rc4_hmac_md5_gettable_ctx_params },
  202. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
  203. (void (*)(void))rc4_hmac_md5_set_ctx_params },
  204. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
  205. (void (*)(void))rc4_hmac_md5_settable_ctx_params },
  206. { 0, NULL }
  207. };