4 Code Example For Symmetric Key Cipher Operation
5 -----------------------------------------------
10 struct tcrypt_result {
11 struct completion completion;
15 /* tie all data structures together */
17 struct scatterlist sg;
18 struct crypto_skcipher *tfm;
19 struct skcipher_request *req;
20 struct tcrypt_result result;
23 /* Callback function */
24 static void test_skcipher_cb(struct crypto_async_request *req, int error)
26 struct tcrypt_result *result = req->data;
28 if (error == -EINPROGRESS)
31 complete(&result->completion);
32 pr_info("Encryption finished successfully\n");
35 /* Perform cipher operation */
36 static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
42 rc = crypto_skcipher_encrypt(sk->req);
44 rc = crypto_skcipher_decrypt(sk->req);
51 rc = wait_for_completion_interruptible(
52 &sk->result.completion);
53 if (!rc && !sk->result.err) {
54 reinit_completion(&sk->result.completion);
58 pr_info("skcipher encrypt returned with %d result %d\n",
62 init_completion(&sk->result.completion);
67 /* Initialize and trigger cipher operation */
68 static int test_skcipher(void)
70 struct skcipher_def sk;
71 struct crypto_skcipher *skcipher = NULL;
72 struct skcipher_request *req = NULL;
73 char *scratchpad = NULL;
75 unsigned char key[32];
78 skcipher = crypto_alloc_skcipher("cbc-aes-aesni", 0, 0);
79 if (IS_ERR(skcipher)) {
80 pr_info("could not allocate skcipher handle\n");
81 return PTR_ERR(skcipher);
84 req = skcipher_request_alloc(skcipher, GFP_KERNEL);
86 pr_info("could not allocate skcipher request\n");
91 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
95 /* AES 256 with random key */
96 get_random_bytes(&key, 32);
97 if (crypto_skcipher_setkey(skcipher, key, 32)) {
98 pr_info("key could not be set\n");
103 /* IV will be random */
104 ivdata = kmalloc(16, GFP_KERNEL);
106 pr_info("could not allocate ivdata\n");
109 get_random_bytes(ivdata, 16);
111 /* Input data will be random */
112 scratchpad = kmalloc(16, GFP_KERNEL);
114 pr_info("could not allocate scratchpad\n");
117 get_random_bytes(scratchpad, 16);
122 /* We encrypt one block */
123 sg_init_one(&sk.sg, scratchpad, 16);
124 skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata);
125 init_completion(&sk.result.completion);
128 ret = test_skcipher_encdec(&sk, 1);
132 pr_info("Encryption triggered successfully\n");
136 crypto_free_skcipher(skcipher);
138 skcipher_request_free(req);
147 Code Example For Use of Operational State Memory With SHASH
148 -----------------------------------------------------------
154 struct shash_desc shash;
158 static struct sdescinit_sdesc(struct crypto_shash *alg)
163 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
164 sdesc = kmalloc(size, GFP_KERNEL);
166 return ERR_PTR(-ENOMEM);
167 sdesc->shash.tfm = alg;
168 sdesc->shash.flags = 0x0;
172 static int calc_hash(struct crypto_shashalg,
173 const unsigned chardata, unsigned int datalen,
174 unsigned chardigest) {
178 sdesc = init_sdesc(alg);
180 pr_info("trusted_key: can't alloc %s\n", hash_alg);
181 return PTR_ERR(sdesc);
184 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
190 Code Example For Random Number Generator Usage
191 ----------------------------------------------
196 static int get_random_numbers(u8 *buf, unsigned int len)
198 struct crypto_rngrng = NULL;
199 chardrbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
203 pr_debug("No output buffer provided\n");
207 rng = crypto_alloc_rng(drbg, 0, 0);
209 pr_debug("could not allocate RNG handle for %s\n", drbg);
210 return -PTR_ERR(rng);
213 ret = crypto_rng_get_bytes(rng, buf, len);
215 pr_debug("generation of random numbers failed\n");
217 pr_debug("RNG returned no data");
219 pr_debug("RNG returned %d bytes of data\n", ret);
222 crypto_free_rng(rng);