X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=crypto%2Fchacha.h;fp=crypto%2Fchacha.h;h=3186982e9229156cfbaa2c325f0d661b703fd0c1;hb=66bc25938679f1d6a1d1200f329093d82a5e99b4;hp=0000000000000000000000000000000000000000;hpb=a52ee0733f420ca20224049260d6fc5cf7d8f621;p=zpackage diff --git a/crypto/chacha.h b/crypto/chacha.h new file mode 100644 index 0000000..3186982 --- /dev/null +++ b/crypto/chacha.h @@ -0,0 +1,55 @@ +#ifndef CHACHA_H_ +#define CHACHA_H_ 1 + +#include + +#define TLS_CHACHA20_IV_LENGTH 12 + +/* ChaCha20 implementation by D. J. Bernstein + * Public domain. + */ + +#define CHACHA_MINKEYLEN 16 +#define CHACHA_NONCELEN 8 +#define CHACHA_NONCELEN_96 12 +#define CHACHA_CTRLEN 8 +#define CHACHA_CTRLEN_96 4 +#define CHACHA_STATELEN (CHACHA_NONCELEN+CHACHA_CTRLEN) +#define CHACHA_BLOCKLEN 64 + +#define POLY1305_MAX_AAD 32 +#define POLY1305_KEYLEN 32 +#define POLY1305_TAGLEN 16 + +#define POLY1305_BLOCK_SIZE 16 + +struct chacha_ctx { + uint32_t input[16]; + uint8_t ks[CHACHA_BLOCKLEN]; + uint8_t unused; +}; + +/* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */ +struct poly1305_context { + unsigned long r[5]; + unsigned long h[5]; + unsigned long pad[4]; + size_t leftover; + unsigned char buffer[POLY1305_BLOCK_SIZE]; + unsigned char final; +}; + +void chacha_keysetup(struct chacha_ctx *x, const unsigned char *k, unsigned int kbits); +void chacha_ivsetup(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *ctr); +void chacha_ivsetup_96bitnonce(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *ctr); +void chacha_encrypt_bytes(struct chacha_ctx *x, const unsigned char *m, unsigned char *c, unsigned int bytes); +int poly1305_generate_key(unsigned char *key256, unsigned char *nonce, unsigned int noncelen, unsigned char *poly_key, unsigned int counter); +void chacha_ivupdate(struct chacha_ctx *x, const uint8_t *iv, const uint8_t *aad, const uint8_t *counter); +void chacha20_poly1305_key(struct chacha_ctx *ctx, unsigned char *poly1305_key); +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); +void tls_poly1305_init(struct poly1305_context *ctx, const unsigned char key[32]); +void tls_poly1305_update(struct poly1305_context *ctx, const unsigned char *m, size_t bytes); +void tls_poly1305_finish(struct poly1305_context *ctx, unsigned char mac[16]); + + +#endif