From f104da263de995f66b6861b4f3368264ee483d7f Mon Sep 17 00:00:00 2001 From: Damien Miller Date: Sat, 7 Dec 2013 12:37:53 +1100 Subject: [PATCH] - (djm) [ed25519.c ssh-ed25519.c openbsd-compat/Makefile.in] [openbsd-compat/bcrypt_pbkdf.c] Make ed25519/new key format compile on Linux --- ChangeLog | 11 ++++++--- ed25519.c | 1 + openbsd-compat/Makefile.in | 4 +-- openbsd-compat/bcrypt_pbkdf.c | 46 +++++++++++++++-------------------- ssh-ed25519.c | 2 ++ 5 files changed, 31 insertions(+), 33 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5d630fb57..cf5e0d504 100644 --- a/ChangeLog +++ b/ChangeLog @@ -33,14 +33,17 @@ - djm@cvs.openbsd.org 2013/12/07 00:19:15 [key.c] set k->cert = NULL after freeing it - - [blocks.c ed25519.c fe25519.c fe25519.h ge25519.c ge25519.h] + - (djm) [blocks.c ed25519.c fe25519.c fe25519.h ge25519.c ge25519.h] [ge25519_base.data hash.c sc25519.c sc25519.h verify.c] Fix RCS idents - - [Makefile.in] Add ed25519 sources - - [authfile.c] Conditionalise inclusion of util.h - - [configure.ac openbsd-compat/Makefile.in openbsd-compat/bcrypt_pbkdf.c] + - (djm) [Makefile.in] Add ed25519 sources + - (djm) [authfile.c] Conditionalise inclusion of util.h + - (djm) [configure.ac openbsd-compat/Makefile.in openbsd-compat/bcrypt_pbkdf.c] [openbsd-compat/blf.h openbsd-compat/blowfish.c] [openbsd-compat/openbsd-compat.h] Start at supporting bcrypt_pbkdf in portable. + - (djm) [ed25519.c ssh-ed25519.c openbsd-compat/Makefile.in] + [openbsd-compat/bcrypt_pbkdf.c] Make ed25519/new key format compile on + Linux 20131205 - (djm) OpenBSD CVS Sync diff --git a/ed25519.c b/ed25519.c index c7c861bb6..9c9879c09 100644 --- a/ed25519.c +++ b/ed25519.c @@ -2,6 +2,7 @@ /* Public Domain, from supercop-20130419/crypto_sign/ed25519/ref/ed25519.c */ +#include "includes.h" #include "crypto_api.h" #include "ge25519.h" diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in index 3866a5495..276646fa6 100644 --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in @@ -1,4 +1,4 @@ -# $Id: Makefile.in,v 1.53 2013/12/07 00:51:54 djm Exp $ +# $Id: Makefile.in,v 1.54 2013/12/07 01:37:54 djm Exp $ sysconfdir=@sysconfdir@ piddir=@piddir@ @@ -16,7 +16,7 @@ RANLIB=@RANLIB@ INSTALL=@INSTALL@ LDFLAGS=-L. @LDFLAGS@ -OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt_long.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sha2.o sigact.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.o timingsafe_bcmp.o vis.o +OPENBSD=base64.o basename.o bcrypt_pbkdf.o bindresvport.o blowfish.o daemon.o dirname.o fmt_scaled.o getcwd.o getgrouplist.o getopt_long.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o pwcache.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sha2.o sigact.o strlcat.o strlcpy.o strmode.o strnlen.o strptime.o strsep.o strtonum.o strtoll.o strtoul.o strtoull.o timingsafe_bcmp.o vis.o blowfish.o bcrypt_pbkdf.o COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o diff --git a/openbsd-compat/bcrypt_pbkdf.c b/openbsd-compat/bcrypt_pbkdf.c index 58bbfe15b..e0736feaf 100644 --- a/openbsd-compat/bcrypt_pbkdf.c +++ b/openbsd-compat/bcrypt_pbkdf.c @@ -24,18 +24,13 @@ #include #include -#include #ifdef HAVE_BLF_H # include #endif -#ifdef HAVE_SHA256_UPDATE -# ifdef HAVE_SHA2_H -# include -# elif defined(HAVE_CRYPTO_SHA2_H) -# include -# endif -#endif + +#include "crypto_api.h" +#define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES /* * pkcs #5 pbkdf2 implementation using the "bcrypt" hash @@ -109,12 +104,11 @@ int bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t saltlen, u_int8_t *key, size_t keylen, unsigned int rounds) { - SHA2_CTX ctx; u_int8_t sha2pass[SHA512_DIGEST_LENGTH]; u_int8_t sha2salt[SHA512_DIGEST_LENGTH]; u_int8_t out[BCRYPT_HASHSIZE]; u_int8_t tmpout[BCRYPT_HASHSIZE]; - u_int8_t countsalt[4]; + u_int8_t *countsalt; size_t i, j, amt, stride; uint32_t count; @@ -122,37 +116,34 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t salt if (rounds < 1) return -1; if (passlen == 0 || saltlen == 0 || keylen == 0 || - keylen > sizeof(out) * sizeof(out)) + keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20) + return -1; + if ((countsalt = calloc(1, saltlen + 4)) == NULL) return -1; stride = (keylen + sizeof(out) - 1) / sizeof(out); amt = (keylen + stride - 1) / stride; - /* collapse password */ - SHA512Init(&ctx); - SHA512Update(&ctx, pass, passlen); - SHA512Final(sha2pass, &ctx); + memcpy(countsalt, salt, saltlen); + /* collapse password */ + crypto_hash_sha512(sha2pass, pass, passlen); /* generate key, sizeof(out) at a time */ for (count = 1; keylen > 0; count++) { - countsalt[0] = (count >> 24) & 0xff; - countsalt[1] = (count >> 16) & 0xff; - countsalt[2] = (count >> 8) & 0xff; - countsalt[3] = count & 0xff; + countsalt[saltlen + 0] = (count >> 24) & 0xff; + countsalt[saltlen + 1] = (count >> 16) & 0xff; + countsalt[saltlen + 2] = (count >> 8) & 0xff; + countsalt[saltlen + 3] = count & 0xff; /* first round, salt is salt */ - SHA512Init(&ctx); - SHA512Update(&ctx, salt, saltlen); - SHA512Update(&ctx, countsalt, sizeof(countsalt)); - SHA512Final(sha2salt, &ctx); + crypto_hash_sha512(sha2salt, countsalt, saltlen + 4); + bcrypt_hash(sha2pass, sha2salt, tmpout); memcpy(out, tmpout, sizeof(out)); for (i = 1; i < rounds; i++) { /* subsequent rounds, salt is previous output */ - SHA512Init(&ctx); - SHA512Update(&ctx, tmpout, sizeof(tmpout)); - SHA512Final(sha2salt, &ctx); + crypto_hash_sha512(sha2salt, tmpout, sizeof(tmpout)); bcrypt_hash(sha2pass, sha2salt, tmpout); for (j = 0; j < sizeof(out); j++) out[j] ^= tmpout[j]; @@ -168,8 +159,9 @@ bcrypt_pbkdf(const char *pass, size_t passlen, const u_int8_t *salt, size_t salt } /* zap */ - memset(&ctx, 0, sizeof(ctx)); memset(out, 0, sizeof(out)); + memset(countsalt, 0, saltlen + 4); + free(countsalt); return 0; } diff --git a/ssh-ed25519.c b/ssh-ed25519.c index c03c16346..1aedcf83a 100644 --- a/ssh-ed25519.c +++ b/ssh-ed25519.c @@ -15,6 +15,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "includes.h" + #include #include "crypto_api.h"