mac_lib.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/ossl_typ.h>
  14. #include "internal/nelem.h"
  15. #include "internal/evp_int.h"
  16. #include "evp_locl.h"
  17. EVP_MAC_CTX *EVP_MAC_CTX_new_id(int id)
  18. {
  19. const EVP_MAC *mac = EVP_get_macbynid(id);
  20. if (mac == NULL)
  21. return NULL;
  22. return EVP_MAC_CTX_new(mac);
  23. }
  24. EVP_MAC_CTX *EVP_MAC_CTX_new(const EVP_MAC *mac)
  25. {
  26. EVP_MAC_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MAC_CTX));
  27. if (ctx == NULL || (ctx->data = mac->new()) == NULL) {
  28. EVPerr(EVP_F_EVP_MAC_CTX_NEW, ERR_R_MALLOC_FAILURE);
  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 && ctx->data != NULL) {
  39. ctx->meth->free(ctx->data);
  40. ctx->data = NULL;
  41. }
  42. OPENSSL_free(ctx);
  43. }
  44. int EVP_MAC_CTX_copy(EVP_MAC_CTX *dst, EVP_MAC_CTX *src)
  45. {
  46. EVP_MAC_IMPL *macdata;
  47. if (src->data != NULL && !dst->meth->copy(dst->data, src->data))
  48. return 0;
  49. macdata = dst->data;
  50. *dst = *src;
  51. dst->data = macdata;
  52. return 1;
  53. }
  54. const EVP_MAC *EVP_MAC_CTX_mac(EVP_MAC_CTX *ctx)
  55. {
  56. return ctx->meth;
  57. }
  58. size_t EVP_MAC_size(EVP_MAC_CTX *ctx)
  59. {
  60. if (ctx->data != NULL)
  61. return ctx->meth->size(ctx->data);
  62. /* If the MAC hasn't been initialized yet, we return zero */
  63. return 0;
  64. }
  65. int EVP_MAC_init(EVP_MAC_CTX *ctx)
  66. {
  67. return ctx->meth->init(ctx->data);
  68. }
  69. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
  70. {
  71. return ctx->meth->update(ctx->data, data, datalen);
  72. }
  73. int EVP_MAC_final(EVP_MAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  74. {
  75. int l = ctx->meth->size(ctx->data);
  76. if (l < 0)
  77. return 0;
  78. if (poutlen != NULL)
  79. *poutlen = l;
  80. if (out == NULL)
  81. return 1;
  82. return ctx->meth->final(ctx->data, out);
  83. }
  84. int EVP_MAC_ctrl(EVP_MAC_CTX *ctx, int cmd, ...)
  85. {
  86. int ok = -1;
  87. va_list args;
  88. va_start(args, cmd);
  89. ok = EVP_MAC_vctrl(ctx, cmd, args);
  90. va_end(args);
  91. if (ok == -2)
  92. EVPerr(EVP_F_EVP_MAC_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  93. return ok;
  94. }
  95. int EVP_MAC_vctrl(EVP_MAC_CTX *ctx, int cmd, va_list args)
  96. {
  97. int ok = 1;
  98. if (ctx == NULL || ctx->meth == NULL)
  99. return -2;
  100. switch (cmd) {
  101. #if 0
  102. case ...:
  103. /* code */
  104. ok = 1;
  105. break;
  106. #endif
  107. default:
  108. if (ctx->meth->ctrl != NULL)
  109. ok = ctx->meth->ctrl(ctx->data, cmd, args);
  110. else
  111. ok = -2;
  112. break;
  113. }
  114. return ok;
  115. }
  116. int EVP_MAC_ctrl_str(EVP_MAC_CTX *ctx, const char *type, const char *value)
  117. {
  118. int ok = 1;
  119. if (ctx == NULL || ctx->meth == NULL || ctx->meth->ctrl_str == NULL) {
  120. EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  121. return -2;
  122. }
  123. ok = ctx->meth->ctrl_str(ctx->data, type, value);
  124. if (ok == -2)
  125. EVPerr(EVP_F_EVP_MAC_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  126. return ok;
  127. }
  128. int EVP_MAC_str2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *value)
  129. {
  130. size_t len;
  131. len = strlen(value);
  132. if (len > INT_MAX)
  133. return -1;
  134. return EVP_MAC_ctrl(ctx, cmd, value, len);
  135. }
  136. int EVP_MAC_hex2ctrl(EVP_MAC_CTX *ctx, int cmd, const char *hex)
  137. {
  138. unsigned char *bin;
  139. long binlen;
  140. int rv = -1;
  141. bin = OPENSSL_hexstr2buf(hex, &binlen);
  142. if (bin == NULL)
  143. return 0;
  144. if (binlen <= INT_MAX)
  145. rv = EVP_MAC_ctrl(ctx, cmd, bin, (size_t)binlen);
  146. OPENSSL_free(bin);
  147. return rv;
  148. }
  149. int EVP_MAC_nid(const EVP_MAC *mac)
  150. {
  151. return mac->type;
  152. }