]> pd.if.org Git - zpackage/blob - crypto/chacha.h
remove stray debug fprintf
[zpackage] / crypto / chacha.h
1 #ifndef CHACHA_H_
2 #define CHACHA_H_ 1
3
4 #include <stdint.h>
5
6 #define TLS_CHACHA20_IV_LENGTH    12
7
8 /* ChaCha20 implementation by D. J. Bernstein
9  * Public domain.
10  */
11
12 #define CHACHA_MINKEYLEN    16
13 #define CHACHA_NONCELEN     8
14 #define CHACHA_NONCELEN_96  12
15 #define CHACHA_CTRLEN       8
16 #define CHACHA_CTRLEN_96    4
17 #define CHACHA_STATELEN     (CHACHA_NONCELEN+CHACHA_CTRLEN)
18 #define CHACHA_BLOCKLEN     64
19
20 #define POLY1305_MAX_AAD    32
21 #define POLY1305_KEYLEN     32
22 #define POLY1305_TAGLEN     16
23
24 #define POLY1305_BLOCK_SIZE 16
25
26 struct chacha_ctx {
27         uint32_t input[16];
28         uint8_t ks[CHACHA_BLOCKLEN];
29         uint8_t unused;
30 };
31
32 /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
33 struct poly1305_context {
34     unsigned long r[5];
35     unsigned long h[5];
36     unsigned long pad[4];
37     size_t leftover;
38     unsigned char buffer[POLY1305_BLOCK_SIZE];
39     unsigned char final;
40 };
41
42 void chacha_keysetup(struct chacha_ctx *x, const unsigned char *k, unsigned int kbits);
43 void chacha_ivsetup(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *ctr);
44 void chacha_ivsetup_96bitnonce(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *ctr);
45 void chacha_encrypt_bytes(struct chacha_ctx *x, const unsigned char *m, unsigned char *c, unsigned int bytes);
46 int poly1305_generate_key(unsigned char *key256, unsigned char *nonce, unsigned int noncelen, unsigned char *poly_key, unsigned int counter);
47 void chacha_ivupdate(struct chacha_ctx *x, const uint8_t *iv, const uint8_t *aad, const uint8_t *counter);
48 void chacha20_poly1305_key(struct chacha_ctx *ctx, unsigned char *poly1305_key);
49 int chacha20_poly1305_aead(struct chacha_ctx *ctx,  unsigned char *pt, unsigned int len, unsigned char *aad, unsigned int aad_len, unsigned char *poly_key, unsigned char *out);
50 void tls_poly1305_init(struct poly1305_context *ctx, const unsigned char key[32]);
51 void tls_poly1305_update(struct poly1305_context *ctx, const unsigned char *m, size_t bytes);
52 void tls_poly1305_finish(struct poly1305_context *ctx, unsigned char mac[16]);
53
54
55 #endif