cmac_prov.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2018-2023 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. /*
  10. * CMAC low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_dispatch.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/params.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/cmac.h>
  19. #include <openssl/err.h>
  20. #include <openssl/proverr.h>
  21. #include "prov/implementations.h"
  22. #include "prov/provider_ctx.h"
  23. #include "prov/provider_util.h"
  24. #include "prov/providercommon.h"
  25. /*
  26. * Forward declaration of everything implemented here. This is not strictly
  27. * necessary for the compiler, but provides an assurance that the signatures
  28. * of the functions in the dispatch table are correct.
  29. */
  30. static OSSL_FUNC_mac_newctx_fn cmac_new;
  31. static OSSL_FUNC_mac_dupctx_fn cmac_dup;
  32. static OSSL_FUNC_mac_freectx_fn cmac_free;
  33. static OSSL_FUNC_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
  34. static OSSL_FUNC_mac_get_ctx_params_fn cmac_get_ctx_params;
  35. static OSSL_FUNC_mac_settable_ctx_params_fn cmac_settable_ctx_params;
  36. static OSSL_FUNC_mac_set_ctx_params_fn cmac_set_ctx_params;
  37. static OSSL_FUNC_mac_init_fn cmac_init;
  38. static OSSL_FUNC_mac_update_fn cmac_update;
  39. static OSSL_FUNC_mac_final_fn cmac_final;
  40. /* local CMAC data */
  41. struct cmac_data_st {
  42. void *provctx;
  43. CMAC_CTX *ctx;
  44. PROV_CIPHER cipher;
  45. };
  46. static void *cmac_new(void *provctx)
  47. {
  48. struct cmac_data_st *macctx;
  49. if (!ossl_prov_is_running())
  50. return NULL;
  51. if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  52. || (macctx->ctx = CMAC_CTX_new()) == NULL) {
  53. OPENSSL_free(macctx);
  54. macctx = NULL;
  55. } else {
  56. macctx->provctx = provctx;
  57. }
  58. return macctx;
  59. }
  60. static void cmac_free(void *vmacctx)
  61. {
  62. struct cmac_data_st *macctx = vmacctx;
  63. if (macctx != NULL) {
  64. CMAC_CTX_free(macctx->ctx);
  65. ossl_prov_cipher_reset(&macctx->cipher);
  66. OPENSSL_free(macctx);
  67. }
  68. }
  69. static void *cmac_dup(void *vsrc)
  70. {
  71. struct cmac_data_st *src = vsrc;
  72. struct cmac_data_st *dst;
  73. if (!ossl_prov_is_running())
  74. return NULL;
  75. dst = cmac_new(src->provctx);
  76. if (dst == NULL)
  77. return NULL;
  78. if (!CMAC_CTX_copy(dst->ctx, src->ctx)
  79. || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
  80. cmac_free(dst);
  81. return NULL;
  82. }
  83. return dst;
  84. }
  85. static size_t cmac_size(void *vmacctx)
  86. {
  87. struct cmac_data_st *macctx = vmacctx;
  88. const EVP_CIPHER_CTX *cipherctx = CMAC_CTX_get0_cipher_ctx(macctx->ctx);
  89. if (EVP_CIPHER_CTX_get0_cipher(cipherctx) == NULL)
  90. return 0;
  91. return EVP_CIPHER_CTX_get_block_size(cipherctx);
  92. }
  93. static int cmac_setkey(struct cmac_data_st *macctx,
  94. const unsigned char *key, size_t keylen)
  95. {
  96. int rv = CMAC_Init(macctx->ctx, key, keylen,
  97. ossl_prov_cipher_cipher(&macctx->cipher),
  98. ossl_prov_cipher_engine(&macctx->cipher));
  99. ossl_prov_cipher_reset(&macctx->cipher);
  100. return rv;
  101. }
  102. static int cmac_init(void *vmacctx, const unsigned char *key,
  103. size_t keylen, const OSSL_PARAM params[])
  104. {
  105. struct cmac_data_st *macctx = vmacctx;
  106. if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params))
  107. return 0;
  108. if (key != NULL)
  109. return cmac_setkey(macctx, key, keylen);
  110. /* Reinitialize the CMAC context */
  111. return CMAC_Init(macctx->ctx, NULL, 0, NULL, NULL);
  112. }
  113. static int cmac_update(void *vmacctx, const unsigned char *data,
  114. size_t datalen)
  115. {
  116. struct cmac_data_st *macctx = vmacctx;
  117. return CMAC_Update(macctx->ctx, data, datalen);
  118. }
  119. static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  120. size_t outsize)
  121. {
  122. struct cmac_data_st *macctx = vmacctx;
  123. if (!ossl_prov_is_running())
  124. return 0;
  125. return CMAC_Final(macctx->ctx, out, outl);
  126. }
  127. static const OSSL_PARAM known_gettable_ctx_params[] = {
  128. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  129. OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
  130. OSSL_PARAM_END
  131. };
  132. static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx,
  133. ossl_unused void *provctx)
  134. {
  135. return known_gettable_ctx_params;
  136. }
  137. static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  138. {
  139. OSSL_PARAM *p;
  140. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
  141. && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
  142. return 0;
  143. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
  144. && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
  145. return 0;
  146. return 1;
  147. }
  148. static const OSSL_PARAM known_settable_ctx_params[] = {
  149. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
  150. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  151. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  152. OSSL_PARAM_END
  153. };
  154. static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx,
  155. ossl_unused void *provctx)
  156. {
  157. return known_settable_ctx_params;
  158. }
  159. /*
  160. * ALL parameters should be set before init().
  161. */
  162. static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  163. {
  164. struct cmac_data_st *macctx = vmacctx;
  165. OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
  166. const OSSL_PARAM *p;
  167. if (params == NULL)
  168. return 1;
  169. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
  170. if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
  171. return 0;
  172. if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))
  173. != EVP_CIPH_CBC_MODE) {
  174. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  175. return 0;
  176. }
  177. }
  178. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  179. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  180. return 0;
  181. return cmac_setkey(macctx, p->data, p->data_size);
  182. }
  183. return 1;
  184. }
  185. const OSSL_DISPATCH ossl_cmac_functions[] = {
  186. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
  187. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
  188. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
  189. { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
  190. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
  191. { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
  192. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  193. (void (*)(void))cmac_gettable_ctx_params },
  194. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
  195. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  196. (void (*)(void))cmac_settable_ctx_params },
  197. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
  198. OSSL_DISPATCH_END
  199. };