mac_lib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright 2018-2021 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 <string.h>
  10. #include <stdarg.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/err.h>
  13. #include <openssl/core.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/types.h>
  16. #include "internal/nelem.h"
  17. #include "crypto/evp.h"
  18. #include "internal/provider.h"
  19. #include "evp_local.h"
  20. EVP_MAC_CTX *EVP_MAC_CTX_new(EVP_MAC *mac)
  21. {
  22. EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));
  23. if (ctx == NULL
  24. || (ctx->data = mac->newctx(ossl_provider_ctx(mac->prov))) == NULL
  25. || !EVP_MAC_up_ref(mac)) {
  26. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  27. if (ctx != NULL)
  28. mac->freectx(ctx->data);
  29. OPENSSL_free(ctx);
  30. ctx = NULL;
  31. } else {
  32. ctx->meth = mac;
  33. }
  34. return ctx;
  35. }
  36. void EVP_MAC_CTX_free(EVP_MAC_CTX *ctx)
  37. {
  38. if (ctx != NULL) {
  39. ctx->meth->freectx(ctx->data);
  40. ctx->data = NULL;
  41. /* refcnt-- */
  42. EVP_MAC_free(ctx->meth);
  43. }
  44. OPENSSL_free(ctx);
  45. }
  46. EVP_MAC_CTX *EVP_MAC_CTX_dup(const EVP_MAC_CTX *src)
  47. {
  48. EVP_MAC_CTX *dst;
  49. if (src->data == NULL)
  50. return NULL;
  51. dst = OPENSSL_malloc(sizeof(*dst));
  52. if (dst == NULL) {
  53. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  54. return NULL;
  55. }
  56. *dst = *src;
  57. if (!EVP_MAC_up_ref(dst->meth)) {
  58. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  59. OPENSSL_free(dst);
  60. return NULL;
  61. }
  62. dst->data = src->meth->dupctx(src->data);
  63. if (dst->data == NULL) {
  64. EVP_MAC_CTX_free(dst);
  65. return NULL;
  66. }
  67. return dst;
  68. }
  69. EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
  70. {
  71. return ctx->meth;
  72. }
  73. size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
  74. {
  75. size_t sz = 0;
  76. if (ctx->data != NULL) {
  77. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  78. params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, &sz);
  79. if (ctx->meth->get_ctx_params != NULL) {
  80. if (ctx->meth->get_ctx_params(ctx->data, params))
  81. return sz;
  82. } else if (ctx->meth->get_params != NULL) {
  83. if (ctx->meth->get_params(params))
  84. return sz;
  85. }
  86. }
  87. /*
  88. * If the MAC hasn't been initialized yet, or there is no size to get,
  89. * we return zero
  90. */
  91. return 0;
  92. }
  93. int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
  94. const OSSL_PARAM params[])
  95. {
  96. return ctx->meth->init(ctx->data, key, keylen, params);
  97. }
  98. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
  99. {
  100. return ctx->meth->update(ctx->data, data, datalen);
  101. }
  102. int EVP_MAC_final(EVP_MAC_CTX *ctx,
  103. unsigned char *out, size_t *outl, size_t outsize)
  104. {
  105. size_t l;
  106. int res = 1;
  107. if (out != NULL)
  108. res = ctx->meth->final(ctx->data, out, &l, outsize);
  109. else
  110. l = EVP_MAC_CTX_get_mac_size(ctx);
  111. if (outl != NULL)
  112. *outl = l;
  113. return res;
  114. }
  115. /*
  116. * The {get,set}_params functions return 1 if there is no corresponding
  117. * function in the implementation. This is the same as if there was one,
  118. * but it didn't recognise any of the given params, i.e. nothing in the
  119. * bag of parameters was useful.
  120. */
  121. int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
  122. {
  123. if (mac->get_params != NULL)
  124. return mac->get_params(params);
  125. return 1;
  126. }
  127. int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
  128. {
  129. if (ctx->meth->get_ctx_params != NULL)
  130. return ctx->meth->get_ctx_params(ctx->data, params);
  131. return 1;
  132. }
  133. int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
  134. {
  135. if (ctx->meth->set_ctx_params != NULL)
  136. return ctx->meth->set_ctx_params(ctx->data, params);
  137. return 1;
  138. }
  139. int EVP_MAC_number(const EVP_MAC *mac)
  140. {
  141. return mac->name_id;
  142. }
  143. const char *EVP_MAC_name(const EVP_MAC *mac)
  144. {
  145. if (mac->prov != NULL)
  146. return evp_first_name(mac->prov, mac->name_id);
  147. return NULL;
  148. }
  149. const char *EVP_MAC_description(const EVP_MAC *mac)
  150. {
  151. return mac->description;
  152. }
  153. int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
  154. {
  155. return evp_is_a(mac->prov, mac->name_id, NULL, name);
  156. }
  157. int EVP_MAC_names_do_all(const EVP_MAC *mac,
  158. void (*fn)(const char *name, void *data),
  159. void *data)
  160. {
  161. if (mac->prov != NULL)
  162. return evp_names_do_all(mac->prov, mac->name_id, fn, data);
  163. return 1;
  164. }