mac_lib.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <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. EVPerr(EVP_F_EVP_MAC_CTX_NEW, 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. EVPerr(EVP_F_EVP_MAC_CTX_DUP, ERR_R_MALLOC_FAILURE);
  54. return NULL;
  55. }
  56. *dst = *src;
  57. if (!EVP_MAC_up_ref(dst->meth)) {
  58. EVPerr(EVP_F_EVP_MAC_CTX_DUP, 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_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)
  94. {
  95. return ctx->meth->init(ctx->data);
  96. }
  97. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
  98. {
  99. if (datalen == 0)
  100. return 1;
  101. return ctx->meth->update(ctx->data, data, datalen);
  102. }
  103. int EVP_MAC_final(EVP_MAC_CTX *ctx,
  104. unsigned char *out, size_t *outl, size_t outsize)
  105. {
  106. int l = EVP_MAC_size(ctx);
  107. if (l < 0)
  108. return 0;
  109. if (outl != NULL)
  110. *outl = l;
  111. if (out == NULL)
  112. return 1;
  113. return ctx->meth->final(ctx->data, out, outl, outsize);
  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. int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
  144. {
  145. return evp_is_a(mac->prov, mac->name_id, name);
  146. }
  147. void EVP_MAC_names_do_all(const EVP_MAC *mac,
  148. void (*fn)(const char *name, void *data),
  149. void *data)
  150. {
  151. if (mac->prov != NULL)
  152. evp_names_do_all(mac->prov, mac->name_id, fn, data);
  153. }