Annotation of src/usr.bin/ssh/ssh-xmss.c, Revision 1.11
1.11 ! djm 1: /* $OpenBSD: ssh-xmss.c,v 1.10 2022/10/28 00:41:17 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,
1.11 ! djm 65: enum sshkey_serialize_rep opts)
1.8 djm 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;
1.11 ! djm 72: if ((r = sshbuf_put_cstring(b, key->xmss_name)) != 0 ||
1.8 djm 73: (r = sshbuf_put_string(b, key->xmss_pk,
74: sshkey_xmss_pklen(key))) != 0 ||
75: (r = sshkey_xmss_serialize_pk_info(key, b, opts)) != 0)
76: return r;
77:
78: return 0;
79: }
80:
1.10 djm 81: static int
82: ssh_xmss_copy_public(const struct sshkey *from, struct sshkey *to)
83: {
84: int r = SSH_ERR_INTERNAL_ERROR;
85: u_int32_t left;
86: size_t pklen;
87:
88: if ((r = sshkey_xmss_init(to, from->xmss_name)) != 0)
89: return r;
90: if (from->xmss_pk == NULL)
91: return 0; /* XXX SSH_ERR_INTERNAL_ERROR ? */
92:
93: if ((pklen = sshkey_xmss_pklen(from)) == 0 ||
94: sshkey_xmss_pklen(to) != pklen)
95: return SSH_ERR_INTERNAL_ERROR;
96: if ((to->xmss_pk = malloc(pklen)) == NULL)
97: return SSH_ERR_ALLOC_FAIL;
98: memcpy(to->xmss_pk, from->xmss_pk, pklen);
99: /* simulate number of signatures left on pubkey */
100: left = sshkey_xmss_signatures_left(from);
101: if (left)
102: sshkey_xmss_enable_maxsign(to, left);
103: return 0;
104: }
105:
1.11 ! djm 106: static int
! 107: ssh_xmss_deserialize_public(const char *ktype, struct sshbuf *b,
! 108: struct sshkey *key)
! 109: {
! 110: size_t len = 0;
! 111: char *xmss_name = NULL;
! 112: u_char *pk = NULL;
! 113: int ret = SSH_ERR_INTERNAL_ERROR;
! 114:
! 115: if ((ret = sshbuf_get_cstring(b, &xmss_name, NULL)) != 0)
! 116: goto out;
! 117: if ((ret = sshkey_xmss_init(key, xmss_name)) != 0)
! 118: goto out;
! 119: if ((ret = sshbuf_get_string(b, &pk, &len)) != 0)
! 120: goto out;
! 121: if (len == 0 || len != sshkey_xmss_pklen(key)) {
! 122: ret = SSH_ERR_INVALID_FORMAT;
! 123: goto out;
! 124: }
! 125: key->xmss_pk = pk;
! 126: pk = NULL;
! 127: if (!sshkey_is_cert(key) &&
! 128: (ret = sshkey_xmss_deserialize_pk_info(key, b)) != 0)
! 129: goto out;
! 130: /* success */
! 131: ret = 0;
! 132: out:
! 133: free(xmss_name);
! 134: freezero(pk, len);
! 135: return ret;
! 136: }
! 137:
1.1 markus 138: int
139: ssh_xmss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
140: const u_char *data, size_t datalen, u_int compat)
141: {
142: u_char *sig = NULL;
143: size_t slen = 0, len = 0, required_siglen;
144: unsigned long long smlen;
145: int r, ret;
146: struct sshbuf *b = NULL;
147:
148: if (lenp != NULL)
149: *lenp = 0;
150: if (sigp != NULL)
151: *sigp = NULL;
152:
153: if (key == NULL ||
154: sshkey_type_plain(key->type) != KEY_XMSS ||
155: key->xmss_sk == NULL ||
156: sshkey_xmss_params(key) == NULL)
157: return SSH_ERR_INVALID_ARGUMENT;
158: if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0)
159: return r;
160: if (datalen >= INT_MAX - required_siglen)
161: return SSH_ERR_INVALID_ARGUMENT;
162: smlen = slen = datalen + required_siglen;
163: if ((sig = malloc(slen)) == NULL)
164: return SSH_ERR_ALLOC_FAIL;
1.4 dtucker 165: if ((r = sshkey_xmss_get_state(key, 1)) != 0)
1.1 markus 166: goto out;
167: if ((ret = xmss_sign(key->xmss_sk, sshkey_xmss_bds_state(key), sig, &smlen,
168: data, datalen, sshkey_xmss_params(key))) != 0 || smlen <= datalen) {
169: r = SSH_ERR_INVALID_ARGUMENT; /* XXX better error? */
170: goto out;
171: }
172: /* encode signature */
173: if ((b = sshbuf_new()) == NULL) {
174: r = SSH_ERR_ALLOC_FAIL;
175: goto out;
176: }
177: if ((r = sshbuf_put_cstring(b, "ssh-xmss@openssh.com")) != 0 ||
178: (r = sshbuf_put_string(b, sig, smlen - datalen)) != 0)
179: goto out;
180: len = sshbuf_len(b);
181: if (sigp != NULL) {
182: if ((*sigp = malloc(len)) == NULL) {
183: r = SSH_ERR_ALLOC_FAIL;
184: goto out;
185: }
186: memcpy(*sigp, sshbuf_ptr(b), len);
187: }
188: if (lenp != NULL)
189: *lenp = len;
190: /* success */
191: r = 0;
192: out:
1.4 dtucker 193: if ((ret = sshkey_xmss_update_state(key, 1)) != 0) {
1.1 markus 194: /* discard signature since we cannot update the state */
195: if (r == 0 && sigp != NULL && *sigp != NULL) {
196: explicit_bzero(*sigp, len);
197: free(*sigp);
198: }
199: if (sigp != NULL)
200: *sigp = NULL;
201: if (lenp != NULL)
202: *lenp = 0;
203: r = ret;
204: }
205: sshbuf_free(b);
1.2 jsg 206: if (sig != NULL)
207: freezero(sig, slen);
1.1 markus 208:
209: return r;
210: }
211:
212: int
213: ssh_xmss_verify(const struct sshkey *key,
214: const u_char *signature, size_t signaturelen,
215: const u_char *data, size_t datalen, u_int compat)
216: {
217: struct sshbuf *b = NULL;
218: char *ktype = NULL;
219: const u_char *sigblob;
220: u_char *sm = NULL, *m = NULL;
221: size_t len, required_siglen;
222: unsigned long long smlen = 0, mlen = 0;
223: int r, ret;
224:
225: if (key == NULL ||
226: sshkey_type_plain(key->type) != KEY_XMSS ||
227: key->xmss_pk == NULL ||
228: sshkey_xmss_params(key) == NULL ||
229: signature == NULL || signaturelen == 0)
230: return SSH_ERR_INVALID_ARGUMENT;
231: if ((r = sshkey_xmss_siglen(key, &required_siglen)) != 0)
232: return r;
233: if (datalen >= INT_MAX - required_siglen)
234: return SSH_ERR_INVALID_ARGUMENT;
235:
236: if ((b = sshbuf_from(signature, signaturelen)) == NULL)
237: return SSH_ERR_ALLOC_FAIL;
238: if ((r = sshbuf_get_cstring(b, &ktype, NULL)) != 0 ||
239: (r = sshbuf_get_string_direct(b, &sigblob, &len)) != 0)
240: goto out;
241: if (strcmp("ssh-xmss@openssh.com", ktype) != 0) {
242: r = SSH_ERR_KEY_TYPE_MISMATCH;
243: goto out;
244: }
245: if (sshbuf_len(b) != 0) {
246: r = SSH_ERR_UNEXPECTED_TRAILING_DATA;
247: goto out;
248: }
249: if (len != required_siglen) {
250: r = SSH_ERR_INVALID_FORMAT;
251: goto out;
252: }
253: if (datalen >= SIZE_MAX - len) {
254: r = SSH_ERR_INVALID_ARGUMENT;
255: goto out;
256: }
257: smlen = len + datalen;
258: mlen = smlen;
259: if ((sm = malloc(smlen)) == NULL || (m = malloc(mlen)) == NULL) {
260: r = SSH_ERR_ALLOC_FAIL;
261: goto out;
262: }
263: memcpy(sm, sigblob, len);
264: memcpy(sm+len, data, datalen);
265: if ((ret = xmss_sign_open(m, &mlen, sm, smlen,
266: key->xmss_pk, sshkey_xmss_params(key))) != 0) {
1.3 djm 267: debug2_f("xmss_sign_open failed: %d", ret);
1.1 markus 268: }
269: if (ret != 0 || mlen != datalen) {
270: r = SSH_ERR_SIGNATURE_INVALID;
271: goto out;
272: }
273: /* XXX compare 'm' and 'data' ? */
274: /* success */
275: r = 0;
276: out:
1.2 jsg 277: if (sm != NULL)
278: freezero(sm, smlen);
279: if (m != NULL)
280: freezero(m, smlen);
1.1 markus 281: sshbuf_free(b);
282: free(ktype);
283: return r;
284: }
1.6 djm 285:
286: static const struct sshkey_impl_funcs sshkey_xmss_funcs = {
287: /* .size = */ NULL,
288: /* .alloc = */ NULL,
289: /* .cleanup = */ ssh_xmss_cleanup,
1.7 djm 290: /* .equal = */ ssh_xmss_equal,
1.8 djm 291: /* .ssh_serialize_public = */ ssh_xmss_serialize_public,
1.11 ! djm 292: /* .ssh_deserialize_public = */ ssh_xmss_deserialize_public,
1.9 djm 293: /* .generate = */ sshkey_xmss_generate_private_key,
1.10 djm 294: /* .copy_public = */ ssh_xmss_copy_public,
1.6 djm 295: };
296:
297: const struct sshkey_impl sshkey_xmss_impl = {
298: /* .name = */ "ssh-xmss@openssh.com",
299: /* .shortname = */ "XMSS",
300: /* .sigalg = */ NULL,
301: /* .type = */ KEY_XMSS,
302: /* .nid = */ 0,
303: /* .cert = */ 0,
304: /* .sigonly = */ 0,
305: /* .keybits = */ 256,
306: /* .funcs = */ &sshkey_xmss_funcs,
307: };
308:
309: const struct sshkey_impl sshkey_xmss_cert_impl = {
310: /* .name = */ "ssh-xmss-cert-v01@openssh.com",
311: /* .shortname = */ "XMSS-CERT",
312: /* .sigalg = */ NULL,
313: /* .type = */ KEY_XMSS_CERT,
314: /* .nid = */ 0,
315: /* .cert = */ 1,
316: /* .sigonly = */ 0,
317: /* .keybits = */ 256,
318: /* .funcs = */ &sshkey_xmss_funcs,
319: };