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;
136 sdesc->shash.flags = 0x0;
140 static int calc_hash(struct crypto_shash *alg,
141 const unsigned char *data, unsigned int datalen,
142 unsigned char *digest)
147 sdesc = init_sdesc(alg);
149 pr_info("can't alloc sdesc\n");
150 return PTR_ERR(sdesc);
153 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
158 static int test_hash(const unsigned char *data, unsigned int datalen,
159 unsigned char *digest)
161 struct crypto_shash *alg;
162 char *hash_alg_name = "sha1-padlock-nano";
165 alg = crypto_alloc_shash(hash_alg_name, CRYPTO_ALG_TYPE_SHASH, 0);
167 pr_info("can't alloc alg %s\n", hash_alg_name);
170 ret = calc_hash(alg, data, datalen, digest);
171 crypto_free_shash(alg);
176 Code Example For Random Number Generator Usage
177 ----------------------------------------------
182 static int get_random_numbers(u8 *buf, unsigned int len)
184 struct crypto_rng *rng = NULL;
185 char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
189 pr_debug("No output buffer provided\n");
193 rng = crypto_alloc_rng(drbg, 0, 0);
195 pr_debug("could not allocate RNG handle for %s\n", drbg);
199 ret = crypto_rng_get_bytes(rng, buf, len);
201 pr_debug("generation of random numbers failed\n");
203 pr_debug("RNG returned no data");
205 pr_debug("RNG returned %d bytes of data\n", ret);
208 crypto_free_rng(rng);