Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ed25519 support #75

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ OPTION_HEADERS += localoptions.h
endif

COMMONOBJS=dbutil.o buffer.o dbhelpers.o \
dss.o bignum.o \
dss.o ed25519.o ed25519_crypto.o bignum.o \
signkey.o rsa.o dbrandom.o \
queue.o \
atomicio.o compat.o fake-rfc2553.o \
ltc_prng.o ecc.o ecdsa.o crypto_desc.o \
dbmalloc.o \
gensignkey.o gendss.o genrsa.o
gensignkey.o gendss.o gened25519.o genrsa.o

SVROBJS=svr-kex.o svr-auth.o sshpty.o \
svr-authpasswd.o svr-authpubkey.o svr-authpubkeyoptions.o svr-session.o svr-service.o \
Expand Down
3 changes: 3 additions & 0 deletions common-algo.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ algo_type sshhostkey[] = {
#endif
#if DROPBEAR_DSS
{"ssh-dss", DROPBEAR_SIGNKEY_DSS, NULL, 1, NULL},
#endif
#ifdef DROPBEAR_ED25519
{"ssh-ed25519", DROPBEAR_SIGNKEY_ED25519, NULL, 1, NULL},
#endif
{NULL, 0, NULL, 0, NULL}
};
Expand Down
1 change: 0 additions & 1 deletion common-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "packet.h"
#include "algo.h"
#include "buffer.h"
#include "dss.h"
#include "ssh.h"
#include "dbrandom.h"
#include "kex.h"
Expand Down
1 change: 1 addition & 0 deletions default_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ IMPORTANT: Some options will require "make clean" after changes */
* code (either ECDSA or ECDH) increases binary size - around 30kB
* on x86-64 */
#define DROPBEAR_ECDSA 1
#define DROPBEAR_ED25519 1

/* RSA must be >=1024 */
#define DROPBEAR_DEFAULT_RSA_SIZE 2048
Expand Down
20 changes: 20 additions & 0 deletions dropbearkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
* mp_int y
* mp_int x
*
* ECDSA:
* string "ecdsa-sha2-nistp256" or "ecdsa-sha2-nistp384" or "ecdsa-sha2-nistp521"
* (not discribed here further)
*
* ed25519:
* string "ssh-ed25519"
* string 64 bytes: private_key (32 bytes) + public_key (32 bytes)
*
*/
#include "includes.h"
#include "signkey.h"
Expand Down Expand Up @@ -75,6 +83,9 @@ static void printhelp(char * progname) {
#endif
#if DROPBEAR_ECDSA
" ecdsa\n"
#endif
#ifdef DROPBEAR_ED25519
" ed25519\n"
#endif
"-f filename Use filename for the secret key.\n"
" ~/.ssh/id_dropbear is recommended for client keys.\n"
Expand All @@ -94,6 +105,9 @@ static void printhelp(char * progname) {
"521 "
#endif
"\n"
#endif
#ifdef DROPBEAR_ED25519
" ed25519 has a fixed size of 256 bits\n"
#endif
"-y Just print the publickey and fingerprint for the\n private key in <filename>.\n"
#if DEBUG_TRACE
Expand Down Expand Up @@ -224,6 +238,12 @@ int main(int argc, char ** argv) {
keytype = DROPBEAR_SIGNKEY_ECDSA_KEYGEN;
}
#endif
#ifdef DROPBEAR_ED25519
if (strcmp(typetext, "ed25519") == 0)
{
keytype = DROPBEAR_SIGNKEY_ED25519;
}
#endif

if (keytype == DROPBEAR_SIGNKEY_NONE) {
fprintf(stderr, "Unknown key type '%s'\n", typetext);
Expand Down
130 changes: 130 additions & 0 deletions ed25519.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Dropbear - a SSH2 server
*
* Copyright (c) 2002,2003 Matt Johnston
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. */

#include "includes.h"
#include "dbutil.h"
#include "ed25519.h"
#include "ed25519_crypto.h"
#include "buffer.h"
#include "ssh.h"

/* Handle ed25519 (Edwards 25519 elliptic curve)
* operations, such as key reading, signing, verification. Key generation
* will be in gened25519.c, since it isn't required in the server itself.
*/

#ifdef DROPBEAR_ED25519

/* Load a ed25519 key from a buffer, initialising the values.
* The key will have the same format as buf_put_ed25519_key.
* These should be freed with ed25519_key_free.
* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
int buf_get_ed25519_pub_key(buffer* buf, dropbear_ed25519_key *key) {
/* Format (same as the base64-encoded 2nd word in each line of
* ~/.ssh/authorized_keys used by dropbear and OpenSSH):
*
* "\0\0\0\x0bssh-ed25519\0\0\0 " + pk,
* where pk is 32 bytes of public key.
* It's 51 bytes in total.
*/
if (buf->pos + 51 > buf->len ||
0 != memcmp(buf->data + buf->pos, "\0\0\0\x0bssh-ed25519\0\0\0 ", 19)
) return DROPBEAR_FAILURE;
memset(key->spk, '\0', 32); /* Secret key not known. */
memcpy(key->spk + 32, buf->data + buf->pos + 19, 32);
buf->pos += 51;
return DROPBEAR_SUCCESS;
}

/* Loads a private ed25519 key from a buffer
* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
int buf_get_ed25519_priv_key(buffer* buf, dropbear_ed25519_key *key) {
/* Format (not shared with OpenSSH):
*
* "\0\0\0\x0bssh-ed25519\0\0\0@" + sk + pk,
* where sk is 32 bytes of secret key (private key).
* where pk is 32 bytes of public key.
* It's 83 bytes in total.
*/
if (buf->pos + 83 > buf->len ||
0 != memcmp(buf->data + buf->pos, "\0\0\0\x0bssh-ed25519\0\0\0@", 19)
) return DROPBEAR_FAILURE;
memcpy(key->spk, buf->data + buf->pos + 19, 64);
buf->pos += 83;
return DROPBEAR_SUCCESS;
}


/* Clear and free the memory used by a public or private key */
void ed25519_key_free(dropbear_ed25519_key *key) {
(void)key;
}

/* put the ed25519 public key into the buffer in the required format. */
void buf_put_ed25519_pub_key(buffer* buf, dropbear_ed25519_key *key) {
dropbear_assert(key != NULL);
buf_putstring(buf, SSH_SIGNKEY_ED25519, SSH_SIGNKEY_ED25519_LEN);
buf_putstring(buf, key->spk + 32, 32);
}

/* put the ed25519 private key into the buffer in the required format. */
void buf_put_ed25519_priv_key(buffer* buf, dropbear_ed25519_key *key) {
dropbear_assert(key != NULL);
buf_putstring(buf, SSH_SIGNKEY_ED25519, SSH_SIGNKEY_ED25519_LEN);
buf_putstring(buf, key->spk, 64);
}

#ifdef DROPBEAR_SIGNKEY_VERIFY
/* Verify a ed25519 signature (in buf) made on data by the key given.
* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
int buf_ed25519_verify(buffer* buf, dropbear_ed25519_key *key, buffer *data_buf) {
char sm[512];
int res;
/* Typical data_size is 148 bytes for ssh. */
unsigned data_size;
if (buf_getint(buf) != 64 ||
buf->len - buf->pos != 64 ||
(data_size = data_buf->len) > sizeof(sm) - 64) {
return DROPBEAR_FAILURE;
}
memcpy(sm, buf->data + buf->pos, 64);
memcpy(sm + 64, data_buf->data, data_size);
res = ed25519_crypto_verify(sm, data_size, key->spk + 32);
return res ? DROPBEAR_FAILURE : DROPBEAR_SUCCESS;
}
#endif /* DROPBEAR_SIGNKEY_VERIFY */

/* Sign the data presented with key, writing the signature contents
* to buf */
void buf_put_ed25519_sign(buffer* buf, dropbear_ed25519_key *key, buffer *data_buf) {
char sm[512];
/* Typical data_size is 32 bytes for ssh. */
unsigned data_size = data_buf->len;
if (data_size > sizeof(sm) - 64) dropbear_exit("ed25519 message to sign too long");
ed25519_crypto_sign(sm, data_buf->data, data_size, key->spk);
buf_putstring(buf, SSH_SIGNKEY_ED25519, SSH_SIGNKEY_ED25519_LEN);
buf_putstring(buf, sm, 64);
}

#endif /* DROPBEAR_ED25519 */
49 changes: 49 additions & 0 deletions ed25519.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Dropbear - a SSH2 server
*
* Copyright (c) 2002,2003 Matt Johnston
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE. */

#ifndef DROPBEAR_ED25519_H_
#define DROPBEAR_ED25519_H_

#include "includes.h"
#include "buffer.h"

#ifdef DROPBEAR_ED25519

typedef struct {
char spk[64]; /* The first 32 bytes is th private part. */
} dropbear_ed25519_key;

void buf_put_ed25519_sign(buffer* buf, dropbear_ed25519_key *key, buffer *data_buf);
#ifdef DROPBEAR_SIGNKEY_VERIFY
int buf_ed25519_verify(buffer* buf, dropbear_ed25519_key *key, buffer *data_buf);
#endif
int buf_get_ed25519_pub_key(buffer* buf, dropbear_ed25519_key *key);
int buf_get_ed25519_priv_key(buffer* buf, dropbear_ed25519_key *key);
void buf_put_ed25519_pub_key(buffer* buf, dropbear_ed25519_key *key);
void buf_put_ed25519_priv_key(buffer* buf, dropbear_ed25519_key *key);
void ed25519_key_free(dropbear_ed25519_key *key);

#endif /* DROPBEAR_ED25519 */

#endif /* DROPBEAR_ED25519_H_ */
Loading