1 /* $OpenBSD: ssh-xmss.c,v 1.5 2022/04/20 15:59:18 millert Exp $*/ 2 /* 3 * Copyright (c) 2017 Stefan-Lukas Gazdag. 4 * Copyright (c) 2017 Markus Friedl. 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #include "includes.h" 19 #ifdef WITH_XMSS 20 21 #define SSHKEY_INTERNAL 22 #include <sys/types.h> 23 #include <limits.h> 24 25 #include <stdlib.h> 26 #include <string.h> 27 #include <stdarg.h> 28 #include <stdint.h> 29 #include <unistd.h> 30 31 #include "log.h" 32 #include "sshbuf.h" 33 #include "sshkey.h" 34 #include "sshkey-xmss.h" 35 #include "ssherr.h" 36 #include "ssh.h" 37 38 #include "xmss_fast.h" 39 40 int 41 ssh_xmss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp, 42 const u_char *data, size_t datalen, u_int compat) 43 { 44 u_char *sig = NULL; 45 size_t slen = 0, len = 0, required_siglen; 46 unsigned long long smlen; 47 int r, ret; 48 struct sshbuf *b = NULL; 49 50 if (lenp != NULL) 51 *lenp = 0; 52 if (sigp != NULL) 53 *sigp = NULL; 54 55 if (key == NULL || 56 sshkey_type_plain(key->type) != KEY_XMSS || 57 key->xmss_sk == NULL || 58 sshkey_xmss_params(key) == NULL) 59 return SSH_ERR_INVALID_ARGUMENT; 60 if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0) 61 return r; 62 if (datalen >= INT_MAX - required_siglen) 63 return SSH_ERR_INVALID_ARGUMENT; 64 smlen = slen = datalen + required_siglen; 65 if ((sig = malloc(slen)) == NULL) 66 return SSH_ERR_ALLOC_FAIL; 67 if ((r = sshkey_xmss_get_state(key, 1)) != 0) 68 goto out; 69 if ((ret = xmss_sign(key->xmss_sk, sshkey_xmss_bds_state(key), sig, &smlen, 70 data, datalen, sshkey_xmss_params(key))) != 0 || smlen <= datalen) { 71 r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */ 72 goto out; 73 } 74 /* encode signature */ 75 if ((b = sshbuf_new()) == NULL) { 76 r = SSH_ERR_ALLOC_FAIL; 77 goto out; 78 } 79 if ((r = sshbuf_put_cstring(b, "ssh-xmss@openssh.com")) != 0 || 80 (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0) 81 goto out; 82 len = sshbuf_len(b); 83 if (sigp != NULL) { 84 if ((*sigp = malloc(len)) == NULL) { 85 r = SSH_ERR_ALLOC_FAIL; 86 goto out; 87 } 88 memcpy(*sigp, sshbuf_ptr(b), len); 89 } 90 if (lenp != NULL) 91 *lenp = len; 92 /* success */ 93 r = 0; 94 out: 95 if ((ret = sshkey_xmss_update_state(key, 1)) != 0) { 96 /* discard signature since we cannot update the state */ 97 if (r == 0 && sigp != NULL && *sigp != NULL) { 98 explicit_bzero(*sigp, len); 99 free(*sigp); 100 } 101 if (sigp != NULL) 102 *sigp = NULL; 103 if (lenp != NULL) 104 *lenp = 0; 105 r = ret; 106 } 107 sshbuf_free(b); 108 if (sig != NULL) 109 freezero(sig, slen); 110 111 return r; 112 } 113 114 int 115 ssh_xmss_verify(const struct sshkey *key, 116 const u_char *signature, size_t signaturelen, 117 const u_char *data, size_t datalen, u_int compat) 118 { 119 struct sshbuf *b = NULL; 120 char *ktype = NULL; 121 const u_char *sigblob; 122 u_char *sm = NULL, *m = NULL; 123 size_t len, required_siglen; 124 unsigned long long smlen = 0, mlen = 0; 125 int r, ret; 126 127 if (key == NULL || 128 sshkey_type_plain(key->type) != KEY_XMSS || 129 key->xmss_pk == NULL || 130 sshkey_xmss_params(key) == NULL || 131 signature == NULL || signaturelen == 0) 132 return SSH_ERR_INVALID_ARGUMENT; 133 if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0) 134 return r; 135 if (datalen >= INT_MAX - required_siglen) 136 return SSH_ERR_INVALID_ARGUMENT; 137 138 if ((b = sshbuf_from(signature, signaturelen)) == NULL) 139 return SSH_ERR_ALLOC_FAIL; 140 if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 || 141 (r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0) 142 goto out; 143 if (strcmp("ssh-xmss@openssh.com", ktype) != 0) { 144 r = SSH_ERR_KEY_TYPE_MISMATCH; 145 goto out; 146 } 147 if (sshbuf_len(b) != 0) { 148 r = SSH_ERR_UNEXPECTED_TRAILING_DATA; 149 goto out; 150 } 151 if (len != required_siglen) { 152 r = SSH_ERR_INVALID_FORMAT; 153 goto out; 154 } 155 if (datalen >= SIZE_MAX - len) { 156 r = SSH_ERR_INVALID_ARGUMENT; 157 goto out; 158 } 159 smlen = len + datalen; 160 mlen = smlen; 161 if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) { 162 r = SSH_ERR_ALLOC_FAIL; 163 goto out; 164 } 165 memcpy(sm, sigblob, len); 166 memcpy(sm+len, data, datalen); 167 if ((ret = xmss_sign_open(m, &mlen, sm, smlen, 168 key->xmss_pk, sshkey_xmss_params(key))) != 0) { 169 debug2_f("xmss_sign_open failed: %d", ret); 170 } 171 if (ret != 0 || mlen != datalen) { 172 r = SSH_ERR_SIGNATURE_INVALID; 173 goto out; 174 } 175 /* XXX compare 'm' and 'data' ? */ 176 /* success */ 177 r = 0; 178 out: 179 if (sm != NULL) 180 freezero(sm, smlen); 181 if (m != NULL) 182 freezero(m, smlen); 183 sshbuf_free(b); 184 free(ktype); 185 return r; 186 } 187 #endif /* WITH_XMSS */ 188