Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 1 | /* $OpenBSD: kexc25519.c,v 1.7 2014/05/02 03:27:54 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 | |
| 38 | #include "buffer.h" |
| 39 | #include "ssh2.h" |
| 40 | #include "key.h" |
| 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" |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 45 | |
| 46 | extern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE], |
| 47 | const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE]) |
Damien Miller | 686c7d9 | 2014-05-15 14:37:03 +1000 | [diff] [blame] | 48 | __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE))) |
| 49 | __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE))) |
| 50 | __attribute__((__bounded__(__minbytes__, 3, CURVE25519_SIZE))); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 51 | |
| 52 | void |
| 53 | kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE]) |
| 54 | { |
| 55 | static const u_char basepoint[CURVE25519_SIZE] = {9}; |
| 56 | |
| 57 | arc4random_buf(key, CURVE25519_SIZE); |
| 58 | crypto_scalarmult_curve25519(pub, key, basepoint); |
| 59 | } |
| 60 | |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 61 | void |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 62 | kexc25519_shared_key(const u_char key[CURVE25519_SIZE], |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 63 | const u_char pub[CURVE25519_SIZE], Buffer *out) |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 64 | { |
| 65 | u_char shared_key[CURVE25519_SIZE]; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 66 | |
| 67 | crypto_scalarmult_curve25519(shared_key, key, pub); |
| 68 | #ifdef DEBUG_KEXECDH |
| 69 | dump_digest("shared secret", shared_key, CURVE25519_SIZE); |
| 70 | #endif |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 71 | buffer_clear(out); |
| 72 | buffer_put_bignum2_from_string(out, shared_key, CURVE25519_SIZE); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 73 | explicit_bzero(shared_key, CURVE25519_SIZE); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
| 77 | kex_c25519_hash( |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 78 | int hash_alg, |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 79 | char *client_version_string, |
| 80 | char *server_version_string, |
| 81 | char *ckexinit, int ckexinitlen, |
| 82 | char *skexinit, int skexinitlen, |
| 83 | u_char *serverhostkeyblob, int sbloblen, |
| 84 | const u_char client_dh_pub[CURVE25519_SIZE], |
| 85 | const u_char server_dh_pub[CURVE25519_SIZE], |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 86 | const u_char *shared_secret, u_int secretlen, |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 87 | u_char **hash, u_int *hashlen) |
| 88 | { |
| 89 | Buffer b; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 90 | static u_char digest[SSH_DIGEST_MAX_LENGTH]; |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 91 | |
| 92 | buffer_init(&b); |
| 93 | buffer_put_cstring(&b, client_version_string); |
| 94 | buffer_put_cstring(&b, server_version_string); |
| 95 | |
| 96 | /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */ |
| 97 | buffer_put_int(&b, ckexinitlen+1); |
| 98 | buffer_put_char(&b, SSH2_MSG_KEXINIT); |
| 99 | buffer_append(&b, ckexinit, ckexinitlen); |
| 100 | buffer_put_int(&b, skexinitlen+1); |
| 101 | buffer_put_char(&b, SSH2_MSG_KEXINIT); |
| 102 | buffer_append(&b, skexinit, skexinitlen); |
| 103 | |
| 104 | buffer_put_string(&b, serverhostkeyblob, sbloblen); |
| 105 | buffer_put_string(&b, client_dh_pub, CURVE25519_SIZE); |
| 106 | buffer_put_string(&b, server_dh_pub, CURVE25519_SIZE); |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 107 | buffer_append(&b, shared_secret, secretlen); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 108 | |
| 109 | #ifdef DEBUG_KEX |
| 110 | buffer_dump(&b); |
| 111 | #endif |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 112 | if (ssh_digest_buffer(hash_alg, &b, digest, sizeof(digest)) != 0) |
| 113 | fatal("%s: digest_buffer failed", __func__); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 114 | |
| 115 | buffer_free(&b); |
| 116 | |
| 117 | #ifdef DEBUG_KEX |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 118 | dump_digest("hash", digest, ssh_digest_bytes(hash_alg)); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 119 | #endif |
| 120 | *hash = digest; |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 121 | *hashlen = ssh_digest_bytes(hash_alg); |
Damien Miller | 094003f | 2013-11-04 22:59:27 +1100 | [diff] [blame] | 122 | } |