]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Support salsa20 in nettle-benchmark.
authorNiels Möller <nisse@lysator.liu.se>
Fri, 30 Mar 2012 20:05:49 +0000 (22:05 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Fri, 30 Mar 2012 20:05:49 +0000 (22:05 +0200)
ChangeLog
examples/nettle-benchmark.c
nettle-internal.c
nettle-internal.h

index 9e56980b317b3fc451d5f71bdc466895e7cc8912..0b650a5e6e670255c78f6a83b904a433dfb0b832 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2012-03-30  Niels Möller  <nisse@lysator.liu.se>
 
+       * nettle-internal.c (nettle_salsa20): Cipher struct for
+       benchmarking only. Sets a fix zero IV, and ignores block size.
+       * nettle-internal.h (nettle_salsa20): Declare it.
+
+       * examples/nettle-benchmark.c (block_cipher_p): New function.
+       (time_cipher): Use block_cipher_p.
+       (main): Include salsa20 in benchmark.
+
        * Makefile.in (soname link): Fixed logic.
        (nettle_SOURCES): Removed nettle-internal.c, so that it's not
        parrt of the library...
index 424ef19f561e2415a0d5df3317f7e7e88f5d7d37..3ac2841ff0703bb44a9f0e6c2b1193c79022fab3 100644 (file)
@@ -433,6 +433,23 @@ time_gcm(void)
          time_function(bench_cipher, &cinfo));
 }
 
+static int
+prefix_p(const char *prefix, const char *s)
+{
+  size_t i;
+  for (i = 0; prefix[i]; i++)
+    if (prefix[i] != s[i])
+      return 0;
+  return 1;
+}
+
+static int
+block_cipher_p(const struct nettle_cipher *cipher)
+{
+  /* Don't use nettle cbc and ctr for openssl ciphers. */
+  return cipher->block_size > 0 && !prefix_p("openssl", cipher->name);
+}
+
 static void
 time_cipher(const struct nettle_cipher *cipher)
 {
@@ -472,8 +489,7 @@ time_cipher(const struct nettle_cipher *cipher)
            time_function(bench_cipher, &info));
   }
 
-  /* Don't use nettle cbc to benchmark openssl ciphers */
-  if (cipher->block_size && cipher->name[0] != 'o')
+  if (block_cipher_p(cipher))
     {
       uint8_t *iv = xalloc(cipher->block_size);
       
@@ -619,6 +635,7 @@ main(int argc, char **argv)
       &nettle_des3,
       &nettle_serpent256,
       &nettle_twofish128, &nettle_twofish192, &nettle_twofish256,
+      &nettle_salsa20,
       NULL
     };
 
index b789a5720c07fc22dc1def5355f58881e8aa6262..93199a987a24f4c9100310d3b4c834694325f070 100644 (file)
 #include "blowfish.h"
 #include "des.h"
 #include "gcm.h"
+#include "salsa20.h"
 
 /* DES uses a different signature for the key set function. We ignore
-   the return value incicating weak keys. */
+   the return value indicating weak keys. */
 static void
 des_set_key_hack(void *ctx, unsigned length, const uint8_t *key)
 {
@@ -77,6 +78,25 @@ nettle_des3 = {
 const struct nettle_cipher
 nettle_blowfish128 = _NETTLE_CIPHER(blowfish, BLOWFISH, 128);
 
+/* Sets a fix zero iv. For benchmarking only. */
+static void
+salsa20_set_key_hack(void *ctx, unsigned length, const uint8_t *key)
+{
+  static const uint8_t iv[SALSA20_IV_SIZE];
+  salsa20_set_key (ctx, length, key);
+  salsa20_set_iv (ctx, SALSA20_IV_SIZE, iv);
+}
+
+/* Claim zero block size, to classify as a stream cipher. */
+const struct nettle_cipher
+nettle_salsa20 = {
+  "salsa20", sizeof(struct salsa20_ctx),
+  0, SALSA20_KEY_SIZE,
+  salsa20_set_key_hack, salsa20_set_key_hack,
+  (nettle_crypt_func *) salsa20_crypt,
+  (nettle_crypt_func *) salsa20_crypt
+};
+
 const struct nettle_aead
 nettle_gcm_aes128 = _NETTLE_AEAD(gcm, GCM, aes, 128);
 const struct nettle_aead
index efad02a812e0e4cf7607c9cec99dfe9a53fdc49b..888223932008b5a42e58a160552b930ea12571dd 100644 (file)
@@ -59,9 +59,10 @@ extern const struct nettle_cipher nettle_des3;
 
 extern const struct nettle_cipher nettle_blowfish128;
 
-/* Glue to openssl, for comparative benchmarking. The corresponding
- * code is not included in the nettle library, as that would make the
- * shared library depend on openssl. Instead, look at
+/* For benchmarking only, sets no iv and lies about the block size. */
+extern const struct nettle_cipher nettle_salsa20;
+
+/* Glue to openssl, for comparative benchmarking. Code in
  * examples/nettle-openssl.c. */
 extern const struct nettle_cipher nettle_openssl_aes128;
 extern const struct nettle_cipher nettle_openssl_aes192;