mac_lib.c 8.4 KB

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