hmac_prov.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 2018-2020 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. * HMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_dispatch.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/params.h>
  17. #include <openssl/engine.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/hmac.h>
  20. #include "prov/implementations.h"
  21. #include "prov/provider_ctx.h"
  22. #include "prov/provider_util.h"
  23. /*
  24. * Forward declaration of everything implemented here. This is not strictly
  25. * necessary for the compiler, but provides an assurance that the signatures
  26. * of the functions in the dispatch table are correct.
  27. */
  28. static OSSL_FUNC_mac_newctx_fn hmac_new;
  29. static OSSL_FUNC_mac_dupctx_fn hmac_dup;
  30. static OSSL_FUNC_mac_freectx_fn hmac_free;
  31. static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
  32. static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
  33. static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
  34. static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
  35. static OSSL_FUNC_mac_init_fn hmac_init;
  36. static OSSL_FUNC_mac_update_fn hmac_update;
  37. static OSSL_FUNC_mac_final_fn hmac_final;
  38. /* local HMAC context structure */
  39. /* typedef EVP_MAC_IMPL */
  40. struct hmac_data_st {
  41. void *provctx;
  42. HMAC_CTX *ctx; /* HMAC context */
  43. PROV_DIGEST digest;
  44. };
  45. static size_t hmac_size(void *vmacctx);
  46. static void *hmac_new(void *provctx)
  47. {
  48. struct hmac_data_st *macctx;
  49. if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  50. || (macctx->ctx = HMAC_CTX_new()) == NULL) {
  51. OPENSSL_free(macctx);
  52. return NULL;
  53. }
  54. /* TODO(3.0) Should we do something more with that context? */
  55. macctx->provctx = provctx;
  56. return macctx;
  57. }
  58. static void hmac_free(void *vmacctx)
  59. {
  60. struct hmac_data_st *macctx = vmacctx;
  61. if (macctx != NULL) {
  62. HMAC_CTX_free(macctx->ctx);
  63. ossl_prov_digest_reset(&macctx->digest);
  64. OPENSSL_free(macctx);
  65. }
  66. }
  67. static void *hmac_dup(void *vsrc)
  68. {
  69. struct hmac_data_st *src = vsrc;
  70. struct hmac_data_st *dst = hmac_new(src->provctx);
  71. if (dst == NULL)
  72. return NULL;
  73. if (!HMAC_CTX_copy(dst->ctx, src->ctx)
  74. || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
  75. hmac_free(dst);
  76. return NULL;
  77. }
  78. return dst;
  79. }
  80. static size_t hmac_size(void *vmacctx)
  81. {
  82. struct hmac_data_st *macctx = vmacctx;
  83. return HMAC_size(macctx->ctx);
  84. }
  85. static int hmac_init(void *vmacctx)
  86. {
  87. struct hmac_data_st *macctx = vmacctx;
  88. const EVP_MD *digest = ossl_prov_digest_md(&macctx->digest);
  89. int rv = 1;
  90. /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
  91. if (digest != NULL)
  92. rv = HMAC_Init_ex(macctx->ctx, NULL, 0, digest,
  93. ossl_prov_digest_engine(&macctx->digest));
  94. ossl_prov_digest_reset(&macctx->digest);
  95. return rv;
  96. }
  97. static int hmac_update(void *vmacctx, const unsigned char *data,
  98. size_t datalen)
  99. {
  100. struct hmac_data_st *macctx = vmacctx;
  101. return HMAC_Update(macctx->ctx, data, datalen);
  102. }
  103. static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  104. size_t outsize)
  105. {
  106. unsigned int hlen;
  107. struct hmac_data_st *macctx = vmacctx;
  108. if (!HMAC_Final(macctx->ctx, out, &hlen))
  109. return 0;
  110. *outl = hlen;
  111. return 1;
  112. }
  113. static const OSSL_PARAM known_gettable_ctx_params[] = {
  114. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  115. OSSL_PARAM_END
  116. };
  117. static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *provctx)
  118. {
  119. return known_gettable_ctx_params;
  120. }
  121. static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  122. {
  123. OSSL_PARAM *p;
  124. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  125. return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx));
  126. return 1;
  127. }
  128. static const OSSL_PARAM known_settable_ctx_params[] = {
  129. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
  130. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  131. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  132. OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS, NULL),
  133. OSSL_PARAM_END
  134. };
  135. static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *provctx)
  136. {
  137. return known_settable_ctx_params;
  138. }
  139. /*
  140. * ALL parameters should be set before init().
  141. */
  142. static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  143. {
  144. struct hmac_data_st *macctx = vmacctx;
  145. OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
  146. const OSSL_PARAM *p;
  147. if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
  148. return 0;
  149. /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */
  150. if ((p = OSSL_PARAM_locate_const(params,
  151. OSSL_MAC_PARAM_FLAGS)) != NULL) {
  152. int flags = 0;
  153. if (!OSSL_PARAM_get_int(p, &flags))
  154. return 0;
  155. HMAC_CTX_set_flags(macctx->ctx, flags);
  156. }
  157. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  158. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  159. return 0;
  160. if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size,
  161. ossl_prov_digest_md(&macctx->digest),
  162. NULL /* ENGINE */))
  163. return 0;
  164. ossl_prov_digest_reset(&macctx->digest);
  165. }
  166. return 1;
  167. }
  168. const OSSL_DISPATCH hmac_functions[] = {
  169. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
  170. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
  171. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
  172. { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
  173. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
  174. { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
  175. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  176. (void (*)(void))hmac_gettable_ctx_params },
  177. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
  178. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  179. (void (*)(void))hmac_settable_ctx_params },
  180. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
  181. { 0, NULL }
  182. };