cmac_prov.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2018-2022 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. return EVP_CIPHER_CTX_get_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
  89. }
  90. static int cmac_setkey(struct cmac_data_st *macctx,
  91. const unsigned char *key, size_t keylen)
  92. {
  93. int rv = CMAC_Init(macctx->ctx, key, keylen,
  94. ossl_prov_cipher_cipher(&macctx->cipher),
  95. ossl_prov_cipher_engine(&macctx->cipher));
  96. ossl_prov_cipher_reset(&macctx->cipher);
  97. return rv;
  98. }
  99. static int cmac_init(void *vmacctx, const unsigned char *key,
  100. size_t keylen, const OSSL_PARAM params[])
  101. {
  102. struct cmac_data_st *macctx = vmacctx;
  103. if (!ossl_prov_is_running() || !cmac_set_ctx_params(macctx, params))
  104. return 0;
  105. if (key != NULL)
  106. return cmac_setkey(macctx, key, keylen);
  107. /* Reinitialize the CMAC context */
  108. return CMAC_Init(macctx->ctx, NULL, 0, NULL, NULL);
  109. }
  110. static int cmac_update(void *vmacctx, const unsigned char *data,
  111. size_t datalen)
  112. {
  113. struct cmac_data_st *macctx = vmacctx;
  114. return CMAC_Update(macctx->ctx, data, datalen);
  115. }
  116. static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  117. size_t outsize)
  118. {
  119. struct cmac_data_st *macctx = vmacctx;
  120. if (!ossl_prov_is_running())
  121. return 0;
  122. return CMAC_Final(macctx->ctx, out, outl);
  123. }
  124. static const OSSL_PARAM known_gettable_ctx_params[] = {
  125. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  126. OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
  127. OSSL_PARAM_END
  128. };
  129. static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *ctx,
  130. ossl_unused void *provctx)
  131. {
  132. return known_gettable_ctx_params;
  133. }
  134. static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  135. {
  136. OSSL_PARAM *p;
  137. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
  138. && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
  139. return 0;
  140. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
  141. && !OSSL_PARAM_set_size_t(p, cmac_size(vmacctx)))
  142. return 0;
  143. return 1;
  144. }
  145. static const OSSL_PARAM known_settable_ctx_params[] = {
  146. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
  147. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  148. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  149. OSSL_PARAM_END
  150. };
  151. static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *ctx,
  152. ossl_unused void *provctx)
  153. {
  154. return known_settable_ctx_params;
  155. }
  156. /*
  157. * ALL parameters should be set before init().
  158. */
  159. static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  160. {
  161. struct cmac_data_st *macctx = vmacctx;
  162. OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
  163. const OSSL_PARAM *p;
  164. if (params == NULL)
  165. return 1;
  166. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
  167. if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
  168. return 0;
  169. if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))
  170. != EVP_CIPH_CBC_MODE) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  172. return 0;
  173. }
  174. }
  175. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  176. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  177. return 0;
  178. return cmac_setkey(macctx, p->data, p->data_size);
  179. }
  180. return 1;
  181. }
  182. const OSSL_DISPATCH ossl_cmac_functions[] = {
  183. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
  184. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
  185. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
  186. { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
  187. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
  188. { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
  189. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  190. (void (*)(void))cmac_gettable_ctx_params },
  191. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
  192. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  193. (void (*)(void))cmac_settable_ctx_params },
  194. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
  195. OSSL_DISPATCH_END
  196. };