cmac_prov.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2018-2020 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/engine.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/cmac.h>
  20. #include "prov/implementations.h"
  21. #include "prov/provider_ctx.h"
  22. #include "prov/provider_util.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 ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  48. || (macctx->ctx = CMAC_CTX_new()) == NULL) {
  49. OPENSSL_free(macctx);
  50. macctx = NULL;
  51. } else {
  52. macctx->provctx = provctx;
  53. }
  54. return macctx;
  55. }
  56. static void cmac_free(void *vmacctx)
  57. {
  58. struct cmac_data_st *macctx = vmacctx;
  59. if (macctx != NULL) {
  60. CMAC_CTX_free(macctx->ctx);
  61. ossl_prov_cipher_reset(&macctx->cipher);
  62. OPENSSL_free(macctx);
  63. }
  64. }
  65. static void *cmac_dup(void *vsrc)
  66. {
  67. struct cmac_data_st *src = vsrc;
  68. struct cmac_data_st *dst = cmac_new(src->provctx);
  69. if (!CMAC_CTX_copy(dst->ctx, src->ctx)
  70. || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
  71. cmac_free(dst);
  72. return NULL;
  73. }
  74. return dst;
  75. }
  76. static size_t cmac_size(void *vmacctx)
  77. {
  78. struct cmac_data_st *macctx = vmacctx;
  79. return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
  80. }
  81. static int cmac_init(void *vmacctx)
  82. {
  83. struct cmac_data_st *macctx = vmacctx;
  84. int rv = CMAC_Init(macctx->ctx, NULL, 0,
  85. ossl_prov_cipher_cipher(&macctx->cipher),
  86. ossl_prov_cipher_engine(&macctx->cipher));
  87. ossl_prov_cipher_reset(&macctx->cipher);
  88. return rv;
  89. }
  90. static int cmac_update(void *vmacctx, const unsigned char *data,
  91. size_t datalen)
  92. {
  93. struct cmac_data_st *macctx = vmacctx;
  94. return CMAC_Update(macctx->ctx, data, datalen);
  95. }
  96. static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  97. size_t outsize)
  98. {
  99. struct cmac_data_st *macctx = vmacctx;
  100. return CMAC_Final(macctx->ctx, out, outl);
  101. }
  102. static const OSSL_PARAM known_gettable_ctx_params[] = {
  103. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  104. OSSL_PARAM_END
  105. };
  106. static const OSSL_PARAM *cmac_gettable_ctx_params(ossl_unused void *provctx)
  107. {
  108. return known_gettable_ctx_params;
  109. }
  110. static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  111. {
  112. OSSL_PARAM *p;
  113. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  114. return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
  115. return 1;
  116. }
  117. static const OSSL_PARAM known_settable_ctx_params[] = {
  118. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
  119. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  120. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  121. OSSL_PARAM_END
  122. };
  123. static const OSSL_PARAM *cmac_settable_ctx_params(ossl_unused void *provctx)
  124. {
  125. return known_settable_ctx_params;
  126. }
  127. /*
  128. * ALL parameters should be set before init().
  129. */
  130. static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  131. {
  132. struct cmac_data_st *macctx = vmacctx;
  133. OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
  134. const OSSL_PARAM *p;
  135. if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
  136. return 0;
  137. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  138. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  139. return 0;
  140. if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
  141. ossl_prov_cipher_cipher(&macctx->cipher),
  142. ossl_prov_cipher_engine(&macctx->cipher)))
  143. return 0;
  144. ossl_prov_cipher_reset(&macctx->cipher);
  145. }
  146. return 1;
  147. }
  148. const OSSL_DISPATCH cmac_functions[] = {
  149. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
  150. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
  151. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
  152. { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
  153. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
  154. { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
  155. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  156. (void (*)(void))cmac_gettable_ctx_params },
  157. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
  158. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  159. (void (*)(void))cmac_settable_ctx_params },
  160. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
  161. { 0, NULL }
  162. };