gmac_prov.c 7.4 KB

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