1 /* $OpenBSD: ssh-xmss.c,v 1.1 2018/02/23 15:58:38 markus Exp $*/
3 * Copyright (c) 2017 Stefan-Lukas Gazdag.
4 * Copyright (c) 2017 Markus Friedl.
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.
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.
21 #define SSHKEY_INTERNAL
22 #include <sys/types.h>
32 #include "sshkey-xmss.h"
36 #include "xmss_fast.h"
39 ssh_xmss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
40 const u_char *data, size_t datalen, u_int compat)
43 size_t slen = 0, len = 0, required_siglen;
44 unsigned long long smlen;
46 struct sshbuf *b = NULL;
54 sshkey_type_plain(key->type) != KEY_XMSS ||
55 key->xmss_sk == NULL ||
56 sshkey_xmss_params(key) == NULL)
57 return SSH_ERR_INVALID_ARGUMENT;
58 if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0)
60 if (datalen >= INT_MAX - required_siglen)
61 return SSH_ERR_INVALID_ARGUMENT;
62 smlen = slen = datalen + required_siglen;
63 if ((sig = malloc(slen)) == NULL)
64 return SSH_ERR_ALLOC_FAIL;
65 if ((r = sshkey_xmss_get_state(key, error)) != 0)
67 if ((ret = xmss_sign(key->xmss_sk, sshkey_xmss_bds_state(key), sig, &smlen,
68 data, datalen, sshkey_xmss_params(key))) != 0 || smlen <= datalen) {
69 r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
72 /* encode signature */
73 if ((b = sshbuf_new()) == NULL) {
74 r = SSH_ERR_ALLOC_FAIL;
77 if ((r = sshbuf_put_cstring(b, "ssh-xmss@openssh.com")) != 0 ||
78 (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
82 if ((*sigp = malloc(len)) == NULL) {
83 r = SSH_ERR_ALLOC_FAIL;
86 memcpy(*sigp, sshbuf_ptr(b), len);
93 if ((ret = sshkey_xmss_update_state(key, error)) != 0) {
94 /* discard signature since we cannot update the state */
95 if (r == 0 && sigp != NULL && *sigp != NULL) {
96 explicit_bzero(*sigp, len);
107 explicit_bzero(sig, slen);
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)
119 struct sshbuf *b = 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;
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)
135 if (datalen >= INT_MAX - required_siglen)
136 return SSH_ERR_INVALID_ARGUMENT;
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)
143 if (strcmp("ssh-xmss@openssh.com", ktype) != 0) {
144 r = SSH_ERR_KEY_TYPE_MISMATCH;
147 if (sshbuf_len(b) != 0) {
148 r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
151 if (len != required_siglen) {
152 r = SSH_ERR_INVALID_FORMAT;
155 if (datalen >= SIZE_MAX - len) {
156 r = SSH_ERR_INVALID_ARGUMENT;
159 smlen = len + datalen;
161 if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
162 r = SSH_ERR_ALLOC_FAIL;
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("%s: crypto_sign_xmss_open failed: %d",
172 if (ret != 0 || mlen != datalen) {
173 r = SSH_ERR_SIGNATURE_INVALID;
176 /* XXX compare 'm' and 'data' ? */
181 explicit_bzero(sm, smlen);
185 explicit_bzero(m, smlen); /* NB mlen may be invalid if r != 0 */
192 #endif /* WITH_XMSS */