+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_digest_jiffies(struct crypto_tfm *tfm, char *p, int blen,
+ int plen, char *out, int sec)
+{
+ struct scatterlist sg[1];
+ unsigned long start, end;
+ int bcount, pcount;
+
+ for (start = jiffies, end = start + sec * HZ, bcount = 0;
+ time_before(jiffies, end); bcount++) {
+ crypto_digest_init(tfm);
+ for (pcount = 0; pcount < blen; pcount += plen) {
+ sg_set_buf(sg, p + pcount, plen);
+ crypto_digest_update(tfm, sg, 1);
+ }
+ /* we assume there is enough space in 'out' for the result */
+ crypto_digest_final(tfm, out);
+ }
+
+ printk("%6u opers/sec, %9lu bytes/sec\n",
+ bcount / sec, ((long)bcount * blen) / sec);
+
+ return;
+}
+
+static void test_digest_cycles(struct crypto_tfm *tfm, char *p, int blen,
+ int plen, char *out)
+{
+ struct scatterlist sg[1];
+ unsigned long cycles = 0;
+ int i, pcount;
+
+ local_bh_disable();
+ local_irq_disable();
+
+ /* Warm-up run. */
+ for (i = 0; i < 4; i++) {
+ crypto_digest_init(tfm);
+ for (pcount = 0; pcount < blen; pcount += plen) {
+ sg_set_buf(sg, p + pcount, plen);
+ crypto_digest_update(tfm, sg, 1);
+ }
+ crypto_digest_final(tfm, out);
+ }
+
+ /* The real thing. */
+ for (i = 0; i < 8; i++) {
+ cycles_t start, end;
+
+ crypto_digest_init(tfm);
+
+ start = get_cycles();
+
+ for (pcount = 0; pcount < blen; pcount += plen) {
+ sg_set_buf(sg, p + pcount, plen);
+ crypto_digest_update(tfm, sg, 1);
+ }
+ crypto_digest_final(tfm, out);
+
+ end = get_cycles();
+
+ cycles += end - start;
+ }
+
+ local_irq_enable();
+ local_bh_enable();
+
+ printk("%6lu cycles/operation, %4lu cycles/byte\n",
+ cycles / 8, cycles / (8 * blen));
+
+ return;
+}
+
+static void test_digest_speed(char *algo, unsigned int sec,
+ struct digest_speed *speed)
+{
+ struct crypto_tfm *tfm;
+ char output[1024];
+ int i;
+
+ printk("\ntesting speed of %s\n", algo);
+
+ tfm = crypto_alloc_tfm(algo, 0);
+
+ if (tfm == NULL) {
+ printk("failed to load transform for %s\n", algo);
+ return;
+ }
+
+ if (crypto_tfm_alg_digestsize(tfm) > sizeof(output)) {
+ printk("digestsize(%u) > outputbuffer(%zu)\n",
+ crypto_tfm_alg_digestsize(tfm), sizeof(output));
+ goto out;
+ }
+
+ for (i = 0; speed[i].blen != 0; i++) {
+ if (speed[i].blen > TVMEMSIZE) {
+ printk("template (%u) too big for tvmem (%u)\n",
+ speed[i].blen, TVMEMSIZE);
+ goto out;
+ }
+
+ printk("test%3u (%5u byte blocks,%5u bytes per update,%4u updates): ",
+ i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen);
+
+ memset(tvmem, 0xff, speed[i].blen);
+
+ if (sec)
+ test_digest_jiffies(tfm, tvmem, speed[i].blen, speed[i].plen, output, sec);
+ else
+ test_digest_cycles(tfm, tvmem, speed[i].blen, speed[i].plen, output);
+ }
+
+out:
+ crypto_free_tfm(tfm);
+}
+
+static void test_deflate(void)