chacha.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Common values and helper functions for the ChaCha and XChaCha stream ciphers.
  4. *
  5. * XChaCha extends ChaCha's nonce to 192 bits, while provably retaining ChaCha's
  6. * security. Here they share the same key size, tfm context, and setkey
  7. * function; only their IV size and encrypt/decrypt function differ.
  8. *
  9. * The ChaCha paper specifies 20, 12, and 8-round variants. In general, it is
  10. * recommended to use the 20-round variant ChaCha20. However, the other
  11. * variants can be needed in some performance-sensitive scenarios. The generic
  12. * ChaCha code currently allows only the 20 and 12-round variants.
  13. */
  14. #ifndef _CRYPTO_CHACHA_H
  15. #define _CRYPTO_CHACHA_H
  16. #include <linux/unaligned.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. /* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
  20. #define CHACHA_IV_SIZE 16
  21. #define CHACHA_KEY_SIZE 32
  22. #define CHACHA_BLOCK_SIZE 64
  23. #define CHACHAPOLY_IV_SIZE 12
  24. #define CHACHA_KEY_WORDS 8
  25. #define CHACHA_STATE_WORDS 16
  26. #define HCHACHA_OUT_WORDS 8
  27. /* 192-bit nonce, then 64-bit stream position */
  28. #define XCHACHA_IV_SIZE 32
  29. struct chacha_state {
  30. u32 x[CHACHA_STATE_WORDS];
  31. };
  32. void chacha_block_generic(struct chacha_state *state,
  33. u8 out[at_least CHACHA_BLOCK_SIZE], int nrounds);
  34. static inline void chacha20_block(struct chacha_state *state,
  35. u8 out[at_least CHACHA_BLOCK_SIZE])
  36. {
  37. chacha_block_generic(state, out, 20);
  38. }
  39. void hchacha_block_generic(const struct chacha_state *state,
  40. u32 out[at_least HCHACHA_OUT_WORDS], int nrounds);
  41. void hchacha_block(const struct chacha_state *state,
  42. u32 out[at_least HCHACHA_OUT_WORDS], int nrounds);
  43. enum chacha_constants { /* expand 32-byte k */
  44. CHACHA_CONSTANT_EXPA = 0x61707865U,
  45. CHACHA_CONSTANT_ND_3 = 0x3320646eU,
  46. CHACHA_CONSTANT_2_BY = 0x79622d32U,
  47. CHACHA_CONSTANT_TE_K = 0x6b206574U
  48. };
  49. static inline void chacha_init_consts(struct chacha_state *state)
  50. {
  51. state->x[0] = CHACHA_CONSTANT_EXPA;
  52. state->x[1] = CHACHA_CONSTANT_ND_3;
  53. state->x[2] = CHACHA_CONSTANT_2_BY;
  54. state->x[3] = CHACHA_CONSTANT_TE_K;
  55. }
  56. static inline void chacha_init(struct chacha_state *state,
  57. const u32 key[at_least CHACHA_KEY_WORDS],
  58. const u8 iv[at_least CHACHA_IV_SIZE])
  59. {
  60. chacha_init_consts(state);
  61. state->x[4] = key[0];
  62. state->x[5] = key[1];
  63. state->x[6] = key[2];
  64. state->x[7] = key[3];
  65. state->x[8] = key[4];
  66. state->x[9] = key[5];
  67. state->x[10] = key[6];
  68. state->x[11] = key[7];
  69. state->x[12] = get_unaligned_le32(iv + 0);
  70. state->x[13] = get_unaligned_le32(iv + 4);
  71. state->x[14] = get_unaligned_le32(iv + 8);
  72. state->x[15] = get_unaligned_le32(iv + 12);
  73. }
  74. void chacha_crypt(struct chacha_state *state, u8 *dst, const u8 *src,
  75. unsigned int bytes, int nrounds);
  76. static inline void chacha20_crypt(struct chacha_state *state,
  77. u8 *dst, const u8 *src, unsigned int bytes)
  78. {
  79. chacha_crypt(state, dst, src, bytes, 20);
  80. }
  81. static inline void chacha_zeroize_state(struct chacha_state *state)
  82. {
  83. memzero_explicit(state, sizeof(*state));
  84. }
  85. #endif /* _CRYPTO_CHACHA_H */