chacha.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* chacha.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. DESCRIPTION
  23. This library contains implementation for the ChaCha20 stream cipher.
  24. */
  25. /*!
  26. \file wolfssl/wolfcrypt/chacha.h
  27. */
  28. #ifndef WOLF_CRYPT_CHACHA_H
  29. #define WOLF_CRYPT_CHACHA_H
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #ifdef HAVE_CHACHA
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /*
  36. Initialization vector starts at 13 with zero being the index origin of a matrix.
  37. Block counter is located at index 12.
  38. 0 1 2 3
  39. 4 5 6 7
  40. 8 9 10 11
  41. 12 13 14 15
  42. */
  43. #define CHACHA_MATRIX_CNT_IV 12
  44. /* Size of the IV */
  45. #define CHACHA_IV_WORDS 3
  46. /* Size of IV in bytes*/
  47. #define CHACHA_IV_BYTES 12
  48. #ifdef HAVE_XCHACHA
  49. #define XCHACHA_NONCE_BYTES 24
  50. #endif
  51. /* Size of ChaCha chunks */
  52. #define CHACHA_CHUNK_WORDS 16
  53. #define CHACHA_CHUNK_BYTES (CHACHA_CHUNK_WORDS * sizeof(word32))
  54. #ifdef WOLFSSL_X86_64_BUILD
  55. #if defined(USE_INTEL_SPEEDUP) && !defined(NO_CHACHA_ASM)
  56. #define USE_INTEL_CHACHA_SPEEDUP
  57. #define HAVE_INTEL_AVX1
  58. #endif
  59. #endif
  60. enum {
  61. CHACHA_ENC_TYPE = WC_CIPHER_CHACHA, /* cipher unique type */
  62. CHACHA_MAX_KEY_SZ = 32,
  63. };
  64. typedef struct ChaCha {
  65. word32 X[CHACHA_CHUNK_WORDS]; /* state of cipher */
  66. #ifdef HAVE_INTEL_AVX1
  67. /* vpshufd reads 16 bytes but we only use bottom 4. */
  68. byte extra[12];
  69. #endif
  70. word32 left; /* number of bytes leftover */
  71. #if defined(USE_INTEL_CHACHA_SPEEDUP) || defined(WOLFSSL_ARMASM)
  72. word32 over[CHACHA_CHUNK_WORDS];
  73. #endif
  74. } ChaCha;
  75. /**
  76. * IV(nonce) changes with each record
  77. * counter is for what value the block counter should start ... usually 0
  78. */
  79. WOLFSSL_API int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter);
  80. WOLFSSL_API int wc_Chacha_Process(ChaCha* ctx, byte* cipher, const byte* plain,
  81. word32 msglen);
  82. WOLFSSL_LOCAL void wc_Chacha_purge_current_block(ChaCha* ctx);
  83. WOLFSSL_API int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz);
  84. #ifdef HAVE_XCHACHA
  85. WOLFSSL_API int wc_XChacha_SetKey(ChaCha *ctx, const byte *key, word32 keySz,
  86. const byte *nonce, word32 nonceSz,
  87. word32 counter);
  88. #endif
  89. #ifdef __cplusplus
  90. } /* extern "C" */
  91. #endif
  92. #endif /* HAVE_CHACHA */
  93. #endif /* WOLF_CRYPT_CHACHA_H */