salsa20.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef SALSA20_H
  15. #define SALSA20_H
  16. #include <stdint.h>
  17. #include <stddef.h>
  18. /**
  19. * The Salsa20/Chacha20 stream ciphers
  20. * ===================================
  21. *
  22. * These are similar stream ciphers by djb.
  23. *
  24. * A reduced round variant of Salsa20 (Salsa20/12)
  25. * was selected as a finalist of the eSTREAM stream
  26. * cipher competition. This implementation does
  27. * the full 20 rounds.
  28. *
  29. * ChaCha20 is fundamentally like Salsa20, but
  30. * has a tweaked round function to improve security
  31. * margin without damaging performance.
  32. */
  33. /* Salsa20 core transform. */
  34. void cf_salsa20_core(const uint8_t key0[16],
  35. const uint8_t key1[16],
  36. const uint8_t nonce[16],
  37. const uint8_t constant[16],
  38. uint8_t out[64]);
  39. /* Chacha20 core transform. */
  40. void cf_chacha20_core(const uint8_t key0[16],
  41. const uint8_t key1[16],
  42. const uint8_t nonce[16],
  43. const uint8_t constant[16],
  44. uint8_t out[64]);
  45. /* .. c:type:: cf_salsa20_ctx
  46. * Incremental interface to Salsa20.
  47. *
  48. * .. c:member:: cf_salsa20_ctx.key0
  49. * Half of key material.
  50. *
  51. * .. c:member:: cf_salsa20_ctx.key1
  52. * Half of key material.
  53. *
  54. * .. c:member:: cf_salsa20_ctx.nonce
  55. * Nonce and counter block.
  56. *
  57. * .. c:member:: cf_salsa20_ctx.constant
  58. * Per-key-length constants.
  59. *
  60. * .. c:member:: cf_salsa20_ctx.block
  61. * Buffer for unused key stream material.
  62. *
  63. * .. c:member:: cf_salsa20_ctx.nblock
  64. * Number of bytes at end of `block` that can be used as key stream.
  65. *
  66. */
  67. typedef struct
  68. {
  69. uint8_t key0[16], key1[16];
  70. uint8_t nonce[16];
  71. const uint8_t *constant;
  72. uint8_t block[64];
  73. size_t nblock;
  74. size_t ncounter;
  75. } cf_salsa20_ctx, cf_chacha20_ctx;
  76. /* .. c:type:: cf_chacha20_ctx
  77. * Incremental interface to Chacha20. This structure
  78. * is identical to :c:type:`cf_salsa20_ctx`.
  79. */
  80. /* .. c:function:: $DECL
  81. * Salsa20 initialisation function.
  82. *
  83. * :param ctx: salsa20 context.
  84. * :param key: key material.
  85. * :param nkey: length of key in bytes, either 16 or 32.
  86. * :param nonce: per-message nonce.
  87. */
  88. void cf_salsa20_init(cf_salsa20_ctx *ctx, const uint8_t *key, size_t nkey, const uint8_t nonce[8]);
  89. /* .. c:function:: $DECL
  90. * Chacha20 initialisation function.
  91. *
  92. * :param ctx: chacha20 context (written).
  93. * :param key: key material.
  94. * :param nkey: length of key in bytes, either 16 or 32.
  95. * :param nonce: per-message nonce.
  96. */
  97. void cf_chacha20_init(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey, const uint8_t nonce[8]);
  98. /* .. c:function:: $DECL
  99. * Chacha20 initialisation function. This version gives full control over the whole
  100. * initial nonce value, and the size of the counter. The counter is always at the front
  101. * of the nonce.
  102. *
  103. * :param ctx: chacha20 context (written).
  104. * :param key: key material.
  105. * :param nkey: length of key in bytes, either 16 or 32.
  106. * :param nonce: per-message nonce. `ncounter` bytes at the start are the block counter.
  107. * :param ncounter: length, in bytes, of the counter portion of the nonce.
  108. */
  109. void cf_chacha20_init_custom(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey,
  110. const uint8_t nonce[16], size_t ncounter);
  111. /* .. c:function:: $DECL
  112. * Salsa20 encryption/decryption function.
  113. *
  114. * :param ctx: salsa20 context.
  115. * :param input: input data buffer (read), `count` bytes long.
  116. * :param output: output data buffer (written), `count` bytes long.
  117. */
  118. void cf_salsa20_cipher(cf_salsa20_ctx *ctx, const uint8_t *input, uint8_t *output, size_t count);
  119. /* .. c:function:: $DECL
  120. * Chacha20 encryption/decryption function.
  121. *
  122. * :param ctx: chacha20 context.
  123. * :param input: input data buffer (read), `count` bytes long.
  124. * :param output: output data buffer (written), `count` bytes long.
  125. */
  126. void cf_chacha20_cipher(cf_chacha20_ctx *ctx, const uint8_t *input, uint8_t *output, size_t count);
  127. #endif