File: sshcrypto_kex.c

package info (click to toggle)
tinyssh 20250501-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (181 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
20140203
20241210 - reformated using clang-format
Jan Mojzis
Public domain.
*/

#include "crypto.h"
#include "stringparser.h"
#include "str.h"
#include "byte.h"
#include "e.h"
#include "log.h"
#include "bug.h"
#include "sshcrypto.h"

struct sshcrypto_kex sshcrypto_kexs[] = {
    {
        "curve25519-sha256",
        curve25519_enc,
        crypto_scalarmult_curve25519_BYTES, /* pk */
        crypto_scalarmult_curve25519_BYTES, /* c */
        crypto_scalarmult_curve25519_BYTES, /* k */
        crypto_hash_sha256,
        crypto_hash_sha256_BYTES,
        curve25519_putkemkey,
        sshcrypto_TYPENEWCRYPTO,
        0,
    },
    {
        "curve25519-sha256@libssh.org",
        curve25519_enc,
        crypto_scalarmult_curve25519_BYTES, /* pk */
        crypto_scalarmult_curve25519_BYTES, /* c */
        crypto_scalarmult_curve25519_BYTES, /* k */
        crypto_hash_sha256,
        crypto_hash_sha256_BYTES,
        curve25519_putkemkey,
        sshcrypto_TYPENEWCRYPTO,
        0,
    },
    {
        "sntrup761x25519-sha512@openssh.com",
        crypto_kem_sntrup761x25519_enc,
        crypto_kem_sntrup761x25519_PUBLICKEYBYTES,  /* pk */
        crypto_kem_sntrup761x25519_CIPHERTEXTBYTES, /* c */
        crypto_kem_sntrup761x25519_BYTES,           /* k */
        crypto_hash_sha512,
        crypto_hash_sha512_BYTES,
        sntrup761x25519_putkemkey,
        sshcrypto_TYPEPQCRYPTO,
        0,
    },
    {
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    },
};

struct sshcrypto_pseudokex sshcrypto_pseudokexs[] = {
    {
        "kex-strict-s-v00@openssh.com",
        "kex-strict-c-v00@openssh.com",
        sshcrypto_FLAGSTRICTKEX,
    },
    {
        0,
        0,
        0,
    },
};

int sshcrypto_kex_flags = 0;
const char *sshcrypto_kex_name = 0;
int (*sshcrypto_enc)(unsigned char *, unsigned char *,
                     const unsigned char *) = 0;
long long sshcrypto_kem_publickeybytes = 0;
long long sshcrypto_kem_ciphertextbytes = 0;
long long sshcrypto_kem_bytes = 0;
int (*sshcrypto_hash)(unsigned char *, const unsigned char *,
                      unsigned long long) = 0;
long long sshcrypto_hash_bytes = 0;
void (*sshcrypto_buf_putkemkey)(struct buf *, const unsigned char *) = 0;

int sshcrypto_kex_select(const unsigned char *buf, long long len,
                         crypto_uint8 *kex_guess) {

    long long i, pos;
    const unsigned char *x;
    long long xlen;

    if (sshcrypto_kex_name) return 1;

    if (buf[len] != 0) bug_proto();
    log_d2("kex: client: kex algorithms: ", (const char *) buf);

    *kex_guess = 1;

    pos = 0;
    for (;;) {
        pos = stringparser(buf, len, pos, &x, &xlen);
        if (!pos) break;
        for (i = 0; sshcrypto_pseudokexs[i].name; ++i) {
            if (str_equaln((const char *) x, xlen,
                           sshcrypto_pseudokexs[i].cname)) {
                log_d2("kex: pseudokex selected: ",
                       sshcrypto_pseudokexs[i].name);
                sshcrypto_kex_flags |= sshcrypto_pseudokexs[i].flag;
            }
        }
    }

    pos = 0;
    for (;;) {
        pos = stringparser(buf, len, pos, &x, &xlen);
        if (!pos) break;

        for (i = 0; sshcrypto_kexs[i].name; ++i) {
            if (!sshcrypto_kexs[i].flagenabled) continue;
            if (str_equaln((const char *) x, xlen, sshcrypto_kexs[i].name)) {
                sshcrypto_kex_name = sshcrypto_kexs[i].name;
                sshcrypto_enc = sshcrypto_kexs[i].enc;
                sshcrypto_kem_publickeybytes =
                    sshcrypto_kexs[i].kem_publickeybytes;
                sshcrypto_kem_ciphertextbytes =
                    sshcrypto_kexs[i].kem_ciphertextbytes;
                sshcrypto_kem_bytes = sshcrypto_kexs[i].kem_bytes;
                sshcrypto_hash = sshcrypto_kexs[i].hash;
                sshcrypto_hash_bytes = sshcrypto_kexs[i].hash_bytes;
                sshcrypto_buf_putkemkey = sshcrypto_kexs[i].buf_putkemkey;
                log_d2("kex: kex selected: ", sshcrypto_kexs[i].name);
                return 1;
            }
        }
        *kex_guess = 0;
    }
    log_d2("kex: kex not available ", (const char *) buf);
    errno = EPROTO;
    return 0;
}

void sshcrypto_kex_put(struct buf *b) {

    crypto_uint32 len = 0;
    long long i, j, start;

    j = 0;
    for (i = 0; sshcrypto_kexs[i].name; ++i) {
        if (!sshcrypto_kexs[i].flagenabled) continue;
        if (j++) ++len;
        len += str_len(sshcrypto_kexs[i].name);
    }
    for (i = 0; sshcrypto_pseudokexs[i].name; ++i) {
        if (j++) ++len;
        len += str_len(sshcrypto_pseudokexs[i].name);
    }

    buf_putnum32(b, len);
    start = b->len;

    j = 0;
    for (i = 0; sshcrypto_kexs[i].name; ++i) {
        if (!sshcrypto_kexs[i].flagenabled) continue;
        if (j++) buf_puts(b, ",");
        buf_puts(b, sshcrypto_kexs[i].name);
    }
    for (i = 0; sshcrypto_pseudokexs[i].name; ++i) {
        if (j++) buf_puts(b, ",");
        buf_puts(b, sshcrypto_pseudokexs[i].name);
    }
    b->buf[b->len] = 0;
    log_d2("kex: server: kex algorithms: ", (char *) b->buf + start);
}