• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #ifndef CRYPT_CHACHA20_H
17 #define CRYPT_CHACHA20_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_CHACHA20
21 
22 #include <stdint.h>
23 #include "crypt_types.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif // __cplusplus
28 
29 #define CHACHA20_STATESIZE 16
30 #define CHACHA20_STATEBYTES (CHACHA20_STATESIZE * sizeof(uint32_t))
31 #define CHACHA20_KEYLEN 32
32 #define CHACHA20_NONCELEN 12
33 
34 typedef struct {
35     uint32_t state[CHACHA20_STATESIZE]; // state RFC 7539
36     union {
37         uint32_t c[CHACHA20_STATESIZE];
38         uint8_t u[CHACHA20_STATEBYTES];
39     } last; // save the last data
40     uint32_t lastLen; // remaining length of the last data in bytes
41     uint8_t set; // indicates whether the key and nonce are set
42 } CRYPT_CHACHA20_Ctx;
43 
44 int32_t CRYPT_CHACHA20_SetKey(CRYPT_CHACHA20_Ctx *ctx, const uint8_t *key, uint32_t keyLen);
45 
46 int32_t CRYPT_CHACHA20_Update(CRYPT_CHACHA20_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len);
47 
48 int32_t CRYPT_CHACHA20_Ctrl(CRYPT_CHACHA20_Ctx *ctx, int32_t opt, void *val, uint32_t len);
49 
50 void CRYPT_CHACHA20_Clean(CRYPT_CHACHA20_Ctx *ctx);
51 
52 #ifdef __cplusplus
53 }
54 #endif // __cplusplus
55 
56 #endif // HITLS_CRYPTO_CHACHA20
57 
58 #endif // CRYPT_CHACHA20_H
59