diff options
Diffstat (limited to 'ssh-ed25519.c')
-rw-r--r-- | ssh-ed25519.c | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/ssh-ed25519.c b/ssh-ed25519.c new file mode 100644 index 000000000..1aedcf83a --- /dev/null +++ b/ssh-ed25519.c | |||
@@ -0,0 +1,143 @@ | |||
1 | /* $OpenBSD: ssh-ed25519.c,v 1.1 2013/12/06 13:39:49 markus Exp $ */ | ||
2 | /* | ||
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 | |||
18 | #include "includes.h" | ||
19 | |||
20 | #include <sys/types.h> | ||
21 | |||
22 | #include "crypto_api.h" | ||
23 | |||
24 | #include <string.h> | ||
25 | #include <stdarg.h> | ||
26 | |||
27 | #include "xmalloc.h" | ||
28 | #include "log.h" | ||
29 | #include "buffer.h" | ||
30 | #include "key.h" | ||
31 | #include "ssh.h" | ||
32 | |||
33 | int | ||
34 | ssh_ed25519_sign(const Key *key, u_char **sigp, u_int *lenp, | ||
35 | const u_char *data, u_int datalen) | ||
36 | { | ||
37 | u_char *sig; | ||
38 | u_int slen, len; | ||
39 | unsigned long long smlen; | ||
40 | int ret; | ||
41 | Buffer b; | ||
42 | |||
43 | if (key == NULL || key_type_plain(key->type) != KEY_ED25519 || | ||
44 | key->ed25519_sk == NULL) { | ||
45 | error("%s: no ED25519 key", __func__); | ||
46 | return -1; | ||
47 | } | ||
48 | smlen = slen = datalen + crypto_sign_ed25519_BYTES; | ||
49 | sig = xmalloc(slen); | ||
50 | |||
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); | ||
54 | free(sig); | ||
55 | return -1; | ||
56 | } | ||
57 | /* encode signature */ | ||
58 | buffer_init(&b); | ||
59 | buffer_put_cstring(&b, "ssh-ed25519"); | ||
60 | buffer_put_string(&b, sig, smlen - datalen); | ||
61 | len = buffer_len(&b); | ||
62 | if (lenp != NULL) | ||
63 | *lenp = len; | ||
64 | if (sigp != NULL) { | ||
65 | *sigp = xmalloc(len); | ||
66 | memcpy(*sigp, buffer_ptr(&b), len); | ||
67 | } | ||
68 | buffer_free(&b); | ||
69 | memset(sig, 's', slen); | ||
70 | free(sig); | ||
71 | |||
72 | return 0; | ||
73 | } | ||
74 | |||
75 | int | ||
76 | ssh_ed25519_verify(const Key *key, const u_char *signature, u_int signaturelen, | ||
77 | const u_char *data, u_int datalen) | ||
78 | { | ||
79 | Buffer b; | ||
80 | char *ktype; | ||
81 | u_char *sigblob, *sm, *m; | ||
82 | u_int len; | ||
83 | unsigned long long smlen, mlen; | ||
84 | int rlen, ret; | ||
85 | |||
86 | if (key == NULL || key_type_plain(key->type) != KEY_ED25519 || | ||
87 | key->ed25519_pk == NULL) { | ||
88 | error("%s: no ED25519 key", __func__); | ||
89 | return -1; | ||
90 | } | ||
91 | buffer_init(&b); | ||
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); | ||
96 | buffer_free(&b); | ||
97 | free(ktype); | ||
98 | return -1; | ||
99 | } | ||
100 | free(ktype); | ||
101 | sigblob = buffer_get_string(&b, &len); | ||
102 | rlen = buffer_len(&b); | ||
103 | buffer_free(&b); | ||
104 | if (rlen != 0) { | ||
105 | error("%s: remaining bytes in signature %d", __func__, rlen); | ||
106 | free(sigblob); | ||
107 | return -1; | ||
108 | } | ||
109 | if (len > crypto_sign_ed25519_BYTES) { | ||
110 | error("%s: len %u > crypto_sign_ed25519_BYTES %u", __func__, | ||
111 | len, crypto_sign_ed25519_BYTES); | ||
112 | free(sigblob); | ||
113 | return -1; | ||
114 | } | ||
115 | smlen = len + datalen; | ||
116 | sm = xmalloc(smlen); | ||
117 | memcpy(sm, sigblob, len); | ||
118 | memcpy(sm+len, data, datalen); | ||
119 | mlen = smlen; | ||
120 | m = xmalloc(mlen); | ||
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", | ||
124 | __func__, ret); | ||
125 | } | ||
126 | if (ret == 0 && mlen != datalen) { | ||
127 | debug2("%s: crypto_sign_ed25519_open " | ||
128 | "mlen != datalen (%llu != %u)", __func__, mlen, datalen); | ||
129 | ret = -1; | ||
130 | } | ||
131 | /* XXX compare 'm' and 'data' ? */ | ||
132 | |||
133 | memset(sigblob, 's', len); | ||
134 | memset(sm, 'S', smlen); | ||
135 | memset(m, 'm', smlen); /* NB. mlen may be invalid if ret != 0 */ | ||
136 | free(sigblob); | ||
137 | free(sm); | ||
138 | free(m); | ||
139 | debug("%s: signature %scorrect", __func__, (ret != 0) ? "in" : ""); | ||
140 | |||
141 | /* translate return code carefully */ | ||
142 | return (ret == 0) ? 1 : -1; | ||
143 | } | ||