mac_lib.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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->algctx = 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->algctx);
  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. return;
  40. ctx->meth->freectx(ctx->algctx);
  41. ctx->algctx = NULL;
  42. /* refcnt-- */
  43. EVP_MAC_free(ctx->meth);
  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->algctx == 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->algctx = src->meth->dupctx(src->algctx);
  63. if (dst->algctx == NULL) {
  64. EVP_MAC_CTX_free(dst);
  65. return NULL;
  66. }
  67. return dst;
  68. }
  69. EVP_MAC *EVP_MAC_CTX_get0_mac(EVP_MAC_CTX *ctx)
  70. {
  71. return ctx->meth;
  72. }
  73. static size_t get_size_t_ctx_param(EVP_MAC_CTX *ctx, const char *name)
  74. {
  75. size_t sz = 0;
  76. if (ctx->algctx != NULL) {
  77. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  78. params[0] = OSSL_PARAM_construct_size_t(name, &sz);
  79. if (ctx->meth->get_ctx_params != NULL) {
  80. if (ctx->meth->get_ctx_params(ctx->algctx, 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. size_t EVP_MAC_CTX_get_mac_size(EVP_MAC_CTX *ctx)
  94. {
  95. return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_SIZE);
  96. }
  97. size_t EVP_MAC_CTX_get_block_size(EVP_MAC_CTX *ctx)
  98. {
  99. return get_size_t_ctx_param(ctx, OSSL_MAC_PARAM_BLOCK_SIZE);
  100. }
  101. int EVP_MAC_init(EVP_MAC_CTX *ctx, const unsigned char *key, size_t keylen,
  102. const OSSL_PARAM params[])
  103. {
  104. return ctx->meth->init(ctx->algctx, key, keylen, params);
  105. }
  106. int EVP_MAC_update(EVP_MAC_CTX *ctx, const unsigned char *data, size_t datalen)
  107. {
  108. return ctx->meth->update(ctx->algctx, data, datalen);
  109. }
  110. static int evp_mac_final(EVP_MAC_CTX *ctx, int xof,
  111. unsigned char *out, size_t *outl, size_t outsize)
  112. {
  113. size_t l;
  114. int res;
  115. OSSL_PARAM params[2];
  116. if (ctx == NULL || ctx->meth == NULL) {
  117. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
  118. return 0;
  119. }
  120. if (ctx->meth->final == NULL) {
  121. ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
  122. return 0;
  123. }
  124. if (out == NULL) {
  125. if (outl == NULL) {
  126. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  127. return 0;
  128. }
  129. *outl = EVP_MAC_CTX_get_mac_size(ctx);
  130. return 1;
  131. }
  132. if (xof) {
  133. params[0] = OSSL_PARAM_construct_int(OSSL_MAC_PARAM_XOF, &xof);
  134. params[1] = OSSL_PARAM_construct_end();
  135. if (EVP_MAC_CTX_set_params(ctx, params) <= 0) {
  136. ERR_raise(ERR_LIB_EVP, EVP_R_SETTING_XOF_FAILED);
  137. return 0;
  138. }
  139. }
  140. res = ctx->meth->final(ctx->algctx, out, &l, outsize);
  141. if (outl != NULL)
  142. *outl = l;
  143. return res;
  144. }
  145. int EVP_MAC_final(EVP_MAC_CTX *ctx,
  146. unsigned char *out, size_t *outl, size_t outsize)
  147. {
  148. return evp_mac_final(ctx, 0, out, outl, outsize);
  149. }
  150. int EVP_MAC_finalXOF(EVP_MAC_CTX *ctx, unsigned char *out, size_t outsize)
  151. {
  152. return evp_mac_final(ctx, 1, out, NULL, outsize);
  153. }
  154. /*
  155. * The {get,set}_params functions return 1 if there is no corresponding
  156. * function in the implementation. This is the same as if there was one,
  157. * but it didn't recognise any of the given params, i.e. nothing in the
  158. * bag of parameters was useful.
  159. */
  160. int EVP_MAC_get_params(EVP_MAC *mac, OSSL_PARAM params[])
  161. {
  162. if (mac->get_params != NULL)
  163. return mac->get_params(params);
  164. return 1;
  165. }
  166. int EVP_MAC_CTX_get_params(EVP_MAC_CTX *ctx, OSSL_PARAM params[])
  167. {
  168. if (ctx->meth->get_ctx_params != NULL)
  169. return ctx->meth->get_ctx_params(ctx->algctx, params);
  170. return 1;
  171. }
  172. int EVP_MAC_CTX_set_params(EVP_MAC_CTX *ctx, const OSSL_PARAM params[])
  173. {
  174. if (ctx->meth->set_ctx_params != NULL)
  175. return ctx->meth->set_ctx_params(ctx->algctx, params);
  176. return 1;
  177. }
  178. int evp_mac_get_number(const EVP_MAC *mac)
  179. {
  180. return mac->name_id;
  181. }
  182. const char *EVP_MAC_get0_name(const EVP_MAC *mac)
  183. {
  184. return mac->type_name;
  185. }
  186. const char *EVP_MAC_get0_description(const EVP_MAC *mac)
  187. {
  188. return mac->description;
  189. }
  190. int EVP_MAC_is_a(const EVP_MAC *mac, const char *name)
  191. {
  192. return evp_is_a(mac->prov, mac->name_id, NULL, name);
  193. }
  194. int EVP_MAC_names_do_all(const EVP_MAC *mac,
  195. void (*fn)(const char *name, void *data),
  196. void *data)
  197. {
  198. if (mac->prov != NULL)
  199. return evp_names_do_all(mac->prov, mac->name_id, fn, data);
  200. return 1;
  201. }
  202. unsigned char *EVP_Q_mac(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
  203. const char *subalg, const OSSL_PARAM *params,
  204. const void *key, size_t keylen,
  205. const unsigned char *data, size_t datalen,
  206. unsigned char *out, size_t outsize, unsigned int *outlen)
  207. {
  208. EVP_MAC *mac = EVP_MAC_fetch(libctx, name, propq);
  209. OSSL_PARAM subalg_param[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  210. EVP_MAC_CTX *ctx = NULL;
  211. size_t len;
  212. unsigned char *res = NULL;
  213. if (outlen != NULL)
  214. *outlen = 0;
  215. if (mac == NULL)
  216. return NULL;
  217. if (subalg != NULL) {
  218. const OSSL_PARAM *defined_params = EVP_MAC_settable_ctx_params(mac);
  219. const char *param_name = OSSL_MAC_PARAM_DIGEST;
  220. /*
  221. * The underlying algorithm may be a cipher or a digest.
  222. * We don't know which it is, but we can ask the MAC what it
  223. * should be and bet on that.
  224. */
  225. if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
  226. param_name = OSSL_MAC_PARAM_CIPHER;
  227. if (OSSL_PARAM_locate_const(defined_params, param_name) == NULL) {
  228. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
  229. goto err;
  230. }
  231. }
  232. subalg_param[0] =
  233. OSSL_PARAM_construct_utf8_string(param_name, (char *)subalg, 0);
  234. }
  235. /* Single-shot - on NULL key input, set dummy key value for EVP_MAC_Init. */
  236. if (key == NULL && keylen == 0)
  237. key = data;
  238. if ((ctx = EVP_MAC_CTX_new(mac)) != NULL
  239. && EVP_MAC_CTX_set_params(ctx, subalg_param)
  240. && EVP_MAC_CTX_set_params(ctx, params)
  241. && EVP_MAC_init(ctx, key, keylen, params)
  242. && EVP_MAC_update(ctx, data, datalen)
  243. && EVP_MAC_final(ctx, out, &len, outsize)) {
  244. if (out == NULL) {
  245. out = OPENSSL_malloc(len);
  246. if (out != NULL && !EVP_MAC_final(ctx, out, NULL, len)) {
  247. OPENSSL_free(out);
  248. out = NULL;
  249. }
  250. }
  251. res = out;
  252. if (res != NULL && outlen != NULL)
  253. *outlen = (unsigned int)len;
  254. }
  255. err:
  256. EVP_MAC_CTX_free(ctx);
  257. EVP_MAC_free(mac);
  258. return res;
  259. }