chacha.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * The "hash function" used as the core of the ChaCha stream cipher (RFC7539)
  3. *
  4. * Copyright (C) 2015 Martin Willi
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/export.h>
  13. #include <linux/bitops.h>
  14. #include <linux/cryptohash.h>
  15. #include <asm/unaligned.h>
  16. #include <crypto/chacha.h>
  17. static void chacha_permute(u32 *x, int nrounds)
  18. {
  19. int i;
  20. /* whitelist the allowed round counts */
  21. WARN_ON_ONCE(nrounds != 20 && nrounds != 12);
  22. for (i = 0; i < nrounds; i += 2) {
  23. x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 16);
  24. x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 16);
  25. x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 16);
  26. x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 16);
  27. x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 12);
  28. x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 12);
  29. x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 12);
  30. x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 12);
  31. x[0] += x[4]; x[12] = rol32(x[12] ^ x[0], 8);
  32. x[1] += x[5]; x[13] = rol32(x[13] ^ x[1], 8);
  33. x[2] += x[6]; x[14] = rol32(x[14] ^ x[2], 8);
  34. x[3] += x[7]; x[15] = rol32(x[15] ^ x[3], 8);
  35. x[8] += x[12]; x[4] = rol32(x[4] ^ x[8], 7);
  36. x[9] += x[13]; x[5] = rol32(x[5] ^ x[9], 7);
  37. x[10] += x[14]; x[6] = rol32(x[6] ^ x[10], 7);
  38. x[11] += x[15]; x[7] = rol32(x[7] ^ x[11], 7);
  39. x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 16);
  40. x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 16);
  41. x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 16);
  42. x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 16);
  43. x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 12);
  44. x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 12);
  45. x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 12);
  46. x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 12);
  47. x[0] += x[5]; x[15] = rol32(x[15] ^ x[0], 8);
  48. x[1] += x[6]; x[12] = rol32(x[12] ^ x[1], 8);
  49. x[2] += x[7]; x[13] = rol32(x[13] ^ x[2], 8);
  50. x[3] += x[4]; x[14] = rol32(x[14] ^ x[3], 8);
  51. x[10] += x[15]; x[5] = rol32(x[5] ^ x[10], 7);
  52. x[11] += x[12]; x[6] = rol32(x[6] ^ x[11], 7);
  53. x[8] += x[13]; x[7] = rol32(x[7] ^ x[8], 7);
  54. x[9] += x[14]; x[4] = rol32(x[4] ^ x[9], 7);
  55. }
  56. }
  57. /**
  58. * chacha_block - generate one keystream block and increment block counter
  59. * @state: input state matrix (16 32-bit words)
  60. * @stream: output keystream block (64 bytes)
  61. * @nrounds: number of rounds (20 or 12; 20 is recommended)
  62. *
  63. * This is the ChaCha core, a function from 64-byte strings to 64-byte strings.
  64. * The caller has already converted the endianness of the input. This function
  65. * also handles incrementing the block counter in the input matrix.
  66. */
  67. void chacha_block(u32 *state, u8 *stream, int nrounds)
  68. {
  69. u32 x[16];
  70. int i;
  71. memcpy(x, state, 64);
  72. chacha_permute(x, nrounds);
  73. for (i = 0; i < ARRAY_SIZE(x); i++)
  74. put_unaligned_le32(x[i] + state[i], &stream[i * sizeof(u32)]);
  75. state[12]++;
  76. }
  77. EXPORT_SYMBOL(chacha_block);
  78. /**
  79. * hchacha_block - abbreviated ChaCha core, for XChaCha
  80. * @in: input state matrix (16 32-bit words)
  81. * @out: output (8 32-bit words)
  82. * @nrounds: number of rounds (20 or 12; 20 is recommended)
  83. *
  84. * HChaCha is the ChaCha equivalent of HSalsa and is an intermediate step
  85. * towards XChaCha (see https://cr.yp.to/snuffle/xsalsa-20081128.pdf). HChaCha
  86. * skips the final addition of the initial state, and outputs only certain words
  87. * of the state. It should not be used for streaming directly.
  88. */
  89. void hchacha_block(const u32 *in, u32 *out, int nrounds)
  90. {
  91. u32 x[16];
  92. memcpy(x, in, 64);
  93. chacha_permute(x, nrounds);
  94. memcpy(&out[0], &x[0], 16);
  95. memcpy(&out[4], &x[12], 16);
  96. }
  97. EXPORT_SYMBOL(hchacha_block);