blob: 006827e30d0664939d71b716e870ee08c9532852 [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;
136 sdesc->shash.flags = 0x0;
137 return sdesc;
138 }
139
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200140 static int calc_hash(struct crypto_shash *alg,
141 const unsigned char *data, unsigned int datalen,
142 unsigned char *digest)
143 {
144 struct sdesc *sdesc;
Stephan Mueller3b72c812016-10-21 04:54:22 +0200145 int ret;
146
147 sdesc = init_sdesc(alg);
148 if (IS_ERR(sdesc)) {
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200149 pr_info("can't alloc sdesc\n");
Stephan Mueller3b72c812016-10-21 04:54:22 +0200150 return PTR_ERR(sdesc);
151 }
152
153 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
154 kfree(sdesc);
155 return ret;
156 }
157
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200158 static int test_hash(const unsigned char *data, unsigned int datalen,
159 unsigned char *digest)
160 {
161 struct crypto_shash *alg;
162 char *hash_alg_name = "sha1-padlock-nano";
163 int ret;
164
165 alg = crypto_alloc_shash(hash_alg_name, CRYPTO_ALG_TYPE_SHASH, 0);
166 if (IS_ERR(alg)) {
167 pr_info("can't alloc alg %s\n", hash_alg_name);
168 return PTR_ERR(alg);
169 }
170 ret = calc_hash(alg, data, datalen, digest);
171 crypto_free_shash(alg);
172 return ret;
173 }
174
Stephan Mueller3b72c812016-10-21 04:54:22 +0200175
176Code Example For Random Number Generator Usage
177----------------------------------------------
178
179::
180
181
182 static int get_random_numbers(u8 *buf, unsigned int len)
183 {
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200184 struct crypto_rng *rng = NULL;
185 char *drbg = "drbg_nopr_sha256"; /* Hash DRBG with SHA-256, no PR */
Stephan Mueller3b72c812016-10-21 04:54:22 +0200186 int ret;
187
188 if (!buf || !len) {
189 pr_debug("No output buffer provided\n");
190 return -EINVAL;
191 }
192
193 rng = crypto_alloc_rng(drbg, 0, 0);
194 if (IS_ERR(rng)) {
195 pr_debug("could not allocate RNG handle for %s\n", drbg);
Kamil Koniecznyea644b82017-05-12 17:38:02 +0200196 return PTR_ERR(rng);
Stephan Mueller3b72c812016-10-21 04:54:22 +0200197 }
198
199 ret = crypto_rng_get_bytes(rng, buf, len);
200 if (ret < 0)
201 pr_debug("generation of random numbers failed\n");
202 else if (ret == 0)
203 pr_debug("RNG returned no data");
204 else
205 pr_debug("RNG returned %d bytes of data\n", ret);
206
207 out:
208 crypto_free_rng(rng);
209 return ret;
210 }