cmac_prov.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2018 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. #include <openssl/core_numbers.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/engine.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/cmac.h>
  15. #include "internal/provider_algs.h"
  16. #include "internal/provider_ctx.h"
  17. #include "internal/provider_util.h"
  18. /*
  19. * Forward declaration of everything implemented here. This is not strictly
  20. * necessary for the compiler, but provides an assurance that the signatures
  21. * of the functions in the dispatch table are correct.
  22. */
  23. static OSSL_OP_mac_newctx_fn cmac_new;
  24. static OSSL_OP_mac_dupctx_fn cmac_dup;
  25. static OSSL_OP_mac_freectx_fn cmac_free;
  26. static OSSL_OP_mac_gettable_ctx_params_fn cmac_gettable_ctx_params;
  27. static OSSL_OP_mac_get_ctx_params_fn cmac_get_ctx_params;
  28. static OSSL_OP_mac_settable_ctx_params_fn cmac_settable_ctx_params;
  29. static OSSL_OP_mac_set_ctx_params_fn cmac_set_ctx_params;
  30. static OSSL_OP_mac_init_fn cmac_init;
  31. static OSSL_OP_mac_update_fn cmac_update;
  32. static OSSL_OP_mac_final_fn cmac_final;
  33. /* local CMAC data */
  34. struct cmac_data_st {
  35. void *provctx;
  36. CMAC_CTX *ctx;
  37. PROV_CIPHER cipher;
  38. };
  39. static void *cmac_new(void *provctx)
  40. {
  41. struct cmac_data_st *macctx;
  42. if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  43. || (macctx->ctx = CMAC_CTX_new()) == NULL) {
  44. OPENSSL_free(macctx);
  45. macctx = NULL;
  46. } else {
  47. macctx->provctx = provctx;
  48. }
  49. return macctx;
  50. }
  51. static void cmac_free(void *vmacctx)
  52. {
  53. struct cmac_data_st *macctx = vmacctx;
  54. if (macctx != NULL) {
  55. CMAC_CTX_free(macctx->ctx);
  56. ossl_prov_cipher_reset(&macctx->cipher);
  57. OPENSSL_free(macctx);
  58. }
  59. }
  60. static void *cmac_dup(void *vsrc)
  61. {
  62. struct cmac_data_st *src = vsrc;
  63. struct cmac_data_st *dst = cmac_new(src->provctx);
  64. if (!CMAC_CTX_copy(dst->ctx, src->ctx)
  65. || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
  66. cmac_free(dst);
  67. return NULL;
  68. }
  69. return dst;
  70. }
  71. static size_t cmac_size(void *vmacctx)
  72. {
  73. struct cmac_data_st *macctx = vmacctx;
  74. return EVP_CIPHER_CTX_block_size(CMAC_CTX_get0_cipher_ctx(macctx->ctx));
  75. }
  76. static int cmac_init(void *vmacctx)
  77. {
  78. struct cmac_data_st *macctx = vmacctx;
  79. int rv = CMAC_Init(macctx->ctx, NULL, 0,
  80. ossl_prov_cipher_cipher(&macctx->cipher),
  81. ossl_prov_cipher_engine(&macctx->cipher));
  82. ossl_prov_cipher_reset(&macctx->cipher);
  83. return rv;
  84. }
  85. static int cmac_update(void *vmacctx, const unsigned char *data,
  86. size_t datalen)
  87. {
  88. struct cmac_data_st *macctx = vmacctx;
  89. return CMAC_Update(macctx->ctx, data, datalen);
  90. }
  91. static int cmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  92. size_t outsize)
  93. {
  94. struct cmac_data_st *macctx = vmacctx;
  95. return CMAC_Final(macctx->ctx, out, outl);
  96. }
  97. static const OSSL_PARAM known_gettable_ctx_params[] = {
  98. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  99. OSSL_PARAM_END
  100. };
  101. static const OSSL_PARAM *cmac_gettable_ctx_params(void)
  102. {
  103. return known_gettable_ctx_params;
  104. }
  105. static int cmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
  106. {
  107. OSSL_PARAM *p;
  108. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  109. return OSSL_PARAM_set_size_t(p, cmac_size(vmacctx));
  110. return 1;
  111. }
  112. static const OSSL_PARAM known_settable_ctx_params[] = {
  113. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
  114. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  115. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  116. OSSL_PARAM_END
  117. };
  118. static const OSSL_PARAM *cmac_settable_ctx_params(void)
  119. {
  120. return known_settable_ctx_params;
  121. }
  122. /*
  123. * ALL parameters should be set before init().
  124. */
  125. static int cmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  126. {
  127. struct cmac_data_st *macctx = vmacctx;
  128. OPENSSL_CTX *ctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
  129. const OSSL_PARAM *p;
  130. if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, ctx))
  131. return 0;
  132. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  133. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  134. return 0;
  135. if (!CMAC_Init(macctx->ctx, p->data, p->data_size,
  136. ossl_prov_cipher_cipher(&macctx->cipher),
  137. ossl_prov_cipher_engine(&macctx->cipher)))
  138. return 0;
  139. ossl_prov_cipher_reset(&macctx->cipher);
  140. }
  141. return 1;
  142. }
  143. const OSSL_DISPATCH cmac_functions[] = {
  144. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))cmac_new },
  145. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))cmac_dup },
  146. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))cmac_free },
  147. { OSSL_FUNC_MAC_INIT, (void (*)(void))cmac_init },
  148. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))cmac_update },
  149. { OSSL_FUNC_MAC_FINAL, (void (*)(void))cmac_final },
  150. { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
  151. (void (*)(void))cmac_gettable_ctx_params },
  152. { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))cmac_get_ctx_params },
  153. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  154. (void (*)(void))cmac_settable_ctx_params },
  155. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))cmac_set_ctx_params },
  156. { 0, NULL }
  157. };