cregit-Linux how code gets into the kernel

Release 4.11 crypto/tcrypt.c

Directory: crypto
/*
 * Quick & dirty crypto testing module.
 *
 * This will only exist until we have a better testing mechanism
 * (e.g. a char device).
 *
 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
 * Copyright (c) 2002 Jean-Francois Dive <jef@linuxbe.org>
 * Copyright (c) 2007 Nokia Siemens Networks
 *
 * Updated RFC4106 AES-GCM testing.
 *    Authors: Aidan O'Mahony (aidan.o.mahony@intel.com)
 *             Adrian Hoban <adrian.hoban@intel.com>
 *             Gabriele Paoloni <gabriele.paoloni@intel.com>
 *             Tadeusz Struk (tadeusz.struk@intel.com)
 *             Copyright (c) 2010, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 */


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <crypto/aead.h>
#include <crypto/hash.h>
#include <crypto/skcipher.h>
#include <linux/err.h>
#include <linux/fips.h>
#include <linux/init.h>
#include <linux/gfp.h>
#include <linux/module.h>
#include <linux/scatterlist.h>
#include <linux/string.h>
#include <linux/moduleparam.h>
#include <linux/jiffies.h>
#include <linux/timex.h>
#include <linux/interrupt.h>
#include "tcrypt.h"

/*
 * Need slab memory for testing (size in number of pages).
 */

#define TVMEMSIZE	4

/*
* Used by test_cipher_speed()
*/

#define ENCRYPT 1

#define DECRYPT 0


#define MAX_DIGEST_SIZE		64

/*
 * return a string with the driver name
 */

#define get_driver_name(tfm_type, tfm) crypto_tfm_alg_driver_name(tfm_type ## _tfm(tfm))

/*
 * Used by test_cipher_speed()
 */

static unsigned int sec;


static char *alg = NULL;

static u32 type;

static u32 mask;

static int mode;

static char *tvmem[TVMEMSIZE];


static char *check[] = {
	"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
	"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
	"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
	"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta",  "fcrypt",
	"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
	"lzo", "cts", "zlib", "sha3-224", "sha3-256", "sha3-384", "sha3-512",
	NULL
};


struct tcrypt_result {
	
struct completion completion;
	
int err;
};


static void tcrypt_complete(struct crypto_async_request *req, int err) { struct tcrypt_result *res = req->data; if (err == -EINPROGRESS) return; res->err = err; complete(&res->completion); }

Contributors

PersonTokensPropCommitsCommitProp
Lokesh Vutla45100.00%1100.00%
Total45100.00%1100.00%


static inline int do_one_aead_op(struct aead_request *req, int ret) { if (ret == -EINPROGRESS || ret == -EBUSY) { struct tcrypt_result *tr = req->base.data; ret = wait_for_completion_interruptible(&tr->completion); if (!ret) ret = tr->err; reinit_completion(&tr->completion); } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu6995.83%266.67%
James Morris34.17%133.33%
Total72100.00%3100.00%


static int test_aead_jiffies(struct aead_request *req, int enc, int blen, int secs) { unsigned long start, end; int bcount; int ret; for (start = jiffies, end = start + secs * HZ, bcount = 0; time_before(jiffies, end); bcount++) { if (enc) ret = do_one_aead_op(req, crypto_aead_encrypt(req)); else ret = do_one_aead_op(req, crypto_aead_decrypt(req)); if (ret) return ret; } printk("%d operations in %d seconds (%ld bytes)\n", bcount, secs, (long)bcount * blen); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu10388.03%342.86%
James Morris86.84%228.57%
Mark D Rustad32.56%114.29%
Cheng Renquan32.56%114.29%
Total117100.00%7100.00%


static int test_aead_cycles(struct aead_request *req, int enc, int blen) { unsigned long cycles = 0; int ret = 0; int i; local_irq_disable(); /* Warm-up run. */ for (i = 0; i < 4; i++) { if (enc) ret = do_one_aead_op(req, crypto_aead_encrypt(req)); else ret = do_one_aead_op(req, crypto_aead_decrypt(req)); if (ret) goto out; } /* The real thing. */ for (i = 0; i < 8; i++) { cycles_t start, end; start = get_cycles(); if (enc) ret = do_one_aead_op(req, crypto_aead_encrypt(req)); else ret = do_one_aead_op(req, crypto_aead_decrypt(req)); end = get_cycles(); if (ret) goto out; cycles += end - start; } out: local_irq_enable(); if (ret == 0) printk("1 operation in %lu cycles (%d bytes)\n", (cycles + 4) / 8, blen); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu14677.66%545.45%
James Morris2010.64%218.18%
Loc Ho105.32%19.09%
Sebastian Andrzej Siewior84.26%19.09%
Kartikey Mahendra Bhatt31.60%19.09%
David Härdeman10.53%19.09%
Total188100.00%11100.00%

static u32 block_sizes[] = { 16, 64, 256, 1024, 8192, 0 }; static u32 aead_sizes[] = { 16, 64, 256, 512, 1024, 2048, 4096, 8192, 0 }; #define XBUFSIZE 8 #define MAX_IVLEN 32
static int testmgr_alloc_buf(char *buf[XBUFSIZE]) { int i; for (i = 0; i < XBUFSIZE; i++) { buf[i] = (void *)__get_free_page(GFP_KERNEL); if (!buf[i]) goto err_free_buf; } return 0; err_free_buf: while (i-- > 0) free_page((unsigned long)buf[i]); return -ENOMEM; }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu4958.33%133.33%
Tim Chen2833.33%133.33%
Lokesh Vutla78.33%133.33%
Total84100.00%3100.00%


static void testmgr_free_buf(char *buf[XBUFSIZE]) { int i; for (i = 0; i < XBUFSIZE; i++) free_page((unsigned long)buf[i]); }

Contributors

PersonTokensPropCommitsCommitProp
Tim Chen2356.10%133.33%
Herbert Xu1741.46%133.33%
Lokesh Vutla12.44%133.33%
Total41100.00%3100.00%


static void sg_init_aead(struct scatterlist *sg, char *xbuf[XBUFSIZE], unsigned int buflen) { int np = (buflen + PAGE_SIZE - 1)/PAGE_SIZE; int k, rem; if (np > XBUFSIZE) { rem = PAGE_SIZE; np = XBUFSIZE; } else { rem = buflen % PAGE_SIZE; } sg_init_table(sg, np + 1); np--; for (k = 0; k < np; k++) sg_set_buf(&sg[k + 1], xbuf[k], PAGE_SIZE); sg_set_buf(&sg[k + 1], xbuf[k], rem); }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu9071.43%125.00%
Tim Chen3225.40%125.00%
Cristian Stoica32.38%125.00%
Christian Engelmayer10.79%125.00%
Total126100.00%4100.00%


static void test_aead_speed(const char *algo, int enc, unsigned int secs, struct aead_speed_template *template, unsigned int tcount, u8 authsize, unsigned int aad_size, u8 *keysize) { unsigned int i, j; struct crypto_aead *tfm; int ret = -ENOMEM; const char *key; struct aead_request *req; struct scatterlist *sg; struct scatterlist *sgout; const char *e; void *assoc; char *iv; char *xbuf[XBUFSIZE]; char *xoutbuf[XBUFSIZE]; char *axbuf[XBUFSIZE]; unsigned int *b_size; unsigned int iv_len; struct tcrypt_result result; iv = kzalloc(MAX_IVLEN, GFP_KERNEL); if (!iv) return; if (aad_size >= PAGE_SIZE) { pr_err("associate data length (%u) too big\n", aad_size); goto out_noxbuf; } if (enc == ENCRYPT) e = "encryption"; else e = "decryption"; if (testmgr_alloc_buf(xbuf)) goto out_noxbuf; if (testmgr_alloc_buf(axbuf)) goto out_noaxbuf; if (testmgr_alloc_buf(xoutbuf)) goto out_nooutbuf; sg = kmalloc(sizeof(*sg) * 9 * 2, GFP_KERNEL); if (!sg) goto out_nosg; sgout = &sg[9]; tfm = crypto_alloc_aead(algo, 0, 0); if (IS_ERR(tfm)) { pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo, PTR_ERR(tfm)); goto out_notfm; } init_completion(&result.completion); printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo, get_driver_name(crypto_aead, tfm), e); req = aead_request_alloc(tfm, GFP_KERNEL); if (!req) { pr_err("alg: aead: Failed to allocate request for %s\n", algo); goto out_noreq; } aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, tcrypt_complete, &result); i = 0; do { b_size = aead_sizes; do { assoc = axbuf[0]; memset(assoc, 0xff, aad_size); if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) { pr_err("template (%u) too big for tvmem (%lu)\n", *keysize + *b_size, TVMEMSIZE * PAGE_SIZE); goto out; } key = tvmem[0]; for (j = 0; j < tcount; j++) { if (template[j].klen == *keysize) { key = template[j].key; break; } } ret = crypto_aead_setkey(tfm, key, *keysize); ret = crypto_aead_setauthsize(tfm, authsize); iv_len = crypto_aead_ivsize(tfm); if (iv_len) memset(iv, 0xff, iv_len); crypto_aead_clear_flags(tfm, ~0); printk(KERN_INFO "test %u (%d bit key, %d byte blocks): ", i, *keysize * 8, *b_size); memset(tvmem[0], 0xff, PAGE_SIZE); if (ret) { pr_err("setkey() failed flags=%x\n", crypto_aead_get_flags(tfm)); goto out; } sg_init_aead(sg, xbuf, *b_size + (enc ? authsize : 0)); sg_init_aead(sgout, xoutbuf, *b_size + (enc ? authsize : 0)); sg_set_buf(&sg[0], assoc, aad_size); sg_set_buf(&sgout[0], assoc, aad_size); aead_request_set_crypt(req, sg, sgout, *b_size, iv); aead_request_set_ad(req, aad_size); if (secs) ret = test_aead_jiffies(req, enc, *b_size, secs); else ret = test_aead_cycles(req, enc, *b_size); if (ret) { pr_err("%s() failed return code=%d\n", e, ret); break; } b_size++; i++; } while (*b_size); keysize++; } while (*keysize); out: aead_request_free(req); out_noreq: crypto_free_aead(tfm); out_notfm: kfree(sg); out_nosg: testmgr_free_buf(xoutbuf); out_nooutbuf: testmgr_free_buf(axbuf); out_noaxbuf: testmgr_free_buf(xbuf); out_noxbuf: kfree(iv); return; }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu61683.47%433.33%
Mikko Herranen587.86%18.33%
Loc Ho172.30%18.33%
Luca Clementi162.17%18.33%
James Morris152.03%18.33%
Kartikey Mahendra Bhatt91.22%18.33%
Sebastian Andrzej Siewior30.41%18.33%
Mark D Rustad30.41%18.33%
David Sterba10.14%18.33%
Total738100.00%12100.00%


static void test_hash_sg_init(struct scatterlist *sg) { int i; sg_init_table(sg, TVMEMSIZE); for (i = 0; i < TVMEMSIZE; i++) { sg_set_buf(sg + i, tvmem[i], PAGE_SIZE); memset(tvmem[i], 0xff, PAGE_SIZE); } }

Contributors

PersonTokensPropCommitsCommitProp
Herbert Xu4369.35%654.55%
Mikko Herranen812.90%19.09%
David S. Miller711.29%19.09%
Joy Latten23.23%19.09%
Kartikey Mahendra Bhatt11.61%19.09%
James Morris11.61%19.09%
Total62100.00%11100.00%


static inline int do_one_ahash_op(struct ahash_request *req, int ret) { if (ret == -EINPROGRESS || ret == -EBUSY) { struct tcrypt_result *tr = req->base.data; wait_for_completion(&tr->completion); reinit_completion(&tr->completion); ret = tr->err; } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller3960.00%114.29%
Herbert Xu913.85%114.29%
Rabin Vincent710.77%114.29%
Jarod Wilson69.23%114.29%
Wolfram Sang23.08%114.29%
Kazunori Miyazawa11.54%114.29%
Andrew Donofrio11.54%114.29%
Total65100.00%7100.00%

struct test_mb_ahash_data { struct scatterlist sg[TVMEMSIZE]; char result[64]; struct ahash_request *req; struct tcrypt_result tresult; char *xbuf[XBUFSIZE]; };
static void test_mb_ahash_speed(const char *algo, unsigned int sec, struct hash_speed *speed) { struct test_mb_ahash_data *data; struct crypto_ahash *tfm; unsigned long start, end; unsigned long cycles; unsigned int i, j, k; int ret; data = kzalloc(sizeof(*data) * 8, GFP_KERNEL); if (!data) return; tfm = crypto_alloc_ahash(algo, 0, 0); if (IS_ERR(tfm)) { pr_err("failed to load transform for %s: %ld\n", algo, PTR_ERR(tfm)); goto free_data; } for (i = 0; i < 8; ++i) { if (testmgr_alloc_buf(data[i].xbuf)) goto out; init_completion(&data[i].tresult.completion); data[i].req = ahash_request_alloc(tfm, GFP_KERNEL); if (!data[i].req) { pr_err("alg: hash: Failed to allocate request for %s\n", algo); goto out; } ahash_request_set_callback(data[i].req, 0, tcrypt_complete, &data[i].tresult); test_hash_sg_init(data[i].sg); } pr_info("\ntesting speed of multibuffer %s (%s)\n", algo, get_driver_name(crypto_ahash, tfm)); for (i = 0; speed[i].blen != 0; i++) { /* For some reason this only tests digests. */ if (speed[i].blen != speed[i].plen) continue; if (speed[i].blen > TVMEMSIZE * PAGE_SIZE) { pr_err("template (%u) too big for tvmem (%lu)\n", speed[i].blen, TVMEMSIZE * PAGE_SIZE); goto out; } if (speed[i].klen) crypto_ahash_setkey(tfm, tvmem[0], speed[i].klen); for (k = 0; k < 8; k++) ahash_request_set_crypt(data[k].req, data[k].sg, data[k].result, speed[i].blen); pr_info("test%3u " "(%5u byte blocks,%5u bytes per update,%4u updates): ", i, speed[i].blen, speed[i].plen, speed[i].blen / speed[i].plen); start = get_cycles(); for (k = 0; k < 8; k++) { ret = crypto_ahash_digest(data[k].req); if (ret == -EINPROGRESS) { ret = 0; continue; } if (ret) break; complete(&data[k].tresult.completion); data[k].tresult.err = 0; } for (j = 0; j < k; j++) { struct tcrypt_result *tr = &data[j].tresult; wait_for_completion(&tr->completion); if (tr->err) ret = tr->err; } end = get_cycles(); cycles = end - start; pr_cont("%6lu cycles/operation, %4lu cycles/byte\n", cycles, cycles / (8 * speed[i].blen)); if (ret) { pr_err("At least one hashing failed ret=%d\n", ret); break; } } out: for (k = 0; k < 8; ++k) ahash_request_free(data[k].req); for (k = 0; k < 8; ++k) testmgr_free_buf(data[k].xbuf); crypto_free_ahash(tfm); free_data: kfree(data); }

Contributors

PersonTokensPropCommitsCommitProp
Megha Dey43668.34%120.00%
Herbert Xu19931.19%360.00%
Krzysztof Kozlowski30.47%120.00%
Total638100.00%5100.00%


static int test_ahash_jiffies_digest(struct ahash_request *req, int blen, char *out, int secs) { unsigned long start, end; int bcount; int ret; for (start = jiffies, end = start + secs * HZ, bcount = 0; time_before(jiffies, end); bcount++) { ret = do_one_ahash_op(req, crypto_ahash_digest(req)); if (ret) return ret; } printk("%6u opers/sec, %9lu bytes/sec\n", bcount / secs, ((long)bcount * blen) / secs); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller8681.90%125.00%
Herbert Xu1110.48%125.00%
Mark D Rustad43.81%125.00%
Jarod Wilson43.81%125.00%
Total105100.00%4100.00%


static int test_ahash_jiffies(struct ahash_request *req, int blen, int plen, char *out, int secs) { unsigned long start, end; int bcount, pcount; int ret; if (plen == blen) return test_ahash_jiffies_digest(req, blen, out, secs); for (start = jiffies, end = start + secs * HZ, bcount = 0; time_before(jiffies, end); bcount++) { ret = do_one_ahash_op(req, crypto_ahash_init(req)); if (ret) return ret; for (pcount = 0; pcount < blen; pcount += plen) { ret = do_one_ahash_op(req, crypto_ahash_update(req)); if (ret) return ret; } /* we assume there is enough space in 'out' for the result */ ret = do_one_ahash_op(req, crypto_ahash_final(req)); if (ret) return ret; } pr_cont("%6u opers/sec, %9lu bytes/sec\n", bcount / secs, ((long)bcount * blen) / secs); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller17394.54%133.33%
Herbert Xu52.73%133.33%
Mark D Rustad52.73%133.33%
Total183100.00%3100.00%


static int test_ahash_cycles_digest(struct ahash_request *req, int blen, char *out) { unsigned long cycles = 0; int ret, i; /* Warm-up run. */ for (i = 0; i < 4; i++) { ret = do_one_ahash_op(req, crypto_ahash_digest(req)); if (ret) goto out; } /* The real thing. */ for (i = 0; i < 8; i++) { cycles_t start, end; start = get_cycles(); ret = do_one_ahash_op(req, crypto_ahash_digest(req)); if (ret) goto out; end = get_cycles(); cycles += end - start; } out: if (ret) return ret; pr_cont("%6lu cycles/operation, %4lu cycles/byte\n", cycles / 8, cycles / (8 * blen)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
David S. Miller149100.00%1100.00%
Total149100.00%1100.00%


static int test_ahash_cycles(struct ahash_request *req, int blen, int plen, char *out) { unsigned long cycles = 0; int i, pcount, ret; if (plen == blen) return test_ahash_cycles_digest(req, blen, out); /* Warm-up run. */ for (i = 0; i < 4; i++) { ret = do_one_ahash_op(req, crypto_ahash_init(req)); if (ret) goto out; for (pcount = 0; pcount < blen; pcount += plen) { ret = do_one_ahash_op