markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-ed25519-sk.c,v 1.2 2019/11/12 19:34:40 markus Exp $ */ |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2019 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Permission to use, copy, modify, and distribute this software for any |
| 6 | * purpose with or without fee is hereby granted, provided that the above |
| 7 | * copyright notice and this permission notice appear in all copies. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | */ |
| 17 | #define SSHKEY_INTERNAL |
| 18 | #include <sys/types.h> |
| 19 | #include <limits.h> |
| 20 | |
| 21 | #include "crypto_api.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <stdarg.h> |
| 25 | |
| 26 | #include "log.h" |
| 27 | #include "sshbuf.h" |
| 28 | #include "sshkey.h" |
| 29 | #include "ssherr.h" |
| 30 | #include "ssh.h" |
| 31 | #include "digest.h" |
| 32 | |
| 33 | int |
| 34 | ssh_ed25519_sk_verify(const struct sshkey *key, |
| 35 | const u_char *signature, size_t signaturelen, |
| 36 | const u_char *data, size_t datalen, u_int compat) |
| 37 | { |
| 38 | struct sshbuf *b = NULL; |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 39 | struct sshbuf *encoded = NULL; |
| 40 | char *ktype = NULL; |
| 41 | const u_char *sigblob; |
| 42 | const u_char *sm; |
| 43 | u_char *m = NULL; |
| 44 | u_char apphash[32]; |
| 45 | u_char msghash[32]; |
| 46 | u_char sig_flags; |
| 47 | u_int sig_counter; |
| 48 | size_t len; |
| 49 | unsigned long long smlen = 0, mlen = 0; |
| 50 | int r = SSH_ERR_INTERNAL_ERROR; |
| 51 | int ret; |
| 52 | |
| 53 | if (key == NULL || |
| 54 | sshkey_type_plain(key->type) != KEY_ED25519_SK || |
| 55 | key->ed25519_pk == NULL || |
| 56 | signature == NULL || signaturelen == 0) |
| 57 | return SSH_ERR_INVALID_ARGUMENT; |
| 58 | |
| 59 | if ((b = sshbuf_from(signature, signaturelen)) == NULL) |
| 60 | return SSH_ERR_ALLOC_FAIL; |
| 61 | if (sshbuf_get_cstring(b, &ktype, NULL) != 0 || |
markus@openbsd.org | b556cc3 | 2019-11-12 19:34:40 +0000 | [diff] [blame] | 62 | sshbuf_get_string_direct(b, &sigblob, &len) != 0 || |
| 63 | sshbuf_get_u8(b, &sig_flags) != 0 || |
| 64 | sshbuf_get_u32(b, &sig_counter) != 0) { |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 65 | r = SSH_ERR_INVALID_FORMAT; |
| 66 | goto out; |
| 67 | } |
| 68 | if (strcmp(sshkey_ssh_name_plain(key), ktype) != 0) { |
| 69 | r = SSH_ERR_KEY_TYPE_MISMATCH; |
| 70 | goto out; |
| 71 | } |
| 72 | if (sshbuf_len(b) != 0) { |
| 73 | r = SSH_ERR_UNEXPECTED_TRAILING_DATA; |
| 74 | goto out; |
| 75 | } |
| 76 | if (len > crypto_sign_ed25519_BYTES) { |
| 77 | r = SSH_ERR_INVALID_FORMAT; |
| 78 | goto out; |
| 79 | } |
| 80 | if (ssh_digest_memory(SSH_DIGEST_SHA256, key->sk_application, |
| 81 | strlen(key->sk_application), apphash, sizeof(apphash)) != 0 || |
| 82 | ssh_digest_memory(SSH_DIGEST_SHA256, data, datalen, |
| 83 | msghash, sizeof(msghash)) != 0) { |
| 84 | r = SSH_ERR_INVALID_ARGUMENT; |
| 85 | goto out; |
| 86 | } |
| 87 | if ((encoded = sshbuf_new()) == NULL) { |
| 88 | r = SSH_ERR_ALLOC_FAIL; |
| 89 | goto out; |
| 90 | } |
| 91 | if (sshbuf_put(encoded, sigblob, len) != 0 || |
| 92 | sshbuf_put(encoded, apphash, sizeof(apphash)) != 0 || |
| 93 | sshbuf_put_u8(encoded, sig_flags) != 0 || |
| 94 | sshbuf_put_u32(encoded, sig_counter) != 0 || |
| 95 | sshbuf_put(encoded, msghash, sizeof(msghash)) != 0) { |
| 96 | r = SSH_ERR_ALLOC_FAIL; |
| 97 | goto out; |
| 98 | } |
| 99 | sm = sshbuf_ptr(encoded); |
| 100 | smlen = sshbuf_len(encoded); |
| 101 | mlen = smlen; |
| 102 | if ((m = malloc(smlen)) == NULL) { |
| 103 | r = SSH_ERR_ALLOC_FAIL; |
| 104 | goto out; |
| 105 | } |
| 106 | if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen, |
| 107 | key->ed25519_pk)) != 0) { |
| 108 | debug2("%s: crypto_sign_ed25519_open failed: %d", |
| 109 | __func__, ret); |
| 110 | } |
| 111 | if (ret != 0 || mlen != smlen - len) { |
| 112 | r = SSH_ERR_SIGNATURE_INVALID; |
| 113 | goto out; |
| 114 | } |
| 115 | /* XXX compare 'm' and 'sm + len' ? */ |
| 116 | /* success */ |
| 117 | r = 0; |
| 118 | out: |
| 119 | if (m != NULL) { |
| 120 | explicit_bzero(m, smlen); /* NB mlen may be invalid if r != 0 */ |
| 121 | free(m); |
| 122 | } |
| 123 | sshbuf_free(b); |
markus@openbsd.org | 7c096c4 | 2019-11-12 19:29:24 +0000 | [diff] [blame] | 124 | sshbuf_free(encoded); |
| 125 | free(ktype); |
| 126 | return r; |
| 127 | } |