|
| 1 | +/* SPDX-License-Identifier: BSD-2-Clause |
| 2 | + * |
| 3 | + * Blake2s-SIV implementation |
| 4 | + * |
| 5 | + * Copyright (c) 2018-2023, Marek Koza (qyx@krtko.org) |
| 6 | + * All rights reserved. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <stdint.h> |
| 10 | +#include <stddef.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +#include <main.h> |
| 14 | +#include <blake2.h> |
| 15 | + |
| 16 | +#include "blake2s-siv.h" |
| 17 | + |
| 18 | +/** |
| 19 | + * @brief A SIV-mode authenticated encryption based on the Blake2s PRF |
| 20 | + * |
| 21 | + * The idea comes from https://web.cs.ucdavis.edu/~rogaway/papers/siv.pdf: |
| 22 | + * - the input is a key, header and a plaintext |
| 23 | + * - output of the first phase is a MAC, serving as a SIV (synthetic IV) |
| 24 | + * - output of the second phase is a ciphertext using a key and the SIV as an IV |
| 25 | + * |
| 26 | + * Using the SIV mode in this use case have the advantage of not requiring |
| 27 | + * keeping a message counter value between reboots. In an embedded device this |
| 28 | + * could be a significant challenge. We also save some bytes by not requiring |
| 29 | + * and explicit IV. |
| 30 | + * |
| 31 | + * Although the original paper doesn't use Blake2s, we made this change |
| 32 | + * considering the following: |
| 33 | + * |
| 34 | + * - only a single PRF is used to do a key derivation (to get an encryption key Ke |
| 35 | + * and a MAC key Km from a single shared key), a MAC (a keyed Blake2s can be |
| 36 | + * used as a MAC, even without the HMAC construct) and encryption (Blake2s |
| 37 | + * is used in a stream cipher mode where B2S(Ke | counter) is used to generate |
| 38 | + * a cipherstream). This reduces code and memory footprint. |
| 39 | + * - it serves as an "obstruction layer" on layer 2/3. A proper encryption with |
| 40 | + * a key exchange is done on higher layers. We may tolerate some fuckups here. |
| 41 | + * - don't roll your own crypto, huh! |
| 42 | + * |
| 43 | + * encryption: |
| 44 | + * |
| 45 | + * input key is K, input message is M, ciphertext is C |
| 46 | + * derive Ke, Km as (Ke | Km) = H(K) |
| 47 | + * compute a MAC serving as a IV: SIV = H(Km, M) |
| 48 | + * construct cipherstream as i:0->n, Cs = H(Ke, SIV | (i as a big endian uint32)) | H(..) | H(..) |
| 49 | + * encrypt C = M^Cs | SIV |
| 50 | + * |
| 51 | + * decryption: |
| 52 | + * |
| 53 | + * input key is K, ciphertext is C, output message is M |
| 54 | + * derive Ke, Km as (Ke | Km) = H(K) |
| 55 | + * construct cipherstream as i:0->n, Cs = H(Ke, SIV | (i as a big endian uint32)) | H(..) | H(..) |
| 56 | + * split the input Me | SIV = C |
| 57 | + * decrypt the message M = Me^Cs |
| 58 | + * validate MAC H(Km, M) == SIV?, return M if matches |
| 59 | + */ |
| 60 | + |
| 61 | + |
| 62 | +void b2s_derive_keys(const uint8_t *key, size_t len, uint8_t ke[B2S_KE_LEN], uint8_t km[B2S_KM_LEN]) { |
| 63 | + /* No length constraints for the key except it must be non-empty. */ |
| 64 | + u_assert(key != NULL); |
| 65 | + u_assert(len > 0); |
| 66 | + |
| 67 | + uint8_t res[B2S_KE_LEN + B2S_KM_LEN] = {0}; |
| 68 | + blake2s(res, B2S_KE_LEN + B2S_KM_LEN, key, len, NULL, 0); |
| 69 | + |
| 70 | + /* Now split the key. */ |
| 71 | + memcpy(ke, res, B2S_KE_LEN); |
| 72 | + memcpy(km, res + B2S_KE_LEN, B2S_KM_LEN); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +void b2s_crypt(uint8_t *buf, size_t len, const uint8_t *siv, size_t siv_len, const uint8_t ke[B2S_KE_LEN]) { |
| 77 | + u_assert(buf != NULL); |
| 78 | + u_assert(len > 0); |
| 79 | + u_assert(siv != NULL); |
| 80 | + u_assert(siv_len >= 4); |
| 81 | + u_assert(ke != NULL); |
| 82 | + |
| 83 | + /* Block counter */ |
| 84 | + uint8_t i = 0; |
| 85 | + while (len > 0) { |
| 86 | + /* Generate a BLAKE2S_OUTBYTES of keystream. */ |
| 87 | + uint8_t keystream[BLAKE2S_OUTBYTES] = {0}; |
| 88 | + blake2s_state s; |
| 89 | + blake2s_init_key(&s, BLAKE2S_OUTBYTES, ke, B2S_KE_LEN); |
| 90 | + blake2s_update(&s, siv, siv_len); |
| 91 | + uint8_t b[4] = {0, 0, 0, i}; |
| 92 | + blake2s_update(&s, b, sizeof(b)); |
| 93 | + blake2s_final(&s, keystream, BLAKE2S_OUTBYTES); |
| 94 | + |
| 95 | + /* Crunch BLAKE2S_OUTBYTES or less in one step. */ |
| 96 | + size_t block_len = len; |
| 97 | + if (block_len > BLAKE2S_OUTBYTES) { |
| 98 | + block_len = BLAKE2S_OUTBYTES; |
| 99 | + } |
| 100 | + for (size_t j = 0; j < block_len; j++) { |
| 101 | + buf[j] = buf[j] ^ keystream[j]; |
| 102 | + } |
| 103 | + |
| 104 | + /* Advance to the next block. */ |
| 105 | + buf += block_len; |
| 106 | + len -= block_len; |
| 107 | + i++; |
| 108 | + } |
| 109 | + /* Now len = 0 and buf is at the end. */ |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +void b2s_siv(const uint8_t *buf, size_t len, uint8_t *siv, size_t siv_len, const uint8_t km[B2S_KM_LEN]) { |
| 114 | + u_assert(buf != NULL); |
| 115 | + u_assert(len > 0); |
| 116 | + u_assert(siv != NULL); |
| 117 | + u_assert(siv_len >= 4); |
| 118 | + u_assert(km != NULL); |
| 119 | + |
| 120 | + blake2s_state s; |
| 121 | + blake2s_init_key(&s, BLAKE2S_OUTBYTES, km, B2S_KM_LEN); |
| 122 | + blake2s_update(&s, buf, len); |
| 123 | + blake2s_final(&s, siv, siv_len); |
| 124 | +} |
0 commit comments