| 223 | #define CHACHA20_BLOCK_BYTES 64 |
| 224 | |
| 225 | static void xor_cipher_stream_off(const struct secret *k, |
| 226 | size_t off, |
| 227 | void *dst, size_t dstlen) |
| 228 | { |
| 229 | const u8 nonce[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 230 | u8 block[CHACHA20_BLOCK_BYTES]; |
| 231 | size_t block_off; |
| 232 | size_t ic = off / CHACHA20_BLOCK_BYTES; |
| 233 | |
| 234 | /* From https://libsodium.gitbook.io/doc/advanced/stream_ciphers/chacha20: |
| 235 | * |
| 236 | * The crypto_stream_chacha20_xor_ic() function is similar to |
| 237 | * crypto_stream_chacha20_xor() but adds the ability to set |
| 238 | * the initial value of the block counter to a non-zero value, |
| 239 | * ic. |
| 240 | * |
| 241 | * This permits direct access to any block without having to |
| 242 | * compute the previous ones. |
| 243 | */ |
| 244 | block_off = (off % CHACHA20_BLOCK_BYTES); |
| 245 | if (block_off != 0) { |
| 246 | size_t rem = CHACHA20_BLOCK_BYTES - block_off; |
| 247 | if (rem > dstlen) |
| 248 | rem = dstlen; |
| 249 | memcpy(block + block_off, dst, rem); |
| 250 | crypto_stream_chacha20_xor_ic(block, block, block_off + rem, |
| 251 | nonce, |
| 252 | ic, |
| 253 | k->data); |
| 254 | ic++; |
| 255 | memcpy(dst, block + block_off, rem); |
| 256 | dst = (char *)dst + rem; |
| 257 | dstlen -= rem; |
| 258 | } |
| 259 | crypto_stream_chacha20_xor_ic(dst, dst, dstlen, nonce, ic, k->data); |
| 260 | } |
| 261 | |
| 262 | /* Convenience function: s2/s2len can be NULL/0 if unwanted */ |
| 263 | static void compute_hmac(const struct secret *key, |
no outgoing calls