4 Code Example For Symmetric Key Cipher Operation
5 -----------------------------------------------
10 /* tie all data structures together */
12 struct scatterlist sg;
13 struct crypto_skcipher *tfm;
14 struct skcipher_request *req;
15 struct crypto_wait wait;
18 /* Perform cipher operation */
19 static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
25 rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait);
27 rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait);
30 pr_info("skcipher encrypt returned with result %d\n", rc);
35 /* Initialize and trigger cipher operation */
36 static int test_skcipher(void)
38 struct skcipher_def sk;
39 struct crypto_skcipher *skcipher = NULL;
40 struct skcipher_request *req = NULL;
41 char *scratchpad = NULL;
43 unsigned char key[32];
46 skcipher = crypto_alloc_skcipher("cbc-aes-aesni", 0, 0);
47 if (IS_ERR(skcipher)) {
48 pr_info("could not allocate skcipher handle\n");
49 return PTR_ERR(skcipher);
52 req = skcipher_request_alloc(skcipher, GFP_KERNEL);
54 pr_info("could not allocate skcipher request\n");
59 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
63 /* AES 256 with random key */
64 get_random_bytes(&key, 32);
65 if (crypto_skcipher_setkey(skcipher, key, 32)) {
66 pr_info("key could not be set\n");
71 /* IV will be random */
72 ivdata = kmalloc(16, GFP_KERNEL);
74 pr_info("could not allocate ivdata\n");
77 get_random_bytes(ivdata, 16);
79 /* Input data will be random */
80 scratchpad = kmalloc(16, GFP_KERNEL);
82 pr_info("could not allocate scratchpad\n");
85 get_random_bytes(scratchpad, 16);
90 /* We encrypt one block */
91 sg_init_one(&sk.sg, scratchpad, 16);
92 skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata);
93 crypto_init_wait(&sk.wait);
96 ret = test_skcipher_encdec(&sk, 1);
100 pr_info("Encryption triggered successfully\n");
104 crypto_free_skcipher(skcipher);
106 skcipher_request_free(req);
115 Code Example For Use of Operational State Memory With SHASH
116 -----------------------------------------------------------
122 struct shash_desc shash;
126 static struct sdesc *init_sdesc(struct crypto_shash *alg)
131 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
132 sdesc = kmalloc(size, GFP_KERNEL);
134 return ERR_PTR(-ENOMEM);
135 sdesc->shash.tfm = alg;
139 static int calc_hash(struct crypto_shash *alg,
140 const unsigned char *data, unsigned int datalen,
141 unsigned char *digest)
146 sdesc = init_sdesc(alg);
148 pr_info("can't alloc sdesc\n");
149 return PTR_ERR(sdesc);
152 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
157 static int test_hash(const unsigned char *data, unsigned int datalen,
158 unsigned char *digest)
160 struct crypto_shash *alg;
161 char *hash_alg_name = "sha1-padlock-nano";
164 alg = crypto_alloc_shash(hash_alg_name, 0, 0);
166 pr_info("can't alloc alg %s\n", hash_alg_name);
169 ret = calc_hash(alg, data, datalen, digest);
170 crypto_free_shash(alg);
175 Code Example For Random Number Generator Usage
176 ----------------------------------------------
181 static int get_random_numbers(u8 *buf, unsigned int len)
183 struct crypto_rng *rng = NULL;
184 char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
188 pr_debug("No output buffer provided\n");
192 rng = crypto_alloc_rng(drbg, 0, 0);
194 pr_debug("could not allocate RNG handle for %s\n", drbg);
198 ret = crypto_rng_get_bytes(rng, buf, len);
200 pr_debug("generation of random numbers failed\n");
202 pr_debug("RNG returned no data");
204 pr_debug("RNG returned %d bytes of data\n", ret);
207 crypto_free_rng(rng);