1 /* $OpenBSD: ssh-ed25519.c,v 1.3 2014/02/23 20:03:42 djm Exp $ */
3 * Copyright (c) 2013 Markus Friedl <markus@openbsd.org>
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.
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.
20 #include <sys/types.h>
22 #include "crypto_api.h"
35 ssh_ed25519_sign(const Key *key, u_char **sigp, u_int *lenp,
36 const u_char *data, u_int datalen)
40 unsigned long long smlen;
44 if (key == NULL || key_type_plain(key->type) != KEY_ED25519 ||
45 key->ed25519_sk == NULL) {
46 error("%s: no ED25519 key", __func__);
50 if (datalen >= UINT_MAX - crypto_sign_ed25519_BYTES) {
51 error("%s: datalen %u too long", __func__, datalen);
54 smlen = slen = datalen + crypto_sign_ed25519_BYTES;
57 if ((ret = crypto_sign_ed25519(sig, &smlen, data, datalen,
58 key->ed25519_sk)) != 0 || smlen <= datalen) {
59 error("%s: crypto_sign_ed25519 failed: %d", __func__, ret);
63 /* encode signature */
65 buffer_put_cstring(&b, "ssh-ed25519");
66 buffer_put_string(&b, sig, smlen - datalen);
72 memcpy(*sigp, buffer_ptr(&b), len);
75 explicit_bzero(sig, slen);
82 ssh_ed25519_verify(const Key *key, const u_char *signature, u_int signaturelen,
83 const u_char *data, u_int datalen)
87 u_char *sigblob, *sm, *m;
89 unsigned long long smlen, mlen;
92 if (key == NULL || key_type_plain(key->type) != KEY_ED25519 ||
93 key->ed25519_pk == NULL) {
94 error("%s: no ED25519 key", __func__);
98 buffer_append(&b, signature, signaturelen);
99 ktype = buffer_get_cstring(&b, NULL);
100 if (strcmp("ssh-ed25519", ktype) != 0) {
101 error("%s: cannot handle type %s", __func__, ktype);
107 sigblob = buffer_get_string(&b, &len);
108 rlen = buffer_len(&b);
111 error("%s: remaining bytes in signature %d", __func__, rlen);
115 if (len > crypto_sign_ed25519_BYTES) {
116 error("%s: len %u > crypto_sign_ed25519_BYTES %u", __func__,
117 len, crypto_sign_ed25519_BYTES);
121 smlen = len + datalen;
123 memcpy(sm, sigblob, len);
124 memcpy(sm+len, data, datalen);
127 if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
128 key->ed25519_pk)) != 0) {
129 debug2("%s: crypto_sign_ed25519_open failed: %d",
132 if (ret == 0 && mlen != datalen) {
133 debug2("%s: crypto_sign_ed25519_open "
134 "mlen != datalen (%llu != %u)", __func__, mlen, datalen);
137 /* XXX compare 'm' and 'data' ? */
139 explicit_bzero(sigblob, len);
140 explicit_bzero(sm, smlen);
141 explicit_bzero(m, smlen); /* NB. mlen may be invalid if ret != 0 */
145 debug("%s: signature %scorrect", __func__, (ret != 0) ? "in" : "");
147 /* translate return code carefully */
148 return (ret == 0) ? 1 : -1;