winzik/docker/ext/cryo/cryo_crypto.c

203 lines
7.6 KiB
C

/*
* cryo_crypto.c — XChaCha20-Poly1305 (IETF AEAD, as libsodium's
* crypto_aead_xchacha20poly1305_ietf_*) for builds without libsodium,
* e.g. the WebAssembly engine. Same output as libsodium: a file encrypted by
* one build decrypts with the other (tests/015_encrypt_portable.phpt checks
* both ways when libsodium is there). ChaCha20 and HChaCha20 from RFC 8439 /
* draft-irtf-cfrg-xchacha; Poly1305 in 26-bit limbs (poly1305-donna 32).
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_cryo.h"
#include <stdint.h>
#include <string.h>
#define ROTL32(v, n) (((v) << (n)) | ((v) >> (32 - (n))))
#define QR(a, b, c, d) \
a += b; d ^= a; d = ROTL32(d, 16); \
c += d; b ^= c; b = ROTL32(b, 12); \
a += b; d ^= a; d = ROTL32(d, 8); \
c += d; b ^= c; b = ROTL32(b, 7)
static uint32_t ld32(const uint8_t *p)
{
return (uint32_t) p[0] | ((uint32_t) p[1] << 8) | ((uint32_t) p[2] << 16) | ((uint32_t) p[3] << 24);
}
static void st32(uint8_t *p, uint32_t v)
{
p[0] = (uint8_t) v; p[1] = (uint8_t) (v >> 8); p[2] = (uint8_t) (v >> 16); p[3] = (uint8_t) (v >> 24);
}
static void chacha_rounds(uint32_t x[16])
{
for (int i = 0; i < 10; i++) {
QR(x[0], x[4], x[8], x[12]); QR(x[1], x[5], x[9], x[13]);
QR(x[2], x[6], x[10], x[14]); QR(x[3], x[7], x[11], x[15]);
QR(x[0], x[5], x[10], x[15]); QR(x[1], x[6], x[11], x[12]);
QR(x[2], x[7], x[8], x[13]); QR(x[3], x[4], x[9], x[14]);
}
}
static void chacha_init(uint32_t s[16], const uint8_t key[32])
{
s[0] = 0x61707865; s[1] = 0x3320646e; s[2] = 0x79622d32; s[3] = 0x6b206574;
for (int i = 0; i < 8; i++) s[4 + i] = ld32(key + 4 * i);
}
static void hchacha20(uint8_t out[32], const uint8_t key[32], const uint8_t nonce[16])
{
uint32_t x[16];
chacha_init(x, key);
for (int i = 0; i < 4; i++) x[12 + i] = ld32(nonce + 4 * i);
chacha_rounds(x);
for (int i = 0; i < 4; i++) st32(out + 4 * i, x[i]);
for (int i = 0; i < 4; i++) st32(out + 16 + 4 * i, x[12 + i]);
}
/* RFC 8439 ChaCha20: 32-bit counter, 96-bit nonce; out = in XOR keystream */
static void chacha20_xor(uint8_t *out, const uint8_t *in, size_t len, const uint8_t key[32], uint32_t counter, const uint8_t nonce[12])
{
uint32_t s[16], x[16];
uint8_t block[64];
chacha_init(s, key);
s[13] = ld32(nonce); s[14] = ld32(nonce + 4); s[15] = ld32(nonce + 8);
while (len) {
s[12] = counter++;
memcpy(x, s, sizeof(x));
chacha_rounds(x);
for (int i = 0; i < 16; i++) st32(block + 4 * i, x[i] + s[i]);
size_t n = len < 64 ? len : 64;
for (size_t i = 0; i < n; i++) out[i] = (uint8_t) (in ? in[i] ^ block[i] : block[i]);
out += n;
if (in) in += n;
len -= n;
}
}
/* Poly1305, 26-bit limbs */
typedef struct { uint32_t r[5], h[5], pad[4]; } poly1305;
static void poly_init(poly1305 *p, const uint8_t key[32])
{
p->r[0] = (ld32(key + 0)) & 0x3ffffff;
p->r[1] = (ld32(key + 3) >> 2) & 0x3ffff03;
p->r[2] = (ld32(key + 6) >> 4) & 0x3ffc0ff;
p->r[3] = (ld32(key + 9) >> 6) & 0x3f03fff;
p->r[4] = (ld32(key + 12) >> 8) & 0x00fffff;
memset(p->h, 0, sizeof(p->h));
for (int i = 0; i < 4; i++) p->pad[i] = ld32(key + 16 + 4 * i);
}
/* one 16-byte block (already padded); hibit = 1 << 24 for full blocks */
static void poly_block(poly1305 *p, const uint8_t m[16], uint32_t hibit)
{
uint32_t r0 = p->r[0], r1 = p->r[1], r2 = p->r[2], r3 = p->r[3], r4 = p->r[4];
uint32_t s1 = r1 * 5, s2 = r2 * 5, s3 = r3 * 5, s4 = r4 * 5;
uint32_t h0 = p->h[0], h1 = p->h[1], h2 = p->h[2], h3 = p->h[3], h4 = p->h[4];
h0 += (ld32(m + 0)) & 0x3ffffff;
h1 += (ld32(m + 3) >> 2) & 0x3ffffff;
h2 += (ld32(m + 6) >> 4) & 0x3ffffff;
h3 += (ld32(m + 9) >> 6) & 0x3ffffff;
h4 += (ld32(m + 12) >> 8) | hibit;
uint64_t d0 = (uint64_t) h0 * r0 + (uint64_t) h1 * s4 + (uint64_t) h2 * s3 + (uint64_t) h3 * s2 + (uint64_t) h4 * s1;
uint64_t d1 = (uint64_t) h0 * r1 + (uint64_t) h1 * r0 + (uint64_t) h2 * s4 + (uint64_t) h3 * s3 + (uint64_t) h4 * s2;
uint64_t d2 = (uint64_t) h0 * r2 + (uint64_t) h1 * r1 + (uint64_t) h2 * r0 + (uint64_t) h3 * s4 + (uint64_t) h4 * s3;
uint64_t d3 = (uint64_t) h0 * r3 + (uint64_t) h1 * r2 + (uint64_t) h2 * r1 + (uint64_t) h3 * r0 + (uint64_t) h4 * s4;
uint64_t d4 = (uint64_t) h0 * r4 + (uint64_t) h1 * r3 + (uint64_t) h2 * r2 + (uint64_t) h3 * r1 + (uint64_t) h4 * r0;
uint32_t c;
c = (uint32_t) (d0 >> 26); h0 = (uint32_t) d0 & 0x3ffffff;
d1 += c; c = (uint32_t) (d1 >> 26); h1 = (uint32_t) d1 & 0x3ffffff;
d2 += c; c = (uint32_t) (d2 >> 26); h2 = (uint32_t) d2 & 0x3ffffff;
d3 += c; c = (uint32_t) (d3 >> 26); h3 = (uint32_t) d3 & 0x3ffffff;
d4 += c; c = (uint32_t) (d4 >> 26); h4 = (uint32_t) d4 & 0x3ffffff;
h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff;
h1 += c;
p->h[0] = h0; p->h[1] = h1; p->h[2] = h2; p->h[3] = h3; p->h[4] = h4;
}
static void poly_update_padded(poly1305 *p, const uint8_t *m, size_t len)
{
while (len >= 16) { poly_block(p, m, 1u << 24); m += 16; len -= 16; }
if (len) {
uint8_t b[16] = {0};
memcpy(b, m, len); /* AEAD pads with zeros to 16: a full block */
poly_block(p, b, 1u << 24);
}
}
static void poly_finish(poly1305 *p, uint8_t tag[16])
{
uint32_t h0 = p->h[0], h1 = p->h[1], h2 = p->h[2], h3 = p->h[3], h4 = p->h[4], c, g0, g1, g2, g3, g4, mask;
c = h1 >> 26; h1 &= 0x3ffffff;
h2 += c; c = h2 >> 26; h2 &= 0x3ffffff;
h3 += c; c = h3 >> 26; h3 &= 0x3ffffff;
h4 += c; c = h4 >> 26; h4 &= 0x3ffffff;
h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff;
h1 += c;
g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
g4 = h4 + c - (1u << 26);
mask = (g4 >> 31) - 1;
g0 &= mask; g1 &= mask; g2 &= mask; g3 &= mask; g4 &= mask;
mask = ~mask;
h0 = (h0 & mask) | g0; h1 = (h1 & mask) | g1; h2 = (h2 & mask) | g2; h3 = (h3 & mask) | g3; h4 = (h4 & mask) | g4;
h0 = (h0 | (h1 << 26));
h1 = ((h1 >> 6) | (h2 << 20));
h2 = ((h2 >> 12) | (h3 << 14));
h3 = ((h3 >> 18) | (h4 << 8));
uint64_t f;
f = (uint64_t) h0 + p->pad[0]; h0 = (uint32_t) f;
f = (uint64_t) h1 + p->pad[1] + (f >> 32); h1 = (uint32_t) f;
f = (uint64_t) h2 + p->pad[2] + (f >> 32); h2 = (uint32_t) f;
f = (uint64_t) h3 + p->pad[3] + (f >> 32); h3 = (uint32_t) f;
st32(tag, h0); st32(tag + 4, h1); st32(tag + 8, h2); st32(tag + 12, h3);
}
static void aead_tag(uint8_t tag[16], const uint8_t polykey[32], const uint8_t *c, size_t clen)
{
poly1305 p;
uint8_t lens[16];
poly_init(&p, polykey);
poly_update_padded(&p, c, clen); /* no associated data */
memset(lens, 0, 8);
uint64_t cl = clen;
for (int i = 0; i < 8; i++) lens[8 + i] = (uint8_t) (cl >> (8 * i));
poly_block(&p, lens, 1u << 24);
poly_finish(&p, tag);
}
/* c = ciphertext (mlen) followed by the 16-byte tag */
void cryo_xchacha_encrypt(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t nonce[24], const uint8_t key[32])
{
uint8_t sub[32], n12[12] = {0}, polykey[32];
hchacha20(sub, key, nonce);
memcpy(n12 + 4, nonce + 16, 8);
chacha20_xor(polykey, NULL, 32, sub, 0, n12);
chacha20_xor(c, m, mlen, sub, 1, n12);
aead_tag(c + mlen, polykey, c, mlen);
ZEND_SECURE_ZERO(sub, sizeof(sub));
ZEND_SECURE_ZERO(polykey, sizeof(polykey));
}
/* 0 = authentic (m filled), -1 = forged or wrong key */
int cryo_xchacha_decrypt(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t nonce[24], const uint8_t key[32])
{
if (clen < 16) return -1;
size_t mlen = clen - 16;
uint8_t sub[32], n12[12] = {0}, polykey[32], tag[16], diff = 0;
hchacha20(sub, key, nonce);
memcpy(n12 + 4, nonce + 16, 8);
chacha20_xor(polykey, NULL, 32, sub, 0, n12);
aead_tag(tag, polykey, c, mlen);
for (int i = 0; i < 16; i++) diff |= tag[i] ^ c[mlen + i];
if (diff == 0) chacha20_xor(m, c, mlen, sub, 1, n12);
ZEND_SECURE_ZERO(sub, sizeof(sub));
ZEND_SECURE_ZERO(polykey, sizeof(polykey));
return diff == 0 ? 0 : -1;
}