mac_legacy_sig.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  57. goto err;
  58. }
  59. mac = EVP_MAC_fetch(pmacctx->libctx, macname, propq);
  60. if (mac == NULL)
  61. goto err;
  62. pmacctx->macctx = EVP_MAC_CTX_new(mac);
  63. if (pmacctx->macctx == NULL)
  64. goto err;
  65. EVP_MAC_free(mac);
  66. return pmacctx;
  67. err:
  68. OPENSSL_free(pmacctx->propq);
  69. OPENSSL_free(pmacctx);
  70. EVP_MAC_free(mac);
  71. return NULL;
  72. }
  73. #define MAC_NEWCTX(funcname, macname) \
  74. static void *mac_##funcname##_newctx(void *provctx, const char *propq) \
  75. { \
  76. return mac_newctx(provctx, propq, macname); \
  77. }
  78. MAC_NEWCTX(hmac, "HMAC")
  79. MAC_NEWCTX(siphash, "SIPHASH")
  80. MAC_NEWCTX(poly1305, "POLY1305")
  81. MAC_NEWCTX(cmac, "CMAC")
  82. static int mac_digest_sign_init(void *vpmacctx, const char *mdname, void *vkey,
  83. const OSSL_PARAM params[])
  84. {
  85. PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
  86. const char *ciphername = NULL, *engine = NULL;
  87. if (!ossl_prov_is_running()
  88. || pmacctx == NULL)
  89. return 0;
  90. if (pmacctx->key == NULL && vkey == NULL) {
  91. ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
  92. return 0;
  93. }
  94. if (vkey != NULL) {
  95. if (!ossl_mac_key_up_ref(vkey))
  96. return 0;
  97. ossl_mac_key_free(pmacctx->key);
  98. pmacctx->key = vkey;
  99. }
  100. if (pmacctx->key->cipher.cipher != NULL)
  101. ciphername = (char *)EVP_CIPHER_get0_name(pmacctx->key->cipher.cipher);
  102. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  103. if (pmacctx->key->cipher.engine != NULL)
  104. engine = (char *)ENGINE_get_id(pmacctx->key->cipher.engine);
  105. #endif
  106. if (!ossl_prov_set_macctx(pmacctx->macctx, NULL,
  107. (char *)ciphername,
  108. (char *)mdname,
  109. (char *)engine,
  110. pmacctx->key->properties,
  111. NULL, 0))
  112. return 0;
  113. if (!EVP_MAC_init(pmacctx->macctx, pmacctx->key->priv_key,
  114. pmacctx->key->priv_key_len, params))
  115. return 0;
  116. return 1;
  117. }
  118. int mac_digest_sign_update(void *vpmacctx, const unsigned char *data,
  119. size_t datalen)
  120. {
  121. PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
  122. if (pmacctx == NULL || pmacctx->macctx == NULL)
  123. return 0;
  124. return EVP_MAC_update(pmacctx->macctx, data, datalen);
  125. }
  126. int mac_digest_sign_final(void *vpmacctx, unsigned char *mac, size_t *maclen,
  127. size_t macsize)
  128. {
  129. PROV_MAC_CTX *pmacctx = (PROV_MAC_CTX *)vpmacctx;
  130. if (!ossl_prov_is_running() || pmacctx == NULL || pmacctx->macctx == NULL)
  131. return 0;
  132. return EVP_MAC_final(pmacctx->macctx, mac, maclen, macsize);
  133. }
  134. static void mac_freectx(void *vpmacctx)
  135. {
  136. PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
  137. OPENSSL_free(ctx->propq);
  138. EVP_MAC_CTX_free(ctx->macctx);
  139. ossl_mac_key_free(ctx->key);
  140. OPENSSL_free(ctx);
  141. }
  142. static void *mac_dupctx(void *vpmacctx)
  143. {
  144. PROV_MAC_CTX *srcctx = (PROV_MAC_CTX *)vpmacctx;
  145. PROV_MAC_CTX *dstctx;
  146. if (!ossl_prov_is_running())
  147. return NULL;
  148. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  149. if (dstctx == NULL)
  150. return NULL;
  151. *dstctx = *srcctx;
  152. dstctx->propq = NULL;
  153. dstctx->key = NULL;
  154. dstctx->macctx = NULL;
  155. if (srcctx->propq != NULL && (dstctx->propq = OPENSSL_strdup(srcctx->propq)) == NULL)
  156. goto err;
  157. if (srcctx->key != NULL && !ossl_mac_key_up_ref(srcctx->key))
  158. goto err;
  159. dstctx->key = srcctx->key;
  160. if (srcctx->macctx != NULL) {
  161. dstctx->macctx = EVP_MAC_CTX_dup(srcctx->macctx);
  162. if (dstctx->macctx == NULL)
  163. goto err;
  164. }
  165. return dstctx;
  166. err:
  167. mac_freectx(dstctx);
  168. return NULL;
  169. }
  170. static int mac_set_ctx_params(void *vpmacctx, const OSSL_PARAM params[])
  171. {
  172. PROV_MAC_CTX *ctx = (PROV_MAC_CTX *)vpmacctx;
  173. return EVP_MAC_CTX_set_params(ctx->macctx, params);
  174. }
  175. static const OSSL_PARAM *mac_settable_ctx_params(ossl_unused void *ctx,
  176. void *provctx,
  177. const char *macname)
  178. {
  179. EVP_MAC *mac = EVP_MAC_fetch(PROV_LIBCTX_OF(provctx), macname,
  180. NULL);
  181. const OSSL_PARAM *params;
  182. if (mac == NULL)
  183. return NULL;
  184. params = EVP_MAC_settable_ctx_params(mac);
  185. EVP_MAC_free(mac);
  186. return params;
  187. }
  188. #define MAC_SETTABLE_CTX_PARAMS(funcname, macname) \
  189. static const OSSL_PARAM *mac_##funcname##_settable_ctx_params(void *ctx, \
  190. void *provctx) \
  191. { \
  192. return mac_settable_ctx_params(ctx, provctx, macname); \
  193. }
  194. MAC_SETTABLE_CTX_PARAMS(hmac, "HMAC")
  195. MAC_SETTABLE_CTX_PARAMS(siphash, "SIPHASH")
  196. MAC_SETTABLE_CTX_PARAMS(poly1305, "POLY1305")
  197. MAC_SETTABLE_CTX_PARAMS(cmac, "CMAC")
  198. #define MAC_SIGNATURE_FUNCTIONS(funcname) \
  199. const OSSL_DISPATCH ossl_mac_legacy_##funcname##_signature_functions[] = { \
  200. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))mac_##funcname##_newctx }, \
  201. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, \
  202. (void (*)(void))mac_digest_sign_init }, \
  203. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, \
  204. (void (*)(void))mac_digest_sign_update }, \
  205. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, \
  206. (void (*)(void))mac_digest_sign_final }, \
  207. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))mac_freectx }, \
  208. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))mac_dupctx }, \
  209. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, \
  210. (void (*)(void))mac_set_ctx_params }, \
  211. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, \
  212. (void (*)(void))mac_##funcname##_settable_ctx_params }, \
  213. { 0, NULL } \
  214. };
  215. MAC_SIGNATURE_FUNCTIONS(hmac)
  216. MAC_SIGNATURE_FUNCTIONS(siphash)
  217. MAC_SIGNATURE_FUNCTIONS(poly1305)
  218. MAC_SIGNATURE_FUNCTIONS(cmac)