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 10 11 /* RFC 8439 "ChaCha20 and Poly1305 for IETF Protocols" */ 12 13 #ifndef GMSSL_CHACHA20_H 14 #define GMSSL_CHACHA20_H 15 16 #define CHACHA20_IS_BIG_ENDIAN 0 17 18 #include <stdint.h> 19 #include <stdlib.h> 20 21 #include <string.h> 22 23 #define CHACHA20_KEY_BITS 256 24 #define CHACHA20_NONCE_BITS 96 25 #define CHACHA20_COUNTER_BITS 32 26 27 #define CHACHA20_KEY_SIZE (CHACHA20_KEY_BITS/8) 28 #define CHACHA20_NONCE_SIZE (CHACHA20_NONCE_BITS/8) 29 #define CHACHA20_COUNTER_SIZE (CHACHA20_COUNTER_BITS/8) 30 31 #define CHACHA20_KEY_WORDS (CHACHA20_KEY_SIZE/sizeof(uint32_t)) 32 #define CHACHA20_NONCE_WORDS (CHACHA20_NONCE_SIZE/sizeof(uint32_t)) 33 #define CHACHA20_COUNTER_WORDS (CHACHA20_COUNTER_SIZE/sizeof(uint32_t)) 34 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 41 typedef struct { 42 uint32_t d[16]; 43 } CHACHA20_STATE; 44 45 46 void chacha20_init(CHACHA20_STATE *state, 47 const uint8_t key[CHACHA20_KEY_SIZE], 48 const uint8_t nonce[CHACHA20_NONCE_SIZE], uint32_t counter); 49 50 void chacha20_generate_keystream(CHACHA20_STATE *state, 51 size_t counts, uint8_t *out); 52 53 54 #ifdef __cplusplus 55 } 56 #endif 57 #endif 58