chacha.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <asm/unaligned.h>
  17. #include <linux/types.h>
  18. /* 32-bit stream position, then 96-bit nonce (RFC7539 convention) */
  19. #define CHACHA_IV_SIZE 16
  20. #define CHACHA_KEY_SIZE 32
  21. #define CHACHA_BLOCK_SIZE 64
  22. #define CHACHAPOLY_IV_SIZE 12
  23. #define CHACHA_STATE_WORDS (CHACHA_BLOCK_SIZE / sizeof(u32))
  24. /* 192-bit nonce, then 64-bit stream position */
  25. #define XCHACHA_IV_SIZE 32
  26. void chacha_block_generic(u32 *state, u8 *stream, int nrounds);
  27. static inline void chacha20_block(u32 *state, u8 *stream)
  28. {
  29. chacha_block_generic(state, stream, 20);
  30. }
  31. void hchacha_block_arch(const u32 *state, u32 *out, int nrounds);
  32. void hchacha_block_generic(const u32 *state, u32 *out, int nrounds);
  33. static inline void hchacha_block(const u32 *state, u32 *out, int nrounds)
  34. {
  35. if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
  36. hchacha_block_arch(state, out, nrounds);
  37. else
  38. hchacha_block_generic(state, out, nrounds);
  39. }
  40. enum chacha_constants { /* expand 32-byte k */
  41. CHACHA_CONSTANT_EXPA = 0x61707865U,
  42. CHACHA_CONSTANT_ND_3 = 0x3320646eU,
  43. CHACHA_CONSTANT_2_BY = 0x79622d32U,
  44. CHACHA_CONSTANT_TE_K = 0x6b206574U
  45. };
  46. static inline void chacha_init_consts(u32 *state)
  47. {
  48. state[0] = CHACHA_CONSTANT_EXPA;
  49. state[1] = CHACHA_CONSTANT_ND_3;
  50. state[2] = CHACHA_CONSTANT_2_BY;
  51. state[3] = CHACHA_CONSTANT_TE_K;
  52. }
  53. void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv);
  54. static inline void chacha_init_generic(u32 *state, const u32 *key, const u8 *iv)
  55. {
  56. chacha_init_consts(state);
  57. state[4] = key[0];
  58. state[5] = key[1];
  59. state[6] = key[2];
  60. state[7] = key[3];
  61. state[8] = key[4];
  62. state[9] = key[5];
  63. state[10] = key[6];
  64. state[11] = key[7];
  65. state[12] = get_unaligned_le32(iv + 0);
  66. state[13] = get_unaligned_le32(iv + 4);
  67. state[14] = get_unaligned_le32(iv + 8);
  68. state[15] = get_unaligned_le32(iv + 12);
  69. }
  70. static inline void chacha_init(u32 *state, const u32 *key, const u8 *iv)
  71. {
  72. if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
  73. chacha_init_arch(state, key, iv);
  74. else
  75. chacha_init_generic(state, key, iv);
  76. }
  77. void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
  78. unsigned int bytes, int nrounds);
  79. void chacha_crypt_generic(u32 *state, u8 *dst, const u8 *src,
  80. unsigned int bytes, int nrounds);
  81. static inline void chacha_crypt(u32 *state, u8 *dst, const u8 *src,
  82. unsigned int bytes, int nrounds)
  83. {
  84. if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CHACHA))
  85. chacha_crypt_arch(state, dst, src, bytes, nrounds);
  86. else
  87. chacha_crypt_generic(state, dst, src, bytes, nrounds);
  88. }
  89. static inline void chacha20_crypt(u32 *state, u8 *dst, const u8 *src,
  90. unsigned int bytes)
  91. {
  92. chacha_crypt(state, dst, src, bytes, 20);
  93. }
  94. #endif /* _CRYPTO_CHACHA_H */