Skip to content
Snippets Groups Projects
Select Git revision
  • 640cf2b78f279191bfa098f1cc57cdfdc0fa34b3
  • master default
  • chacha-poly1305-test
  • rsa-crt-hardening
  • chacha96
  • fat-library
  • versioned-symbols
  • curve25519
  • dsa-reorg
  • aead-api
  • set_key-changes
  • poly1305
  • aes-reorg
  • nettle-2.7-fixes
  • size_t-changes
  • ecc-support
  • experimental-20050201
  • lsh-1.4.2
  • nettle_3.1.1_release_20150424
  • nettle_3.1_release_20150407
  • nettle_3.1rc3
  • nettle_3.1rc2
  • nettle_3.1rc1
  • nettle_3.0_release_20140607
  • nettle_2.7.1_release_20130528
  • nettle_2.7_release_20130424
  • nettle_2.6_release_20130116
  • nettle_2.5_release_20120707
  • converted-master-branch-to-git
  • nettle_2.4_release_20110903
  • nettle_2.3_release_20110902
  • nettle_2.2_release_20110711
  • nettle_2.1_release_20100725
  • camellia_32bit_20100720
  • nettle_2.0_release_20090608
  • nettle_1.15_release_20061128
  • after_experimental_merge_20060516
  • head_before_experimental_merge_20060516
38 results

nettle-benchmark.c

Blame
  • Forked from Nettle / nettle
    4551 commits behind the upstream repository.
    Niels Möller's avatar
    Niels Möller authored
    Rev: src/nettle/examples/.c-style:1.1
    Rev: src/nettle/examples/Makefile.am:1.1
    Rev: src/nettle/examples/nettle-benchmark.c:1.1
    640cf2b7
    History
    nettle-benchmark.c 3.93 KiB
    /* nettle-benchmark.c
     *
     * Tries the performance of the various algorithms.
     *
     */
     
    /* nettle, low-level cryptographics library
     *
     * Copyright (C) 2001 Niels Mller
     *  
     * 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., 59 Temple Place - Suite 330, Boston,
     * MA 02111-1307, USA.
     */
    
    #include "aes.h"
    #include "cbc.h"
    
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <time.h>
    
    /* Encrypt 1MB, 1K at a time. */
    #define BENCH_SIZE 1024
    
    typedef void (*crypt_func)(void *ctx,
    			   unsigned length, uint8_t *dst,
    			   const uint8_t *src);
    static double
    time_function(void (*f)(void *arg), void *arg)
    {
      clock_t before;
      clock_t after;
    
      before = clock();
      
      f(arg);
      
      after = clock();
    
      return ((double)(after - before)) / CLOCKS_PER_SEC;
    }
    
    struct bench_cipher_info
    {
      void *ctx;
      crypt_func crypt;
      uint8_t *data;
    };
    
    static void
    bench_cipher(void *arg)
    {
      struct bench_cipher_info *info = arg;
      unsigned i;
      
      for (i = 0; i<BENCH_SIZE; i++)
        info->crypt(info->ctx, BENCH_SIZE, info->data, info->data);
    }
    
    struct bench_cbc_info
    {
      void *ctx;
      crypt_func crypt;
    
      uint8_t *data;
    
      unsigned block_size;
      uint8_t *iv;
    };
    
    static void
    bench_cbc_encrypt(void *arg)
    {
      struct bench_cbc_info *info = arg;
      unsigned i;
    
      for (i = 0; i<BENCH_SIZE; i++)
        cbc_encrypt(info->ctx, info->crypt,
    		info->block_size, info->iv,
    		BENCH_SIZE, info->data, info->data);
    }
    
    static void
    bench_cbc_decrypt(void *arg)
    {
      struct bench_cbc_info *info = arg;
      unsigned i;
    
      for (i = 0; i<BENCH_SIZE; i++)
        cbc_decrypt(info->ctx, info->crypt,
    		info->block_size, info->iv,
    		BENCH_SIZE, info->data, info->data);
    }
    
    /* Set data[i] = floor(sqrt(i)) */
    static void
    init_data(uint8_t *data)
    {
      unsigned i,j;
      for (i = j = 0; i<BENCH_SIZE;  i++)
        {
          if (j*j < i)
    	j++;
          data[i] = j;
        }
    }
    
    static void
    bench(const char *name, void (*f)(void *), void *arg)
    {
      printf("%15s: %f\n", name, time_function(f, arg));
    }
    
    int
    main(int argc, char **argv)
    {
      /* Time block ciphers */
      uint8_t iv[AES_BLOCK_SIZE];
      uint8_t key[AES_MAX_KEY_SIZE];
      
      uint8_t data[BENCH_SIZE];
    
      {
        struct aes_ctx ctx;
        struct bench_cipher_info info
          = { &ctx, (crypt_func) aes_encrypt, data };
        
        memset(key, 0, sizeof(key));
    
        aes_set_key(&ctx, sizeof(key), key);
        init_data(data);
    
        bench("AES (ECB encrypt)", bench_cipher, &info);
      }
    
      {
        struct aes_ctx ctx;
        struct bench_cipher_info info
          = { &ctx, (crypt_func) aes_decrypt, data };
        
        memset(key, 0, sizeof(key));
    
        aes_set_key(&ctx, sizeof(key), key);
        init_data(data);
    
        bench("AES (ECB decrypt)", bench_cipher, &info);
      }
    
      {
        struct aes_ctx ctx;
        struct bench_cbc_info info
          = { &ctx, (crypt_func) aes_encrypt, data, AES_BLOCK_SIZE, iv };
        
        memset(key, 0, sizeof(key));
        memset(iv, 0, sizeof(iv));
        
        aes_set_key(&ctx, sizeof(key), key);
        init_data(data);
    
        bench("AES (CBC encrypt)", bench_cbc_encrypt, &info);
      }
    
      {
        struct aes_ctx ctx;
        struct bench_cbc_info info
          = { &ctx, (crypt_func) aes_decrypt, data, AES_BLOCK_SIZE, iv };
        
        memset(key, 0, sizeof(key));
        memset(iv, 0, sizeof(iv));
    
        aes_set_key(&ctx, sizeof(key), key);
        init_data(data);
    
        bench("AES (CBC decrypt)", bench_cbc_decrypt, &info);
      }
    
      return 0;
    }