summaryrefslogtreecommitdiff
path: root/kexc25519.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2019-01-21 10:20:12 +0000
committerDamien Miller <djm@mindrot.org>2019-01-21 22:07:02 +1100
commitdfd591618cdf2c96727ac0eb65f89cf54af0d97e (patch)
tree59700563da0dc6f1de649394ffb4c787710eda5a /kexc25519.c
parentb1b2ff4ed559051d1035419f8f236275fa66d5d6 (diff)
upstream: Add support for a PQC KEX/KEM:
sntrup4591761x25519-sha512@tinyssh.org using the Streamlined NTRU Prime 4591^761 implementation from SUPERCOP coupled with X25519 as a stop-loss. Not enabled by default. introduce KEM API; a simplified framework for DH-ish KEX methods. from markus@ feedback & ok djm@ OpenBSD-Commit-ID: d687f76cffd3561dd73eb302d17a1c3bf321d1a7
Diffstat (limited to 'kexc25519.c')
-rw-r--r--kexc25519.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/kexc25519.c b/kexc25519.c
index acddcab37..3911baf14 100644
--- a/kexc25519.c
+++ b/kexc25519.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: kexc25519.c,v 1.12 2019/01/21 09:49:37 djm Exp $ */ 1/* $OpenBSD: kexc25519.c,v 1.13 2019/01/21 10:20:12 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2001, 2013 Markus Friedl. All rights reserved. 3 * Copyright (c) 2001, 2013 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved. 4 * Copyright (c) 2010 Damien Miller. All rights reserved.
@@ -60,8 +60,8 @@ kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
60} 60}
61 61
62int 62int
63kexc25519_shared_key(const u_char key[CURVE25519_SIZE], 63kexc25519_shared_key_ext(const u_char key[CURVE25519_SIZE],
64 const u_char pub[CURVE25519_SIZE], struct sshbuf *out) 64 const u_char pub[CURVE25519_SIZE], struct sshbuf *out, int raw)
65{ 65{
66 u_char shared_key[CURVE25519_SIZE]; 66 u_char shared_key[CURVE25519_SIZE];
67 u_char zero[CURVE25519_SIZE]; 67 u_char zero[CURVE25519_SIZE];
@@ -77,13 +77,22 @@ kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
77#ifdef DEBUG_KEXECDH 77#ifdef DEBUG_KEXECDH
78 dump_digest("shared secret", shared_key, CURVE25519_SIZE); 78 dump_digest("shared secret", shared_key, CURVE25519_SIZE);
79#endif 79#endif
80 sshbuf_reset(out); 80 if (raw)
81 r = sshbuf_put_bignum2_bytes(out, shared_key, CURVE25519_SIZE); 81 r = sshbuf_put(out, shared_key, CURVE25519_SIZE);
82 else
83 r = sshbuf_put_bignum2_bytes(out, shared_key, CURVE25519_SIZE);
82 explicit_bzero(shared_key, CURVE25519_SIZE); 84 explicit_bzero(shared_key, CURVE25519_SIZE);
83 return r; 85 return r;
84} 86}
85 87
86int 88int
89kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
90 const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
91{
92 return kexc25519_shared_key_ext(key, pub, out, 0);
93}
94
95int
87kex_c25519_hash( 96kex_c25519_hash(
88 int hash_alg, 97 int hash_alg,
89 const struct sshbuf *client_version, 98 const struct sshbuf *client_version,
@@ -91,8 +100,8 @@ kex_c25519_hash(
91 const u_char *ckexinit, size_t ckexinitlen, 100 const u_char *ckexinit, size_t ckexinitlen,
92 const u_char *skexinit, size_t skexinitlen, 101 const u_char *skexinit, size_t skexinitlen,
93 const u_char *serverhostkeyblob, size_t sbloblen, 102 const u_char *serverhostkeyblob, size_t sbloblen,
94 const u_char client_dh_pub[CURVE25519_SIZE], 103 const u_char *client_pub, size_t client_pub_len,
95 const u_char server_dh_pub[CURVE25519_SIZE], 104 const u_char *server_pub, size_t server_pub_len,
96 const u_char *shared_secret, size_t secretlen, 105 const u_char *shared_secret, size_t secretlen,
97 u_char *hash, size_t *hashlen) 106 u_char *hash, size_t *hashlen)
98{ 107{
@@ -103,19 +112,19 @@ kex_c25519_hash(
103 return SSH_ERR_INVALID_ARGUMENT; 112 return SSH_ERR_INVALID_ARGUMENT;
104 if ((b = sshbuf_new()) == NULL) 113 if ((b = sshbuf_new()) == NULL)
105 return SSH_ERR_ALLOC_FAIL; 114 return SSH_ERR_ALLOC_FAIL;
106 if ((r = sshbuf_put_stringb(b, client_version)) < 0 || 115 if ((r = sshbuf_put_stringb(b, client_version)) != 0 ||
107 (r = sshbuf_put_stringb(b, server_version)) < 0 || 116 (r = sshbuf_put_stringb(b, server_version)) != 0 ||
108 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ 117 /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
109 (r = sshbuf_put_u32(b, ckexinitlen+1)) < 0 || 118 (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
110 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 || 119 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
111 (r = sshbuf_put(b, ckexinit, ckexinitlen)) < 0 || 120 (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
112 (r = sshbuf_put_u32(b, skexinitlen+1)) < 0 || 121 (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
113 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 || 122 (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
114 (r = sshbuf_put(b, skexinit, skexinitlen)) < 0 || 123 (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
115 (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) < 0 || 124 (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
116 (r = sshbuf_put_string(b, client_dh_pub, CURVE25519_SIZE)) < 0 || 125 (r = sshbuf_put_string(b, client_pub, client_pub_len)) != 0 ||
117 (r = sshbuf_put_string(b, server_dh_pub, CURVE25519_SIZE)) < 0 || 126 (r = sshbuf_put_string(b, server_pub, server_pub_len)) != 0 ||
118 (r = sshbuf_put(b, shared_secret, secretlen)) < 0) { 127 (r = sshbuf_put(b, shared_secret, secretlen)) != 0) {
119 sshbuf_free(b); 128 sshbuf_free(b);
120 return r; 129 return r;
121 } 130 }