2
0

gmac_prov.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include <stdlib.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/params.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/err.h>
  15. #include <openssl/proverr.h>
  16. #include "prov/implementations.h"
  17. #include "prov/provider_ctx.h"
  18. #include "prov/provider_util.h"
  19. #include "prov/providercommon.h"
  20. /*
  21. * Forward declaration of everything implemented here. This is not strictly
  22. * necessary for the compiler, but provides an assurance that the signatures
  23. * of the functions in the dispatch table are correct.
  24. */
  25. static OSSL_FUNC_mac_newctx_fn gmac_new;
  26. static OSSL_FUNC_mac_dupctx_fn gmac_dup;
  27. static OSSL_FUNC_mac_freectx_fn gmac_free;
  28. static OSSL_FUNC_mac_gettable_params_fn gmac_gettable_params;
  29. static OSSL_FUNC_mac_get_params_fn gmac_get_params;
  30. static OSSL_FUNC_mac_settable_ctx_params_fn gmac_settable_ctx_params;
  31. static OSSL_FUNC_mac_set_ctx_params_fn gmac_set_ctx_params;
  32. static OSSL_FUNC_mac_init_fn gmac_init;
  33. static OSSL_FUNC_mac_update_fn gmac_update;
  34. static OSSL_FUNC_mac_final_fn gmac_final;
  35. /* local GMAC pkey structure */
  36. struct gmac_data_st {
  37. void *provctx;
  38. EVP_CIPHER_CTX *ctx; /* Cipher context */
  39. PROV_CIPHER cipher;
  40. };
  41. static void gmac_free(void *vmacctx)
  42. {
  43. struct gmac_data_st *macctx = vmacctx;
  44. if (macctx != NULL) {
  45. EVP_CIPHER_CTX_free(macctx->ctx);
  46. ossl_prov_cipher_reset(&macctx->cipher);
  47. OPENSSL_free(macctx);
  48. }
  49. }
  50. static void *gmac_new(void *provctx)
  51. {
  52. struct gmac_data_st *macctx;
  53. if (!ossl_prov_is_running())
  54. return NULL;
  55. if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  56. || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
  57. gmac_free(macctx);
  58. return NULL;
  59. }
  60. macctx->provctx = provctx;
  61. return macctx;
  62. }
  63. static void *gmac_dup(void *vsrc)
  64. {
  65. struct gmac_data_st *src = vsrc;
  66. struct gmac_data_st *dst;
  67. if (!ossl_prov_is_running())
  68. return NULL;
  69. dst = gmac_new(src->provctx);
  70. if (dst == NULL)
  71. return NULL;
  72. if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
  73. || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
  74. gmac_free(dst);
  75. return NULL;
  76. }
  77. return dst;
  78. }
  79. static size_t gmac_size(void)
  80. {
  81. return EVP_GCM_TLS_TAG_LEN;
  82. }
  83. static int gmac_setkey(struct gmac_data_st *macctx,
  84. const unsigned char *key, size_t keylen)
  85. {
  86. EVP_CIPHER_CTX *ctx = macctx->ctx;
  87. if (keylen != (size_t)EVP_CIPHER_CTX_get_key_length(ctx)) {
  88. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  89. return 0;
  90. }
  91. if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
  92. return 0;
  93. return 1;
  94. }
  95. static int gmac_init(void *vmacctx, const unsigned char *key,
  96. size_t keylen, const OSSL_PARAM params[])
  97. {
  98. struct gmac_data_st *macctx = vmacctx;
  99. if (!ossl_prov_is_running() || !gmac_set_ctx_params(macctx, params))
  100. return 0;
  101. if (key != NULL)
  102. return gmac_setkey(macctx, key, keylen);
  103. return EVP_EncryptInit_ex(macctx->ctx, NULL, NULL, NULL, NULL);
  104. }
  105. static int gmac_update(void *vmacctx, const unsigned char *data,
  106. size_t datalen)
  107. {
  108. struct gmac_data_st *macctx = vmacctx;
  109. EVP_CIPHER_CTX *ctx = macctx->ctx;
  110. int outlen;
  111. if (datalen == 0)
  112. return 1;
  113. while (datalen > INT_MAX) {
  114. if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
  115. return 0;
  116. data += INT_MAX;
  117. datalen -= INT_MAX;
  118. }
  119. return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
  120. }
  121. static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  122. size_t outsize)
  123. {
  124. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  125. struct gmac_data_st *macctx = vmacctx;
  126. int hlen = 0;
  127. if (!ossl_prov_is_running())
  128. return 0;
  129. if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
  130. return 0;
  131. hlen = gmac_size();
  132. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  133. out, (size_t)hlen);
  134. if (!EVP_CIPHER_CTX_get_params(macctx->ctx, params))
  135. return 0;
  136. *outl = hlen;
  137. return 1;
  138. }
  139. static const OSSL_PARAM known_gettable_params[] = {
  140. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  141. OSSL_PARAM_END
  142. };
  143. static const OSSL_PARAM *gmac_gettable_params(void *provctx)
  144. {
  145. return known_gettable_params;
  146. }
  147. static int gmac_get_params(OSSL_PARAM params[])
  148. {
  149. OSSL_PARAM *p;
  150. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  151. return OSSL_PARAM_set_size_t(p, gmac_size());
  152. return 1;
  153. }
  154. static const OSSL_PARAM known_settable_ctx_params[] = {
  155. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
  156. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  157. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  158. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
  159. OSSL_PARAM_END
  160. };
  161. static const OSSL_PARAM *gmac_settable_ctx_params(ossl_unused void *ctx,
  162. ossl_unused void *provctx)
  163. {
  164. return known_settable_ctx_params;
  165. }
  166. /*
  167. * ALL parameters should be set before init().
  168. */
  169. static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  170. {
  171. struct gmac_data_st *macctx = vmacctx;
  172. EVP_CIPHER_CTX *ctx = macctx->ctx;
  173. OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(macctx->provctx);
  174. const OSSL_PARAM *p;
  175. if (params == NULL)
  176. return 1;
  177. if (ctx == NULL)
  178. return 0;
  179. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CIPHER)) != NULL) {
  180. if (!ossl_prov_cipher_load_from_params(&macctx->cipher, params, provctx))
  181. return 0;
  182. if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher))
  183. != EVP_CIPH_GCM_MODE) {
  184. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
  185. return 0;
  186. }
  187. if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher),
  188. ossl_prov_cipher_engine(&macctx->cipher), NULL,
  189. NULL))
  190. return 0;
  191. }
  192. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
  193. if (p->data_type != OSSL_PARAM_OCTET_STRING
  194. || !gmac_setkey(macctx, p->data, p->data_size))
  195. return 0;
  196. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
  197. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  198. return 0;
  199. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  200. p->data_size, NULL) <= 0
  201. || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. const OSSL_DISPATCH ossl_gmac_functions[] = {
  207. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
  208. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
  209. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
  210. { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
  211. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
  212. { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
  213. { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
  214. { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
  215. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  216. (void (*)(void))gmac_settable_ctx_params },
  217. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },
  218. OSSL_DISPATCH_END
  219. };