cipher_chacha20_poly1305.c 12 KB

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