1 /* $OpenBSD: ssh-ed25519.c,v 1.1 2013/12/06 13:39:49 markus 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"
34 ssh_ed25519_sign(const Key *key, u_char **sigp, u_int *lenp,
35 const u_char *data, u_int datalen)
39 unsigned long long smlen;
43 if (key == NULL || key_type_plain(key->type) != KEY_ED25519 ||
44 key->ed25519_sk == NULL) {
45 error("%s: no ED25519 key", __func__);
48 smlen = slen = datalen + crypto_sign_ed25519_BYTES;
51 if ((ret = crypto_sign_ed25519(sig, &smlen, data, datalen,
52 key->ed25519_sk)) != 0 || smlen <= datalen) {
53 error("%s: crypto_sign_ed25519 failed: %d", __func__, ret);
57 /* encode signature */
59 buffer_put_cstring(&b, "ssh-ed25519");
60 buffer_put_string(&b, sig, smlen - datalen);
66 memcpy(*sigp, buffer_ptr(&b), len);
69 memset(sig, 's', slen);
76 ssh_ed25519_verify(const Key *key, const u_char *signature, u_int signaturelen,
77 const u_char *data, u_int datalen)
81 u_char *sigblob, *sm, *m;
83 unsigned long long smlen, mlen;
86 if (key == NULL || key_type_plain(key->type) != KEY_ED25519 ||
87 key->ed25519_pk == NULL) {
88 error("%s: no ED25519 key", __func__);
92 buffer_append(&b, signature, signaturelen);
93 ktype = buffer_get_cstring(&b, NULL);
94 if (strcmp("ssh-ed25519", ktype) != 0) {
95 error("%s: cannot handle type %s", __func__, ktype);
101 sigblob = buffer_get_string(&b, &len);
102 rlen = buffer_len(&b);
105 error("%s: remaining bytes in signature %d", __func__, rlen);
109 if (len > crypto_sign_ed25519_BYTES) {
110 error("%s: len %u > crypto_sign_ed25519_BYTES %u", __func__,
111 len, crypto_sign_ed25519_BYTES);
115 smlen = len + datalen;
117 memcpy(sm, sigblob, len);
118 memcpy(sm+len, data, datalen);
121 if ((ret = crypto_sign_ed25519_open(m, &mlen, sm, smlen,
122 key->ed25519_pk)) != 0) {
123 debug2("%s: crypto_sign_ed25519_open failed: %d",
126 if (ret == 0 && mlen != datalen) {
127 debug2("%s: crypto_sign_ed25519_open "
128 "mlen != datalen (%llu != %u)", __func__, mlen, datalen);
131 /* XXX compare 'm' and 'data' ? */
133 memset(sigblob, 's', len);
134 memset(sm, 'S', smlen);
135 memset(m, 'm', smlen); /* NB. mlen may be invalid if ret != 0 */
139 debug("%s: signature %scorrect", __func__, (ret != 0) ? "in" : "");
141 /* translate return code carefully */
142 return (ret == 0) ? 1 : -1;