cipher_aes_siv.c 11 KB

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