blob: f14afaaf2f32483d94094a302a9628e2fd487e8e [file] [log] [blame]
Stephan Mueller3b72c812016-10-21 04:54:22 +02001Code Examples
2=============
3
4Code Example For Symmetric Key Cipher Operation
5-----------------------------------------------
6
7::
8
9
Stephan Mueller3b72c812016-10-21 04:54:22 +020010 /* tie all data structures together */
11 struct skcipher_def {
12 struct scatterlist sg;
13 struct crypto_skcipher *tfm;
14 struct skcipher_request *req;
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010015 struct crypto_wait wait;
Stephan Mueller3b72c812016-10-21 04:54:22 +020016 };
17
Stephan Mueller3b72c812016-10-21 04:54:22 +020018 /* Perform cipher operation */
19 static unsigned int test_skcipher_encdec(struct skcipher_def *sk,
20 int enc)
21 {
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010022 int rc;
Stephan Mueller3b72c812016-10-21 04:54:22 +020023
24 if (enc)
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010025 rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait);
Stephan Mueller3b72c812016-10-21 04:54:22 +020026 else
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010027 rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait);
Stephan Mueller3b72c812016-10-21 04:54:22 +020028
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010029 if (rc)
30 pr_info("skcipher encrypt returned with result %d\n", rc);
Stephan Mueller3b72c812016-10-21 04:54:22 +020031
32 return rc;
33 }
34
35 /* Initialize and trigger cipher operation */
36 static int test_skcipher(void)
37 {
38 struct skcipher_def sk;
39 struct crypto_skcipher *skcipher = NULL;
40 struct skcipher_request *req = NULL;
41 char *scratchpad = NULL;
42 char *ivdata = NULL;
43 unsigned char key[32];
44 int ret = -EFAULT;
45
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);
50 }
51
52 req = skcipher_request_alloc(skcipher, GFP_KERNEL);
53 if (!req) {
54 pr_info("could not allocate skcipher request\n");
55 ret = -ENOMEM;
56 goto out;
57 }
58
59 skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010060 crypto_req_done,
61 &sk.wait);
Stephan Mueller3b72c812016-10-21 04:54:22 +020062
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");
67 ret = -EAGAIN;
68 goto out;
69 }
70
71 /* IV will be random */
72 ivdata = kmalloc(16, GFP_KERNEL);
73 if (!ivdata) {
74 pr_info("could not allocate ivdata\n");
75 goto out;
76 }
77 get_random_bytes(ivdata, 16);
78
79 /* Input data will be random */
80 scratchpad = kmalloc(16, GFP_KERNEL);
81 if (!scratchpad) {
82 pr_info("could not allocate scratchpad\n");
83 goto out;
84 }
85 get_random_bytes(scratchpad, 16);
86
87 sk.tfm = skcipher;
88 sk.req = req;
89
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);
Gilad Ben-Yossefaba973c2017-10-18 08:00:52 +010093 crypto_init_wait(&sk.wait);
Stephan Mueller3b72c812016-10-21 04:54:22 +020094
95 /* encrypt data */
96 ret = test_skcipher_encdec(&sk, 1);
97 if (ret)
98 goto out;
99
100 pr_info("Encryption triggered successfully\n");
101
102 out:
103 if (skcipher)
104 crypto_free_skcipher(skcipher);
105 if (req)
106 skcipher_request_free(req);
107 if (ivdata)
108 kfree(ivdata);
109 if (scratchpad)
110 kfree(scratchpad);
111 return ret;
112 }
113
114
115Code Example For Use of Operational State Memory With SHASH
116-----------------------------------------------------------
117
118::
119
120
121 struct sdesc {
122 struct shash_desc shash;
123 char ctx[];
124 };
125
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200126 static struct sdesc *init_sdesc(struct crypto_shash *alg)
Stephan Mueller3b72c812016-10-21 04:54:22 +0200127 {
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200128 struct sdesc *sdesc;
Stephan Mueller3b72c812016-10-21 04:54:22 +0200129 int size;
130
131 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
132 sdesc = kmalloc(size, GFP_KERNEL);
133 if (!sdesc)
134 return ERR_PTR(-ENOMEM);
135 sdesc->shash.tfm = alg;
Stephan Mueller3b72c812016-10-21 04:54:22 +0200136 return sdesc;
137 }
138
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200139 static int calc_hash(struct crypto_shash *alg,
140 const unsigned char *data, unsigned int datalen,
141 unsigned char *digest)
142 {
143 struct sdesc *sdesc;
Stephan Mueller3b72c812016-10-21 04:54:22 +0200144 int ret;
145
146 sdesc = init_sdesc(alg);
147 if (IS_ERR(sdesc)) {
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200148 pr_info("can't alloc sdesc\n");
Stephan Mueller3b72c812016-10-21 04:54:22 +0200149 return PTR_ERR(sdesc);
150 }
151
152 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
153 kfree(sdesc);
154 return ret;
155 }
156
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200157 static int test_hash(const unsigned char *data, unsigned int datalen,
158 unsigned char *digest)
159 {
160 struct crypto_shash *alg;
161 char *hash_alg_name = "sha1-padlock-nano";
162 int ret;
163
Eric Biggers85d73112018-06-30 15:16:16 -0700164 alg = crypto_alloc_shash(hash_alg_name, 0, 0);
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200165 if (IS_ERR(alg)) {
166 pr_info("can't alloc alg %s\n", hash_alg_name);
167 return PTR_ERR(alg);
168 }
169 ret = calc_hash(alg, data, datalen, digest);
170 crypto_free_shash(alg);
171 return ret;
172 }
173
Stephan Mueller3b72c812016-10-21 04:54:22 +0200174
175Code Example For Random Number Generator Usage
176----------------------------------------------
177
178::
179
180
181 static int get_random_numbers(u8 *buf, unsigned int len)
182 {
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200183 struct crypto_rng *rng = NULL;
184 char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
Stephan Mueller3b72c812016-10-21 04:54:22 +0200185 int ret;
186
187 if (!buf || !len) {
188 pr_debug("No output buffer provided\n");
189 return -EINVAL;
190 }
191
192 rng = crypto_alloc_rng(drbg, 0, 0);
193 if (IS_ERR(rng)) {
194 pr_debug("could not allocate RNG handle for %s\n", drbg);
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200195 return PTR_ERR(rng);
Stephan Mueller3b72c812016-10-21 04:54:22 +0200196 }
197
198 ret = crypto_rng_get_bytes(rng, buf, len);
199 if (ret < 0)
200 pr_debug("generation of random numbers failed\n");
201 else if (ret == 0)
202 pr_debug("RNG returned no data");
203 else
204 pr_debug("RNG returned %d bytes of data\n", ret);
205
206 out:
207 crypto_free_rng(rng);
208 return ret;
209 }