| 1 |
root |
1.3 |
/* spritz.h, spritz C implementation, header |
| 2 |
|
|
* |
| 3 |
|
|
* Copyright (c) 2015 Marc Alexander Lehmann <libev@schmorp.de> |
| 4 |
|
|
* All rights reserved. |
| 5 |
|
|
* |
| 6 |
|
|
* Redistribution and use in source and binary forms, with or without modifica- |
| 7 |
|
|
* tion, are permitted provided that the following conditions are met: |
| 8 |
|
|
* |
| 9 |
|
|
* 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
|
|
* this list of conditions and the following disclaimer. |
| 11 |
|
|
* |
| 12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
| 14 |
|
|
* documentation and/or other materials provided with the distribution. |
| 15 |
|
|
* |
| 16 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
| 18 |
|
|
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 |
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
| 20 |
|
|
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
| 24 |
|
|
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 25 |
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
|
|
* |
| 27 |
|
|
* Alternatively, the contents of this file may be used under the terms of |
| 28 |
|
|
* the GNU General Public License ("GPL") version 2 or any later version, |
| 29 |
|
|
* in which case the provisions of the GPL are applicable instead of |
| 30 |
|
|
* the above. If you wish to allow the use of your version of this file |
| 31 |
|
|
* only under the terms of the GPL and not to allow others to use your |
| 32 |
|
|
* version of this file under the BSD license, indicate your decision |
| 33 |
|
|
* by deleting the provisions above and replace them with the notice |
| 34 |
|
|
* and other provisions required by the GPL. If you do not delete the |
| 35 |
|
|
* provisions above, a recipient may use your version of this file under |
| 36 |
|
|
* either the BSD or the GPL. |
| 37 |
|
|
*/ |
| 38 |
root |
1.1 |
|
| 39 |
|
|
#include <stdint.h> |
| 40 |
|
|
#include <sys/types.h> |
| 41 |
|
|
|
| 42 |
|
|
/*******************************************************************************/ |
| 43 |
|
|
/* spritz parameters/state type */ |
| 44 |
|
|
|
| 45 |
|
|
enum { |
| 46 |
|
|
spritz_N = 256 |
| 47 |
|
|
}; |
| 48 |
|
|
|
| 49 |
|
|
typedef struct |
| 50 |
|
|
{ |
| 51 |
|
|
uint8_t a, i, j, k, z, w; |
| 52 |
|
|
uint8_t S[spritz_N]; |
| 53 |
|
|
} spritz_state; |
| 54 |
|
|
|
| 55 |
|
|
/*******************************************************************************/ |
| 56 |
|
|
/* the spritz primitives */ |
| 57 |
|
|
|
| 58 |
|
|
void spritz_init (spritz_state *s); |
| 59 |
|
|
void spritz_update (spritz_state *s); |
| 60 |
|
|
void spritz_whip (spritz_state *s, uint_fast16_t r); |
| 61 |
|
|
void spritz_crush (spritz_state *s); |
| 62 |
|
|
void spritz_shuffle (spritz_state *s); |
| 63 |
|
|
void spritz_absorb_nibble (spritz_state *s, uint8_t x); |
| 64 |
|
|
void spritz_absorb (spritz_state *s, const void *I, size_t I_len); |
| 65 |
|
|
void spritz_absorb_stop (spritz_state *s); |
| 66 |
|
|
void spritz_absorb_and_stop (spritz_state *s, const void *I, size_t I_len); /* commonly used helper function */ |
| 67 |
|
|
uint8_t spritz_output (spritz_state *s); |
| 68 |
|
|
void spritz_squeeze (spritz_state *s, void *P, size_t P_len); |
| 69 |
|
|
uint8_t spritz_drip (spritz_state *s); |
| 70 |
|
|
|
| 71 |
|
|
/*******************************************************************************/ |
| 72 |
|
|
/* the spritz-xor cipher */ |
| 73 |
|
|
|
| 74 |
|
|
/* no IV is used if IV_len == 0 */ |
| 75 |
|
|
void spritz_xor_init (spritz_state *s, const void *K, size_t K_len, const void *IV, size_t IV_len); |
| 76 |
|
|
|
| 77 |
|
|
/* can be called multiple times/incrementally */ |
| 78 |
|
|
/* can work inplace */ |
| 79 |
|
|
/* works for both encryption and decryption */ |
| 80 |
|
|
void spritz_xor_crypt (spritz_state *s, const void *I, void *O, size_t len); |
| 81 |
|
|
|
| 82 |
|
|
/*******************************************************************************/ |
| 83 |
|
|
/* the spritz hash */ |
| 84 |
|
|
|
| 85 |
|
|
static void spritz_hash_init (spritz_state *s); |
| 86 |
|
|
static void spritz_hash_add (spritz_state *s, const void *M, size_t M_len); /* can be called multiple times/incrementally */ |
| 87 |
|
|
void spritz_hash_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
| 88 |
|
|
|
| 89 |
|
|
/*******************************************************************************/ |
| 90 |
|
|
/* the spritz MAC */ |
| 91 |
|
|
|
| 92 |
|
|
void spritz_mac_init (spritz_state *s, const void *K, size_t K_len); |
| 93 |
|
|
static void spritz_mac_add (spritz_state *s, const void *M, size_t M_len); /* can be called multiple times/incrementally */ |
| 94 |
|
|
static void spritz_mac_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
| 95 |
|
|
|
| 96 |
|
|
/*******************************************************************************/ |
| 97 |
|
|
/* spritz authenticated encryption */ |
| 98 |
|
|
|
| 99 |
|
|
void spritz_aead_init (spritz_state *s, const void *K, size_t K_len); |
| 100 |
|
|
static void spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len); /* must be called after construction, before associated_data */ |
| 101 |
|
|
static void spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len); /* must be called after nonce, before crypt */ |
| 102 |
|
|
void spritz_aead_crypt (spritz_state *s, const void *I, void *O, size_t len); |
| 103 |
|
|
/* must be called after associated_data, only once, before finish */ |
| 104 |
|
|
/* works for both encryption and decryption */ |
| 105 |
|
|
static void spritz_aead_finish (spritz_state *s, void *H, size_t H_len); /* must be called at most once at the end */ |
| 106 |
|
|
|
| 107 |
|
|
/*******************************************************************************/ |
| 108 |
|
|
/* the spritz drbg/csprng */ |
| 109 |
|
|
|
| 110 |
|
|
/* constructor takes a seed if S_len != 0, same add spritz_prng_put */ |
| 111 |
|
|
void spritz_prng_init (spritz_state *s, const void *S, size_t S_len); |
| 112 |
|
|
static void spritz_prng_put (spritz_state *s, const void *S, size_t S_len); /* add additional entropy */ |
| 113 |
|
|
static void spritz_prng_get (spritz_state *s, void *R, size_t R_len); /* get random bytes */ |
| 114 |
|
|
|
| 115 |
|
|
/*******************************************************************************/ |
| 116 |
|
|
/* inline functions - some functions are so simple, they are defined inline */ |
| 117 |
|
|
|
| 118 |
|
|
/* the spritz hash inline functions */ |
| 119 |
|
|
|
| 120 |
|
|
static void |
| 121 |
|
|
spritz_hash_init (spritz_state *s) |
| 122 |
|
|
{ |
| 123 |
|
|
spritz_init (s); |
| 124 |
|
|
} |
| 125 |
|
|
|
| 126 |
|
|
static void |
| 127 |
|
|
spritz_hash_add (spritz_state *s, const void *M, size_t M_len) |
| 128 |
|
|
{ |
| 129 |
|
|
spritz_absorb (s, M, M_len); |
| 130 |
|
|
} |
| 131 |
|
|
|
| 132 |
|
|
/* the spritz MAC inline functions */ |
| 133 |
|
|
|
| 134 |
|
|
static void |
| 135 |
|
|
spritz_mac_add (spritz_state *s, const void *M, size_t M_len) |
| 136 |
|
|
{ |
| 137 |
|
|
spritz_hash_add (s, M, M_len); |
| 138 |
|
|
} |
| 139 |
|
|
|
| 140 |
|
|
static void |
| 141 |
|
|
spritz_mac_finish (spritz_state *s, void *H, size_t H_len) |
| 142 |
|
|
{ |
| 143 |
|
|
spritz_hash_finish (s, H, H_len); |
| 144 |
|
|
} |
| 145 |
|
|
|
| 146 |
|
|
/* spritz authenticated encryption inline functions */ |
| 147 |
|
|
|
| 148 |
|
|
static void |
| 149 |
|
|
spritz_aead_nonce (spritz_state *s, const void *N, size_t N_len) |
| 150 |
|
|
{ |
| 151 |
|
|
spritz_absorb_and_stop (s, N, N_len); |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
static void |
| 155 |
|
|
spritz_aead_associated_data (spritz_state *s, const void *D, size_t D_len) |
| 156 |
|
|
{ |
| 157 |
|
|
spritz_absorb_and_stop (s, D, D_len); |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
static void |
| 161 |
|
|
spritz_aead_finish (spritz_state *s, void *H, size_t H_len) |
| 162 |
|
|
{ |
| 163 |
|
|
spritz_mac_finish (s, H, H_len); |
| 164 |
|
|
} |
| 165 |
|
|
|
| 166 |
|
|
/* the spritz drbg/csprng inline functions */ |
| 167 |
|
|
|
| 168 |
|
|
static void |
| 169 |
|
|
spritz_prng_put (spritz_state *s, const void *S, size_t S_len) |
| 170 |
|
|
{ |
| 171 |
|
|
spritz_absorb (s, S, S_len); |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
|
/* get random bytes */ |
| 175 |
|
|
static void |
| 176 |
|
|
spritz_prng_get (spritz_state *s, void *R, size_t R_len) |
| 177 |
|
|
{ |
| 178 |
|
|
spritz_squeeze (s, R, R_len); |
| 179 |
|
|
} |
| 180 |
|
|
|