cipher_chacha20_poly1305.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 chacha20_poly1305 cipher */
  10. #include <openssl/proverr.h>
  11. #include "cipher_chacha20_poly1305.h"
  12. #include "prov/implementations.h"
  13. #include "prov/providercommon.h"
  14. #define CHACHA20_POLY1305_KEYLEN CHACHA_KEY_SIZE
  15. #define CHACHA20_POLY1305_BLKLEN 1
  16. #define CHACHA20_POLY1305_MAX_IVLEN 12
  17. #define CHACHA20_POLY1305_MODE 0
  18. #define CHACHA20_POLY1305_FLAGS (PROV_CIPHER_FLAG_AEAD \
  19. | PROV_CIPHER_FLAG_CUSTOM_IV)
  20. static OSSL_FUNC_cipher_newctx_fn chacha20_poly1305_newctx;
  21. static OSSL_FUNC_cipher_freectx_fn chacha20_poly1305_freectx;
  22. static OSSL_FUNC_cipher_dupctx_fn chacha20_poly1305_dupctx;
  23. static OSSL_FUNC_cipher_encrypt_init_fn chacha20_poly1305_einit;
  24. static OSSL_FUNC_cipher_decrypt_init_fn chacha20_poly1305_dinit;
  25. static OSSL_FUNC_cipher_get_params_fn chacha20_poly1305_get_params;
  26. static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_poly1305_get_ctx_params;
  27. static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_poly1305_set_ctx_params;
  28. static OSSL_FUNC_cipher_cipher_fn chacha20_poly1305_cipher;
  29. static OSSL_FUNC_cipher_final_fn chacha20_poly1305_final;
  30. static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_poly1305_gettable_ctx_params;
  31. #define chacha20_poly1305_settable_ctx_params ossl_cipher_aead_settable_ctx_params
  32. #define chacha20_poly1305_gettable_params ossl_cipher_generic_gettable_params
  33. #define chacha20_poly1305_update chacha20_poly1305_cipher
  34. static void *chacha20_poly1305_newctx(void *provctx)
  35. {
  36. PROV_CHACHA20_POLY1305_CTX *ctx;
  37. if (!ossl_prov_is_running())
  38. return NULL;
  39. ctx = OPENSSL_zalloc(sizeof(*ctx));
  40. if (ctx != NULL) {
  41. ossl_cipher_generic_initkey(&ctx->base, CHACHA20_POLY1305_KEYLEN * 8,
  42. CHACHA20_POLY1305_BLKLEN * 8,
  43. CHACHA20_POLY1305_IVLEN * 8,
  44. CHACHA20_POLY1305_MODE,
  45. CHACHA20_POLY1305_FLAGS,
  46. ossl_prov_cipher_hw_chacha20_poly1305(
  47. CHACHA20_POLY1305_KEYLEN * 8),
  48. NULL);
  49. ctx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;
  50. ossl_chacha20_initctx(&ctx->chacha);
  51. }
  52. return ctx;
  53. }
  54. static void *chacha20_poly1305_dupctx(void *provctx)
  55. {
  56. PROV_CHACHA20_POLY1305_CTX *ctx = provctx;
  57. PROV_CHACHA20_POLY1305_CTX *dctx = NULL;
  58. if (ctx == NULL)
  59. return NULL;
  60. dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
  61. if (dctx != NULL && dctx->base.tlsmac != NULL && dctx->base.alloced) {
  62. dctx->base.tlsmac = OPENSSL_memdup(dctx->base.tlsmac,
  63. dctx->base.tlsmacsize);
  64. if (dctx->base.tlsmac == NULL) {
  65. OPENSSL_free(dctx);
  66. dctx = NULL;
  67. }
  68. }
  69. return dctx;
  70. }
  71. static void chacha20_poly1305_freectx(void *vctx)
  72. {
  73. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
  74. if (ctx != NULL) {
  75. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  76. OPENSSL_clear_free(ctx, sizeof(*ctx));
  77. }
  78. }
  79. static int chacha20_poly1305_get_params(OSSL_PARAM params[])
  80. {
  81. return ossl_cipher_generic_get_params(params, 0, CHACHA20_POLY1305_FLAGS,
  82. CHACHA20_POLY1305_KEYLEN * 8,
  83. CHACHA20_POLY1305_BLKLEN * 8,
  84. CHACHA20_POLY1305_IVLEN * 8);
  85. }
  86. static int chacha20_poly1305_get_ctx_params(void *vctx, OSSL_PARAM params[])
  87. {
  88. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
  89. OSSL_PARAM *p;
  90. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  91. if (p != NULL) {
  92. if (!OSSL_PARAM_set_size_t(p, CHACHA20_POLY1305_IVLEN)) {
  93. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  94. return 0;
  95. }
  96. }
  97. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  98. if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_POLY1305_KEYLEN)) {
  99. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  100. return 0;
  101. }
  102. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
  103. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tag_len)) {
  104. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  105. return 0;
  106. }
  107. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
  108. if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tls_aad_pad_sz)) {
  109. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  110. return 0;
  111. }
  112. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  113. if (p != NULL) {
  114. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  115. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  116. return 0;
  117. }
  118. if (!ctx->base.enc) {
  119. ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_SET);
  120. return 0;
  121. }
  122. if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) {
  123. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
  124. return 0;
  125. }
  126. memcpy(p->data, ctx->tag, p->data_size);
  127. }
  128. return 1;
  129. }
  130. static const OSSL_PARAM chacha20_poly1305_known_gettable_ctx_params[] = {
  131. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  132. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  133. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
  134. OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
  135. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
  136. OSSL_PARAM_END
  137. };
  138. static const OSSL_PARAM *chacha20_poly1305_gettable_ctx_params
  139. (ossl_unused void *cctx, ossl_unused void *provctx)
  140. {
  141. return chacha20_poly1305_known_gettable_ctx_params;
  142. }
  143. static int chacha20_poly1305_set_ctx_params(void *vctx,
  144. const OSSL_PARAM params[])
  145. {
  146. const OSSL_PARAM *p;
  147. size_t len;
  148. PROV_CHACHA20_POLY1305_CTX *ctx = (PROV_CHACHA20_POLY1305_CTX *)vctx;
  149. PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
  150. (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->base.hw;
  151. if (params == NULL)
  152. return 1;
  153. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  154. if (p != NULL) {
  155. if (!OSSL_PARAM_get_size_t(p, &len)) {
  156. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  157. return 0;
  158. }
  159. if (len != CHACHA20_POLY1305_KEYLEN) {
  160. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  161. return 0;
  162. }
  163. }
  164. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
  165. if (p != NULL) {
  166. if (!OSSL_PARAM_get_size_t(p, &len)) {
  167. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  168. return 0;
  169. }
  170. if (len != CHACHA20_POLY1305_MAX_IVLEN) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  172. return 0;
  173. }
  174. }
  175. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
  176. if (p != NULL) {
  177. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  178. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  179. return 0;
  180. }
  181. if (p->data_size == 0 || p->data_size > POLY1305_BLOCK_SIZE) {
  182. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_TAG_LENGTH);
  183. return 0;
  184. }
  185. if (p->data != NULL) {
  186. if (ctx->base.enc) {
  187. ERR_raise(ERR_LIB_PROV, PROV_R_TAG_NOT_NEEDED);
  188. return 0;
  189. }
  190. memcpy(ctx->tag, p->data, p->data_size);
  191. }
  192. ctx->tag_len = p->data_size;
  193. }
  194. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
  195. if (p != NULL) {
  196. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  197. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  198. return 0;
  199. }
  200. len = hw->tls_init(&ctx->base, p->data, p->data_size);
  201. if (len == 0) {
  202. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DATA);
  203. return 0;
  204. }
  205. ctx->tls_aad_pad_sz = len;
  206. }
  207. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
  208. if (p != NULL) {
  209. if (p->data_type != OSSL_PARAM_OCTET_STRING) {
  210. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  211. return 0;
  212. }
  213. if (hw->tls_iv_set_fixed(&ctx->base, p->data, p->data_size) == 0) {
  214. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  215. return 0;
  216. }
  217. }
  218. /* ignore OSSL_CIPHER_PARAM_AEAD_MAC_KEY */
  219. return 1;
  220. }
  221. static int chacha20_poly1305_einit(void *vctx, const unsigned char *key,
  222. size_t keylen, const unsigned char *iv,
  223. size_t ivlen, const OSSL_PARAM params[])
  224. {
  225. int ret;
  226. /* The generic function checks for ossl_prov_is_running() */
  227. ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
  228. if (ret && iv != NULL) {
  229. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  230. PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
  231. (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
  232. hw->initiv(ctx);
  233. }
  234. if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
  235. ret = 0;
  236. return ret;
  237. }
  238. static int chacha20_poly1305_dinit(void *vctx, const unsigned char *key,
  239. size_t keylen, const unsigned char *iv,
  240. size_t ivlen, const OSSL_PARAM params[])
  241. {
  242. int ret;
  243. /* The generic function checks for ossl_prov_is_running() */
  244. ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
  245. if (ret && iv != NULL) {
  246. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  247. PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
  248. (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
  249. hw->initiv(ctx);
  250. }
  251. if (ret && !chacha20_poly1305_set_ctx_params(vctx, params))
  252. ret = 0;
  253. return ret;
  254. }
  255. static int chacha20_poly1305_cipher(void *vctx, unsigned char *out,
  256. size_t *outl, size_t outsize,
  257. const unsigned char *in, size_t inl)
  258. {
  259. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  260. PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
  261. (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
  262. if (!ossl_prov_is_running())
  263. return 0;
  264. if (inl == 0) {
  265. *outl = 0;
  266. return 1;
  267. }
  268. if (outsize < inl) {
  269. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  270. return 0;
  271. }
  272. if (!hw->aead_cipher(ctx, out, outl, in, inl))
  273. return 0;
  274. return 1;
  275. }
  276. static int chacha20_poly1305_final(void *vctx, unsigned char *out, size_t *outl,
  277. size_t outsize)
  278. {
  279. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  280. PROV_CIPHER_HW_CHACHA20_POLY1305 *hw =
  281. (PROV_CIPHER_HW_CHACHA20_POLY1305 *)ctx->hw;
  282. if (!ossl_prov_is_running())
  283. return 0;
  284. if (hw->aead_cipher(ctx, out, outl, NULL, 0) <= 0)
  285. return 0;
  286. *outl = 0;
  287. return 1;
  288. }
  289. /* ossl_chacha20_ossl_poly1305_functions */
  290. const OSSL_DISPATCH ossl_chacha20_ossl_poly1305_functions[] = {
  291. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_poly1305_newctx },
  292. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_poly1305_freectx },
  293. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_poly1305_dupctx },
  294. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))chacha20_poly1305_einit },
  295. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))chacha20_poly1305_dinit },
  296. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_poly1305_update },
  297. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_poly1305_final },
  298. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_poly1305_cipher },
  299. { OSSL_FUNC_CIPHER_GET_PARAMS,
  300. (void (*)(void))chacha20_poly1305_get_params },
  301. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS,
  302. (void (*)(void))chacha20_poly1305_gettable_params },
  303. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS,
  304. (void (*)(void))chacha20_poly1305_get_ctx_params },
  305. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
  306. (void (*)(void))chacha20_poly1305_gettable_ctx_params },
  307. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS,
  308. (void (*)(void))chacha20_poly1305_set_ctx_params },
  309. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
  310. (void (*)(void))chacha20_poly1305_settable_ctx_params },
  311. OSSL_DISPATCH_END
  312. };