cmac_prov.c 6.5 KB

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