1 /* 2 * Copyright (c) 2015 Mike Belopuhov 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef _CHACHAPOLY_H_ 18 #define _CHACHAPOLY_H_ 19 20 #define CHACHA20_KEYSIZE 32 21 #define CHACHA20_CTR 4 22 #define CHACHA20_SALT 4 23 #define CHACHA20_NONCE 8 24 #define CHACHA20_BLOCK_LEN 64 25 26 struct chacha20_ctx { 27 uint8_t block[CHACHA20_BLOCK_LEN]; 28 uint8_t nonce[CHACHA20_NONCE]; 29 }; 30 31 int chacha20_setkey(void *, u_int8_t *, int); 32 void chacha20_reinit(caddr_t, u_int8_t *); 33 void chacha20_crypt(caddr_t, u_int8_t *); 34 35 36 #define POLY1305_KEYLEN 32 37 #define POLY1305_TAGLEN 16 38 #define POLY1305_BLOCK_LEN 16 39 40 struct poly1305_ctx { 41 /* r, h, pad, leftover */ 42 unsigned long state[5+5+4]; 43 size_t leftover; 44 unsigned char buffer[POLY1305_BLOCK_LEN]; 45 unsigned char final; 46 }; 47 48 typedef struct { 49 uint8_t key[POLY1305_KEYLEN]; 50 /* counter, salt */ 51 uint8_t nonce[CHACHA20_NONCE]; 52 struct chacha20_ctx chacha; 53 struct poly1305_ctx poly; 54 } CHACHA20_POLY1305_CTX; 55 56 void Chacha20_Poly1305_Init(void *); 57 void Chacha20_Poly1305_Setkey(void *, const uint8_t *, uint16_t); 58 void Chacha20_Poly1305_Reinit(void *, const uint8_t *, uint16_t); 59 int Chacha20_Poly1305_Update(void *, const uint8_t *, uint16_t); 60 void Chacha20_Poly1305_Final(uint8_t[POLY1305_TAGLEN], void *); 61 62 #endif /* _CHACHAPOLY_H_ */ 63