23#if defined(CONFIG_IDF_TARGET_ESP32S3) && CONFIG_IDF_TARGET_ESP32S3
33 return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
37 return (v << c) | (v >> (32 - c));
40#define QR(a, b, c, d) \
64 "Chacha20Work outgrew PC_WORK_CHACHA20 - raise it in protocore_config.h, which derives "
65 "PC_SECURE_ARENA_SIZE from it");
70 for (
int i = 0; i < 16; i++)
74 for (
int i = 0; i < 10; i++)
76 QR(w->
x[0], w->
x[4], w->
x[8], w->
x[12]);
77 QR(w->
x[1], w->
x[5], w->
x[9], w->
x[13]);
78 QR(w->
x[2], w->
x[6], w->
x[10], w->
x[14]);
79 QR(w->
x[3], w->
x[7], w->
x[11], w->
x[15]);
80 QR(w->
x[0], w->
x[5], w->
x[10], w->
x[15]);
81 QR(w->
x[1], w->
x[6], w->
x[11], w->
x[12]);
82 QR(w->
x[2], w->
x[7], w->
x[8], w->
x[13]);
83 QR(w->
x[3], w->
x[4], w->
x[9], w->
x[14]);
85 for (
int i = 0; i < 16; i++)
87 uint32_t v = w->
x[i] + w->
st[i];
88 out[4 * i + 0] = (uint8_t)v;
89 out[4 * i + 1] = (uint8_t)(v >> 8);
90 out[4 * i + 2] = (uint8_t)(v >> 16);
91 out[4 * i + 3] = (uint8_t)(v >> 24);
103 uint8_t *out,
size_t len)
105 SecureBorrow ws_b(
sizeof(Chacha20Work),
alignof(Chacha20Work));
111 Chacha20Work *w =
reinterpret_cast<Chacha20Work *
>(ws.
buf);
116 for (
int i = 0; i < 8; i++)
118 w->st[4 + i] = rd_le32(key + 4 * i);
120 w->st[14] = rd_le32(iv + 0);
121 w->st[15] = rd_le32(iv + 4);
125 w->st[12] = (uint32_t)(counter & 0xffffffffu);
126 w->st[13] = (uint32_t)(counter >> 32);
127 chacha_core(w, w->ks);
128 size_t n = (len - off < 64) ? (len - off) : 64;
129 for (
size_t i = 0; i < n; i++)
131 out[off + i] = (uint8_t)((in ? in[off + i] : 0) ^ w->ks[i]);
141 SecureBorrow ws_b(
sizeof(Chacha20Work),
alignof(Chacha20Work));
147 Chacha20Work *w =
reinterpret_cast<Chacha20Work *
>(ws.
buf);
152 for (
int i = 0; i < 8; i++)
154 w->st[4 + i] = rd_le32(key + 4 * i);
157 w->st[13] = rd_le32(nonce + 0);
158 w->st[14] = rd_le32(nonce + 4);
159 w->st[15] = rd_le32(nonce + 8);
void pc_chacha20_block_ietf(const uint8_t key[PC_CHACHA20_KEY_LEN], uint32_t counter, const uint8_t nonce[12], uint8_t out[PC_CHACHA20_BLOCK_LEN])
One 64-byte ChaCha20 keystream block in the RFC 8439 layout (for the KAT).
void pc_chacha20_xor(const uint8_t key[PC_CHACHA20_KEY_LEN], const uint8_t iv[8], uint64_t counter, const uint8_t *in, uint8_t *out, size_t len)
XOR len bytes of in with the ChaCha20 keystream into out (OpenSSH layout).
ChaCha20 stream cipher (D. J. Bernstein; RFC 8439).
#define PC_CHACHA20_BLOCK_LEN
#define PC_CHACHA20_KEY_LEN
A secure borrow whose acquire and release are one call each.
const pc_span & span() const
The borrowed region; empty if the pool could not satisfy it.
Per-translation-unit optimization override for hot, pure-integer crypto.
#define PC_CRYPTO_HOT_UNSWITCH
uint32_t rotl32(uint32_t v, int c)
void chacha_core(Chacha20Work *w, uint8_t out[64])
uint32_t rd_le32(const uint8_t *p)
Secure pool accessor - borrows that hold key material.
bool pc_span_ok(const pc_span &s)
True when the span refers to real storage and every write so far has fit.
A writable byte region: the storage, the capacity that belongs to it, and what has been produced into...
uint8_t * buf
first byte, or nullptr when the region could not be obtained