1 /* $OpenBSD: kexsntrup4591761x25519.c,v 1.3 2019/01/21 10:40:11 djm Exp $ */
3 * Copyright (c) 2019 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
41 kex_kem_sntrup4591761x25519_keypair(struct kex *kex)
43 struct sshbuf *buf = NULL;
48 if ((buf = sshbuf_new()) == NULL)
49 return SSH_ERR_ALLOC_FAIL;
50 need = crypto_kem_sntrup4591761_PUBLICKEYBYTES + CURVE25519_SIZE;
51 if ((r = sshbuf_reserve(buf, need, &cp)) != 0)
53 crypto_kem_sntrup4591761_keypair(cp, kex->sntrup4591761_client_key);
55 dump_digest("client public key sntrup4591761:", cp,
56 crypto_kem_sntrup4591761_PUBLICKEYBYTES);
58 cp += crypto_kem_sntrup4591761_PUBLICKEYBYTES;
59 kexc25519_keygen(kex->c25519_client_key, cp);
61 dump_digest("client public key c25519:", cp, CURVE25519_SIZE);
63 kex->client_pub = buf;
71 kex_kem_sntrup4591761x25519_enc(struct kex *kex,
72 const struct sshbuf *client_blob, struct sshbuf **server_blobp,
73 struct sshbuf **shared_secretp)
75 struct sshbuf *server_blob = NULL;
76 struct sshbuf *buf = NULL;
77 const u_char *client_pub;
78 u_char *kem_key, *ciphertext, *server_pub;
79 u_char server_key[CURVE25519_SIZE];
80 u_char hash[SSH_DIGEST_MAX_LENGTH];
85 *shared_secretp = NULL;
87 /* client_blob contains both KEM and ECDH client pubkeys */
88 need = crypto_kem_sntrup4591761_PUBLICKEYBYTES + CURVE25519_SIZE;
89 if (sshbuf_len(client_blob) != need) {
90 r = SSH_ERR_SIGNATURE_INVALID;
93 client_pub = sshbuf_ptr(client_blob);
95 dump_digest("client public key sntrup4591761:", client_pub,
96 crypto_kem_sntrup4591761_PUBLICKEYBYTES);
97 dump_digest("client public key 25519:",
98 client_pub + crypto_kem_sntrup4591761_PUBLICKEYBYTES,
101 /* allocate buffer for concatenation of KEM key and ECDH shared key */
102 /* the buffer will be hashed and the result is the shared secret */
103 if ((buf = sshbuf_new()) == NULL) {
104 r = SSH_ERR_ALLOC_FAIL;
107 if ((r = sshbuf_reserve(buf, crypto_kem_sntrup4591761_BYTES,
110 /* allocate space for encrypted KEM key and ECDH pub key */
111 if ((server_blob = sshbuf_new()) == NULL) {
112 r = SSH_ERR_ALLOC_FAIL;
115 need = crypto_kem_sntrup4591761_CIPHERTEXTBYTES + CURVE25519_SIZE;
116 if ((r = sshbuf_reserve(server_blob, need, &ciphertext)) != 0)
118 /* generate and encrypt KEM key with client key */
119 crypto_kem_sntrup4591761_enc(ciphertext, kem_key, client_pub);
120 /* generate ECDH key pair, store server pubkey after ciphertext */
121 server_pub = ciphertext + crypto_kem_sntrup4591761_CIPHERTEXTBYTES;
122 kexc25519_keygen(server_key, server_pub);
123 /* append ECDH shared key */
124 client_pub += crypto_kem_sntrup4591761_PUBLICKEYBYTES;
125 if ((r = kexc25519_shared_key_ext(server_key, client_pub, buf, 1)) < 0)
127 if ((r = ssh_digest_buffer(kex->hash_alg, buf, hash, sizeof(hash))) != 0)
130 dump_digest("server public key 25519:", server_pub, CURVE25519_SIZE);
131 dump_digest("server cipher text:", ciphertext,
132 crypto_kem_sntrup4591761_CIPHERTEXTBYTES);
133 dump_digest("server kem key:", kem_key, sizeof(kem_key));
134 dump_digest("concatenation of KEM key and ECDH shared key:",
135 sshbuf_ptr(buf), sshbuf_len(buf));
137 /* string-encoded hash is resulting shared secret */
139 if ((r = sshbuf_put_string(buf, hash,
140 ssh_digest_bytes(kex->hash_alg))) != 0)
143 dump_digest("encoded shared secret:", sshbuf_ptr(buf), sshbuf_len(buf));
145 *server_blobp = server_blob;
146 *shared_secretp = buf;
150 explicit_bzero(hash, sizeof(hash));
151 explicit_bzero(server_key, sizeof(server_key));
152 sshbuf_free(server_blob);
158 kex_kem_sntrup4591761x25519_dec(struct kex *kex,
159 const struct sshbuf *server_blob, struct sshbuf **shared_secretp)
161 struct sshbuf *buf = NULL;
162 u_char *kem_key = NULL;
163 const u_char *ciphertext, *server_pub;
164 u_char hash[SSH_DIGEST_MAX_LENGTH];
168 *shared_secretp = NULL;
170 need = crypto_kem_sntrup4591761_CIPHERTEXTBYTES + CURVE25519_SIZE;
171 if (sshbuf_len(server_blob) != need) {
172 r = SSH_ERR_SIGNATURE_INVALID;
175 ciphertext = sshbuf_ptr(server_blob);
176 server_pub = ciphertext + crypto_kem_sntrup4591761_CIPHERTEXTBYTES;
178 dump_digest("server cipher text:", ciphertext,
179 crypto_kem_sntrup4591761_CIPHERTEXTBYTES);
180 dump_digest("server public key c25519:", server_pub, CURVE25519_SIZE);
182 /* hash concatenation of KEM key and ECDH shared key */
183 if ((buf = sshbuf_new()) == NULL) {
184 r = SSH_ERR_ALLOC_FAIL;
187 if ((r = sshbuf_reserve(buf, crypto_kem_sntrup4591761_BYTES,
190 decoded = crypto_kem_sntrup4591761_dec(kem_key, ciphertext,
191 kex->sntrup4591761_client_key);
192 if ((r = kexc25519_shared_key_ext(kex->c25519_client_key, server_pub,
195 if ((r = ssh_digest_buffer(kex->hash_alg, buf, hash, sizeof(hash))) != 0)
198 dump_digest("client kem key:", kem_key, sizeof(kem_key));
199 dump_digest("concatenation of KEM key and ECDH shared key:",
200 sshbuf_ptr(buf), sshbuf_len(buf));
203 if ((r = sshbuf_put_string(buf, hash,
204 ssh_digest_bytes(kex->hash_alg))) != 0)
207 dump_digest("encoded shared secret:", sshbuf_ptr(buf), sshbuf_len(buf));
210 r = SSH_ERR_SIGNATURE_INVALID;
213 *shared_secretp = buf;
216 explicit_bzero(hash, sizeof(hash));