mac_legacy.c 7.8 KB

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