blob: 3b9b664f8b1fc955b50db69fdf3c78fb7718d03a [file] [log] [blame]
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +00001/* $OpenBSD: kexsntrup4591761x25519.c,v 1.3 2019/01/21 10:40:11 djm Exp $ */
djm@openbsd.orgdfd59162019-01-21 10:20:12 +00002/*
3 * Copyright (c) 2019 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
Damien Millerd50ab3c2019-01-22 00:02:23 +110026#include "includes.h"
27
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000028#include <sys/types.h>
29
30#include <stdio.h>
31#include <string.h>
32#include <signal.h>
33
34#include "sshkey.h"
35#include "kex.h"
36#include "sshbuf.h"
37#include "digest.h"
38#include "ssherr.h"
39
40int
41kex_kem_sntrup4591761x25519_keypair(struct kex *kex)
42{
43 struct sshbuf *buf = NULL;
44 u_char *cp = NULL;
45 size_t need;
46 int r;
47
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)
52 goto out;
53 crypto_kem_sntrup4591761_keypair(cp, kex->sntrup4591761_client_key);
54#ifdef DEBUG_KEXECDH
55 dump_digest("client public key sntrup4591761:", cp,
56 crypto_kem_sntrup4591761_PUBLICKEYBYTES);
57#endif
58 cp += crypto_kem_sntrup4591761_PUBLICKEYBYTES;
59 kexc25519_keygen(kex->c25519_client_key, cp);
60#ifdef DEBUG_KEXECDH
61 dump_digest("client public key c25519:", cp, CURVE25519_SIZE);
62#endif
djm@openbsd.orgaaca72d2019-01-21 10:40:11 +000063 kex->client_pub = buf;
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000064 buf = NULL;
65 out:
66 sshbuf_free(buf);
67 return r;
68}
69
70int
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000071kex_kem_sntrup4591761x25519_enc(struct kex *kex,
72 const struct sshbuf *client_blob, struct sshbuf **server_blobp,
73 struct sshbuf **shared_secretp)
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000074{
75 struct sshbuf *server_blob = NULL;
76 struct sshbuf *buf = NULL;
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000077 const u_char *client_pub;
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000078 u_char *kem_key, *ciphertext, *server_pub;
79 u_char server_key[CURVE25519_SIZE];
80 u_char hash[SSH_DIGEST_MAX_LENGTH];
81 size_t need;
82 int r;
83
84 *server_blobp = NULL;
85 *shared_secretp = NULL;
86
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000087 /* client_blob contains both KEM and ECDH client pubkeys */
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000088 need = crypto_kem_sntrup4591761_PUBLICKEYBYTES + CURVE25519_SIZE;
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000089 if (sshbuf_len(client_blob) != need) {
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000090 r = SSH_ERR_SIGNATURE_INVALID;
91 goto out;
92 }
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000093 client_pub = sshbuf_ptr(client_blob);
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000094#ifdef DEBUG_KEXECDH
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000095 dump_digest("client public key sntrup4591761:", client_pub,
djm@openbsd.orgdfd59162019-01-21 10:20:12 +000096 crypto_kem_sntrup4591761_PUBLICKEYBYTES);
97 dump_digest("client public key 25519:",
djm@openbsd.org71e67ff2019-01-21 10:35:09 +000098 client_pub + crypto_kem_sntrup4591761_PUBLICKEYBYTES,
99 CURVE25519_SIZE);
djm@openbsd.orgdfd59162019-01-21 10:20:12 +0000100#endif
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;
105 goto out;
106 }
107 if ((r = sshbuf_reserve(buf, crypto_kem_sntrup4591761_BYTES,
108 &kem_key)) != 0)
109 goto out;
110 /* allocate space for encrypted KEM key and ECDH pub key */
111 if ((server_blob = sshbuf_new()) == NULL) {
112 r = SSH_ERR_ALLOC_FAIL;
113 goto out;
114 }
115 need = crypto_kem_sntrup4591761_CIPHERTEXTBYTES + CURVE25519_SIZE;
116 if ((r = sshbuf_reserve(server_blob, need, &ciphertext)) != 0)
117 goto out;
118 /* generate and encrypt KEM key with client key */
djm@openbsd.org71e67ff2019-01-21 10:35:09 +0000119 crypto_kem_sntrup4591761_enc(ciphertext, kem_key, client_pub);
djm@openbsd.orgdfd59162019-01-21 10:20:12 +0000120 /* 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 */
djm@openbsd.org71e67ff2019-01-21 10:35:09 +0000124 client_pub += crypto_kem_sntrup4591761_PUBLICKEYBYTES;
125 if ((r = kexc25519_shared_key_ext(server_key, client_pub, buf, 1)) < 0)
djm@openbsd.orgdfd59162019-01-21 10:20:12 +0000126 goto out;
127 if ((r = ssh_digest_buffer(kex->hash_alg, buf, hash, sizeof(hash))) != 0)
128 goto out;
129#ifdef DEBUG_KEXECDH
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));
136#endif
137 /* string-encoded hash is resulting shared secret */
138 sshbuf_reset(buf);
139 if ((r = sshbuf_put_string(buf, hash,
140 ssh_digest_bytes(kex->hash_alg))) != 0)
141 goto out;
142#ifdef DEBUG_KEXECDH
143 dump_digest("encoded shared secret:", sshbuf_ptr(buf), sshbuf_len(buf));
144#endif
145 *server_blobp = server_blob;
146 *shared_secretp = buf;
147 server_blob = NULL;
148 buf = NULL;
149 out:
150 explicit_bzero(hash, sizeof(hash));
151 explicit_bzero(server_key, sizeof(server_key));
152 sshbuf_free(server_blob);
153 sshbuf_free(buf);
154 return r;
155}
156
157int
djm@openbsd.org71e67ff2019-01-21 10:35:09 +0000158kex_kem_sntrup4591761x25519_dec(struct kex *kex,
159 const struct sshbuf *server_blob, struct sshbuf **shared_secretp)
djm@openbsd.orgdfd59162019-01-21 10:20:12 +0000160{
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];
165 size_t need;
166 int r, decoded;
167
168 *shared_secretp = NULL;
169
170 need = crypto_kem_sntrup4591761_CIPHERTEXTBYTES + CURVE25519_SIZE;
djm@openbsd.org71e67ff2019-01-21 10:35:09 +0000171 if (sshbuf_len(server_blob) != need) {
djm@openbsd.orgdfd59162019-01-21 10:20:12 +0000172 r = SSH_ERR_SIGNATURE_INVALID;
173 goto out;
174 }
djm@openbsd.org71e67ff2019-01-21 10:35:09 +0000175 ciphertext = sshbuf_ptr(server_blob);
176 server_pub = ciphertext + crypto_kem_sntrup4591761_CIPHERTEXTBYTES;
djm@openbsd.orgdfd59162019-01-21 10:20:12 +0000177#ifdef DEBUG_KEXECDH
178 dump_digest("server cipher text:", ciphertext,
179 crypto_kem_sntrup4591761_CIPHERTEXTBYTES);
180 dump_digest("server public key c25519:", server_pub, CURVE25519_SIZE);
181#endif
182 /* hash concatenation of KEM key and ECDH shared key */
183 if ((buf = sshbuf_new()) == NULL) {
184 r = SSH_ERR_ALLOC_FAIL;
185 goto out;
186 }
187 if ((r = sshbuf_reserve(buf, crypto_kem_sntrup4591761_BYTES,
188 &kem_key)) != 0)
189 goto out;
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,
193 buf, 1)) < 0)
194 goto out;
195 if ((r = ssh_digest_buffer(kex->hash_alg, buf, hash, sizeof(hash))) != 0)
196 goto out;
197#ifdef DEBUG_KEXECDH
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));
201#endif
202 sshbuf_reset(buf);
203 if ((r = sshbuf_put_string(buf, hash,
204 ssh_digest_bytes(kex->hash_alg))) != 0)
205 goto out;
206#ifdef DEBUG_KEXECDH
207 dump_digest("encoded shared secret:", sshbuf_ptr(buf), sshbuf_len(buf));
208#endif
209 if (decoded != 0) {
210 r = SSH_ERR_SIGNATURE_INVALID;
211 goto out;
212 }
213 *shared_secretp = buf;
214 buf = NULL;
215 out:
216 explicit_bzero(hash, sizeof(hash));
217 sshbuf_free(buf);
218 return r;
219}