Annotation of src/usr.bin/ssh/ssh-xmss.c, Revision 1.10
1.10 ! djm 1: /* $OpenBSD: ssh-xmss.c,v 1.9 2022/10/28 00:39:29 djm Exp $*/
1.1 markus 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: #define SSHKEY_INTERNAL
19: #include <sys/types.h>
20: #include <limits.h>
21:
1.5 millert 22: #include <stdlib.h>
1.1 markus 23: #include <string.h>
24: #include <stdarg.h>
1.5 millert 25: #include <stdint.h>
1.1 markus 26: #include <unistd.h>
27:
28: #include "log.h"
29: #include "sshbuf.h"
30: #include "sshkey.h"
31: #include "sshkey-xmss.h"
32: #include "ssherr.h"
33: #include "ssh.h"
34:
35: #include "xmss_fast.h"
36:
1.6 djm 37: static void
38: ssh_xmss_cleanup(struct sshkey *k)
39: {
40: freezero(k->xmss_pk, sshkey_xmss_pklen(k));
41: freezero(k->xmss_sk, sshkey_xmss_sklen(k));
42: sshkey_xmss_free_state(k);
43: free(k->xmss_name);
44: free(k->xmss_filename);
45: k->xmss_pk = NULL;
46: k->xmss_sk = NULL;
47: k->xmss_name = NULL;
48: k->xmss_filename = NULL;
49: }
50:
1.7 djm 51: static int
52: ssh_xmss_equal(const struct sshkey *a, const struct sshkey *b)
53: {
54: if (a->xmss_pk == NULL || b->xmss_pk == NULL)
55: return 0;
56: if (sshkey_xmss_pklen(a) != sshkey_xmss_pklen(b))
57: return 0;
58: if (memcmp(a->xmss_pk, b->xmss_pk, sshkey_xmss_pklen(a)) != 0)
59: return 0;
60: return 1;
61: }
62:
1.8 djm 63: static int
64: ssh_xmss_serialize_public(const struct sshkey *key, struct sshbuf *b,
65: const char *typename, enum sshkey_serialize_rep opts)
66: {
67: int r;
68:
69: if (key->xmss_name == NULL || key->xmss_pk == NULL ||
70: sshkey_xmss_pklen(key) == 0)
71: return SSH_ERR_INVALID_ARGUMENT;
72: if ((r = sshbuf_put_cstring(b, typename)) != 0 ||
73: (r = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
74: (r = sshbuf_put_string(b, key->xmss_pk,
75: sshkey_xmss_pklen(key))) != 0 ||
76: (r = sshkey_xmss_serialize_pk_info(key, b, opts)) != 0)
77: return r;
78:
79: return 0;
80: }
81:
1.10 ! djm 82: static int
! 83: ssh_xmss_copy_public(const struct sshkey *from, struct sshkey *to)
! 84: {
! 85: int r = SSH_ERR_INTERNAL_ERROR;
! 86: u_int32_t left;
! 87: size_t pklen;
! 88:
! 89: if ((r = sshkey_xmss_init(to, from->xmss_name)) != 0)
! 90: return r;
! 91: if (from->xmss_pk == NULL)
! 92: return 0; /* XXX SSH_ERR_INTERNAL_ERROR ? */
! 93:
! 94: if ((pklen = sshkey_xmss_pklen(from)) == 0 ||
! 95: sshkey_xmss_pklen(to) != pklen)
! 96: return SSH_ERR_INTERNAL_ERROR;
! 97: if ((to->xmss_pk = malloc(pklen)) == NULL)
! 98: return SSH_ERR_ALLOC_FAIL;
! 99: memcpy(to->xmss_pk, from->xmss_pk, pklen);
! 100: /* simulate number of signatures left on pubkey */
! 101: left = sshkey_xmss_signatures_left(from);
! 102: if (left)
! 103: sshkey_xmss_enable_maxsign(to, left);
! 104: return 0;
! 105: }
! 106:
1.1 markus 107: int
108: ssh_xmss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
109: const u_char *data, size_t datalen, u_int compat)
110: {
111: u_char *sig = NULL;
112: size_t slen = 0, len = 0, required_siglen;
113: unsigned long long smlen;
114: int r, ret;
115: struct sshbuf *b = NULL;
116:
117: if (lenp != NULL)
118: *lenp = 0;
119: if (sigp != NULL)
120: *sigp = NULL;
121:
122: if (key == NULL ||
123: sshkey_type_plain(key->type) != KEY_XMSS ||
124: key->xmss_sk == NULL ||
125: sshkey_xmss_params(key) == NULL)
126: return SSH_ERR_INVALID_ARGUMENT;
127: if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0)
128: return r;
129: if (datalen >= INT_MAX - required_siglen)
130: return SSH_ERR_INVALID_ARGUMENT;
131: smlen = slen = datalen + required_siglen;
132: if ((sig = malloc(slen)) == NULL)
133: return SSH_ERR_ALLOC_FAIL;
1.4 dtucker 134: if ((r = sshkey_xmss_get_state(key, 1)) != 0)
1.1 markus 135: goto out;
136: if ((ret = xmss_sign(key->xmss_sk, sshkey_xmss_bds_state(key), sig, &smlen,
137: data, datalen, sshkey_xmss_params(key))) != 0 || smlen <= datalen) {
138: r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
139: goto out;
140: }
141: /* encode signature */
142: if ((b = sshbuf_new()) == NULL) {
143: r = SSH_ERR_ALLOC_FAIL;
144: goto out;
145: }
146: if ((r = sshbuf_put_cstring(b, "ssh-xmss@openssh.com")) != 0 ||
147: (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
148: goto out;
149: len = sshbuf_len(b);
150: if (sigp != NULL) {
151: if ((*sigp = malloc(len)) == NULL) {
152: r = SSH_ERR_ALLOC_FAIL;
153: goto out;
154: }
155: memcpy(*sigp, sshbuf_ptr(b), len);
156: }
157: if (lenp != NULL)
158: *lenp = len;
159: /* success */
160: r = 0;
161: out:
1.4 dtucker 162: if ((ret = sshkey_xmss_update_state(key, 1)) != 0) {
1.1 markus 163: /* discard signature since we cannot update the state */
164: if (r == 0 && sigp != NULL && *sigp != NULL) {
165: explicit_bzero(*sigp, len);
166: free(*sigp);
167: }
168: if (sigp != NULL)
169: *sigp = NULL;
170: if (lenp != NULL)
171: *lenp = 0;
172: r = ret;
173: }
174: sshbuf_free(b);
1.2 jsg 175: if (sig != NULL)
176: freezero(sig, slen);
1.1 markus 177:
178: return r;
179: }
180:
181: int
182: ssh_xmss_verify(const struct sshkey *key,
183: const u_char *signature, size_t signaturelen,
184: const u_char *data, size_t datalen, u_int compat)
185: {
186: struct sshbuf *b = NULL;
187: char *ktype = NULL;
188: const u_char *sigblob;
189: u_char *sm = NULL, *m = NULL;
190: size_t len, required_siglen;
191: unsigned long long smlen = 0, mlen = 0;
192: int r, ret;
193:
194: if (key == NULL ||
195: sshkey_type_plain(key->type) != KEY_XMSS ||
196: key->xmss_pk == NULL ||
197: sshkey_xmss_params(key) == NULL ||
198: signature == NULL || signaturelen == 0)
199: return SSH_ERR_INVALID_ARGUMENT;
200: if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0)
201: return r;
202: if (datalen >= INT_MAX - required_siglen)
203: return SSH_ERR_INVALID_ARGUMENT;
204:
205: if ((b = sshbuf_from(signature, signaturelen)) == NULL)
206: return SSH_ERR_ALLOC_FAIL;
207: if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 ||
208: (r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0)
209: goto out;
210: if (strcmp("ssh-xmss@openssh.com", ktype) != 0) {
211: r = SSH_ERR_KEY_TYPE_MISMATCH;
212: goto out;
213: }
214: if (sshbuf_len(b) != 0) {
215: r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
216: goto out;
217: }
218: if (len != required_siglen) {
219: r = SSH_ERR_INVALID_FORMAT;
220: goto out;
221: }
222: if (datalen >= SIZE_MAX - len) {
223: r = SSH_ERR_INVALID_ARGUMENT;
224: goto out;
225: }
226: smlen = len + datalen;
227: mlen = smlen;
228: if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
229: r = SSH_ERR_ALLOC_FAIL;
230: goto out;
231: }
232: memcpy(sm, sigblob, len);
233: memcpy(sm+len, data, datalen);
234: if ((ret = xmss_sign_open(m, &mlen, sm, smlen,
235: key->xmss_pk, sshkey_xmss_params(key))) != 0) {
1.3 djm 236: debug2_f("xmss_sign_open failed: %d", ret);
1.1 markus 237: }
238: if (ret != 0 || mlen != datalen) {
239: r = SSH_ERR_SIGNATURE_INVALID;
240: goto out;
241: }
242: /* XXX compare 'm' and 'data' ? */
243: /* success */
244: r = 0;
245: out:
1.2 jsg 246: if (sm != NULL)
247: freezero(sm, smlen);
248: if (m != NULL)
249: freezero(m, smlen);
1.1 markus 250: sshbuf_free(b);
251: free(ktype);
252: return r;
253: }
1.6 djm 254:
255: static const struct sshkey_impl_funcs sshkey_xmss_funcs = {
256: /* .size = */ NULL,
257: /* .alloc = */ NULL,
258: /* .cleanup = */ ssh_xmss_cleanup,
1.7 djm 259: /* .equal = */ ssh_xmss_equal,
1.8 djm 260: /* .ssh_serialize_public = */ ssh_xmss_serialize_public,
1.9 djm 261: /* .generate = */ sshkey_xmss_generate_private_key,
1.10 ! djm 262: /* .copy_public = */ ssh_xmss_copy_public,
1.6 djm 263: };
264:
265: const struct sshkey_impl sshkey_xmss_impl = {
266: /* .name = */ "ssh-xmss@openssh.com",
267: /* .shortname = */ "XMSS",
268: /* .sigalg = */ NULL,
269: /* .type = */ KEY_XMSS,
270: /* .nid = */ 0,
271: /* .cert = */ 0,
272: /* .sigonly = */ 0,
273: /* .keybits = */ 256,
274: /* .funcs = */ &sshkey_xmss_funcs,
275: };
276:
277: const struct sshkey_impl sshkey_xmss_cert_impl = {
278: /* .name = */ "ssh-xmss-cert-v01@openssh.com",
279: /* .shortname = */ "XMSS-CERT",
280: /* .sigalg = */ NULL,
281: /* .type = */ KEY_XMSS_CERT,
282: /* .nid = */ 0,
283: /* .cert = */ 1,
284: /* .sigonly = */ 0,
285: /* .keybits = */ 256,
286: /* .funcs = */ &sshkey_xmss_funcs,
287: };