cipher_aes_siv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright 2019-2023 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. /* Dispatch functions for AES SIV mode */
  10. /*
  11. * This file uses the low level AES functions (which are deprecated for
  12. * non-internal use) in order to implement provider AES ciphers.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <openssl/proverr.h>
  16. #include "cipher_aes_siv.h"
  17. #include "prov/implementations.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/ciphercommon_aead.h"
  20. #include "prov/provider_ctx.h"
  21. #define siv_stream_update siv_cipher
  22. #define SIV_FLAGS AEAD_FLAGS
  23. static OSSL_FUNC_cipher_set_ctx_params_fn aes_siv_set_ctx_params;
  24. static void *aes_siv_newctx(void *provctx, size_t keybits, unsigned int mode,
  25. uint64_t flags)
  26. {
  27. PROV_AES_SIV_CTX *ctx;
  28. if (!ossl_prov_is_running())
  29. return NULL;
  30. ctx = OPENSSL_zalloc(sizeof(*ctx));
  31. if (ctx != NULL) {
  32. ctx->taglen = SIV_LEN;
  33. ctx->mode = mode;
  34. ctx->keylen = keybits / 8;
  35. ctx->hw = ossl_prov_cipher_hw_aes_siv(keybits);
  36. ctx->libctx = PROV_LIBCTX_OF(provctx);
  37. }
  38. return ctx;
  39. }
  40. static void aes_siv_freectx(void *vctx)
  41. {
  42. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  43. if (ctx != NULL) {
  44. ctx->hw->cleanup(ctx);
  45. OPENSSL_clear_free(ctx, sizeof(*ctx));
  46. }
  47. }
  48. static void *siv_dupctx(void *vctx)
  49. {
  50. PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)vctx;
  51. PROV_AES_SIV_CTX *ret;
  52. if (!ossl_prov_is_running())
  53. return NULL;
  54. ret = OPENSSL_malloc(sizeof(*ret));
  55. if (ret == NULL)
  56. return NULL;
  57. if (!in->hw->dupctx(in, ret)) {
  58. OPENSSL_free(ret);
  59. ret = NULL;
  60. }
  61. return ret;
  62. }
  63. static int siv_init(void *vctx, const unsigned char *key, size_t keylen,
  64. const unsigned char *iv, size_t ivlen,
  65. const OSSL_PARAM params[], int enc)
  66. {
  67. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  68. if (!ossl_prov_is_running())
  69. return 0;
  70. ctx->enc = enc;
  71. if (key != NULL) {
  72. if (keylen != ctx->keylen) {
  73. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  74. return 0;
  75. }
  76. if (!ctx->hw->initkey(ctx, key, ctx->keylen))
  77. return 0;
  78. }
  79. return aes_siv_set_ctx_params(ctx, params);
  80. }
  81. static int siv_einit(void *vctx, const unsigned char *key, size_t keylen,
  82. const unsigned char *iv, size_t ivlen,
  83. const OSSL_PARAM params[])
  84. {
  85. return siv_init(vctx, key, keylen, iv, ivlen, params, 1);
  86. }
  87. static int siv_dinit(void *vctx, const unsigned char *key, size_t keylen,
  88. const unsigned char *iv, size_t ivlen,
  89. const OSSL_PARAM params[])
  90. {
  91. return siv_init(vctx, key, keylen, iv, ivlen, params, 0);
  92. }
  93. static int siv_cipher(void *vctx, unsigned char *out, size_t *outl,
  94. size_t outsize, const unsigned char *in, size_t inl)
  95. {
  96. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  97. if (!ossl_prov_is_running())
  98. return 0;
  99. /* Ignore just empty encryption/decryption call and not AAD. */
  100. if (out != NULL) {
  101. if (inl == 0) {
  102. if (outl != NULL)
  103. *outl = 0;
  104. return 1;
  105. }
  106. if (outsize < inl) {
  107. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  108. return 0;
  109. }
  110. }
  111. if (ctx->hw->cipher(ctx, out, in, inl) <= 0)
  112. return 0;
  113. if (outl != NULL)
  114. *outl = inl;
  115. return 1;
  116. }
  117. static int siv_stream_final(void *vctx, unsigned char *out, size_t *outl,
  118. size_t outsize)
  119. {
  120. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  121. if (!ossl_prov_is_running())
  122. return 0;
  123. if (!ctx->hw->cipher(vctx, out, NULL, 0))
  124. return 0;
  125. if (outl != NULL)
  126. *outl = 0;
  127. return 1;
  128. }
  129. static int aes_siv_get_ctx_params(void *vctx, OSSL_PARAM params[])
  130. {
  131. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  132. SIV128_CONTEXT *sctx = &ctx->siv;
  133. OSSL_PARAM *p;
  134. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  135. if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING) {
  136. if (!ctx->enc
  137. || p->data_size != ctx->taglen
  138. || !OSSL_PARAM_set_octet_string(p, &sctx->tag.byte, ctx->taglen)) {
  139. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  140. return 0;
  141. }
  142. }
  143. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
  144. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->taglen)) {
  145. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  146. return 0;
  147. }
  148. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  149. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  150. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  151. return 0;
  152. }
  153. return 1;
  154. }
  155. static const OSSL_PARAM aes_siv_known_gettable_ctx_params[] = {
  156. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  157. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
  158. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  159. OSSL_PARAM_END
  160. };
  161. static const OSSL_PARAM *aes_siv_gettable_ctx_params(ossl_unused void *cctx,
  162. ossl_unused void *provctx)
  163. {
  164. return aes_siv_known_gettable_ctx_params;
  165. }
  166. static int aes_siv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  167. {
  168. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  169. const OSSL_PARAM *p;
  170. unsigned int speed = 0;
  171. if (params == NULL)
  172. return 1;
  173. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  174. if (p != NULL) {
  175. if (ctx->enc)
  176. return 1;
  177. if (p->data_type != OSSL_PARAM_OCTET_STRING
  178. || !ctx->hw->settag(ctx, p->data, p->data_size)) {
  179. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  180. return 0;
  181. }
  182. }
  183. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_SPEED);
  184. if (p != NULL) {
  185. if (!OSSL_PARAM_get_uint(p, &speed)) {
  186. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  187. return 0;
  188. }
  189. ctx->hw->setspeed(ctx, (int)speed);
  190. }
  191. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  192. if (p != NULL) {
  193. size_t keylen;
  194. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  195. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  196. return 0;
  197. }
  198. /* The key length can not be modified */
  199. if (keylen != ctx->keylen)
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. static const OSSL_PARAM aes_siv_known_settable_ctx_params[] = {
  205. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  206. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_SPEED, NULL),
  207. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  208. OSSL_PARAM_END
  209. };
  210. static const OSSL_PARAM *aes_siv_settable_ctx_params(ossl_unused void *cctx,
  211. ossl_unused void *provctx)
  212. {
  213. return aes_siv_known_settable_ctx_params;
  214. }
  215. #define IMPLEMENT_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits) \
  216. static OSSL_FUNC_cipher_newctx_fn alg##kbits##lc##_newctx; \
  217. static OSSL_FUNC_cipher_freectx_fn alg##_##lc##_freectx; \
  218. static OSSL_FUNC_cipher_dupctx_fn lc##_dupctx; \
  219. static OSSL_FUNC_cipher_encrypt_init_fn lc##_einit; \
  220. static OSSL_FUNC_cipher_decrypt_init_fn lc##_dinit; \
  221. static OSSL_FUNC_cipher_update_fn lc##_stream_update; \
  222. static OSSL_FUNC_cipher_final_fn lc##_stream_final; \
  223. static OSSL_FUNC_cipher_cipher_fn lc##_cipher; \
  224. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lc##_get_params; \
  225. static OSSL_FUNC_cipher_get_ctx_params_fn alg##_##lc##_get_ctx_params; \
  226. static OSSL_FUNC_cipher_gettable_ctx_params_fn \
  227. alg##_##lc##_gettable_ctx_params; \
  228. static OSSL_FUNC_cipher_set_ctx_params_fn alg##_##lc##_set_ctx_params; \
  229. static OSSL_FUNC_cipher_settable_ctx_params_fn \
  230. alg##_##lc##_settable_ctx_params; \
  231. static int alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[]) \
  232. { \
  233. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  234. flags, 2*kbits, blkbits, ivbits); \
  235. } \
  236. static void *alg##kbits##lc##_newctx(void *provctx) \
  237. { \
  238. return alg##_##lc##_newctx(provctx, 2*kbits, EVP_CIPH_##UCMODE##_MODE, \
  239. flags); \
  240. } \
  241. const OSSL_DISPATCH ossl_##alg##kbits##lc##_functions[] = { \
  242. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))alg##kbits##lc##_newctx }, \
  243. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_##lc##_freectx }, \
  244. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) lc##_dupctx }, \
  245. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) lc##_einit }, \
  246. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) lc##_dinit }, \
  247. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void)) lc##_stream_update }, \
  248. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void)) lc##_stream_final }, \
  249. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void)) lc##_cipher }, \
  250. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  251. (void (*)(void)) alg##_##kbits##_##lc##_get_params }, \
  252. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  253. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  254. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  255. (void (*)(void)) alg##_##lc##_get_ctx_params }, \
  256. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  257. (void (*)(void)) alg##_##lc##_gettable_ctx_params }, \
  258. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  259. (void (*)(void)) alg##_##lc##_set_ctx_params }, \
  260. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  261. (void (*)(void)) alg##_##lc##_settable_ctx_params }, \
  262. OSSL_DISPATCH_END \
  263. };
  264. IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 128, 8, 0)
  265. IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 192, 8, 0)
  266. IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 256, 8, 0)