| 1 |
|
| #include "symprim/symprim.h" |
| 2 |
|
| #include "symprim/blake2b.h" |
| 3 |
|
| #include "symprim/wipe.h" |
| 4 |
|
| |
| 5 |
|
| #include <string.h> |
| 6 |
|
| |
| 7 |
|
| static const uint64_t iv[8] = { |
| 8 |
|
| UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b), |
| 9 |
|
| UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1), |
| 10 |
|
| UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f), |
| 11 |
|
| UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179) |
| 12 |
|
| }; |
| 13 |
|
| |
| 14 |
|
| static const uint8_t sigma[12][16] = { |
| 15 |
|
| { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
| 16 |
|
| { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, |
| 17 |
|
| { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, |
| 18 |
|
| { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, |
| 19 |
|
| { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, |
| 20 |
|
| { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, |
| 21 |
|
| { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, |
| 22 |
|
| { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, |
| 23 |
|
| { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, |
| 24 |
|
| { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, |
| 25 |
|
| { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
| 26 |
|
| { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } |
| 27 |
|
| }; |
| 28 |
|
| |
| 29 |
|
| /* A byte at a time, so the answer is the same whatever the machine's byte |
| 30 |
|
| * order or alignment rules. */ |
| 31 |
1092056 |
| static uint64_t load64(const uint8_t *p) |
| 32 |
|
| { |
| 33 |
1092056 |
| return (uint64_t)p[0] | ((uint64_t)p[1] << 8) |
| 34 |
1092056 |
| | ((uint64_t)p[2] << 16) | ((uint64_t)p[3] << 24) |
| 35 |
1092056 |
| | ((uint64_t)p[4] << 32) | ((uint64_t)p[5] << 40) |
| 36 |
1092056 |
| | ((uint64_t)p[6] << 48) | ((uint64_t)p[7] << 56); |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
299272 |
| static void store64(uint8_t *p, uint64_t v) |
| 40 |
|
| { |
| 41 |
|
| int i; |
| 42 |
2693448 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
2394176 |
299272 |
2693448 |
|
| 43 |
2394176 |
| p[i] = (uint8_t)(v >> (8 * i)); |
| 44 |
299272 |
| } |
| 45 |
|
| |
| 46 |
1580 |
| static void store32(uint8_t *p, uint32_t v) |
| 47 |
|
| { |
| 48 |
1580 |
| p[0] = (uint8_t)v; |
| 49 |
1580 |
| p[1] = (uint8_t)(v >> 8); |
| 50 |
1580 |
| p[2] = (uint8_t)(v >> 16); |
| 51 |
1580 |
| p[3] = (uint8_t)(v >> 24); |
| 52 |
1580 |
| } |
| 53 |
|
| |
| 54 |
|
| #define ROTR(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) |
| 55 |
|
| |
| 56 |
|
| #define G(a, b, c, d, x, y) \ |
| 57 |
|
| do { \ |
| 58 |
|
| a = a + b + (x); \ |
| 59 |
|
| d = ROTR(d ^ a, 32); \ |
| 60 |
|
| c = c + d; \ |
| 61 |
|
| b = ROTR(b ^ c, 24); \ |
| 62 |
|
| a = a + b + (y); \ |
| 63 |
|
| d = ROTR(d ^ a, 16); \ |
| 64 |
|
| c = c + d; \ |
| 65 |
|
| b = ROTR(b ^ c, 63); \ |
| 66 |
|
| } while (0) |
| 67 |
|
| |
| 68 |
49550 |
| static void compress(symprim_blake2b_state *s, const uint8_t *block, int last) |
| 69 |
|
| { |
| 70 |
|
| uint64_t m[16], v[16]; |
| 71 |
|
| int i, round; |
| 72 |
|
| |
| 73 |
842350 |
▶ | for (i = 0; i < 16; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) |
792800 |
49550 |
842350 |
|
| 74 |
792800 |
| m[i] = load64(block + 8 * i); |
| 75 |
445950 |
▶ | for (i = 0; i < 8; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) { |
396400 |
49550 |
445950 |
|
| 76 |
396400 |
| v[i] = s->h[i]; |
| 77 |
396400 |
| v[i + 8] = iv[i]; |
| 78 |
|
| } |
| 79 |
49550 |
| v[12] ^= s->t[0]; |
| 80 |
49550 |
| v[13] ^= s->t[1]; |
| 81 |
49550 |
▶ | if (last) |
Branch: 1 branch
| Branch | True | False | Total |
| if (last) |
37409 |
12141 |
49550 |
|
| 82 |
37409 |
| v[14] = ~v[14]; |
| 83 |
|
| |
| 84 |
644150 |
▶ | for (round = 0; round < 12; round++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (round = 0; round < 12; round++) { |
594600 |
49550 |
644150 |
|
| 85 |
594600 |
| const uint8_t *g = sigma[round]; |
| 86 |
594600 |
| G(v[0], v[4], v[8], v[12], m[g[0]], m[g[1]]); |
| 87 |
594600 |
| G(v[1], v[5], v[9], v[13], m[g[2]], m[g[3]]); |
| 88 |
594600 |
| G(v[2], v[6], v[10], v[14], m[g[4]], m[g[5]]); |
| 89 |
594600 |
| G(v[3], v[7], v[11], v[15], m[g[6]], m[g[7]]); |
| 90 |
594600 |
| G(v[0], v[5], v[10], v[15], m[g[8]], m[g[9]]); |
| 91 |
594600 |
| G(v[1], v[6], v[11], v[12], m[g[10]], m[g[11]]); |
| 92 |
594600 |
| G(v[2], v[7], v[8], v[13], m[g[12]], m[g[13]]); |
| 93 |
594600 |
| G(v[3], v[4], v[9], v[14], m[g[14]], m[g[15]]); |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
445950 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
396400 |
49550 |
445950 |
|
| 97 |
396400 |
| s->h[i] ^= v[i] ^ v[i + 8]; |
| 98 |
49550 |
| symprim_wipe(m, sizeof m); |
| 99 |
49550 |
| symprim_wipe(v, sizeof v); |
| 100 |
49550 |
| } |
| 101 |
|
| |
| 102 |
|
| /* The counter is 128 bits of bytes hashed so far. */ |
| 103 |
49550 |
| static void count(symprim_blake2b_state *s, uint64_t bytes) |
| 104 |
|
| { |
| 105 |
49550 |
| s->t[0] += bytes; |
| 106 |
49550 |
▶ | if (s->t[0] < bytes) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->t[0] < bytes) |
5 |
49545 |
49550 |
|
| 107 |
5 |
| s->t[1]++; |
| 108 |
49550 |
| } |
| 109 |
|
| |
| 110 |
37422 |
| int symprim_blake2b_init(symprim_blake2b_state *s, size_t outlen, |
| 111 |
|
| const void *key, size_t keylen, |
| 112 |
|
| const uint8_t *salt, const uint8_t *personal) |
| 113 |
|
| { |
| 114 |
|
| uint8_t param[64]; |
| 115 |
|
| int i; |
| 116 |
|
| |
| 117 |
37422 |
▶ | if (!s || outlen < 1 || outlen > SYMPRIM_BLAKE2B_MAX |
Branch: 3 branches
| Branch | True | False | Total |
| if (!s || outlen < 1 || outlen > SYMPRIM_BLAKE2B_MAX |
37421 |
1 |
37422 |
| if (!s || outlen < 1 || outlen > SYMPRIM_BLAKE2B_MAX |
37417 |
4 |
37421 |
| if (!s || outlen < 1 || outlen > SYMPRIM_BLAKE2B_MAX |
37413 |
4 |
37417 |
|
| 118 |
37413 |
▶ | || keylen > SYMPRIM_BLAKE2B_KEY_MAX || (keylen && !key)) |
Branch: 3 branches
| Branch | True | False | Total |
| || keylen > SYMPRIM_BLAKE2B_KEY_MAX || (keylen && !key)) |
37409 |
4 |
37413 |
| || keylen > SYMPRIM_BLAKE2B_KEY_MAX || (keylen && !key)) |
1396 |
36013 |
37409 |
| || keylen > SYMPRIM_BLAKE2B_KEY_MAX || (keylen && !key)) |
2 |
1394 |
1396 |
|
| 119 |
15 |
| return SYMPRIM_E_ARGUMENT; |
| 120 |
|
| |
| 121 |
37407 |
| memset(param, 0, sizeof param); |
| 122 |
37407 |
| param[0] = (uint8_t)outlen; |
| 123 |
37407 |
| param[1] = (uint8_t)keylen; |
| 124 |
37407 |
| param[2] = 1; |
| 125 |
37407 |
| param[3] = 1; |
| 126 |
37407 |
▶ | if (salt) |
Branch: 1 branch
| Branch | True | False | Total |
| if (salt) |
922 |
36485 |
37407 |
|
| 127 |
922 |
| memcpy(param + 32, salt, SYMPRIM_BLAKE2B_SALT); |
| 128 |
37407 |
▶ | if (personal) |
Branch: 1 branch
| Branch | True | False | Total |
| if (personal) |
922 |
36485 |
37407 |
|
| 129 |
922 |
| memcpy(param + 48, personal, SYMPRIM_BLAKE2B_PERSONAL); |
| 130 |
|
| |
| 131 |
336663 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
299256 |
37407 |
336663 |
|
| 132 |
299256 |
| s->h[i] = iv[i] ^ load64(param + 8 * i); |
| 133 |
37407 |
| s->t[0] = 0; |
| 134 |
37407 |
| s->t[1] = 0; |
| 135 |
37407 |
| s->buflen = 0; |
| 136 |
37407 |
| s->outlen = outlen; |
| 137 |
37407 |
| memset(s->buf, 0, sizeof s->buf); |
| 138 |
|
| |
| 139 |
|
| /* A key is one whole block of input, zero padded. No key is no block. */ |
| 140 |
37407 |
▶ | if (keylen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (keylen) { |
1394 |
36013 |
37407 |
|
| 141 |
1394 |
| memcpy(s->buf, key, keylen); |
| 142 |
1394 |
| s->buflen = SYMPRIM_BLAKE2B_BLOCK; |
| 143 |
|
| } |
| 144 |
37407 |
| return SYMPRIM_OK; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| /* A full buffer is held back, not compressed, until more input arrives: the |
| 148 |
|
| * last block is compressed by final with the last flag, and a message of |
| 149 |
|
| * exactly one block is that last block. */ |
| 150 |
62197 |
| void symprim_blake2b_update(symprim_blake2b_state *s, const void *in, size_t len) |
| 151 |
|
| { |
| 152 |
62197 |
| const uint8_t *p = (const uint8_t *)in; |
| 153 |
|
| size_t room; |
| 154 |
|
| |
| 155 |
62197 |
▶ | if (!len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) |
63 |
62134 |
62197 |
|
| 156 |
63 |
| return; |
| 157 |
62134 |
| room = SYMPRIM_BLAKE2B_BLOCK - s->buflen; |
| 158 |
62134 |
▶ | if (len > room) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len > room) { |
6076 |
56058 |
62134 |
|
| 159 |
6076 |
| memcpy(s->buf + s->buflen, p, room); |
| 160 |
6076 |
| count(s, SYMPRIM_BLAKE2B_BLOCK); |
| 161 |
6076 |
| compress(s, s->buf, 0); |
| 162 |
6076 |
| s->buflen = 0; |
| 163 |
6076 |
| p += room; |
| 164 |
6076 |
| len -= room; |
| 165 |
12141 |
▶ | while (len > SYMPRIM_BLAKE2B_BLOCK) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len > SYMPRIM_BLAKE2B_BLOCK) { |
6065 |
6076 |
12141 |
|
| 166 |
6065 |
| count(s, SYMPRIM_BLAKE2B_BLOCK); |
| 167 |
6065 |
| compress(s, p, 0); |
| 168 |
6065 |
| p += SYMPRIM_BLAKE2B_BLOCK; |
| 169 |
6065 |
| len -= SYMPRIM_BLAKE2B_BLOCK; |
| 170 |
|
| } |
| 171 |
|
| } |
| 172 |
62134 |
| memcpy(s->buf + s->buflen, p, len); |
| 173 |
62134 |
| s->buflen += len; |
| 174 |
|
| } |
| 175 |
|
| |
| 176 |
37409 |
| void symprim_blake2b_final(symprim_blake2b_state *s, void *out) |
| 177 |
|
| { |
| 178 |
|
| uint8_t full[SYMPRIM_BLAKE2B_MAX]; |
| 179 |
|
| int i; |
| 180 |
|
| |
| 181 |
37409 |
| count(s, s->buflen); |
| 182 |
37409 |
| memset(s->buf + s->buflen, 0, SYMPRIM_BLAKE2B_BLOCK - s->buflen); |
| 183 |
37409 |
| compress(s, s->buf, 1); |
| 184 |
336681 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
299272 |
37409 |
336681 |
|
| 185 |
299272 |
| store64(full + 8 * i, s->h[i]); |
| 186 |
37409 |
| memcpy(out, full, s->outlen); |
| 187 |
37409 |
| symprim_wipe(full, sizeof full); |
| 188 |
37409 |
| symprim_wipe(s, sizeof *s); |
| 189 |
37409 |
| } |
| 190 |
|
| |
| 191 |
33559 |
| int symprim_blake2b(void *out, size_t outlen, const void *in, size_t len, |
| 192 |
|
| const void *key, size_t keylen) |
| 193 |
|
| { |
| 194 |
|
| symprim_blake2b_state s; |
| 195 |
33559 |
| int code = symprim_blake2b_init(&s, outlen, key, keylen, NULL, NULL); |
| 196 |
|
| |
| 197 |
33559 |
▶ | if (code != SYMPRIM_OK) |
Branch: 1 branch
| Branch | True | False | Total |
| if (code != SYMPRIM_OK) |
7 |
33552 |
33559 |
|
| 198 |
7 |
| return code; |
| 199 |
33552 |
| symprim_blake2b_update(&s, in, len); |
| 200 |
33552 |
| symprim_blake2b_final(&s, out); |
| 201 |
33552 |
| return SYMPRIM_OK; |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| /* Up to 64 bytes it is one hash of the length and the input. Past that it is |
| 205 |
|
| * a chain of 64 byte hashes, 32 bytes taken from each, and a last hash of |
| 206 |
|
| * whatever length is left. */ |
| 207 |
1581 |
| int symprim_blake2b_long(void *out, uint32_t outlen, const void *in, size_t len) |
| 208 |
|
| { |
| 209 |
1581 |
| uint8_t *o = (uint8_t *)out; |
| 210 |
|
| uint8_t prefix[4], v[SYMPRIM_BLAKE2B_MAX], next[SYMPRIM_BLAKE2B_MAX]; |
| 211 |
|
| symprim_blake2b_state s; |
| 212 |
|
| uint32_t left; |
| 213 |
|
| |
| 214 |
|
| /* The long hash is not in the table, and the one door to it and the |
| 215 |
|
| * password hash that calls it always have somewhere to write. |
| 216 |
|
| # uncoverable condition left count:1 */ |
| 217 |
1581 |
▶ | if (!o || outlen < 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!o || outlen < 1) |
1581 |
0 |
1581 |
| if (!o || outlen < 1) |
1 |
1580 |
1581 |
|
| 218 |
1 |
| return SYMPRIM_E_ARGUMENT; |
| 219 |
1580 |
| store32(prefix, outlen); |
| 220 |
|
| |
| 221 |
1580 |
▶ | if (outlen <= SYMPRIM_BLAKE2B_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (outlen <= SYMPRIM_BLAKE2B_MAX) { |
472 |
1108 |
1580 |
|
| 222 |
472 |
| symprim_blake2b_init(&s, outlen, NULL, 0, NULL, NULL); |
| 223 |
472 |
| symprim_blake2b_update(&s, prefix, sizeof prefix); |
| 224 |
472 |
| symprim_blake2b_update(&s, in, len); |
| 225 |
472 |
| symprim_blake2b_final(&s, o); |
| 226 |
472 |
| return SYMPRIM_OK; |
| 227 |
|
| } |
| 228 |
|
| |
| 229 |
1108 |
| symprim_blake2b_init(&s, SYMPRIM_BLAKE2B_MAX, NULL, 0, NULL, NULL); |
| 230 |
1108 |
| symprim_blake2b_update(&s, prefix, sizeof prefix); |
| 231 |
1108 |
| symprim_blake2b_update(&s, in, len); |
| 232 |
1108 |
| symprim_blake2b_final(&s, v); |
| 233 |
1108 |
| memcpy(o, v, 32); |
| 234 |
1108 |
| o += 32; |
| 235 |
1108 |
| left = outlen - 32; |
| 236 |
32092 |
▶ | while (left > SYMPRIM_BLAKE2B_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (left > SYMPRIM_BLAKE2B_MAX) { |
30984 |
1108 |
32092 |
|
| 237 |
30984 |
| symprim_blake2b(next, SYMPRIM_BLAKE2B_MAX, v, SYMPRIM_BLAKE2B_MAX, NULL, 0); |
| 238 |
30984 |
| memcpy(v, next, SYMPRIM_BLAKE2B_MAX); |
| 239 |
30984 |
| memcpy(o, v, 32); |
| 240 |
30984 |
| o += 32; |
| 241 |
30984 |
| left -= 32; |
| 242 |
|
| } |
| 243 |
1108 |
| symprim_blake2b(next, left, v, SYMPRIM_BLAKE2B_MAX, NULL, 0); |
| 244 |
1108 |
| memcpy(o, next, left); |
| 245 |
1108 |
| symprim_wipe(v, sizeof v); |
| 246 |
1108 |
| symprim_wipe(next, sizeof next); |
| 247 |
1108 |
| return SYMPRIM_OK; |
| 248 |
|
| } |