Coverage Report

Created: 2025-07-11 06:24

/src/h2o/deps/picotls/lib/cifra/x25519.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#include <stdlib.h>
23
#include "curve25519.h"
24
#include "picotls.h"
25
#include "picotls/minicrypto.h"
26
27
0
#define X25519_KEY_SIZE 32
28
29
struct st_x25519_key_exchange_t {
30
    ptls_key_exchange_context_t super;
31
    uint8_t priv[X25519_KEY_SIZE];
32
    uint8_t pub[X25519_KEY_SIZE];
33
};
34
35
static void x25519_create_keypair(uint8_t *priv, uint8_t *pub)
36
0
{
37
0
    ptls_minicrypto_random_bytes(priv, X25519_KEY_SIZE);
38
0
    cf_curve25519_mul_base(pub, priv);
39
0
}
40
41
static int x25519_derive_secret(ptls_iovec_t *secret, const uint8_t *clientpriv, const uint8_t *clientpub,
42
                                const uint8_t *serverpriv, const uint8_t *serverpub)
43
0
{
44
0
    if ((secret->base = malloc(X25519_KEY_SIZE)) == NULL)
45
0
        return PTLS_ERROR_NO_MEMORY;
46
47
0
    cf_curve25519_mul(secret->base, clientpriv != NULL ? clientpriv : serverpriv, clientpriv != NULL ? serverpub : clientpub);
48
49
0
    static const uint8_t zeros[X25519_KEY_SIZE] = {0};
50
0
    if (ptls_mem_equal(secret->base, zeros, sizeof(zeros))) {
51
0
        free(secret->base);
52
0
        secret->base = NULL;
53
0
        return PTLS_ERROR_INCOMPATIBLE_KEY;
54
0
    }
55
56
0
    secret->len = X25519_KEY_SIZE;
57
0
    return 0;
58
0
}
59
60
static int x25519_on_exchange(ptls_key_exchange_context_t **_ctx, int release, ptls_iovec_t *secret, ptls_iovec_t peerkey)
61
0
{
62
0
    struct st_x25519_key_exchange_t *ctx = (struct st_x25519_key_exchange_t *)*_ctx;
63
0
    int ret;
64
65
0
    if (secret == NULL) {
66
0
        ret = 0;
67
0
        goto Exit;
68
0
    }
69
70
0
    if (peerkey.len != X25519_KEY_SIZE) {
71
0
        ret = PTLS_ALERT_DECRYPT_ERROR;
72
0
        goto Exit;
73
0
    }
74
0
    ret = x25519_derive_secret(secret, ctx->priv, ctx->pub, NULL, peerkey.base);
75
76
0
Exit:
77
0
    if (release) {
78
0
        ptls_clear_memory(ctx->priv, sizeof(ctx->priv));
79
0
        free(ctx);
80
0
        *_ctx = NULL;
81
0
    }
82
0
    return ret;
83
0
}
84
85
static int x25519_create_key_exchange(ptls_key_exchange_algorithm_t *algo, ptls_key_exchange_context_t **_ctx)
86
0
{
87
0
    struct st_x25519_key_exchange_t *ctx;
88
89
0
    if ((ctx = (struct st_x25519_key_exchange_t *)malloc(sizeof(*ctx))) == NULL)
90
0
        return PTLS_ERROR_NO_MEMORY;
91
0
    ctx->super = (ptls_key_exchange_context_t){algo, ptls_iovec_init(ctx->pub, sizeof(ctx->pub)), x25519_on_exchange};
92
0
    x25519_create_keypair(ctx->priv, ctx->pub);
93
94
0
    *_ctx = &ctx->super;
95
0
    return 0;
96
0
}
97
98
static int x25519_key_exchange(ptls_key_exchange_algorithm_t *algo, ptls_iovec_t *pubkey, ptls_iovec_t *secret,
99
                               ptls_iovec_t peerkey)
100
0
{
101
0
    uint8_t priv[X25519_KEY_SIZE], *pub = NULL;
102
0
    int ret;
103
104
0
    if (peerkey.len != X25519_KEY_SIZE) {
105
0
        ret = PTLS_ALERT_DECRYPT_ERROR;
106
0
        goto Exit;
107
0
    }
108
0
    if ((pub = malloc(X25519_KEY_SIZE)) == NULL) {
109
0
        ret = PTLS_ERROR_NO_MEMORY;
110
0
        goto Exit;
111
0
    }
112
113
0
    x25519_create_keypair(priv, pub);
114
0
    if ((ret = x25519_derive_secret(secret, NULL, peerkey.base, priv, pub)) != 0)
115
0
        goto Exit;
116
117
0
    *pubkey = ptls_iovec_init(pub, X25519_KEY_SIZE);
118
0
    ret = 0;
119
120
0
Exit:
121
0
    ptls_clear_memory(priv, sizeof(priv));
122
0
    if (pub != NULL && ret != 0) {
123
0
        ptls_clear_memory(pub, X25519_KEY_SIZE);
124
0
        free(pub);
125
0
    }
126
0
    return ret;
127
0
}
128
129
ptls_key_exchange_algorithm_t ptls_minicrypto_x25519 = {
130
    .id = PTLS_GROUP_X25519, .name = PTLS_GROUP_NAME_X25519, .create = x25519_create_key_exchange, .exchange = x25519_key_exchange};