Annotation of src/usr.bin/ssh/kexc25519.c, Revision 1.13
1.13 ! djm 1: /* $OpenBSD: kexc25519.c,v 1.12 2019/01/21 09:49:37 djm Exp $ */
1.1 markus 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 <sys/types.h>
29:
30: #include <signal.h>
31: #include <string.h>
32:
33: #include <openssl/bn.h>
34: #include <openssl/evp.h>
35:
1.8 markus 36: #include "sshbuf.h"
1.1 markus 37: #include "ssh2.h"
1.8 markus 38: #include "sshkey.h"
1.1 markus 39: #include "cipher.h"
40: #include "kex.h"
41: #include "log.h"
1.3 djm 42: #include "digest.h"
1.8 markus 43: #include "ssherr.h"
1.1 markus 44:
45: extern int crypto_scalarmult_curve25519(u_char a[CURVE25519_SIZE],
46: const u_char b[CURVE25519_SIZE], const u_char c[CURVE25519_SIZE])
1.7 djm 47: __attribute__((__bounded__(__minbytes__, 1, CURVE25519_SIZE)))
48: __attribute__((__bounded__(__minbytes__, 2, CURVE25519_SIZE)))
49: __attribute__((__bounded__(__minbytes__, 3, CURVE25519_SIZE)));
1.1 markus 50:
51: void
52: kexc25519_keygen(u_char key[CURVE25519_SIZE], u_char pub[CURVE25519_SIZE])
53: {
54: static const u_char basepoint[CURVE25519_SIZE] = {9};
55:
56: arc4random_buf(key, CURVE25519_SIZE);
57: crypto_scalarmult_curve25519(pub, key, basepoint);
58: }
59:
1.8 markus 60: int
1.13 ! djm 61: kexc25519_shared_key_ext(const u_char key[CURVE25519_SIZE],
! 62: const u_char pub[CURVE25519_SIZE], struct sshbuf *out, int raw)
1.1 markus 63: {
64: u_char shared_key[CURVE25519_SIZE];
1.12 djm 65: u_char zero[CURVE25519_SIZE];
1.8 markus 66: int r;
1.9 djm 67:
1.12 djm 68: crypto_scalarmult_curve25519(shared_key, key, pub);
69:
70: /* Check for all-zero shared secret */
71: explicit_bzero(zero, CURVE25519_SIZE);
72: if (timingsafe_bcmp(zero, shared_key, CURVE25519_SIZE) == 0)
1.9 djm 73: return SSH_ERR_KEY_INVALID_EC_VALUE;
1.1 markus 74:
75: #ifdef DEBUG_KEXECDH
76: dump_digest("shared secret", shared_key, CURVE25519_SIZE);
77: #endif
1.13 ! djm 78: if (raw)
! 79: r = sshbuf_put(out, shared_key, CURVE25519_SIZE);
! 80: else
! 81: r = sshbuf_put_bignum2_bytes(out, shared_key, CURVE25519_SIZE);
1.5 tedu 82: explicit_bzero(shared_key, CURVE25519_SIZE);
1.8 markus 83: return r;
1.1 markus 84: }
85:
1.8 markus 86: int
1.13 ! djm 87: kexc25519_shared_key(const u_char key[CURVE25519_SIZE],
! 88: const u_char pub[CURVE25519_SIZE], struct sshbuf *out)
! 89: {
! 90: return kexc25519_shared_key_ext(key, pub, out, 0);
! 91: }
! 92:
! 93: int
1.1 markus 94: kex_c25519_hash(
1.3 djm 95: int hash_alg,
1.11 djm 96: const struct sshbuf *client_version,
97: const struct sshbuf *server_version,
1.10 djm 98: const u_char *ckexinit, size_t ckexinitlen,
99: const u_char *skexinit, size_t skexinitlen,
1.8 markus 100: const u_char *serverhostkeyblob, size_t sbloblen,
1.13 ! djm 101: const u_char *client_pub, size_t client_pub_len,
! 102: const u_char *server_pub, size_t server_pub_len,
1.8 markus 103: const u_char *shared_secret, size_t secretlen,
104: u_char *hash, size_t *hashlen)
1.1 markus 105: {
1.8 markus 106: struct sshbuf *b;
107: int r;
1.1 markus 108:
1.8 markus 109: if (*hashlen < ssh_digest_bytes(hash_alg))
110: return SSH_ERR_INVALID_ARGUMENT;
111: if ((b = sshbuf_new()) == NULL)
112: return SSH_ERR_ALLOC_FAIL;
1.13 ! djm 113: if ((r = sshbuf_put_stringb(b, client_version)) != 0 ||
! 114: (r = sshbuf_put_stringb(b, server_version)) != 0 ||
1.8 markus 115: /* kexinit messages: fake header: len+SSH2_MSG_KEXINIT */
1.13 ! djm 116: (r = sshbuf_put_u32(b, ckexinitlen+1)) != 0 ||
! 117: (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
! 118: (r = sshbuf_put(b, ckexinit, ckexinitlen)) != 0 ||
! 119: (r = sshbuf_put_u32(b, skexinitlen+1)) != 0 ||
! 120: (r = sshbuf_put_u8(b, SSH2_MSG_KEXINIT)) != 0 ||
! 121: (r = sshbuf_put(b, skexinit, skexinitlen)) != 0 ||
! 122: (r = sshbuf_put_string(b, serverhostkeyblob, sbloblen)) != 0 ||
! 123: (r = sshbuf_put_string(b, client_pub, client_pub_len)) != 0 ||
! 124: (r = sshbuf_put_string(b, server_pub, server_pub_len)) != 0 ||
! 125: (r = sshbuf_put(b, shared_secret, secretlen)) != 0) {
1.8 markus 126: sshbuf_free(b);
127: return r;
128: }
1.1 markus 129: #ifdef DEBUG_KEX
1.8 markus 130: sshbuf_dump(b, stderr);
1.1 markus 131: #endif
1.8 markus 132: if (ssh_digest_buffer(hash_alg, b, hash, *hashlen) != 0) {
133: sshbuf_free(b);
134: return SSH_ERR_LIBCRYPTO_ERROR;
135: }
136: sshbuf_free(b);
137: *hashlen = ssh_digest_bytes(hash_alg);
1.1 markus 138: #ifdef DEBUG_KEX
1.8 markus 139: dump_digest("hash", hash, *hashlen);
1.1 markus 140: #endif
1.8 markus 141: return 0;
1.1 markus 142: }