hmac_prov.c 6.1 KB

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