RFC 7539 - ChaCha20 and Poly1305 + chacha based PRNG
This commit is contained in:
@@ -937,6 +937,27 @@ int cipher_is_valid(int idx);
|
||||
|
||||
LTC_MUTEX_PROTO(ltc_cipher_mutex)
|
||||
|
||||
/* ---- stream ciphers ---- */
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
|
||||
typedef struct {
|
||||
ulong32 input[16];
|
||||
unsigned char kstream[64];
|
||||
unsigned long ksleft;
|
||||
unsigned long ivlen;
|
||||
int rounds;
|
||||
} chacha_state;
|
||||
|
||||
int chacha_setup(chacha_state *st, const unsigned char *key, unsigned long keylen, int rounds);
|
||||
int chacha_ivctr32(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong32 counter);
|
||||
int chacha_ivctr64(chacha_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 counter);
|
||||
int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
|
||||
int chacha_keystream(chacha_state *st, unsigned char *out, unsigned long outlen);
|
||||
int chacha_test(void);
|
||||
|
||||
#endif /* LTC_CHACHA */
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
||||
@@ -189,6 +189,8 @@
|
||||
#define LTC_KASUMI
|
||||
#define LTC_MULTI2
|
||||
#define LTC_CAMELLIA
|
||||
/* ChaCha is special (a stream cipher) */
|
||||
#define LTC_CHACHA
|
||||
|
||||
#endif /* LTC_NO_CIPHERS */
|
||||
|
||||
@@ -255,6 +257,7 @@
|
||||
#define LTC_XCBC
|
||||
#define LTC_F9_MODE
|
||||
#define LTC_PELICAN
|
||||
#define LTC_POLY1305
|
||||
|
||||
/* ---> Encrypt + Authenticate Modes <--- */
|
||||
|
||||
@@ -264,6 +267,7 @@
|
||||
#define LTC_OCB3_MODE
|
||||
#define LTC_CCM_MODE
|
||||
#define LTC_GCM_MODE
|
||||
#define LTC_CHACHA20POLY1305_MODE
|
||||
|
||||
/* Use 64KiB tables */
|
||||
#ifndef LTC_NO_TABLES
|
||||
@@ -504,6 +508,10 @@
|
||||
#error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
|
||||
#endif
|
||||
|
||||
#if defined(LTC_CHACHA20POLY1305_MODE) && (!defined(LTC_CHACHA) || !defined(LTC_POLY1305))
|
||||
#error LTC_CHACHA20POLY1305_MODE requires LTC_CHACHA + LTC_POLY1305
|
||||
#endif
|
||||
|
||||
/* THREAD management */
|
||||
#ifdef LTC_PTHREAD
|
||||
|
||||
|
||||
@@ -96,6 +96,26 @@ void pmac_shift_xor(pmac_state *pmac);
|
||||
|
||||
#endif /* PMAC */
|
||||
|
||||
#ifdef LTC_POLY1305
|
||||
typedef struct {
|
||||
ulong32 r[5];
|
||||
ulong32 h[5];
|
||||
ulong32 pad[4];
|
||||
unsigned long leftover;
|
||||
unsigned char buffer[16];
|
||||
int final;
|
||||
} poly_state;
|
||||
|
||||
int poly1305_init(poly_state *st, const unsigned char *key, unsigned long keylen);
|
||||
int poly1305_process(poly_state *st, const unsigned char *in, unsigned long inlen);
|
||||
int poly1305_done(poly_state *st, unsigned char *mac, unsigned long *maclen);
|
||||
int poly1305_test(void);
|
||||
int poly1305_memory(const unsigned char *key, unsigned long keylen, const unsigned char *in, unsigned long inlen, unsigned char *mac, unsigned long *maclen);
|
||||
int poly1305_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...);
|
||||
int poly1305_file(const char *fname, const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen);
|
||||
int poly1305_test(void);
|
||||
#endif /* LTC_POLY1305 */
|
||||
|
||||
#ifdef LTC_EAX_MODE
|
||||
|
||||
#if !(defined(LTC_OMAC) && defined(LTC_CTR_MODE))
|
||||
@@ -477,6 +497,36 @@ int f9_test(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHACHA20POLY1305_MODE
|
||||
|
||||
typedef struct {
|
||||
poly_state poly;
|
||||
chacha_state chacha;
|
||||
ulong64 aadlen;
|
||||
ulong64 ctlen;
|
||||
int aadflg;
|
||||
} chachapoly_state;
|
||||
|
||||
#define CHCHA20POLY1305_ENCRYPT 0
|
||||
#define CHCHA20POLY1305_DECRYPT 1
|
||||
|
||||
int chacha20poly1305_init(chachapoly_state *st, const unsigned char *key, unsigned long keylen);
|
||||
int chacha20poly1305_setiv(chachapoly_state *st, const unsigned char *iv, unsigned long ivlen);
|
||||
int chacha20poly1305_setiv_rfc7905(chachapoly_state *st, const unsigned char *iv, unsigned long ivlen, ulong64 sequence_number);
|
||||
int chacha20poly1305_add_aad(chachapoly_state *st, const unsigned char *in, unsigned long inlen);
|
||||
int chacha20poly1305_encrypt(chachapoly_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
|
||||
int chacha20poly1305_decrypt(chachapoly_state *st, const unsigned char *in, unsigned long inlen, unsigned char *out);
|
||||
int chacha20poly1305_done(chachapoly_state *st, unsigned char *tag, unsigned long *taglen);
|
||||
int chacha20poly1305_memory(const unsigned char *key, unsigned long keylen,
|
||||
const unsigned char *iv, unsigned long ivlen,
|
||||
const unsigned char *aad, unsigned long aadlen,
|
||||
const unsigned char *in, unsigned long inlen,
|
||||
unsigned char *out,
|
||||
unsigned char *tag, unsigned long *taglen,
|
||||
int direction);
|
||||
int chacha20poly1305_test(void);
|
||||
|
||||
#endif /* LTC_CHACHA20POLY1305_MODE */
|
||||
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
|
||||
@@ -15,6 +15,15 @@ struct rc4_prng {
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
struct chacha_prng {
|
||||
chacha_state s; /* chacha state */
|
||||
unsigned char ent[40]; /* entropy buffer */
|
||||
unsigned long idx; /* entropy counter */
|
||||
short ready; /* ready flag 0-1 */
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
struct fortuna_prng {
|
||||
hash_state pool[LTC_FORTUNA_POOLS]; /* the pools */
|
||||
@@ -55,6 +64,9 @@ typedef union Prng_state {
|
||||
#ifdef LTC_RC4
|
||||
struct rc4_prng rc4;
|
||||
#endif
|
||||
#ifdef LTC_CHACHA
|
||||
struct chacha_prng chacha;
|
||||
#endif
|
||||
#ifdef LTC_FORTUNA
|
||||
struct fortuna_prng fortuna;
|
||||
#endif
|
||||
@@ -154,6 +166,18 @@ int rc4_test(void);
|
||||
extern const struct ltc_prng_descriptor rc4_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_CHACHA
|
||||
int chacha_prng_start(prng_state *prng);
|
||||
int chacha_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int chacha_prng_ready(prng_state *prng);
|
||||
unsigned long chacha_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng);
|
||||
int chacha_prng_done(prng_state *prng);
|
||||
int chacha_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
|
||||
int chacha_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
int chacha_prng_test(void);
|
||||
extern const struct ltc_prng_descriptor chacha_prng_desc;
|
||||
#endif
|
||||
|
||||
#ifdef LTC_SPRNG
|
||||
int sprng_start(prng_state *prng);
|
||||
int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng);
|
||||
|
||||
Reference in New Issue
Block a user