mac_legacy_sig.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2019-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. /* We need to use some engine deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/crypto.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/core_dispatch.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/params.h>
  16. #include <openssl/err.h>
  17. #include <openssl/proverr.h>
  18. #ifndef FIPS_MODULE
  19. # include <openssl/engine.h>
  20. #endif
  21. #include "prov/implementations.h"
  22. #include "prov/provider_ctx.h"
  23. #include "prov/macsignature.h"
  24. #include "prov/providercommon.h"
  25. static OSSL_FUNC_signature_newctx_fn mac_hmac_newctx;
  26. static OSSL_FUNC_signature_newctx_fn mac_siphash_newctx;
  27. static OSSL_FUNC_signature_newctx_fn mac_poly1305_newctx;
  28. static OSSL_FUNC_signature_newctx_fn mac_cmac_newctx;
  29. static OSSL_FUNC_signature_digest_sign_init_fn mac_digest_sign_init;
  30. static OSSL_FUNC_signature_digest_sign_update_fn mac_digest_sign_update;
  31. static OSSL_FUNC_signature_digest_sign_final_fn mac_digest_sign_final;
  32. static OSSL_FUNC_signature_freectx_fn mac_freectx;
  33. static OSSL_FUNC_signature_dupctx_fn mac_dupctx;
  34. static OSSL_FUNC_signature_set_ctx_params_fn mac_set_ctx_params;
  35. static OSSL_FUNC_signature_settable_ctx_params_fn mac_hmac_settable_ctx_params;
  36. static OSSL_FUNC_signature_settable_ctx_params_fn mac_siphash_settable_ctx_params;
  37. static OSSL_FUNC_signature_settable_ctx_params_fn mac_poly1305_settable_ctx_params;
  38. static OSSL_FUNC_signature_settable_ctx_params_fn mac_cmac_settable_ctx_params;
  39. typedef struct {
  40. OSSL_LIB_CTX *libctx;
  41. char *propq;
  42. MAC_KEY *key;
  43. EVP_MAC_CTX *macctx;
  44. } PROV_MAC_CTX;
  45. static void *mac_newctx(void *provctx, const char *propq, const char *macname)
  46. {
  47. PROV_MAC_CTX *pmacctx;
  48. EVP_MAC *mac = NULL;
  49. if (!ossl_prov_is_running())
  50. return NULL;
  51. pmacctx = OPENSSL_zalloc(sizeof(PROV_MAC_CTX));
  52. if (pmacctx == NULL)
  53. return NULL;
  54. pmacctx->libctx = PROV_LIBCTX_OF(provctx);
  55. if (propq != NULL && (pmacctx->propq = OPENSSL_strdup(propq)) == NULL)
  56. goto err;
  57. mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
  58. if (mac == NULL)
  59. goto err;
  60. pmacctx->macctx = EVP_MAC_CTX_new(mac);
  61. if (pmacctx->macctx == NULL)
  62. goto err;
  63. EVP_MAC_free(mac);
  64. return pmacctx;
  65. err:
  66. OPENSSL_free(pmacctx->propq);
  67. OPENSSL_free(pmacctx);
  68. EVP_MAC_free(mac);
  69. return NULL;
  70. }
  71. #define MAC_NEWCTX(funcname, macname) \
  72. static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
  73. { \
  74. return mac_newctx(provctx, propq, macname); \
  75. }
  76. MAC_NEWCTX(hmac, "HMAC")
  77. MAC_NEWCTX(siphash, "SIPHASH")
  78. MAC_NEWCTX(poly1305, "POLY1305")
  79. MAC_NEWCTX(cmac, "CMAC")
  80. static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey,
  81. const OSSL_PARAM params[])
  82. {
  83. PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
  84. const char *ciphername = NULL, *engine = NULL;
  85. if (!ossl_prov_is_running()
  86. || pmacctx == NULL)
  87. return 0;
  88. if (pmacctx->key == NULL && vkey == NULL) {
  89. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  90. return 0;
  91. }
  92. if (vkey != NULL) {
  93. if (!ossl_mac_key_up_ref(vkey))
  94. return 0;
  95. ossl_mac_key_free(pmacctx->key);
  96. pmacctx->key = vkey;
  97. }
  98. if (pmacctx->key->cipher.cipher != NULL)
  99. ciphername = (char *)EVP_CIPHER_get0_name(pmacctx->key->cipher.cipher);
  100. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  101. if (pmacctx->key->cipher.engine != NULL)
  102. engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
  103. #endif
  104. if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,
  105. (char *)ciphername,
  106. (char *)mdname,
  107. (char *)engine,
  108. pmacctx->key->properties,
  109. NULL, 0))
  110. return 0;
  111. if (!EVP_MAC_init(pmacctx->macctx, pmacctx->key->priv_key,
  112. pmacctx->key->priv_key_len, params))
  113. return 0;
  114. return 1;
  115. }
  116. int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
  117. size_t datalen)
  118. {
  119. PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
  120. if (pmacctx == NULL || pmacctx->macctx == NULL)
  121. return 0;
  122. return EVP_MAC_update(pmacctx->macctx, data, datalen);
  123. }
  124. int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
  125. size_t macsize)
  126. {
  127. PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
  128. if (!ossl_prov_is_running() || pmacctx == NULL || pmacctx->macctx == NULL)
  129. return 0;
  130. return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
  131. }
  132. static void mac_freectx(void *vpmacctx)
  133. {
  134. PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
  135. OPENSSL_free(ctx->propq);
  136. EVP_MAC_CTX_free(ctx->macctx);
  137. ossl_mac_key_free(ctx->key);
  138. OPENSSL_free(ctx);
  139. }
  140. static void *mac_dupctx(void *vpmacctx)
  141. {
  142. PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
  143. PROV_MAC_CTX *dstctx;
  144. if (!ossl_prov_is_running())
  145. return NULL;
  146. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  147. if (dstctx == NULL)
  148. return NULL;
  149. *dstctx = *srcctx;
  150. dstctx->propq = NULL;
  151. dstctx->key = NULL;
  152. dstctx->macctx = NULL;
  153. if (srcctx->propq != NULL && (dstctx->propq = OPENSSL_strdup(srcctx->propq)) == NULL)
  154. goto err;
  155. if (srcctx->key != NULL && !ossl_mac_key_up_ref(srcctx->key))
  156. goto err;
  157. dstctx->key = srcctx->key;
  158. if (srcctx->macctx != NULL) {
  159. dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
  160. if (dstctx->macctx == NULL)
  161. goto err;
  162. }
  163. return dstctx;
  164. err:
  165. mac_freectx(dstctx);
  166. return NULL;
  167. }
  168. static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[])
  169. {
  170. PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
  171. return EVP_MAC_CTX_set_params(ctx->macctx, params);
  172. }
  173. static const OSSL_PARAM *mac_settable_ctx_params(ossl_unused void *ctx,
  174. void *provctx,
  175. const char *macname)
  176. {
  177. EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBCTX_OF(provctx), macname,
  178. NULL);
  179. const OSSL_PARAM *params;
  180. if (mac == NULL)
  181. return NULL;
  182. params = EVP_MAC_settable_ctx_params(mac);
  183. EVP_MAC_free(mac);
  184. return params;
  185. }
  186. #define MAC_SETTABLE_CTX_PARAMS(funcname, macname) \
  187. static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *ctx, \
  188. void *provctx) \
  189. { \
  190. return mac_settable_ctx_params(ctx, provctx, macname); \
  191. }
  192. MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC")
  193. MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH")
  194. MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305")
  195. MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC")
  196. #define MAC_SIGNATURE_FUNCTIONS(funcname) \
  197. const OSSL_DISPATCH ossl_mac_legacy_##funcname##_signature_functions[] = { \
  198. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
  199. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
  200. (void (*)(void))mac_digest_sign_init }, \
  201. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
  202. (void (*)(void))mac_digest_sign_update }, \
  203. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
  204. (void (*)(void))mac_digest_sign_final }, \
  205. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
  206. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
  207. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
  208. (void (*)(void))mac_set_ctx_params }, \
  209. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
  210. (void (*)(void))mac_##funcname##_settable_ctx_params }, \
  211. OSSL_DISPATCH_END \
  212. };
  213. MAC_SIGNATURE_FUNCTIONS(hmac)
  214. MAC_SIGNATURE_FUNCTIONS(siphash)
  215. MAC_SIGNATURE_FUNCTIONS(poly1305)
  216. MAC_SIGNATURE_FUNCTIONS(cmac)