I realized that I didn't have any use for the round-reduced Salsa20 stream functions. So I'll focus on what I needed instead, which resulted in a much smaller patch. What do you think? Open issues:
* I'm not particular happy about the name SALSA20_CORE_INOUT_SIZE. * Where could this be documented in the manual? "Miscellaneous functions"? We could also create a new section "Non-cryptographic Hash functions". There is the FNV and CRC hashes that could be implemented under that umbrella. /Simon
>From 94ed050615fc0435df797da422a665652e8018fd Mon Sep 17 00:00:00 2001 From: Simon Josefsson <[email protected]> Date: Fri, 21 Sep 2012 10:21:22 +0200 Subject: [PATCH] Support Salsa20 core. --- Makefile.in | 2 +- salsa20-core.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ salsa20.h | 7 +++++ testsuite/salsa20-test.c | 14 +++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 salsa20-core.c diff --git a/Makefile.in b/Makefile.in index 9904be5..24d9446 100644 --- a/Makefile.in +++ b/Makefile.in @@ -82,7 +82,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \ md2.c md2-meta.c md4.c md4-meta.c \ md5.c md5-compress.c md5-compat.c md5-meta.c \ ripemd160.c ripemd160-compress.c ripemd160-meta.c \ - salsa20-crypt.c salsa20-set-key.c \ + salsa20-crypt.c salsa20-set-key.c salsa20-core.c \ sha1.c sha1-compress.c sha1-meta.c \ sha256.c sha256-compress.c sha224-meta.c sha256-meta.c \ sha512.c sha512-compress.c sha384-meta.c sha512-meta.c \ diff --git a/salsa20-core.c b/salsa20-core.c new file mode 100644 index 0000000..67895cc --- /dev/null +++ b/salsa20-core.c @@ -0,0 +1,74 @@ +/* salsa20-core.c + * + * The Salsa20 core hash function. + */ + +/* nettle, low-level cryptographics library + * + * Copyright (C) 2012 Simon Josefsson + * + * The nettle library is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at your + * option) any later version. + * + * The nettle library is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the nettle library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02111-1301, USA. + */ + +/* Based on salsa20-crypt.c. */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <string.h> + +#include "salsa20.h" + +#include "macros.h" + +#define QROUND(x0, x1, x2, x3) do { \ + x1 ^= ROTL32(7, x0 + x3); \ + x2 ^= ROTL32(9, x1 + x0); \ + x3 ^= ROTL32(13, x2 + x1); \ + x0 ^= ROTL32(18, x3 + x2); \ + } while(0) + +void +salsa20_core (unsigned rounds, + uint8_t dst[SALSA20_CORE_INOUT_SIZE], + const uint8_t src[SALSA20_CORE_INOUT_SIZE]) +{ + uint32_t x[SALSA20_CORE_INOUT_SIZE / sizeof (uint32_t)]; + unsigned i; + + for (i = 0; i < SALSA20_CORE_INOUT_SIZE / sizeof (uint32_t); i++) + x[i] = LE_READ_UINT32(&src[i * 4]); + + for (i = 0; i < rounds; i += 2) + { + QROUND(x[0], x[4], x[8], x[12]); + QROUND(x[5], x[9], x[13], x[1]); + QROUND(x[10], x[14], x[2], x[6]); + QROUND(x[15], x[3], x[7], x[11]); + + QROUND(x[0], x[1], x[2], x[3]); + QROUND(x[5], x[6], x[7], x[4]); + QROUND(x[10], x[11], x[8], x[9]); + QROUND(x[15], x[12], x[13], x[14]); + } + + for (i = 0; i < SALSA20_CORE_INOUT_SIZE / sizeof (uint32_t); i++) + { + uint32_t t = x[i] + LE_READ_UINT32(&src[i * 4]); + LE_WRITE_UINT32(&dst[i * sizeof (uint32_t)], t); + } +} diff --git a/salsa20.h b/salsa20.h index 7d47f52..f13fd3d 100644 --- a/salsa20.h +++ b/salsa20.h @@ -45,6 +45,8 @@ extern "C" { #define SALSA20_KEY_SIZE 32 #define SALSA20_BLOCK_SIZE 64 +#define SALSA20_CORE_INOUT_SIZE 64 + #define SALSA20_IV_SIZE 8 #define _SALSA20_INPUT_LENGTH 16 @@ -75,6 +77,11 @@ salsa20_crypt(struct salsa20_ctx *ctx, unsigned length, uint8_t *dst, const uint8_t *src); +/* Salsa20 core. */ +void +salsa20_core (unsigned rounds, uint8_t dst[SALSA20_CORE_INOUT_SIZE], + const uint8_t src[SALSA20_CORE_INOUT_SIZE]); + #ifdef __cplusplus } #endif diff --git a/testsuite/salsa20-test.c b/testsuite/salsa20-test.c index d742ce4..f89d3ce 100644 --- a/testsuite/salsa20-test.c +++ b/testsuite/salsa20-test.c @@ -177,6 +177,20 @@ test_salsa20(const struct tstring *key, void test_main(void) { + uint8_t dst[SALSA20_CORE_INOUT_SIZE]; + + /* http://tools.ietf.org/html/draft-josefsson-scrypt-kdf */ + + salsa20_core (8, dst, H("7e879a214f3ec9867ca940e641718f26" + "baee555b8c61c1b50df846116dcd3b1d" + "ee24f319df9b3d8514121e4b5ac5aa32" + "76021d2909c74829edebc68db8b8c25e")); + ASSERT(MEMEQ (SALSA20_CORE_INOUT_SIZE, dst, + H("a41f859c6608cc993b81cacb020cef05" + "044b2181a2fd337dfd7b1c6396682f29" + "b4393168e3c9e6bcfe6bc5b7a06d96ba" + "e424cc102c91745c24ad673dc7618f81"))); + /* http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup */ test_salsa20(SHEX("80000000 00000000 00000000 00000000"), -- 1.7.9.5
_______________________________________________ nettle-bugs mailing list [email protected] http://lists.lysator.liu.se/mailman/listinfo/nettle-bugs
