cipher_chacha20.c 7.0 KB

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