cipher_chacha20.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 cipher */
  10. #include <openssl/proverr.h>
  11. #include "cipher_chacha20.h"
  12. #include "prov/implementations.h"
  13. #include "prov/providercommon.h"
  14. #define CHACHA20_KEYLEN (CHACHA_KEY_SIZE)
  15. #define CHACHA20_BLKLEN (1)
  16. #define CHACHA20_IVLEN (CHACHA_CTR_SIZE)
  17. #define CHACHA20_FLAGS (PROV_CIPHER_FLAG_CUSTOM_IV)
  18. static OSSL_FUNC_cipher_newctx_fn chacha20_newctx;
  19. static OSSL_FUNC_cipher_freectx_fn chacha20_freectx;
  20. static OSSL_FUNC_cipher_dupctx_fn chacha20_dupctx;
  21. static OSSL_FUNC_cipher_get_params_fn chacha20_get_params;
  22. static OSSL_FUNC_cipher_get_ctx_params_fn chacha20_get_ctx_params;
  23. static OSSL_FUNC_cipher_set_ctx_params_fn chacha20_set_ctx_params;
  24. static OSSL_FUNC_cipher_gettable_ctx_params_fn chacha20_gettable_ctx_params;
  25. static OSSL_FUNC_cipher_settable_ctx_params_fn chacha20_settable_ctx_params;
  26. #define chacha20_cipher ossl_cipher_generic_cipher
  27. #define chacha20_update ossl_cipher_generic_stream_update
  28. #define chacha20_final ossl_cipher_generic_stream_final
  29. #define chacha20_gettable_params ossl_cipher_generic_gettable_params
  30. void ossl_chacha20_initctx(PROV_CHACHA20_CTX *ctx)
  31. {
  32. ossl_cipher_generic_initkey(ctx, CHACHA20_KEYLEN * 8,
  33. CHACHA20_BLKLEN * 8,
  34. CHACHA20_IVLEN * 8,
  35. 0, CHACHA20_FLAGS,
  36. ossl_prov_cipher_hw_chacha20(CHACHA20_KEYLEN * 8),
  37. NULL);
  38. }
  39. static void *chacha20_newctx(void *provctx)
  40. {
  41. PROV_CHACHA20_CTX *ctx;
  42. if (!ossl_prov_is_running())
  43. return NULL;
  44. ctx = OPENSSL_zalloc(sizeof(*ctx));
  45. if (ctx != NULL)
  46. ossl_chacha20_initctx(ctx);
  47. return ctx;
  48. }
  49. static void chacha20_freectx(void *vctx)
  50. {
  51. PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
  52. if (ctx != NULL) {
  53. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  54. OPENSSL_clear_free(ctx, sizeof(*ctx));
  55. }
  56. }
  57. static void *chacha20_dupctx(void *vctx)
  58. {
  59. PROV_CHACHA20_CTX *ctx = (PROV_CHACHA20_CTX *)vctx;
  60. PROV_CHACHA20_CTX *dupctx = NULL;
  61. if (ctx != NULL) {
  62. dupctx = OPENSSL_memdup(ctx, sizeof(*dupctx));
  63. if (dupctx != NULL && dupctx->base.tlsmac != NULL && dupctx->base.alloced) {
  64. dupctx->base.tlsmac = OPENSSL_memdup(dupctx->base.tlsmac,
  65. dupctx->base.tlsmacsize);
  66. if (dupctx->base.tlsmac == NULL) {
  67. OPENSSL_free(dupctx);
  68. dupctx = NULL;
  69. }
  70. }
  71. }
  72. return dupctx;
  73. }
  74. static int chacha20_get_params(OSSL_PARAM params[])
  75. {
  76. return ossl_cipher_generic_get_params(params, 0, CHACHA20_FLAGS,
  77. CHACHA20_KEYLEN * 8,
  78. CHACHA20_BLKLEN * 8,
  79. CHACHA20_IVLEN * 8);
  80. }
  81. static int chacha20_get_ctx_params(void *vctx, OSSL_PARAM params[])
  82. {
  83. OSSL_PARAM *p;
  84. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
  85. if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_IVLEN)) {
  86. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  87. return 0;
  88. }
  89. p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
  90. if (p != NULL && !OSSL_PARAM_set_size_t(p, CHACHA20_KEYLEN)) {
  91. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  92. return 0;
  93. }
  94. return 1;
  95. }
  96. static const OSSL_PARAM chacha20_known_gettable_ctx_params[] = {
  97. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  98. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  99. OSSL_PARAM_END
  100. };
  101. const OSSL_PARAM *chacha20_gettable_ctx_params(ossl_unused void *cctx,
  102. ossl_unused void *provctx)
  103. {
  104. return chacha20_known_gettable_ctx_params;
  105. }
  106. static int chacha20_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  107. {
  108. const OSSL_PARAM *p;
  109. size_t len;
  110. if (params == NULL)
  111. return 1;
  112. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  113. if (p != NULL) {
  114. if (!OSSL_PARAM_get_size_t(p, &len)) {
  115. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  116. return 0;
  117. }
  118. if (len != CHACHA20_KEYLEN) {
  119. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  120. return 0;
  121. }
  122. }
  123. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
  124. if (p != NULL) {
  125. if (!OSSL_PARAM_get_size_t(p, &len)) {
  126. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  127. return 0;
  128. }
  129. if (len != CHACHA20_IVLEN) {
  130. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_IV_LENGTH);
  131. return 0;
  132. }
  133. }
  134. return 1;
  135. }
  136. static const OSSL_PARAM chacha20_known_settable_ctx_params[] = {
  137. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  138. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
  139. OSSL_PARAM_END
  140. };
  141. const OSSL_PARAM *chacha20_settable_ctx_params(ossl_unused void *cctx,
  142. ossl_unused void *provctx)
  143. {
  144. return chacha20_known_settable_ctx_params;
  145. }
  146. int ossl_chacha20_einit(void *vctx, const unsigned char *key, size_t keylen,
  147. const unsigned char *iv, size_t ivlen,
  148. const OSSL_PARAM params[])
  149. {
  150. int ret;
  151. /* The generic function checks for ossl_prov_is_running() */
  152. ret = ossl_cipher_generic_einit(vctx, key, keylen, iv, ivlen, NULL);
  153. if (ret && iv != NULL) {
  154. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  155. PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
  156. hw->initiv(ctx);
  157. }
  158. if (ret && !chacha20_set_ctx_params(vctx, params))
  159. ret = 0;
  160. return ret;
  161. }
  162. int ossl_chacha20_dinit(void *vctx, const unsigned char *key, size_t keylen,
  163. const unsigned char *iv, size_t ivlen,
  164. const OSSL_PARAM params[])
  165. {
  166. int ret;
  167. /* The generic function checks for ossl_prov_is_running() */
  168. ret = ossl_cipher_generic_dinit(vctx, key, keylen, iv, ivlen, NULL);
  169. if (ret && iv != NULL) {
  170. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  171. PROV_CIPHER_HW_CHACHA20 *hw = (PROV_CIPHER_HW_CHACHA20 *)ctx->hw;
  172. hw->initiv(ctx);
  173. }
  174. if (ret && !chacha20_set_ctx_params(vctx, params))
  175. ret = 0;
  176. return ret;
  177. }
  178. /* ossl_chacha20_functions */
  179. const OSSL_DISPATCH ossl_chacha20_functions[] = {
  180. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))chacha20_newctx },
  181. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))chacha20_freectx },
  182. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))chacha20_dupctx },
  183. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_chacha20_einit },
  184. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_chacha20_dinit },
  185. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))chacha20_update },
  186. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))chacha20_final },
  187. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))chacha20_cipher},
  188. { OSSL_FUNC_CIPHER_GET_PARAMS, (void (*)(void))chacha20_get_params },
  189. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (void (*)(void))chacha20_gettable_params },
  190. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (void (*)(void))chacha20_get_ctx_params },
  191. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS,
  192. (void (*)(void))chacha20_gettable_ctx_params },
  193. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (void (*)(void))chacha20_set_ctx_params },
  194. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS,
  195. (void (*)(void))chacha20_settable_ctx_params },
  196. OSSL_DISPATCH_END
  197. };