From aa54647108f9ca1c338a38c996a775e06920d84d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Tue, 4 Mar 2014 14:11:19 +0100
Subject: [PATCH] Renamed chacha256_set_key to chacha_set_key.

---
 ChangeLog               | 10 +++++++
 Makefile.in             |  1 -
 chacha-poly1305.c       |  2 +-
 chacha-set-key.c        | 27 +++++++++++------
 chacha.h                | 12 ++------
 chacha256-set-key.c     | 65 -----------------------------------------
 nettle-internal.c       |  4 +--
 testsuite/chacha-test.c |  3 +-
 8 files changed, 35 insertions(+), 89 deletions(-)
 delete mode 100644 chacha256-set-key.c

diff --git a/ChangeLog b/ChangeLog
index 72abf3d5..53bd31fd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2014-03-04  Niels Möller  <nisse@lysator.liu.se>
 
+	* Makefile.in (nettle_SOURCES): Deleted chacha128-set-key.c and
+	chacha256-set-key.c.
+
+	* chacha.h (CHACHA256_KEY_SIZE): Deleted.
+	(chacha_set_key): Updated prototype.
+	* chacha256-set-key.c (chacha256_set_key): Deleted file and
+	function, moved to...
+	* chacha-set-key.c (chacha_set_key): Do 256-bit keys only. Deleted
+	length argument. Updated all callers.
+
 	* chacha128-set-key.c (chacha128_set_key): Deleted file and
 	function. Support for 128-bit chacha keys may be reintroduced
 	later, if really needed.
diff --git a/Makefile.in b/Makefile.in
index a9c70481..24349c3b 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -90,7 +90,6 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
 		 chacha-crypt.c chacha-core-internal.c \
 		 chacha-poly1305.c chacha-poly1305-meta.c \
 		 chacha-set-key.c chacha-set-nonce.c \
-		 chacha256-set-key.c \
 		 ctr.c des.c des3.c des-compat.c \
 		 eax.c eax-aes128.c eax-aes128-meta.c \
 		 gcm.c gcm-aes.c \
diff --git a/chacha-poly1305.c b/chacha-poly1305.c
index a5d683fa..46a992ef 100644
--- a/chacha-poly1305.c
+++ b/chacha-poly1305.c
@@ -40,7 +40,7 @@ void
 chacha_poly1305_set_key (struct chacha_poly1305_ctx *ctx,
 			 const uint8_t *key)
 {
-  chacha256_set_key (&ctx->chacha, key);
+  chacha_set_key (&ctx->chacha, key);
 }
 
 void
diff --git a/chacha-set-key.c b/chacha-set-key.c
index e9edea3b..04f86398 100644
--- a/chacha-set-key.c
+++ b/chacha-set-key.c
@@ -26,15 +26,24 @@
 
 #include "chacha.h"
 
+#include "macros.h"
+
 void
-chacha_set_key(struct chacha_ctx *ctx, size_t length, const uint8_t *key)
+chacha_set_key(struct chacha_ctx *ctx, const uint8_t *key)
 {
-  switch (length)
-    {
-    default:
-      abort ();
-    case CHACHA256_KEY_SIZE:
-      chacha256_set_key (ctx, key);
-      break;
-    }
+  static const uint32_t sigma[4] = {
+    /* "expand 32-byte k" */
+    0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
+  };
+  ctx->state[4] = LE_READ_UINT32(key + 0);
+  ctx->state[5] = LE_READ_UINT32(key + 4);
+  ctx->state[6] = LE_READ_UINT32(key + 8);
+  ctx->state[7] = LE_READ_UINT32(key + 12);
+
+  ctx->state[8]  = LE_READ_UINT32(key + 16);
+  ctx->state[9]  = LE_READ_UINT32(key + 20);
+  ctx->state[10] = LE_READ_UINT32(key + 24);
+  ctx->state[11] = LE_READ_UINT32(key + 28);
+
+  memcpy (ctx->state, sigma, sizeof(sigma));
 }
diff --git a/chacha.h b/chacha.h
index 8f703dec..7249f425 100644
--- a/chacha.h
+++ b/chacha.h
@@ -37,17 +37,13 @@ extern "C" {
 
 /* Name mangling */
 #define chacha_set_key nettle_chacha_set_key
-#define chacha256_set_key nettle_chacha256_set_key
 #define chacha_set_nonce nettle_chacha_set_nonce
 #define chacha_crypt nettle_chacha_crypt
 #define _chacha_core _nettle_chacha_core
 
-/* Possible keysizes, and a reasonable default. In octets. */
-#define CHACHA256_KEY_SIZE 32
+/* Currently, only 256-bit keys are supported. */
 #define CHACHA_KEY_SIZE 32
-
 #define CHACHA_BLOCK_SIZE 64
-
 #define CHACHA_NONCE_SIZE 8
 
 #define _CHACHA_STATE_LENGTH 16
@@ -69,11 +65,7 @@ struct chacha_ctx
 };
 
 void
-chacha256_set_key(struct chacha_ctx *ctx, const uint8_t *key);
-
-void
-chacha_set_key(struct chacha_ctx *ctx,
-	       size_t length, const uint8_t *key);
+chacha_set_key(struct chacha_ctx *ctx, const uint8_t *key);
 
 void
 chacha_set_nonce(struct chacha_ctx *ctx, const uint8_t *nonce);
diff --git a/chacha256-set-key.c b/chacha256-set-key.c
deleted file mode 100644
index 66e314b0..00000000
--- a/chacha256-set-key.c
+++ /dev/null
@@ -1,65 +0,0 @@
-/* chacha256-set-key.c
- *
- * ChaCha key setup for 256-bit keys.
- * Based on the Salsa20 implementation in Nettle.
- */
-
-/* nettle, low-level cryptographics library
- *
- * Copyright (C) 2013 Joachim Strömbergon
- * Copyright (C) 2012 Simon Josefsson
- * Copyright (C) 2012, 2014 Niels Möller
- *  
- * 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:
-   ChaCha specification (doc id: 4027b5256e17b9796842e6d0f68b0b5e) and reference 
-   implementation dated 2008.01.20
-   D. J. Bernstein
-   Public domain.
-*/
-
-#if HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <assert.h>
-#include <string.h>
-
-#include "chacha.h"
-
-#include "macros.h"
-
-void
-chacha256_set_key(struct chacha_ctx *ctx, const uint8_t *key)
-{
-  static const uint32_t sigma[4] = {
-    /* "expand 32-byte k" */
-    0x61707865, 0x3320646e, 0x79622d32, 0x6b206574
-  };
-  ctx->state[4] = LE_READ_UINT32(key + 0);
-  ctx->state[5] = LE_READ_UINT32(key + 4);
-  ctx->state[6] = LE_READ_UINT32(key + 8);
-  ctx->state[7] = LE_READ_UINT32(key + 12);
-
-  ctx->state[8]  = LE_READ_UINT32(key + 16);
-  ctx->state[9]  = LE_READ_UINT32(key + 20);
-  ctx->state[10] = LE_READ_UINT32(key + 24);
-  ctx->state[11] = LE_READ_UINT32(key + 28);
-
-  memcpy (ctx->state, sigma, sizeof(sigma));
-}
diff --git a/nettle-internal.c b/nettle-internal.c
index 6b56b1db..96e06f42 100644
--- a/nettle-internal.c
+++ b/nettle-internal.c
@@ -76,7 +76,7 @@ static void
 chacha_set_key_hack(void *ctx, const uint8_t *key)
 {
   static const uint8_t nonce[CHACHA_NONCE_SIZE];
-  chacha256_set_key (ctx, key);
+  chacha_set_key (ctx, key);
   chacha_set_nonce (ctx, nonce);
 }
 
@@ -84,7 +84,7 @@ chacha_set_key_hack(void *ctx, const uint8_t *key)
 const struct nettle_cipher
 nettle_chacha = {
   "chacha", sizeof(struct chacha_ctx),
-  0, CHACHA256_KEY_SIZE,
+  0, CHACHA_KEY_SIZE,
   chacha_set_key_hack, chacha_set_key_hack,
   (nettle_crypt_func *) chacha_crypt,
   (nettle_crypt_func *) chacha_crypt
diff --git a/testsuite/chacha-test.c b/testsuite/chacha-test.c
index 024b7806..154abd59 100644
--- a/testsuite/chacha-test.c
+++ b/testsuite/chacha-test.c
@@ -34,7 +34,8 @@ test_chacha(const struct tstring *key, const struct tstring *nonce,
 {
   struct chacha_ctx ctx;
 
-  chacha_set_key (&ctx, key->length, key->data);
+  ASSERT (key->length == CHACHA_KEY_SIZE);
+  chacha_set_key (&ctx, key->data);
   ASSERT (nonce->length == CHACHA_NONCE_SIZE);
 
   if (rounds == 20)
-- 
GitLab