djm@openbsd.org | f3ebaff | 2019-01-21 09:49:37 +0000 | [diff] [blame] | 1 | /* $OpenBSD: kexc25519.c,v 1.12 2019/01/21 09:49:37 djm Exp $ */ |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2001, 2013 Markus Friedl. All rights reserved. |
| 4 | * Copyright (c) 2010 Damien Miller. All rights reserved. |
| 5 | * Copyright (c) 2013 Aris Adamantiadis. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "includes.h" |
| 29 | |
| 30 | #include <sys/types.h> |
| 31 | |
| 32 | #include <signal.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include <openssl/bn.h> |
| 36 | #include <openssl/evp.h> |
| 37 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 38 | #include "sshbuf.h" |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 39 | #include "ssh2.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 40 | #include "sshkey.h" |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 41 | #include "cipher.h" |
| 42 | #include "kex.h" |
| 43 | #include "log.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 44 | #include "digest.h" |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 45 | #include "ssherr.h" |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 46 | |
| 47 | extern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE], |
| 48 | const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE]) |
Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 49 | __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE))) |
| 50 | __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE))) |
| 51 | __attribute__((__bounded__(__minbytes__, 3, CURVE25519_SIZE))); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 52 | |
| 53 | void |
| 54 | kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE]) |
| 55 | { |
| 56 | static const u_char basepoint[CURVE25519_SIZE] = {9}; |
| 57 | |
| 58 | arc4random_buf(key, CURVE25519_SIZE); |
| 59 | crypto_scalarmult_curve25519(pub, key, basepoint); |
| 60 | } |
| 61 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 62 | int |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 63 | kexc25519_shared_key(const u_char key[CURVE25519_SIZE], |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 64 | const u_char pub[CURVE25519_SIZE], struct sshbuf *out) |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 65 | { |
| 66 | u_char shared_key[CURVE25519_SIZE]; |
djm@openbsd.org | f3ebaff | 2019-01-21 09:49:37 +0000 | [diff] [blame] | 67 | u_char zero[CURVE25519_SIZE]; |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 68 | int r; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 69 | |
djm@openbsd.org | f3ebaff | 2019-01-21 09:49:37 +0000 | [diff] [blame] | 70 | crypto_scalarmult_curve25519(shared_key, key, pub); |
| 71 | |
| 72 | /* Check for all-zero shared secret */ |
| 73 | explicit_bzero(zero, CURVE25519_SIZE); |
| 74 | if (timingsafe_bcmp(zero, shared_key, CURVE25519_SIZE) == 0) |
djm@openbsd.org | f9b7885 | 2015-03-26 07:00:04 +0000 | [diff] [blame] | 75 | return SSH_ERR_KEY_INVALID_EC_VALUE; |
| 76 | |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 77 | #ifdef DEBUG_KEXECDH |
| 78 | dump_digest("shared secret", shared_key, CURVE25519_SIZE); |
| 79 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 80 | sshbuf_reset(out); |
| 81 | r = sshbuf_put_bignum2_bytes(out, shared_key, CURVE25519_SIZE); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 82 | explicit_bzero(shared_key, CURVE25519_SIZE); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 83 | return r; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 84 | } |
| 85 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 86 | int |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 87 | kex_c25519_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 88 | int hash_alg, |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 89 | const struct sshbuf *client_version, |
| 90 | const struct sshbuf *server_version, |
djm@openbsd.org | 1a31d02 | 2016-05-02 08:49:03 +0000 | [diff] [blame] | 91 | const u_char *ckexinit, size_t ckexinitlen, |
| 92 | const u_char *skexinit, size_t skexinitlen, |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 93 | const u_char *serverhostkeyblob, size_t sbloblen, |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 94 | const u_char client_dh_pub[CURVE25519_SIZE], |
| 95 | const u_char server_dh_pub[CURVE25519_SIZE], |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 96 | const u_char *shared_secret, size_t secretlen, |
| 97 | u_char *hash, size_t *hashlen) |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 98 | { |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 99 | struct sshbuf *b; |
| 100 | int r; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 101 | |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 102 | if (*hashlen < ssh_digest_bytes(hash_alg)) |
| 103 | return SSH_ERR_INVALID_ARGUMENT; |
| 104 | if ((b = sshbuf_new()) == NULL) |
| 105 | return SSH_ERR_ALLOC_FAIL; |
djm@openbsd.org | 0a843d9 | 2018-12-27 03:25:24 +0000 | [diff] [blame] | 106 | if ((r = sshbuf_put_stringb(b, client_version)) < 0 || |
| 107 | (r = sshbuf_put_stringb(b, server_version)) < 0 || |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 108 | /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ |
| 109 | (r = sshbuf_put_u32(b, ckexinitlen+1)) < 0 || |
| 110 | (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 || |
| 111 | (r = sshbuf_put(b, ckexinit, ckexinitlen)) < 0 || |
| 112 | (r = sshbuf_put_u32(b, skexinitlen+1)) < 0 || |
| 113 | (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) < 0 || |
| 114 | (r = sshbuf_put(b, skexinit, skexinitlen)) < 0 || |
| 115 | (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) < 0 || |
| 116 | (r = sshbuf_put_string(b, client_dh_pub, CURVE25519_SIZE)) < 0 || |
| 117 | (r = sshbuf_put_string(b, server_dh_pub, CURVE25519_SIZE)) < 0 || |
| 118 | (r = sshbuf_put(b, shared_secret, secretlen)) < 0) { |
| 119 | sshbuf_free(b); |
| 120 | return r; |
| 121 | } |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 122 | #ifdef DEBUG_KEX |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 123 | sshbuf_dump(b, stderr); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 124 | #endif |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 125 | if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) { |
| 126 | sshbuf_free(b); |
| 127 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 128 | } |
| 129 | sshbuf_free(b); |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 130 | *hashlen = ssh_digest_bytes(hash_alg); |
markus@openbsd.org | 57d10cb | 2015-01-19 20:16:15 +0000 | [diff] [blame] | 131 | #ifdef DEBUG_KEX |
| 132 | dump_digest("hash", hash, *hashlen); |
| 133 | #endif |
| 134 | return 0; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 135 | } |