cipher_aes_siv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright 2019-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. /* 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. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  57. return NULL;
  58. }
  59. if (!in->hw->dupctx(in, ret)) {
  60. OPENSSL_free(ret);
  61. ret = NULL;
  62. }
  63. return ret;
  64. }
  65. static int siv_init(void *vctx, const unsigned char *key, size_t keylen,
  66. const unsigned char *iv, size_t ivlen,
  67. const OSSL_PARAM params[], int enc)
  68. {
  69. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  70. if (!ossl_prov_is_running())
  71. return 0;
  72. ctx->enc = enc;
  73. if (key != NULL) {
  74. if (keylen != ctx->keylen) {
  75. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  76. return 0;
  77. }
  78. if (!ctx->hw->initkey(ctx, key, ctx->keylen))
  79. return 0;
  80. }
  81. return aes_siv_set_ctx_params(ctx, params);
  82. }
  83. static int siv_einit(void *vctx, const unsigned char *key, size_t keylen,
  84. const unsigned char *iv, size_t ivlen,
  85. const OSSL_PARAM params[])
  86. {
  87. return siv_init(vctx, key, keylen, iv, ivlen, params, 1);
  88. }
  89. static int siv_dinit(void *vctx, const unsigned char *key, size_t keylen,
  90. const unsigned char *iv, size_t ivlen,
  91. const OSSL_PARAM params[])
  92. {
  93. return siv_init(vctx, key, keylen, iv, ivlen, params, 0);
  94. }
  95. static int siv_cipher(void *vctx, unsigned char *out, size_t *outl,
  96. size_t outsize, const unsigned char *in, size_t inl)
  97. {
  98. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  99. if (!ossl_prov_is_running())
  100. return 0;
  101. if (inl == 0) {
  102. *outl = 0;
  103. return 1;
  104. }
  105. if (outsize < inl) {
  106. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  107. return 0;
  108. }
  109. if (ctx->hw->cipher(ctx, out, in, inl) <= 0)
  110. return 0;
  111. if (outl != NULL)
  112. *outl = inl;
  113. return 1;
  114. }
  115. static int siv_stream_final(void *vctx, unsigned char *out, size_t *outl,
  116. size_t outsize)
  117. {
  118. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  119. if (!ossl_prov_is_running())
  120. return 0;
  121. if (!ctx->hw->cipher(vctx, out, NULL, 0))
  122. return 0;
  123. if (outl != NULL)
  124. *outl = 0;
  125. return 1;
  126. }
  127. static int aes_siv_get_ctx_params(void *vctx, OSSL_PARAM params[])
  128. {
  129. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  130. SIV128_CONTEXT *sctx = &ctx->siv;
  131. OSSL_PARAM *p;
  132. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  133. if (p != NULL && p->data_type == OSSL_PARAM_OCTET_STRING) {
  134. if (!ctx->enc
  135. || p->data_size != ctx->taglen
  136. || !OSSL_PARAM_set_octet_string(p, &sctx->tag.byte, ctx->taglen)) {
  137. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  138. return 0;
  139. }
  140. }
  141. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
  142. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->taglen)) {
  143. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  144. return 0;
  145. }
  146. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  147. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keylen)) {
  148. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static const OSSL_PARAM aes_siv_known_gettable_ctx_params[] = {
  154. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  155. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
  156. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_SPEED, NULL),
  157. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  158. OSSL_PARAM_END
  159. };
  160. static const OSSL_PARAM *aes_siv_gettable_ctx_params(ossl_unused void *cctx,
  161. ossl_unused void *provctx)
  162. {
  163. return aes_siv_known_gettable_ctx_params;
  164. }
  165. static int aes_siv_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  166. {
  167. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  168. const OSSL_PARAM *p;
  169. unsigned int speed = 0;
  170. if (params == NULL)
  171. return 1;
  172. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  173. if (p != NULL) {
  174. if (ctx->enc)
  175. return 1;
  176. if (p->data_type != OSSL_PARAM_OCTET_STRING
  177. || !ctx->hw->settag(ctx, p->data, p->data_size)) {
  178. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  179. return 0;
  180. }
  181. }
  182. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_SPEED);
  183. if (p != NULL) {
  184. if (!OSSL_PARAM_get_uint(p, &speed)) {
  185. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  186. return 0;
  187. }
  188. ctx->hw->setspeed(ctx, (int)speed);
  189. }
  190. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  191. if (p != NULL) {
  192. size_t keylen;
  193. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  194. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  195. return 0;
  196. }
  197. /* The key length can not be modified */
  198. if (keylen != ctx->keylen)
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. static const OSSL_PARAM aes_siv_known_settable_ctx_params[] = {
  204. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  205. OSSL_PARAM_uint(OSSL_CIPHER_PARAM_SPEED, NULL),
  206. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  207. OSSL_PARAM_END
  208. };
  209. static const OSSL_PARAM *aes_siv_settable_ctx_params(ossl_unused void *cctx,
  210. ossl_unused void *provctx)
  211. {
  212. return aes_siv_known_settable_ctx_params;
  213. }
  214. #define IMPLEMENT_cipher(alg, lc, UCMODE, flags, kbits, blkbits, ivbits) \
  215. static OSSL_FUNC_cipher_newctx_fn alg##kbits##lc##_newctx; \
  216. static OSSL_FUNC_cipher_freectx_fn alg##_##lc##_freectx; \
  217. static OSSL_FUNC_cipher_dupctx_fn lc##_dupctx; \
  218. static OSSL_FUNC_cipher_encrypt_init_fn lc##_einit; \
  219. static OSSL_FUNC_cipher_decrypt_init_fn lc##_dinit; \
  220. static OSSL_FUNC_cipher_update_fn lc##_stream_update; \
  221. static OSSL_FUNC_cipher_final_fn lc##_stream_final; \
  222. static OSSL_FUNC_cipher_cipher_fn lc##_cipher; \
  223. static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_##lc##_get_params; \
  224. static OSSL_FUNC_cipher_get_ctx_params_fn alg##_##lc##_get_ctx_params; \
  225. static OSSL_FUNC_cipher_gettable_ctx_params_fn \
  226. alg##_##lc##_gettable_ctx_params; \
  227. static OSSL_FUNC_cipher_set_ctx_params_fn alg##_##lc##_set_ctx_params; \
  228. static OSSL_FUNC_cipher_settable_ctx_params_fn \
  229. alg##_##lc##_settable_ctx_params; \
  230. static int alg##_##kbits##_##lc##_get_params(OSSL_PARAM params[]) \
  231. { \
  232. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  233. flags, 2*kbits, blkbits, ivbits); \
  234. } \
  235. static void * alg##kbits##lc##_newctx(void *provctx) \
  236. { \
  237. return alg##_##lc##_newctx(provctx, 2*kbits, EVP_CIPH_##UCMODE##_MODE, \
  238. flags); \
  239. } \
  240. const OSSL_DISPATCH ossl_##alg##kbits##lc##_functions[] = { \
  241. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))alg##kbits##lc##_newctx }, \
  242. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))alg##_##lc##_freectx }, \
  243. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) lc##_dupctx }, \
  244. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void)) lc##_einit }, \
  245. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void)) lc##_dinit }, \
  246. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void)) lc##_stream_update }, \
  247. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void)) lc##_stream_final }, \
  248. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void)) lc##_cipher }, \
  249. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  250. (void (*)(void)) alg##_##kbits##_##lc##_get_params }, \
  251. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  252. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  253. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  254. (void (*)(void)) alg##_##lc##_get_ctx_params }, \
  255. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  256. (void (*)(void)) alg##_##lc##_gettable_ctx_params }, \
  257. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  258. (void (*)(void)) alg##_##lc##_set_ctx_params }, \
  259. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  260. (void (*)(void)) alg##_##lc##_settable_ctx_params }, \
  261. { 0, NULL } \
  262. };
  263. IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 128, 8, 0)
  264. IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 192, 8, 0)
  265. IMPLEMENT_cipher(aes, siv, SIV, SIV_FLAGS, 256, 8, 0)