chacha20.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. /* RFC 8439 "ChaCha20 and Poly1305 for IETF Protocols" */
  10. #ifndef GMSSL_CHACHA20_H
  11. #define GMSSL_CHACHA20_H
  12. #define CHACHA20_IS_BIG_ENDIAN 0
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #define CHACHA20_KEY_BITS 256
  17. #define CHACHA20_NONCE_BITS 96
  18. #define CHACHA20_COUNTER_BITS 32
  19. #define CHACHA20_KEY_SIZE (CHACHA20_KEY_BITS/8)
  20. #define CHACHA20_NONCE_SIZE (CHACHA20_NONCE_BITS/8)
  21. #define CHACHA20_COUNTER_SIZE (CHACHA20_COUNTER_BITS/8)
  22. #define CHACHA20_KEY_WORDS (CHACHA20_KEY_SIZE/sizeof(uint32_t))
  23. #define CHACHA20_NONCE_WORDS (CHACHA20_NONCE_SIZE/sizeof(uint32_t))
  24. #define CHACHA20_COUNTER_WORDS (CHACHA20_COUNTER_SIZE/sizeof(uint32_t))
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. typedef struct {
  29. uint32_t d[16];
  30. } CHACHA20_STATE;
  31. void chacha20_init(CHACHA20_STATE *state,
  32. const uint8_t key[CHACHA20_KEY_SIZE],
  33. const uint8_t nonce[CHACHA20_NONCE_SIZE], uint32_t counter);
  34. void chacha20_generate_keystream(CHACHA20_STATE *state,
  35. size_t counts, uint8_t *out);
  36. #ifdef __cplusplus
  37. }
  38. #endif
  39. #endif