blob: 160d1f23bf8d37bb4386ed69f5bedf5bbdf38b0e [file] [log] [blame]
Damien Millerbee3a232014-02-24 15:57:22 +11001/* $OpenBSD: ssh-ed25519.c,v 1.3 2014/02/23 20:03:42 djm Exp $ */
Damien Miller5be9d9e2013-12-07 11:24:01 +11002/*
3 * Copyright (c) 2013 Markus Friedl <markus@openbsd.org>
4 *
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.
8 *
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.
16 */
17
Damien Millerf104da22013-12-07 12:37:53 +110018#include "includes.h"
19
Damien Miller5be9d9e2013-12-07 11:24:01 +110020#include <sys/types.h>
21
22#include "crypto_api.h"
23
Damien Millerbee3a232014-02-24 15:57:22 +110024#include <limits.h>
Damien Miller5be9d9e2013-12-07 11:24:01 +110025#include <string.h>
26#include <stdarg.h>
27
28#include "xmalloc.h"
29#include "log.h"
30#include "buffer.h"
31#include "key.h"
32#include "ssh.h"
33
34int
35ssh_ed25519_sign(const Key *key, u_char **sigp, u_int *lenp,
36 const u_char *data, u_int datalen)
37{
38 u_char *sig;
39 u_int slen, len;
40 unsigned long long smlen;
41 int ret;
42 Buffer b;
43
44 if (key == NULL || key_type_plain(key->type) != KEY_ED25519 ||
45 key->ed25519_sk == NULL) {
46 error("%s: no ED25519 key", __func__);
47 return -1;
48 }
Damien Millerbee3a232014-02-24 15:57:22 +110049
50 if (datalen >= UINT_MAX - crypto_sign_ed25519_BYTES) {
51 error("%s: datalen %u too long", __func__, datalen);
52 return -1;
53 }
Damien Miller5be9d9e2013-12-07 11:24:01 +110054 smlen = slen = datalen + crypto_sign_ed25519_BYTES;
55 sig = xmalloc(slen);
56
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);
60 free(sig);
61 return -1;
62 }
63 /* encode signature */
64 buffer_init(&b);
65 buffer_put_cstring(&b, "ssh-ed25519");
66 buffer_put_string(&b, sig, smlen - datalen);
67 len = buffer_len(&b);
68 if (lenp != NULL)
69 *lenp = len;
70 if (sigp != NULL) {
71 *sigp = xmalloc(len);
72 memcpy(*sigp, buffer_ptr(&b), len);
73 }
74 buffer_free(&b);
Damien Millera5103f42014-02-04 11:20:14 +110075 explicit_bzero(sig, slen);
Damien Miller5be9d9e2013-12-07 11:24:01 +110076 free(sig);
77
78 return 0;
79}
80
81int
82ssh_ed25519_verify(const Key *key, const u_char *signature, u_int signaturelen,
83 const u_char *data, u_int datalen)
84{
85 Buffer b;
86 char *ktype;
87 u_char *sigblob, *sm, *m;
88 u_int len;
89 unsigned long long smlen, mlen;
90 int rlen, ret;
91
92 if (key == NULL || key_type_plain(key->type) != KEY_ED25519 ||
93 key->ed25519_pk == NULL) {
94 error("%s: no ED25519 key", __func__);
95 return -1;
96 }
97 buffer_init(&b);
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);
102 buffer_free(&b);
103 free(ktype);
104 return -1;
105 }
106 free(ktype);
107 sigblob = buffer_get_string(&b, &len);
108 rlen = buffer_len(&b);
109 buffer_free(&b);
110 if (rlen != 0) {
111 error("%s: remaining bytes in signature %d", __func__, rlen);
112 free(sigblob);
113 return -1;
114 }
115 if (len > crypto_sign_ed25519_BYTES) {
116 error("%s: len %u > crypto_sign_ed25519_BYTES %u", __func__,
117 len, crypto_sign_ed25519_BYTES);
118 free(sigblob);
119 return -1;
120 }
121 smlen = len + datalen;
122 sm = xmalloc(smlen);
123 memcpy(sm, sigblob, len);
124 memcpy(sm+len, data, datalen);
125 mlen = smlen;
126 m = xmalloc(mlen);
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",
130 __func__, ret);
131 }
132 if (ret == 0 && mlen != datalen) {
133 debug2("%s: crypto_sign_ed25519_open "
134 "mlen != datalen (%llu != %u)", __func__, mlen, datalen);
135 ret = -1;
136 }
137 /* XXX compare 'm' and 'data' ? */
138
Damien Millera5103f42014-02-04 11:20:14 +1100139 explicit_bzero(sigblob, len);
140 explicit_bzero(sm, smlen);
141 explicit_bzero(m, smlen); /* NB. mlen may be invalid if ret != 0 */
Damien Miller5be9d9e2013-12-07 11:24:01 +1100142 free(sigblob);
143 free(sm);
144 free(m);
145 debug("%s: signature %scorrect", __func__, (ret != 0) ? "in" : "");
146
147 /* translate return code carefully */
148 return (ret == 0) ? 1 : -1;
149}