| 1 |
root |
1.2 |
/* spritz.h */ |
| 2 |
|
|
/* (C)2015 Marc Alexander Lehmann, all rights reserved */ |
| 3 |
root |
1.1 |
|
| 4 |
|
|
#include <stdint.h> |
| 5 |
|
|
#include <sys/types.h> |
| 6 |
|
|
|
| 7 |
|
|
/*******************************************************************************/ |
| 8 |
|
|
/* spritz parameters/state type */ |
| 9 |
|
|
|
| 10 |
|
|
enum { |
| 11 |
|
|
spritz_N = 256 |
| 12 |
|
|
}; |
| 13 |
|
|
|
| 14 |
|
|
typedef struct |
| 15 |
|
|
{ |
| 16 |
|
|
uint8_t a, i, j, k, z, w; |
| 17 |
|
|
uint8_t S[spritz_N]; |
| 18 |
|
|
} spritz_state; |
| 19 |
|
|
|
| 20 |
|
|
/*******************************************************************************/ |
| 21 |
|
|
/* the spritz primitives */ |
| 22 |
|
|
|
| 23 |
|
|
void spritz_init (spritz_state *s); |
| 24 |
|
|
void spritz_update (spritz_state *s); |
| 25 |
|
|
void spritz_whip (spritz_state *s, uint_fast16_t r); |
| 26 |
|
|
void spritz_crush (spritz_state *s); |
| 27 |
|
|
void spritz_shuffle (spritz_state *s); |
| 28 |
|
|
void spritz_absorb_nibble (spritz_state *s, uint8_t x); |
| 29 |
|
|
void spritz_absorb (spritz_state *s, const void *I, size_t I_len); |
| 30 |
|
|
void spritz_absorb_stop (spritz_state *s); |
| 31 |
|
|
void spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len); /* commonly used helper function */ |
| 32 |
|
|
uint8_t spritz_output (spritz_state *s); |
| 33 |
|
|
void spritz_squeeze (spritz_state *s, void *P, size_t P_len); |
| 34 |
|
|
uint8_t spritz_drip (spritz_state *s); |
| 35 |
|
|
|
| 36 |
|
|
/*******************************************************************************/ |
| 37 |
|
|
/* the spritz-xor cipher */ |
| 38 |
|
|
|
| 39 |
|
|
/* no IV is used if IV_len == 0 */ |
| 40 |
|
|
void spritz_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len); |
| 41 |
|
|
|
| 42 |
|
|
/* can be called multiple times/incrementally */ |
| 43 |
|
|
/* can work inplace */ |
| 44 |
|
|
/* works for both encryption and decryption */ |
| 45 |
|
|
void spritz_xor_crypt (spritz_state *s, const void *I, void *O, size_t len); |
| 46 |
|
|
|
| 47 |
|
|
/*******************************************************************************/ |
| 48 |
|
|
/* the spritz hash */ |
| 49 |
|
|
|
| 50 |
|
|
static void spritz_hash_init (spritz_state *s); |
| 51 |
|
|
static void spritz_hash_add (spritz_state *s, const void *M, size_t M_len); /* can be called multiple times/incrementally */ |
| 52 |
|
|
void spritz_hash_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
| 53 |
|
|
|
| 54 |
|
|
/*******************************************************************************/ |
| 55 |
|
|
/* the spritz MAC */ |
| 56 |
|
|
|
| 57 |
|
|
void spritz_mac_init (spritz_state *s, const void *K, size_t K_len); |
| 58 |
|
|
static void spritz_mac_add (spritz_state *s, const void *M, size_t M_len); /* can be called multiple times/incrementally */ |
| 59 |
|
|
static void spritz_mac_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
| 60 |
|
|
|
| 61 |
|
|
/*******************************************************************************/ |
| 62 |
|
|
/* spritz authenticated encryption */ |
| 63 |
|
|
|
| 64 |
|
|
void spritz_aead_init (spritz_state *s, const void *K, size_t K_len); |
| 65 |
|
|
static void spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */ |
| 66 |
|
|
static void spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */ |
| 67 |
|
|
void spritz_aead_crypt (spritz_state *s, const void *I, void *O, size_t len); |
| 68 |
|
|
/* must be called after associated_data, only once, before finish */ |
| 69 |
|
|
/* works for both encryption and decryption */ |
| 70 |
|
|
static void spritz_aead_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
| 71 |
|
|
|
| 72 |
|
|
/*******************************************************************************/ |
| 73 |
|
|
/* the spritz drbg/csprng */ |
| 74 |
|
|
|
| 75 |
|
|
/* constructor takes a seed if S_len != 0, same add spritz_prng_put */ |
| 76 |
|
|
void spritz_prng_init (spritz_state *s, const void *S, size_t S_len); |
| 77 |
|
|
static void spritz_prng_put (spritz_state *s, const void *S, size_t S_len); /* add additional entropy */ |
| 78 |
|
|
static void spritz_prng_get (spritz_state *s, void *R, size_t R_len); /* get random bytes */ |
| 79 |
|
|
|
| 80 |
|
|
/*******************************************************************************/ |
| 81 |
|
|
/* inline functions - some functions are so simple, they are defined inline */ |
| 82 |
|
|
|
| 83 |
|
|
/* the spritz hash inline functions */ |
| 84 |
|
|
|
| 85 |
|
|
static void |
| 86 |
|
|
spritz_hash_init (spritz_state *s) |
| 87 |
|
|
{ |
| 88 |
|
|
spritz_init (s); |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
static void |
| 92 |
|
|
spritz_hash_add (spritz_state *s, const void *M, size_t M_len) |
| 93 |
|
|
{ |
| 94 |
|
|
spritz_absorb (s, M, M_len); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
/* the spritz MAC inline functions */ |
| 98 |
|
|
|
| 99 |
|
|
static void |
| 100 |
|
|
spritz_mac_add (spritz_state *s, const void *M, size_t M_len) |
| 101 |
|
|
{ |
| 102 |
|
|
spritz_hash_add (s, M, M_len); |
| 103 |
|
|
} |
| 104 |
|
|
|
| 105 |
|
|
static void |
| 106 |
|
|
spritz_mac_finish (spritz_state *s, void *H, size_t H_len) |
| 107 |
|
|
{ |
| 108 |
|
|
spritz_hash_finish (s, H, H_len); |
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
/* spritz authenticated encryption inline functions */ |
| 112 |
|
|
|
| 113 |
|
|
static void |
| 114 |
|
|
spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len) |
| 115 |
|
|
{ |
| 116 |
|
|
spritz_absorb_and_stop (s, N, N_len); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
static void |
| 120 |
|
|
spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len) |
| 121 |
|
|
{ |
| 122 |
|
|
spritz_absorb_and_stop (s, D, D_len); |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
static void |
| 126 |
|
|
spritz_aead_finish (spritz_state *s, void *H, size_t H_len) |
| 127 |
|
|
{ |
| 128 |
|
|
spritz_mac_finish (s, H, H_len); |
| 129 |
|
|
} |
| 130 |
|
|
|
| 131 |
|
|
/* the spritz drbg/csprng inline functions */ |
| 132 |
|
|
|
| 133 |
|
|
static void |
| 134 |
|
|
spritz_prng_put (spritz_state *s, const void *S, size_t S_len) |
| 135 |
|
|
{ |
| 136 |
|
|
spritz_absorb (s, S, S_len); |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
/* get random bytes */ |
| 140 |
|
|
static void |
| 141 |
|
|
spritz_prng_get (spritz_state *s, void *R, size_t R_len) |
| 142 |
|
|
{ |
| 143 |
|
|
spritz_squeeze (s, R, R_len); |
| 144 |
|
|
} |
| 145 |
|
|
|