lsquic_crypto.c revision 7d09751d
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <string.h>
450aadb33SDmitri Tikhonov
550aadb33SDmitri Tikhonov#include <openssl/ssl.h>
650aadb33SDmitri Tikhonov#include <openssl/crypto.h>
750aadb33SDmitri Tikhonov#include <openssl/stack.h>
850aadb33SDmitri Tikhonov#include <openssl/x509.h>
950aadb33SDmitri Tikhonov#include <openssl/rand.h>
1050aadb33SDmitri Tikhonov#include <openssl/curve25519.h>
117a8b2eceSDmitri Tikhonov#include <openssl/hkdf.h>
12e0197994SDmitri Tikhonov#include <openssl/hmac.h>
1350aadb33SDmitri Tikhonov
1450aadb33SDmitri Tikhonov#include <zlib.h>
15461e84d8SAmol Deshpande#ifdef WIN32
16461e84d8SAmol Deshpande#include <vc_compat.h>
17461e84d8SAmol Deshpande#endif
1850aadb33SDmitri Tikhonov
1950aadb33SDmitri Tikhonov#include "lsquic_types.h"
2050aadb33SDmitri Tikhonov#include "lsquic_crypto.h"
2150aadb33SDmitri Tikhonov#include "lsquic_parse.h"
2250aadb33SDmitri Tikhonov#include "lsquic_util.h"
2350aadb33SDmitri Tikhonov#include "lsquic_str.h"
2450aadb33SDmitri Tikhonov
2550aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_CRYPTO
2650aadb33SDmitri Tikhonov#include "lsquic_logger.h"
2750aadb33SDmitri Tikhonov
2850aadb33SDmitri Tikhonov
2950aadb33SDmitri Tikhonovstatic const char s_hs_signature[] = "QUIC CHLO and server config signature";
3050aadb33SDmitri Tikhonovstatic int crypto_inited = 0;
3150aadb33SDmitri Tikhonov
3250aadb33SDmitri Tikhonov
3350aadb33SDmitri Tikhonovvoid rand_bytes(void *data, int len)
3450aadb33SDmitri Tikhonov{
3550aadb33SDmitri Tikhonov    RAND_bytes(data, len);
3650aadb33SDmitri Tikhonov}
3750aadb33SDmitri Tikhonov
3850aadb33SDmitri Tikhonov
3950aadb33SDmitri Tikhonovuint64_t fnv1a_64(const uint8_t * data, int len)
4050aadb33SDmitri Tikhonov{
4150aadb33SDmitri Tikhonov    uint64_t hash = UINT64_C(14695981039346656037);
4250aadb33SDmitri Tikhonov    const uint8_t *end = data + len;
4350aadb33SDmitri Tikhonov    while(data < end)
4450aadb33SDmitri Tikhonov    {
4550aadb33SDmitri Tikhonov        hash ^= *data;
4650aadb33SDmitri Tikhonov        hash *= UINT64_C(1099511628211);
4750aadb33SDmitri Tikhonov        ++data;
4850aadb33SDmitri Tikhonov    }
4950aadb33SDmitri Tikhonov    return hash;
5050aadb33SDmitri Tikhonov}
5150aadb33SDmitri Tikhonov
5250aadb33SDmitri Tikhonov
5350aadb33SDmitri Tikhonovvoid fnv1a_64_s(const uint8_t * data, int len, char *md)
5450aadb33SDmitri Tikhonov{
5550aadb33SDmitri Tikhonov    uint64_t hash = fnv1a_64(data, len);
5650aadb33SDmitri Tikhonov    memcpy(md, (void *)&hash, 8);
5750aadb33SDmitri Tikhonov}
5850aadb33SDmitri Tikhonov
5950aadb33SDmitri Tikhonov
6050aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ )
6150aadb33SDmitri Tikhonov
6250aadb33SDmitri Tikhonovstatic uint128 s_prime;
6350aadb33SDmitri Tikhonovstatic uint128 s_init_hash;
6450aadb33SDmitri Tikhonov
6550aadb33SDmitri Tikhonov
6650aadb33SDmitri Tikhonovstatic inline void make_uint128(uint128 *v, uint64_t hi, uint64_t lo)
6750aadb33SDmitri Tikhonov{
6850aadb33SDmitri Tikhonov    *v = hi;
6950aadb33SDmitri Tikhonov    *v <<= 64;
7050aadb33SDmitri Tikhonov    *v += lo;
7150aadb33SDmitri Tikhonov}
7250aadb33SDmitri Tikhonov
7350aadb33SDmitri Tikhonov
7450aadb33SDmitri Tikhonovvoid fnv1a_inc(uint128 *hash, const uint8_t *data, int len)
7550aadb33SDmitri Tikhonov{
7650aadb33SDmitri Tikhonov    const uint8_t* end = data + len;
7750aadb33SDmitri Tikhonov    while(data < end)
7850aadb33SDmitri Tikhonov    {
7950aadb33SDmitri Tikhonov        *hash = (*hash ^ (*data)) * s_prime;
8050aadb33SDmitri Tikhonov        ++data;
8150aadb33SDmitri Tikhonov    }
8250aadb33SDmitri Tikhonov}
8350aadb33SDmitri Tikhonov
8450aadb33SDmitri Tikhonovuint128 fnv1a_128_3(const uint8_t *data1, int len1,
8550aadb33SDmitri Tikhonov                      const uint8_t *data2, int len2,
8650aadb33SDmitri Tikhonov                      const uint8_t *data3, int len3)
8750aadb33SDmitri Tikhonov{
8850aadb33SDmitri Tikhonov    uint128 hash;
8950aadb33SDmitri Tikhonov    memcpy(&hash, &s_init_hash, 16);
9050aadb33SDmitri Tikhonov
9150aadb33SDmitri Tikhonov    fnv1a_inc(&hash, data1, len1);
9250aadb33SDmitri Tikhonov    fnv1a_inc(&hash, data2, len2);
9350aadb33SDmitri Tikhonov    fnv1a_inc(&hash, data3, len3);
9450aadb33SDmitri Tikhonov    return hash;
9550aadb33SDmitri Tikhonov}
9650aadb33SDmitri Tikhonov
9750aadb33SDmitri Tikhonov/* HS_PKT_HASH_LENGTH bytes of md */
9850aadb33SDmitri Tikhonovvoid serialize_fnv128_short(uint128 v, uint8_t *md)
9950aadb33SDmitri Tikhonov{
10050aadb33SDmitri Tikhonov    memcpy(md, (void *)&v, 12);
10150aadb33SDmitri Tikhonov}
10250aadb33SDmitri Tikhonov
10350aadb33SDmitri Tikhonov#else
10450aadb33SDmitri Tikhonovuint128  *uint128_times(uint128 *v, const uint128 *factor)
10550aadb33SDmitri Tikhonov{
10650aadb33SDmitri Tikhonov    uint64_t a96 = v->hi_ >> 32;
10750aadb33SDmitri Tikhonov    uint64_t a64 = v->hi_ & 0xffffffffu;
10850aadb33SDmitri Tikhonov    uint64_t a32 = v->lo_ >> 32;
10950aadb33SDmitri Tikhonov    uint64_t a00 = v->lo_ & 0xffffffffu;
11050aadb33SDmitri Tikhonov    uint64_t b96 = factor->hi_ >> 32;
11150aadb33SDmitri Tikhonov    uint64_t b64 = factor->hi_ & 0xffffffffu;
11250aadb33SDmitri Tikhonov    uint64_t b32 = factor->lo_ >> 32;
11350aadb33SDmitri Tikhonov    uint64_t b00 = factor->lo_ & 0xffffffffu;
11450aadb33SDmitri Tikhonov    uint64_t tmp, lolo;
11550aadb33SDmitri Tikhonov    // multiply [a96 .. a00] x [b96 .. b00]
11650aadb33SDmitri Tikhonov    // terms higher than c96 disappear off the high side
11750aadb33SDmitri Tikhonov    // terms c96 and c64 are safe to ignore carry bit
11850aadb33SDmitri Tikhonov    uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96;
11950aadb33SDmitri Tikhonov    uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64;
12050aadb33SDmitri Tikhonov    v->hi_ = (c96 << 32) + c64;
12150aadb33SDmitri Tikhonov    v->lo_ = 0;
12250aadb33SDmitri Tikhonov
12350aadb33SDmitri Tikhonov    tmp = a32 * b00;
12450aadb33SDmitri Tikhonov    v->hi_ += tmp >> 32;
12550aadb33SDmitri Tikhonov    v->lo_ += tmp << 32;
12650aadb33SDmitri Tikhonov
12750aadb33SDmitri Tikhonov    tmp = a00 * b32;
12850aadb33SDmitri Tikhonov    v->hi_ += tmp >> 32;
12950aadb33SDmitri Tikhonov    v->lo_ += tmp << 32;
13050aadb33SDmitri Tikhonov
13150aadb33SDmitri Tikhonov    tmp = a00 * b00;
13250aadb33SDmitri Tikhonov    lolo = v->lo_ + tmp;
13350aadb33SDmitri Tikhonov    if (lolo < v->lo_)
13450aadb33SDmitri Tikhonov        ++v->hi_;
13550aadb33SDmitri Tikhonov    v->lo_ = lolo;
13650aadb33SDmitri Tikhonov
13750aadb33SDmitri Tikhonov    return v;
13850aadb33SDmitri Tikhonov}
13950aadb33SDmitri Tikhonov
14050aadb33SDmitri Tikhonovvoid fnv1a_inc(uint128 *hash, const uint8_t * data, int len)
14150aadb33SDmitri Tikhonov{
14250aadb33SDmitri Tikhonov    static const uint128 kPrime = {16777216, 315};
14350aadb33SDmitri Tikhonov    const uint8_t* end = data + len;
14450aadb33SDmitri Tikhonov    while(data < end)
14550aadb33SDmitri Tikhonov    {
14650aadb33SDmitri Tikhonov        hash->lo_ = (hash->lo_ ^ (uint64_t)*data);
14750aadb33SDmitri Tikhonov        uint128_times(hash, &kPrime);
14850aadb33SDmitri Tikhonov        ++data;
14950aadb33SDmitri Tikhonov    }
15050aadb33SDmitri Tikhonov}
15150aadb33SDmitri Tikhonov
15250aadb33SDmitri Tikhonov
15350aadb33SDmitri Tikhonovuint128 fnv1a_128_3(const uint8_t * data1, int len1,
15450aadb33SDmitri Tikhonov                      const uint8_t * data2, int len2,
15550aadb33SDmitri Tikhonov                      const uint8_t * data3, int len3)
15650aadb33SDmitri Tikhonov{
15750aadb33SDmitri Tikhonov    uint128 hash = {UINT64_C(7809847782465536322), UINT64_C(7113472399480571277)};
15850aadb33SDmitri Tikhonov    fnv1a_inc(&hash, data1, len1);
15950aadb33SDmitri Tikhonov    fnv1a_inc(&hash, data2, len2);
16050aadb33SDmitri Tikhonov    fnv1a_inc(&hash, data3, len3);
16150aadb33SDmitri Tikhonov    return hash;
16250aadb33SDmitri Tikhonov}
16350aadb33SDmitri Tikhonov
16450aadb33SDmitri Tikhonov
16550aadb33SDmitri Tikhonov/* HS_PKT_HASH_LENGTH bytes of md */
16650aadb33SDmitri Tikhonovvoid serialize_fnv128_short(uint128 v, uint8_t *md)
16750aadb33SDmitri Tikhonov{
16850aadb33SDmitri Tikhonov    assert(HS_PKT_HASH_LENGTH == 8 + 4);
16950aadb33SDmitri Tikhonov    memcpy(md, (void *)&v.lo_, 8);
17050aadb33SDmitri Tikhonov    memcpy(md + 8, (void *)&v.hi_, 4);
17150aadb33SDmitri Tikhonov}
17250aadb33SDmitri Tikhonov
17350aadb33SDmitri Tikhonov#endif
17450aadb33SDmitri Tikhonov
17550aadb33SDmitri Tikhonov
1765392f7a3SLiteSpeed Techstatic void sha256(const uint8_t *buf, int len, uint8_t *h)
17750aadb33SDmitri Tikhonov{
17850aadb33SDmitri Tikhonov    SHA256_CTX ctx;
17950aadb33SDmitri Tikhonov    SHA256_Init(&ctx);
18050aadb33SDmitri Tikhonov    SHA256_Update(&ctx, buf, len);
18150aadb33SDmitri Tikhonov    SHA256_Final(h, &ctx);
18250aadb33SDmitri Tikhonov}
18350aadb33SDmitri Tikhonov
18450aadb33SDmitri Tikhonov
18550aadb33SDmitri Tikhonov/* base on rfc 5869 with sha256, prk is 32 bytes*/
18650aadb33SDmitri Tikhonovvoid lshkdf_extract(const unsigned char *ikm, int ikm_len, const unsigned char *salt,
18750aadb33SDmitri Tikhonov                  int salt_len, unsigned char *prk)
18850aadb33SDmitri Tikhonov{
18950aadb33SDmitri Tikhonov#ifndef NDEBUG
19050aadb33SDmitri Tikhonov    unsigned char *out;
19150aadb33SDmitri Tikhonov    unsigned int out_len;
19250aadb33SDmitri Tikhonov    out =
19350aadb33SDmitri Tikhonov#endif
19450aadb33SDmitri Tikhonov        HMAC(EVP_sha256(), salt, salt_len, ikm, ikm_len, prk,
19550aadb33SDmitri Tikhonov#ifndef NDEBUG
19650aadb33SDmitri Tikhonov                                                              &out_len
19750aadb33SDmitri Tikhonov#else
19850aadb33SDmitri Tikhonov                                                              NULL
19950aadb33SDmitri Tikhonov#endif
20050aadb33SDmitri Tikhonov                                                                      );
20150aadb33SDmitri Tikhonov    assert(out);
20250aadb33SDmitri Tikhonov    assert(out_len == 32);
20350aadb33SDmitri Tikhonov}
20450aadb33SDmitri Tikhonov
20550aadb33SDmitri Tikhonov
206461e84d8SAmol Deshpande#define SHA256LEN   32
20750aadb33SDmitri Tikhonovint lshkdf_expand(const unsigned char *prk, const unsigned char *info, int info_len,
20850aadb33SDmitri Tikhonov                uint16_t c_key_len, uint8_t *c_key,
20950aadb33SDmitri Tikhonov                uint16_t s_key_len, uint8_t *s_key,
21050aadb33SDmitri Tikhonov                uint16_t c_key_iv_len, uint8_t *c_key_iv,
21150aadb33SDmitri Tikhonov                uint16_t s_key_iv_len, uint8_t *s_key_iv,
2127a8b2eceSDmitri Tikhonov                uint16_t sub_key_len, uint8_t *sub_key,
2137a8b2eceSDmitri Tikhonov                uint8_t *c_hp, uint8_t *s_hp)
21450aadb33SDmitri Tikhonov{
2157a8b2eceSDmitri Tikhonov    const unsigned L = c_key_len + s_key_len + c_key_iv_len + s_key_iv_len
2167a8b2eceSDmitri Tikhonov            + sub_key_len
2177a8b2eceSDmitri Tikhonov            + (c_hp ? c_key_len : 0)
2187a8b2eceSDmitri Tikhonov            + (s_hp ? s_key_len : 0)
2197a8b2eceSDmitri Tikhonov            ;
22050aadb33SDmitri Tikhonov    unsigned char *p;
2217a8b2eceSDmitri Tikhonov    unsigned char output[
2227a8b2eceSDmitri Tikhonov        EVP_MAX_KEY_LENGTH * 2  /* Keys */
2237a8b2eceSDmitri Tikhonov      + EVP_MAX_IV_LENGTH * 2   /* IVs */
2247a8b2eceSDmitri Tikhonov      + 32                      /* Subkey */
2257a8b2eceSDmitri Tikhonov      + EVP_MAX_KEY_LENGTH * 2  /* Header protection */
2267a8b2eceSDmitri Tikhonov    ];
22750aadb33SDmitri Tikhonov
2287a8b2eceSDmitri Tikhonov    assert((size_t) L <= sizeof(output));
22950aadb33SDmitri Tikhonov
2307a8b2eceSDmitri Tikhonov#ifndef NDEBUG
2317a8b2eceSDmitri Tikhonov    const int s =
2327a8b2eceSDmitri Tikhonov#endif
2337a8b2eceSDmitri Tikhonov    HKDF_expand(output, L, EVP_sha256(), prk, 32, info, info_len);
2347a8b2eceSDmitri Tikhonov    assert(s);
2357a8b2eceSDmitri Tikhonov    p = output;
23650aadb33SDmitri Tikhonov    if (c_key_len)
23750aadb33SDmitri Tikhonov    {
23850aadb33SDmitri Tikhonov        memcpy(c_key, p, c_key_len);
23950aadb33SDmitri Tikhonov        p += c_key_len;
24050aadb33SDmitri Tikhonov    }
24150aadb33SDmitri Tikhonov    if (s_key_len)
24250aadb33SDmitri Tikhonov    {
24350aadb33SDmitri Tikhonov        memcpy(s_key, p, s_key_len);
24450aadb33SDmitri Tikhonov        p += s_key_len;
24550aadb33SDmitri Tikhonov    }
24650aadb33SDmitri Tikhonov    if (c_key_iv_len)
24750aadb33SDmitri Tikhonov    {
24850aadb33SDmitri Tikhonov        memcpy(c_key_iv, p, c_key_iv_len);
24950aadb33SDmitri Tikhonov        p += c_key_iv_len;
25050aadb33SDmitri Tikhonov    }
25150aadb33SDmitri Tikhonov    if (s_key_iv_len)
25250aadb33SDmitri Tikhonov    {
25350aadb33SDmitri Tikhonov        memcpy(s_key_iv, p, s_key_iv_len);
25450aadb33SDmitri Tikhonov        p += s_key_iv_len;
25550aadb33SDmitri Tikhonov    }
25650aadb33SDmitri Tikhonov    if (sub_key_len && sub_key)
25750aadb33SDmitri Tikhonov    {
25850aadb33SDmitri Tikhonov        memcpy(sub_key, p, sub_key_len);
25950aadb33SDmitri Tikhonov        p += sub_key_len;
26050aadb33SDmitri Tikhonov    }
2617a8b2eceSDmitri Tikhonov    if (c_key_len && c_hp)
2627a8b2eceSDmitri Tikhonov    {
2637a8b2eceSDmitri Tikhonov        memcpy(c_hp, p, c_key_len);
2647a8b2eceSDmitri Tikhonov        p += c_key_len;
2657a8b2eceSDmitri Tikhonov    }
2667a8b2eceSDmitri Tikhonov    if (s_key_len && s_hp)
2677a8b2eceSDmitri Tikhonov    {
2687a8b2eceSDmitri Tikhonov        memcpy(s_hp, p, s_key_len);
2697a8b2eceSDmitri Tikhonov        p += s_key_len;
2707a8b2eceSDmitri Tikhonov    }
27150aadb33SDmitri Tikhonov    return 0;
27250aadb33SDmitri Tikhonov}
27350aadb33SDmitri Tikhonov
27450aadb33SDmitri Tikhonov
27550aadb33SDmitri Tikhonovint export_key_material_simple(unsigned char *ikm, uint32_t ikm_len,
27650aadb33SDmitri Tikhonov                        unsigned char *salt, int salt_len,
27750aadb33SDmitri Tikhonov                        char *label, uint32_t label_len,
27850aadb33SDmitri Tikhonov                        const uint8_t *context, uint32_t context_len,
27950aadb33SDmitri Tikhonov                        uint8_t *key, uint16_t key_len)
28050aadb33SDmitri Tikhonov{
28150aadb33SDmitri Tikhonov    unsigned char prk[32];
28250aadb33SDmitri Tikhonov    int info_len;
28350aadb33SDmitri Tikhonov    uint8_t *info = NULL;
28450aadb33SDmitri Tikhonov    info = (uint8_t *)malloc(label_len + 1 + sizeof(uint32_t) + context_len);
28550aadb33SDmitri Tikhonov    if (!info)
28650aadb33SDmitri Tikhonov        return -1;
28750aadb33SDmitri Tikhonov
28850aadb33SDmitri Tikhonov    lshkdf_extract(ikm, ikm_len, salt, salt_len, prk);
28950aadb33SDmitri Tikhonov    memcpy(info, label, label_len);
29050aadb33SDmitri Tikhonov    info[label_len] = 0x00;
29150aadb33SDmitri Tikhonov    info_len = label_len + 1;
29250aadb33SDmitri Tikhonov    memcpy(info + info_len, &context_len, sizeof(uint32_t));
29350aadb33SDmitri Tikhonov    info_len += sizeof(uint32_t);
29450aadb33SDmitri Tikhonov    memcpy(info + info_len, context, context_len);
29550aadb33SDmitri Tikhonov    info_len += context_len;
29650aadb33SDmitri Tikhonov    lshkdf_expand(prk, info, info_len, key_len, key,
2977a8b2eceSDmitri Tikhonov                0, NULL, 0, NULL,0, NULL, 0, NULL, NULL, NULL);
29850aadb33SDmitri Tikhonov    free(info);
29950aadb33SDmitri Tikhonov    return 0;
30050aadb33SDmitri Tikhonov}
30150aadb33SDmitri Tikhonov
30250aadb33SDmitri Tikhonov
3037a8b2eceSDmitri Tikhonovint
3047a8b2eceSDmitri Tikhonovlsquic_export_key_material(const unsigned char *ikm, uint32_t ikm_len,
30550aadb33SDmitri Tikhonov                        const unsigned char *salt, int salt_len,
30650aadb33SDmitri Tikhonov                        const unsigned char *context, uint32_t context_len,
30750aadb33SDmitri Tikhonov                        uint16_t c_key_len, uint8_t *c_key,
30850aadb33SDmitri Tikhonov                        uint16_t s_key_len, uint8_t *s_key,
30950aadb33SDmitri Tikhonov                        uint16_t c_key_iv_len, uint8_t *c_key_iv,
31050aadb33SDmitri Tikhonov                        uint16_t s_key_iv_len, uint8_t *s_key_iv,
3117a8b2eceSDmitri Tikhonov                        uint8_t *sub_key, uint8_t *c_hp, uint8_t *s_hp)
31250aadb33SDmitri Tikhonov{
31350aadb33SDmitri Tikhonov    unsigned char prk[32];
31450aadb33SDmitri Tikhonov    uint16_t sub_key_len = ikm_len;
31550aadb33SDmitri Tikhonov
31650aadb33SDmitri Tikhonov    lshkdf_extract(ikm, ikm_len, salt, salt_len, prk);
31750aadb33SDmitri Tikhonov    lshkdf_expand(prk, context, context_len, c_key_len, c_key,
31850aadb33SDmitri Tikhonov                s_key_len, s_key, c_key_iv_len, c_key_iv, s_key_iv_len,
3197a8b2eceSDmitri Tikhonov                s_key_iv, sub_key_len, sub_key, c_hp, s_hp);
32050aadb33SDmitri Tikhonov    return 0;
32150aadb33SDmitri Tikhonov}
32250aadb33SDmitri Tikhonov
32350aadb33SDmitri Tikhonovvoid c255_get_pub_key(unsigned char *priv_key, unsigned char pub_key[32])
32450aadb33SDmitri Tikhonov{
32550aadb33SDmitri Tikhonov    X25519_public_from_private(pub_key, priv_key);
32650aadb33SDmitri Tikhonov}
32750aadb33SDmitri Tikhonov
32850aadb33SDmitri Tikhonov
32950aadb33SDmitri Tikhonovint c255_gen_share_key(unsigned char *priv_key, unsigned char *peer_pub_key, unsigned char *shared_key)
33050aadb33SDmitri Tikhonov{
33150aadb33SDmitri Tikhonov    return X25519(shared_key, priv_key, peer_pub_key);
33250aadb33SDmitri Tikhonov}
33350aadb33SDmitri Tikhonov
33450aadb33SDmitri Tikhonov
33550aadb33SDmitri Tikhonov
33650aadb33SDmitri Tikhonov/* AEAD nonce is always zero */
33750aadb33SDmitri Tikhonov/* return 0 for OK */
33850aadb33SDmitri Tikhonovint aes_aead_enc(EVP_AEAD_CTX *key,
33950aadb33SDmitri Tikhonov              const uint8_t *ad, size_t ad_len,
34050aadb33SDmitri Tikhonov              const uint8_t *nonce, size_t nonce_len,
34150aadb33SDmitri Tikhonov              const uint8_t *plain, size_t plain_len,
34250aadb33SDmitri Tikhonov              uint8_t *cypher, size_t *cypher_len)
34350aadb33SDmitri Tikhonov{
34450aadb33SDmitri Tikhonov    int ret = 0;
34550aadb33SDmitri Tikhonov    size_t max_out_len;
34650aadb33SDmitri Tikhonov    max_out_len = *cypher_len;//plain_len + EVP_AEAD_max_overhead(aead_);
34750aadb33SDmitri Tikhonov    assert(*cypher_len >= max_out_len);
34850aadb33SDmitri Tikhonov
34950aadb33SDmitri Tikhonov    LSQ_DEBUG("***aes_aead_enc data %s", get_bin_str(plain, plain_len, 40));
35050aadb33SDmitri Tikhonov    ret = EVP_AEAD_CTX_seal(key, cypher, cypher_len, max_out_len,
35150aadb33SDmitri Tikhonov                            nonce, nonce_len, plain, plain_len, ad, ad_len);
35250aadb33SDmitri Tikhonov//     LSQ_DEBUG("***aes_aead_enc nonce: %s", get_bin_str(nonce, nonce_len));
35350aadb33SDmitri Tikhonov//     LSQ_DEBUG("***aes_aead_enc AD: %s", get_bin_str(ad, ad_len));
35450aadb33SDmitri Tikhonov//     LSQ_DEBUG("***aes_aead_enc return %d", (ret ? 0 : -1));
35550aadb33SDmitri Tikhonov    if (ret)
35650aadb33SDmitri Tikhonov    {
35750aadb33SDmitri Tikhonov        LSQ_DEBUG("***aes_aead_enc succeed, cypher content %s",
35850aadb33SDmitri Tikhonov                  get_bin_str(cypher, *cypher_len, 40));
35950aadb33SDmitri Tikhonov        return 0;
36050aadb33SDmitri Tikhonov    }
36150aadb33SDmitri Tikhonov    else
36250aadb33SDmitri Tikhonov    {
36350aadb33SDmitri Tikhonov        LSQ_DEBUG("***aes_aead_enc failed.");
36450aadb33SDmitri Tikhonov        return -1;
36550aadb33SDmitri Tikhonov    }
36650aadb33SDmitri Tikhonov}
36750aadb33SDmitri Tikhonov
36850aadb33SDmitri Tikhonov
36950aadb33SDmitri Tikhonov/* return 0 for OK */
37050aadb33SDmitri Tikhonovint aes_aead_dec(EVP_AEAD_CTX *key,
37150aadb33SDmitri Tikhonov              const uint8_t *ad, size_t ad_len,
37250aadb33SDmitri Tikhonov              const uint8_t *nonce, size_t nonce_len,
37350aadb33SDmitri Tikhonov              const uint8_t *cypher, size_t cypher_len,
37450aadb33SDmitri Tikhonov              uint8_t *plain, size_t *plain_len)
37550aadb33SDmitri Tikhonov{
37650aadb33SDmitri Tikhonov    int ret = 0;
37750aadb33SDmitri Tikhonov    size_t max_out_len = *plain_len;
37850aadb33SDmitri Tikhonov    assert(max_out_len >= cypher_len);
37950aadb33SDmitri Tikhonov
38050aadb33SDmitri Tikhonov    LSQ_DEBUG("***aes_aead_dec data %s", get_bin_str(cypher, cypher_len, 40));
38150aadb33SDmitri Tikhonov
38250aadb33SDmitri Tikhonov
38350aadb33SDmitri Tikhonov    ret = EVP_AEAD_CTX_open(key, plain, plain_len, max_out_len,
38450aadb33SDmitri Tikhonov                            nonce, nonce_len, cypher, cypher_len, ad, ad_len);
38550aadb33SDmitri Tikhonov
38650aadb33SDmitri Tikhonov//    LSQ_DEBUG("***aes_aead_dec nonce: %s", get_bin_str(nonce, nonce_len));
38750aadb33SDmitri Tikhonov//    LSQ_DEBUG("***aes_aead_dec AD: %s", get_bin_str(ad, ad_len));
38850aadb33SDmitri Tikhonov//    LSQ_DEBUG("***aes_aead_dec return %d", (ret ? 0 : -1));
38950aadb33SDmitri Tikhonov    if (ret)
39050aadb33SDmitri Tikhonov    {
39150aadb33SDmitri Tikhonov        LSQ_DEBUG("***aes_aead_dec succeed, plain content %s",
39250aadb33SDmitri Tikhonov              get_bin_str(plain, *plain_len, 20));
39350aadb33SDmitri Tikhonov        return 0;
39450aadb33SDmitri Tikhonov    }
39550aadb33SDmitri Tikhonov    else
39650aadb33SDmitri Tikhonov    {
39750aadb33SDmitri Tikhonov        LSQ_DEBUG("***aes_aead_dec failed.");
39850aadb33SDmitri Tikhonov        return -1;
39950aadb33SDmitri Tikhonov    }
40050aadb33SDmitri Tikhonov}
40150aadb33SDmitri Tikhonov
40250aadb33SDmitri Tikhonov/* 32 bytes client nonce with 4 bytes tm, 8 bytes orbit */
40350aadb33SDmitri Tikhonovvoid gen_nonce_c(unsigned char *buf, uint64_t orbit)
40450aadb33SDmitri Tikhonov{
40550aadb33SDmitri Tikhonov    time_t tm = time(NULL);
40650aadb33SDmitri Tikhonov    unsigned char *p = buf;
40750aadb33SDmitri Tikhonov    memcpy(p, &tm, 4);
40850aadb33SDmitri Tikhonov    p += 4;
40950aadb33SDmitri Tikhonov    memcpy(p, &orbit, 8);
41050aadb33SDmitri Tikhonov    p += 8;
41150aadb33SDmitri Tikhonov    rand_bytes(p, 20);
41250aadb33SDmitri Tikhonov    p += 20;
41350aadb33SDmitri Tikhonov}
41450aadb33SDmitri Tikhonov
41550aadb33SDmitri Tikhonov
41650aadb33SDmitri TikhonovEVP_PKEY *PEM_to_key(const char *buf, int len)
41750aadb33SDmitri Tikhonov{
41850aadb33SDmitri Tikhonov    RSA *rsa = NULL;
41950aadb33SDmitri Tikhonov    EVP_PKEY *key = EVP_PKEY_new();
42050aadb33SDmitri Tikhonov    BIO *bio = BIO_new_mem_buf(buf, len);
42150aadb33SDmitri Tikhonov    if (!bio || !key)
42250aadb33SDmitri Tikhonov        return NULL;
42350aadb33SDmitri Tikhonov
42450aadb33SDmitri Tikhonov    rsa = PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL);
42550aadb33SDmitri Tikhonov    if (!rsa)
42650aadb33SDmitri Tikhonov        return NULL;
42750aadb33SDmitri Tikhonov
42850aadb33SDmitri Tikhonov    EVP_PKEY_assign_RSA(key, rsa);
42950aadb33SDmitri Tikhonov    return key;
43050aadb33SDmitri Tikhonov}
43150aadb33SDmitri Tikhonov
43250aadb33SDmitri Tikhonov
43350aadb33SDmitri Tikhonov/* type 0 DER, 1: PEM */
43450aadb33SDmitri TikhonovX509 *bio_to_crt(const void *buf, int len, int type)
43550aadb33SDmitri Tikhonov{
43650aadb33SDmitri Tikhonov    X509 *crt = NULL;
43750aadb33SDmitri Tikhonov    BIO *bio = BIO_new_mem_buf(buf, len);
43850aadb33SDmitri Tikhonov    if (bio == NULL)
43950aadb33SDmitri Tikhonov        return NULL;
44050aadb33SDmitri Tikhonov
44150aadb33SDmitri Tikhonov    if (type == 0)
44250aadb33SDmitri Tikhonov        crt = d2i_X509_bio(bio, NULL);
44350aadb33SDmitri Tikhonov    else
44450aadb33SDmitri Tikhonov        crt = PEM_read_bio_X509(bio, &crt, 0 , NULL);
44550aadb33SDmitri Tikhonov    BIO_free(bio);
44650aadb33SDmitri Tikhonov    return crt;
44750aadb33SDmitri Tikhonov}
44850aadb33SDmitri Tikhonov
44950aadb33SDmitri Tikhonov
45050aadb33SDmitri Tikhonovint gen_prof(const uint8_t *chlo_data, size_t chlo_data_len,
45150aadb33SDmitri Tikhonov             const uint8_t *scfg_data, uint32_t scfg_data_len,
45250aadb33SDmitri Tikhonov             const EVP_PKEY *priv_key, uint8_t *buf, size_t *buf_len)
45350aadb33SDmitri Tikhonov{
45450aadb33SDmitri Tikhonov    uint8_t chlo_hash[32] = {0};
45550aadb33SDmitri Tikhonov    size_t chlo_hash_len = 32; /* SHA256 */
45650aadb33SDmitri Tikhonov    EVP_MD_CTX sign_context;
45750aadb33SDmitri Tikhonov    EVP_PKEY_CTX* pkey_ctx = NULL;
45850aadb33SDmitri Tikhonov
45950aadb33SDmitri Tikhonov    sha256(chlo_data, chlo_data_len, chlo_hash);
46050aadb33SDmitri Tikhonov    EVP_MD_CTX_init(&sign_context);
46150aadb33SDmitri Tikhonov    if (!EVP_DigestSignInit(&sign_context, &pkey_ctx, EVP_sha256(), NULL, (EVP_PKEY *)priv_key))
46250aadb33SDmitri Tikhonov        return -1;
46350aadb33SDmitri Tikhonov
46450aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING);
46550aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1);
46650aadb33SDmitri Tikhonov
46750aadb33SDmitri Tikhonov    if (!EVP_DigestSignUpdate(&sign_context, s_hs_signature, sizeof(s_hs_signature)) ||
46850aadb33SDmitri Tikhonov        !EVP_DigestSignUpdate(&sign_context, (const uint8_t*)(&chlo_hash_len), 4) ||
46950aadb33SDmitri Tikhonov        !EVP_DigestSignUpdate(&sign_context, chlo_hash, chlo_hash_len) ||
47050aadb33SDmitri Tikhonov        !EVP_DigestSignUpdate(&sign_context, scfg_data, scfg_data_len))
47150aadb33SDmitri Tikhonov    {
47250aadb33SDmitri Tikhonov        return -1;
47350aadb33SDmitri Tikhonov    }
47450aadb33SDmitri Tikhonov
47550aadb33SDmitri Tikhonov    size_t len = 0;
47650aadb33SDmitri Tikhonov    if (!EVP_DigestSignFinal(&sign_context, NULL, &len)) {
47750aadb33SDmitri Tikhonov        return -1;
47850aadb33SDmitri Tikhonov    }
47950aadb33SDmitri Tikhonov
48050aadb33SDmitri Tikhonov    if (len > *buf_len)
48150aadb33SDmitri Tikhonov        return -2;
48250aadb33SDmitri Tikhonov    if (buf)
48350aadb33SDmitri Tikhonov        EVP_DigestSignFinal(&sign_context, buf, buf_len);
48450aadb33SDmitri Tikhonov
48550aadb33SDmitri Tikhonov    EVP_MD_CTX_cleanup(&sign_context);
48650aadb33SDmitri Tikhonov    return 0;
48750aadb33SDmitri Tikhonov}
48850aadb33SDmitri Tikhonov
48950aadb33SDmitri Tikhonov
49050aadb33SDmitri Tikhonovint verify_prof(const uint8_t *chlo_data, size_t chlo_data_len, lsquic_str_t * scfg,
49150aadb33SDmitri Tikhonov                const EVP_PKEY *pub_key, const uint8_t *buf, size_t len)
49250aadb33SDmitri Tikhonov{
49350aadb33SDmitri Tikhonov    return verify_prof0(chlo_data, chlo_data_len,
49450aadb33SDmitri Tikhonov                        (const uint8_t *)lsquic_str_buf(scfg),
49550aadb33SDmitri Tikhonov                        lsquic_str_len(scfg), pub_key, buf, len);
49650aadb33SDmitri Tikhonov}
49750aadb33SDmitri Tikhonov
49850aadb33SDmitri Tikhonov
49950aadb33SDmitri Tikhonov
50050aadb33SDmitri Tikhonov
50150aadb33SDmitri Tikhonov/* -3 internal error, -1: verify failed, 0: Success */
50250aadb33SDmitri Tikhonovint verify_prof0(const uint8_t *chlo_data, size_t chlo_data_len,
50350aadb33SDmitri Tikhonov                const uint8_t *scfg_data, uint32_t scfg_data_len,
50450aadb33SDmitri Tikhonov                const EVP_PKEY *pub_key, const uint8_t *buf, size_t len)
50550aadb33SDmitri Tikhonov{
50650aadb33SDmitri Tikhonov    uint8_t chlo_hash[32] = {0};
50750aadb33SDmitri Tikhonov    size_t chlo_hash_len = 32; /* SHA256 */
50850aadb33SDmitri Tikhonov    EVP_MD_CTX sign_context;
50950aadb33SDmitri Tikhonov    EVP_PKEY_CTX* pkey_ctx = NULL;
51050aadb33SDmitri Tikhonov    int ret = 0;
51150aadb33SDmitri Tikhonov    EVP_MD_CTX_init(&sign_context);
51250aadb33SDmitri Tikhonov    sha256(chlo_data, chlo_data_len, chlo_hash);
51350aadb33SDmitri Tikhonov
51450aadb33SDmitri Tikhonov    // discarding const below to quiet compiler warning on call to ssl library code
51550aadb33SDmitri Tikhonov    if (!EVP_DigestVerifyInit(&sign_context, &pkey_ctx, EVP_sha256(), NULL, (EVP_PKEY *)pub_key))
51650aadb33SDmitri Tikhonov        return -4;
51750aadb33SDmitri Tikhonov
51850aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING);
51950aadb33SDmitri Tikhonov    EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, -1);
52050aadb33SDmitri Tikhonov
52150aadb33SDmitri Tikhonov
52250aadb33SDmitri Tikhonov    if (!EVP_DigestVerifyUpdate(&sign_context, s_hs_signature, sizeof(s_hs_signature)) ||
52350aadb33SDmitri Tikhonov        !EVP_DigestVerifyUpdate(&sign_context, (const uint8_t*)(&chlo_hash_len), 4) ||
52450aadb33SDmitri Tikhonov        !EVP_DigestVerifyUpdate(&sign_context, chlo_hash, chlo_hash_len) ||
52550aadb33SDmitri Tikhonov        !EVP_DigestVerifyUpdate(&sign_context, scfg_data, scfg_data_len))
52650aadb33SDmitri Tikhonov    {
52750aadb33SDmitri Tikhonov        return -3;  /* set to -3, to avoid same as "not enough data" -2 */
52850aadb33SDmitri Tikhonov    }
52950aadb33SDmitri Tikhonov
53050aadb33SDmitri Tikhonov    ret = EVP_DigestVerifyFinal(&sign_context, buf, len);
53150aadb33SDmitri Tikhonov    EVP_MD_CTX_cleanup(&sign_context);
53250aadb33SDmitri Tikhonov
53350aadb33SDmitri Tikhonov    if (ret == 1)
53450aadb33SDmitri Tikhonov        return 0; //OK
53550aadb33SDmitri Tikhonov    else
53650aadb33SDmitri Tikhonov        return -1;  //failed
53750aadb33SDmitri Tikhonov}
53850aadb33SDmitri Tikhonov
53950aadb33SDmitri Tikhonov
54067b0dc15SDmitri Tikhonovvoid crypto_init(void)
54150aadb33SDmitri Tikhonov{
54250aadb33SDmitri Tikhonov    if (crypto_inited)
54350aadb33SDmitri Tikhonov        return ;
54450aadb33SDmitri Tikhonov
54550aadb33SDmitri Tikhonov    //SSL_library_init();
54650aadb33SDmitri Tikhonov    CRYPTO_library_init();
54767b0dc15SDmitri Tikhonov    /* XXX Should we seed? If yes, wherewith? */ // RAND_seed(seed, seed_len);
54850aadb33SDmitri Tikhonov
54950aadb33SDmitri Tikhonov#if defined( __x86_64 )||defined( __x86_64__ )
55050aadb33SDmitri Tikhonov    make_uint128(&s_prime, 16777216, 315);
55150aadb33SDmitri Tikhonov    make_uint128(&s_init_hash, 7809847782465536322, 7113472399480571277);
55250aadb33SDmitri Tikhonov#endif
55350aadb33SDmitri Tikhonov
55450aadb33SDmitri Tikhonov    /* MORE .... */
55550aadb33SDmitri Tikhonov    crypto_inited = 1;
55650aadb33SDmitri Tikhonov}
55750aadb33SDmitri Tikhonov
558