[PATCH] ipmi: watchdog parms in sysfs
[safe/jmp/linux-2.6] / crypto / tcrypt.c
index 5a95b4a..53f4ee8 100644 (file)
  * Software Foundation; either version 2 of the License, or (at your option)
  * any later version.
  *
- * 14 - 09 - 2003 
- *     Rewritten by Kartikey Mahendra Bhatt
+ * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
+ * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
+ *
  */
 
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
-#include <asm/scatterlist.h>
+#include <linux/scatterlist.h>
 #include <linux/string.h>
 #include <linux/crypto.h>
 #include <linux/highmem.h>
 #include <linux/moduleparam.h>
+#include <linux/jiffies.h>
+#include <linux/timex.h>
+#include <linux/interrupt.h>
 #include "tcrypt.h"
 
 /*
  * Need to kmalloc() memory for testing kmap().
  */
-#define TVMEMSIZE      4096
+#define TVMEMSIZE      16384
 #define XBUFSIZE       32768
 
 /*
 
 static unsigned int IDX[8] = { IDX1, IDX2, IDX3, IDX4, IDX5, IDX6, IDX7, IDX8 };
 
+/*
+ * Used by test_cipher_speed()
+ */
+static unsigned int sec;
+
 static int mode;
 static char *xbuf;
 static char *tvmem;
@@ -63,7 +72,7 @@ static char *check[] = {
        "des", "md5", "des3_ede", "rot13", "sha1", "sha256", "blowfish",
        "twofish", "serpent", "sha384", "sha512", "md4", "aes", "cast6",
        "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
-       "khazad", "wp512", "wp384", "wp256", "tnepres", NULL
+       "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", NULL
 };
 
 static void hexdump(unsigned char *buf, unsigned int len)
@@ -77,7 +86,6 @@ static void hexdump(unsigned char *buf, unsigned int len)
 static void test_hash(char *algo, struct hash_testvec *template,
                      unsigned int tcount)
 {
-       char *p;
        unsigned int i, j, k, temp;
        struct scatterlist sg[8];
        char result[64];
@@ -107,10 +115,7 @@ static void test_hash(char *algo, struct hash_testvec *template,
                printk("test %u:\n", i + 1);
                memset(result, 0, 64);
 
-               p = hash_tv[i].plaintext;
-               sg[0].page = virt_to_page(p);
-               sg[0].offset = offset_in_page(p);
-               sg[0].length = hash_tv[i].psize;
+               sg_set_buf(&sg[0], hash_tv[i].plaintext, hash_tv[i].psize);
 
                crypto_digest_init(tfm);
                if (tfm->crt_u.digest.dit_setkey) {
@@ -145,10 +150,8 @@ static void test_hash(char *algo, struct hash_testvec *template,
                                       hash_tv[i].plaintext + temp,
                                       hash_tv[i].tap[k]);
                                temp += hash_tv[i].tap[k];
-                               p = &xbuf[IDX[k]];
-                               sg[k].page = virt_to_page(p);
-                               sg[k].offset = offset_in_page(p);
-                               sg[k].length = hash_tv[i].tap[k];
+                               sg_set_buf(&sg[k], &xbuf[IDX[k]],
+                                           hash_tv[i].tap[k]);
                        }
 
                        crypto_digest_digest(tfm, sg, hash_tv[i].np, result);
@@ -170,7 +173,6 @@ static void test_hash(char *algo, struct hash_testvec *template,
 static void test_hmac(char *algo, struct hmac_testvec *template,
                      unsigned int tcount)
 {
-       char *p;
        unsigned int i, j, k, temp;
        struct scatterlist sg[8];
        char result[64];
@@ -201,11 +203,8 @@ static void test_hmac(char *algo, struct hmac_testvec *template,
                printk("test %u:\n", i + 1);
                memset(result, 0, sizeof (result));
 
-               p = hmac_tv[i].plaintext;
                klen = hmac_tv[i].ksize;
-               sg[0].page = virt_to_page(p);
-               sg[0].offset = offset_in_page(p);
-               sg[0].length = hmac_tv[i].psize;
+               sg_set_buf(&sg[0], hmac_tv[i].plaintext, hmac_tv[i].psize);
 
                crypto_hmac(tfm, hmac_tv[i].key, &klen, sg, 1, result);
 
@@ -234,10 +233,8 @@ static void test_hmac(char *algo, struct hmac_testvec *template,
                                       hmac_tv[i].plaintext + temp,
                                       hmac_tv[i].tap[k]);
                                temp += hmac_tv[i].tap[k];
-                               p = &xbuf[IDX[k]];
-                               sg[k].page = virt_to_page(p);
-                               sg[k].offset = offset_in_page(p);
-                               sg[k].length = hmac_tv[i].tap[k];
+                               sg_set_buf(&sg[k], &xbuf[IDX[k]],
+                                           hmac_tv[i].tap[k]);
                        }
 
                        crypto_hmac(tfm, hmac_tv[i].key, &klen, sg,
@@ -261,21 +258,21 @@ static void test_cipher(char *algo, int mode, int enc,
 {
        unsigned int ret, i, j, k, temp;
        unsigned int tsize;
-       char *p, *q;
+       char *q;
        struct crypto_tfm *tfm;
        char *key;
        struct cipher_testvec *cipher_tv;
        struct scatterlist sg[8];
-       char e[11], m[4];
+       const char *e, *m;
 
        if (enc == ENCRYPT)
-               strncpy(e, "encryption", 11);
+               e = "encryption";
        else
-               strncpy(e, "decryption", 11);
+               e = "decryption";
        if (mode == MODE_ECB)
-               strncpy(m, "ECB", 4);
+               m = "ECB";
        else
-               strncpy(m, "CBC", 4);
+               m = "CBC";
 
        printk("\ntesting %s %s %s\n", algo, m, e);
 
@@ -321,10 +318,8 @@ static void test_cipher(char *algo, int mode, int enc,
                                        goto out;
                        }
 
-                       p = cipher_tv[i].input;
-                       sg[0].page = virt_to_page(p);
-                       sg[0].offset = offset_in_page(p);
-                       sg[0].length = cipher_tv[i].ilen;
+                       sg_set_buf(&sg[0], cipher_tv[i].input,
+                                  cipher_tv[i].ilen);
 
                        if (!mode) {
                                crypto_cipher_set_iv(tfm, cipher_tv[i].iv,
@@ -380,10 +375,8 @@ static void test_cipher(char *algo, int mode, int enc,
                                       cipher_tv[i].input + temp,
                                       cipher_tv[i].tap[k]);
                                temp += cipher_tv[i].tap[k];
-                               p = &xbuf[IDX[k]];
-                               sg[k].page = virt_to_page(p);
-                               sg[k].offset = offset_in_page(p);
-                               sg[k].length = cipher_tv[i].tap[k];
+                               sg_set_buf(&sg[k], &xbuf[IDX[k]],
+                                          cipher_tv[i].tap[k]);
                        }
 
                        if (!mode) {
@@ -419,6 +412,164 @@ out:
        crypto_free_tfm(tfm);
 }
 
+static int test_cipher_jiffies(struct crypto_tfm *tfm, int enc, char *p,
+                              int blen, int sec)
+{
+       struct scatterlist sg[1];
+       unsigned long start, end;
+       int bcount;
+       int ret;
+
+       sg_set_buf(sg, p, blen);
+
+       for (start = jiffies, end = start + sec * HZ, bcount = 0;
+            time_before(jiffies, end); bcount++) {
+               if (enc)
+                       ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
+               else
+                       ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
+
+               if (ret)
+                       return ret;
+       }
+
+       printk("%d operations in %d seconds (%ld bytes)\n",
+              bcount, sec, (long)bcount * blen);
+       return 0;
+}
+
+static int test_cipher_cycles(struct crypto_tfm *tfm, int enc, char *p,
+                             int blen)
+{
+       struct scatterlist sg[1];
+       unsigned long cycles = 0;
+       int ret = 0;
+       int i;
+
+       sg_set_buf(sg, p, blen);
+
+       local_bh_disable();
+       local_irq_disable();
+
+       /* Warm-up run. */
+       for (i = 0; i < 4; i++) {
+               if (enc)
+                       ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
+               else
+                       ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
+
+               if (ret)
+                       goto out;
+       }
+
+       /* The real thing. */
+       for (i = 0; i < 8; i++) {
+               cycles_t start, end;
+
+               start = get_cycles();
+               if (enc)
+                       ret = crypto_cipher_encrypt(tfm, sg, sg, blen);
+               else
+                       ret = crypto_cipher_decrypt(tfm, sg, sg, blen);
+               end = get_cycles();
+
+               if (ret)
+                       goto out;
+
+               cycles += end - start;
+       }
+
+out:
+       local_irq_enable();
+       local_bh_enable();
+
+       if (ret == 0)
+               printk("1 operation in %lu cycles (%d bytes)\n",
+                      (cycles + 4) / 8, blen);
+
+       return ret;
+}
+
+static void test_cipher_speed(char *algo, int mode, int enc, unsigned int sec,
+                             struct cipher_testvec *template,
+                             unsigned int tcount, struct cipher_speed *speed)
+{
+       unsigned int ret, i, j, iv_len;
+       unsigned char *key, *p, iv[128];
+       struct crypto_tfm *tfm;
+       const char *e, *m;
+
+       if (enc == ENCRYPT)
+               e = "encryption";
+       else
+               e = "decryption";
+       if (mode == MODE_ECB)
+               m = "ECB";
+       else
+               m = "CBC";
+
+       printk("\ntesting speed of %s %s %s\n", algo, m, e);
+
+       if (mode)
+               tfm = crypto_alloc_tfm(algo, 0);
+       else
+               tfm = crypto_alloc_tfm(algo, CRYPTO_TFM_MODE_CBC);
+
+       if (tfm == NULL) {
+               printk("failed to load transform for %s %s\n", algo, m);
+               return;
+       }
+
+       for (i = 0; speed[i].klen != 0; i++) {
+               if ((speed[i].blen + speed[i].klen) > TVMEMSIZE) {
+                       printk("template (%u) too big for tvmem (%u)\n",
+                              speed[i].blen + speed[i].klen, TVMEMSIZE);
+                       goto out;
+               }
+
+               printk("test %u (%d bit key, %d byte blocks): ", i,
+                      speed[i].klen * 8, speed[i].blen);
+
+               memset(tvmem, 0xff, speed[i].klen + speed[i].blen);
+
+               /* set key, plain text and IV */
+               key = (unsigned char *)tvmem;
+               for (j = 0; j < tcount; j++) {
+                       if (template[j].klen == speed[i].klen) {
+                               key = template[j].key;
+                               break;
+                       }
+               }
+               p = (unsigned char *)tvmem + speed[i].klen;
+
+               ret = crypto_cipher_setkey(tfm, key, speed[i].klen);
+               if (ret) {
+                       printk("setkey() failed flags=%x\n", tfm->crt_flags);
+                       goto out;
+               }
+
+               if (!mode) {
+                       iv_len = crypto_tfm_alg_ivsize(tfm);
+                       memset(&iv, 0xff, iv_len);
+                       crypto_cipher_set_iv(tfm, iv, iv_len);
+               }
+
+               if (sec)
+                       ret = test_cipher_jiffies(tfm, enc, p, speed[i].blen,
+                                                 sec);
+               else
+                       ret = test_cipher_cycles(tfm, enc, p, speed[i].blen);
+
+               if (ret) {
+                       printk("%s() failed flags=%x\n", e, tfm->crt_flags);
+                       break;
+               }
+       }
+
+out:
+       crypto_free_tfm(tfm);
+}
+
 static void test_deflate(void)
 {
        unsigned int i;
@@ -538,9 +689,7 @@ static void test_crc32c(void)
        for (i = 0; i < NUMVEC; i++) {
                for (j = 0; j < VECSIZE; j++)
                        test_vec[i][j] = ++b;
-               sg[i].page = virt_to_page(test_vec[i]);
-               sg[i].offset = offset_in_page(test_vec[i]);
-               sg[i].length = VECSIZE;
+               sg_set_buf(&sg[i], test_vec[i], VECSIZE);
        }
 
        seed = SEEDTESTVAL;
@@ -688,6 +837,10 @@ static void do_test(void)
                test_cipher ("anubis", MODE_CBC, ENCRYPT, anubis_cbc_enc_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
                test_cipher ("anubis", MODE_CBC, DECRYPT, anubis_cbc_dec_tv_template, ANUBIS_CBC_ENC_TEST_VECTORS);
 
+               //XETA
+               test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
+               test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
+
                test_hash("sha384", sha384_tv_template, SHA384_TEST_VECTORS);
                test_hash("sha512", sha512_tv_template, SHA512_TEST_VECTORS);
                test_hash("wp512", wp512_tv_template, WP512_TEST_VECTORS);
@@ -845,6 +998,11 @@ static void do_test(void)
        case 29:
                test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
                break;
+               
+       case 30:
+               test_cipher ("xeta", MODE_ECB, ENCRYPT, xeta_enc_tv_template, XETA_ENC_TEST_VECTORS);
+               test_cipher ("xeta", MODE_ECB, DECRYPT, xeta_dec_tv_template, XETA_DEC_TEST_VECTORS);
+               break;
 
 #ifdef CONFIG_CRYPTO_HMAC
        case 100:
@@ -861,6 +1019,69 @@ static void do_test(void)
 
 #endif
 
+       case 200:
+               test_cipher_speed("aes", MODE_ECB, ENCRYPT, sec, NULL, 0,
+                                 aes_speed_template);
+               test_cipher_speed("aes", MODE_ECB, DECRYPT, sec, NULL, 0,
+                                 aes_speed_template);
+               test_cipher_speed("aes", MODE_CBC, ENCRYPT, sec, NULL, 0,
+                                 aes_speed_template);
+               test_cipher_speed("aes", MODE_CBC, DECRYPT, sec, NULL, 0,
+                                 aes_speed_template);
+               break;
+
+       case 201:
+               test_cipher_speed("des3_ede", MODE_ECB, ENCRYPT, sec,
+                                 des3_ede_enc_tv_template,
+                                 DES3_EDE_ENC_TEST_VECTORS,
+                                 des3_ede_speed_template);
+               test_cipher_speed("des3_ede", MODE_ECB, DECRYPT, sec,
+                                 des3_ede_dec_tv_template,
+                                 DES3_EDE_DEC_TEST_VECTORS,
+                                 des3_ede_speed_template);
+               test_cipher_speed("des3_ede", MODE_CBC, ENCRYPT, sec,
+                                 des3_ede_enc_tv_template,
+                                 DES3_EDE_ENC_TEST_VECTORS,
+                                 des3_ede_speed_template);
+               test_cipher_speed("des3_ede", MODE_CBC, DECRYPT, sec,
+                                 des3_ede_dec_tv_template,
+                                 DES3_EDE_DEC_TEST_VECTORS,
+                                 des3_ede_speed_template);
+               break;
+
+       case 202:
+               test_cipher_speed("twofish", MODE_ECB, ENCRYPT, sec, NULL, 0,
+                                 twofish_speed_template);
+               test_cipher_speed("twofish", MODE_ECB, DECRYPT, sec, NULL, 0,
+                                 twofish_speed_template);
+               test_cipher_speed("twofish", MODE_CBC, ENCRYPT, sec, NULL, 0,
+                                 twofish_speed_template);
+               test_cipher_speed("twofish", MODE_CBC, DECRYPT, sec, NULL, 0,
+                                 twofish_speed_template);
+               break;
+
+       case 203:
+               test_cipher_speed("blowfish", MODE_ECB, ENCRYPT, sec, NULL, 0,
+                                 blowfish_speed_template);
+               test_cipher_speed("blowfish", MODE_ECB, DECRYPT, sec, NULL, 0,
+                                 blowfish_speed_template);
+               test_cipher_speed("blowfish", MODE_CBC, ENCRYPT, sec, NULL, 0,
+                                 blowfish_speed_template);
+               test_cipher_speed("blowfish", MODE_CBC, DECRYPT, sec, NULL, 0,
+                                 blowfish_speed_template);
+               break;
+
+       case 204:
+               test_cipher_speed("des", MODE_ECB, ENCRYPT, sec, NULL, 0,
+                                 des_speed_template);
+               test_cipher_speed("des", MODE_ECB, DECRYPT, sec, NULL, 0,
+                                 des_speed_template);
+               test_cipher_speed("des", MODE_CBC, ENCRYPT, sec, NULL, 0,
+                                 des_speed_template);
+               test_cipher_speed("des", MODE_CBC, DECRYPT, sec, NULL, 0,
+                                 des_speed_template);
+               break;
+
        case 1000:
                test_available();
                break;
@@ -901,6 +1122,9 @@ module_init(init);
 module_exit(fini);
 
 module_param(mode, int, 0);
+module_param(sec, uint, 0);
+MODULE_PARM_DESC(sec, "Length in seconds of speed tests "
+                     "(defaults to zero which uses CPU cycles instead)");
 
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("Quick & dirty crypto testing module");