gmac_prov.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <stdlib.h>
  10. #include <openssl/core_numbers.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 "internal/providercommonerr.h"
  17. #include "internal/provider_algs.h"
  18. #include "internal/provider_ctx.h"
  19. #include "internal/provider_util.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_OP_mac_newctx_fn gmac_new;
  26. static OSSL_OP_mac_dupctx_fn gmac_dup;
  27. static OSSL_OP_mac_freectx_fn gmac_free;
  28. static OSSL_OP_mac_gettable_params_fn gmac_gettable_params;
  29. static OSSL_OP_mac_get_params_fn gmac_get_params;
  30. static OSSL_OP_mac_settable_ctx_params_fn gmac_settable_ctx_params;
  31. static OSSL_OP_mac_set_ctx_params_fn gmac_set_ctx_params;
  32. static OSSL_OP_mac_init_fn gmac_init;
  33. static OSSL_OP_mac_update_fn gmac_update;
  34. static OSSL_OP_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 size_t gmac_size(void);
  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 ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
  55. || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) {
  56. gmac_free(macctx);
  57. return NULL;
  58. }
  59. macctx->provctx = provctx;
  60. return macctx;
  61. }
  62. static void *gmac_dup(void *vsrc)
  63. {
  64. struct gmac_data_st *src = vsrc;
  65. struct gmac_data_st *dst = gmac_new(src->provctx);
  66. if (dst == NULL)
  67. return NULL;
  68. if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx)
  69. || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) {
  70. gmac_free(dst);
  71. return NULL;
  72. }
  73. return dst;
  74. }
  75. static int gmac_init(void *vmacctx)
  76. {
  77. return 1;
  78. }
  79. static int gmac_update(void *vmacctx, const unsigned char *data,
  80. size_t datalen)
  81. {
  82. struct gmac_data_st *macctx = vmacctx;
  83. EVP_CIPHER_CTX *ctx = macctx->ctx;
  84. int outlen;
  85. while (datalen > INT_MAX) {
  86. if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX))
  87. return 0;
  88. data += INT_MAX;
  89. datalen -= INT_MAX;
  90. }
  91. return EVP_EncryptUpdate(ctx, NULL, &outlen, data, datalen);
  92. }
  93. static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl,
  94. size_t outsize)
  95. {
  96. struct gmac_data_st *macctx = vmacctx;
  97. int hlen = 0;
  98. if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen))
  99. return 0;
  100. /* TODO(3.0) Use params */
  101. hlen = gmac_size();
  102. if (!EVP_CIPHER_CTX_ctrl(macctx->ctx, EVP_CTRL_AEAD_GET_TAG,
  103. hlen, out))
  104. return 0;
  105. *outl = hlen;
  106. return 1;
  107. }
  108. static size_t gmac_size(void)
  109. {
  110. return EVP_GCM_TLS_TAG_LEN;
  111. }
  112. static const OSSL_PARAM known_gettable_params[] = {
  113. OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
  114. OSSL_PARAM_END
  115. };
  116. static const OSSL_PARAM *gmac_gettable_params(void)
  117. {
  118. return known_gettable_params;
  119. }
  120. static int gmac_get_params(OSSL_PARAM params[])
  121. {
  122. OSSL_PARAM *p;
  123. if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
  124. return OSSL_PARAM_set_size_t(p, gmac_size());
  125. return 1;
  126. }
  127. static const OSSL_PARAM known_settable_ctx_params[] = {
  128. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0),
  129. OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
  130. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
  131. OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0),
  132. OSSL_PARAM_END
  133. };
  134. static const OSSL_PARAM *gmac_settable_ctx_params(void)
  135. {
  136. return known_settable_ctx_params;
  137. }
  138. /*
  139. * ALL parameters should be set before init().
  140. */
  141. static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
  142. {
  143. struct gmac_data_st *macctx = vmacctx;
  144. EVP_CIPHER_CTX *ctx = macctx->ctx;
  145. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(macctx->provctx);
  146. const OSSL_PARAM *p;
  147. if (ctx == NULL
  148. || !ossl_prov_cipher_load_from_params(&macctx->cipher, params, provctx))
  149. return 0;
  150. if (EVP_CIPHER_mode(ossl_prov_cipher_cipher(&macctx->cipher))
  151. != EVP_CIPH_GCM_MODE) {
  152. ERR_raise(ERR_LIB_PROV, EVP_R_CIPHER_NOT_GCM_MODE);
  153. return 0;
  154. }
  155. if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher),
  156. ossl_prov_cipher_engine(&macctx->cipher), NULL,
  157. NULL))
  158. return 0;
  159. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
  160. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  161. return 0;
  162. if (p->data_size != (size_t)EVP_CIPHER_CTX_key_length(ctx)) {
  163. ERR_raise(ERR_LIB_PROV, EVP_R_INVALID_KEY_LENGTH);
  164. return 0;
  165. }
  166. if (!EVP_EncryptInit_ex(ctx, NULL, NULL, p->data, NULL))
  167. return 0;
  168. }
  169. if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_IV)) != NULL) {
  170. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  171. return 0;
  172. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  173. p->data_size, NULL)
  174. || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p->data))
  175. return 0;
  176. }
  177. return 1;
  178. }
  179. const OSSL_DISPATCH gmac_functions[] = {
  180. { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new },
  181. { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup },
  182. { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free },
  183. { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init },
  184. { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update },
  185. { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final },
  186. { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params },
  187. { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params },
  188. { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
  189. (void (*)(void))gmac_settable_ctx_params },
  190. { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params },
  191. { 0, NULL }
  192. };