1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Code Example For Synchronous Block Cipher Operation</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Linux Kernel Crypto API"><link rel="up" href="Code.html" title="Chapter&#160;6.&#160;Code Examples"><link rel="prev" href="Code.html" title="Chapter&#160;6.&#160;Code Examples"><link rel="next" href="ch06s03.html" title="Code Example For Use of Operational State Memory With SHASH"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Code Example For Synchronous Block Cipher Operation</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="Code.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;6.&#160;Code Examples</th><td width="20%" align="right">&#160;<a accesskey="n" href="ch06s03.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idp1098426268"></a>Code Example For Synchronous Block Cipher Operation</h2></div></div></div><pre class="programlisting">
2
3static int test_blkcipher(void)
4{
5	struct crypto_blkcipher *blkcipher = NULL;
6	char *cipher = "cbc(aes)";
7	// AES 128
8	charkey =
9"\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
10	chariv =
11"\x12\x34\x56\x78\x90\xab\xcd\xef\x12\x34\x56\x78\x90\xab\xcd\xef";
12	unsigned int ivsize = 0;
13	char *scratchpad = NULL; // holds plaintext and ciphertext
14	struct scatterlist sg;
15	struct blkcipher_desc desc;
16	int ret = -EFAULT;
17
18	blkcipher = crypto_alloc_blkcipher(cipher, 0, 0);
19	if (IS_ERR(blkcipher)) {
20		printk("could not allocate blkcipher handle for %s\n", cipher);
21		return -PTR_ERR(blkcipher);
22	}
23
24	if (crypto_blkcipher_setkey(blkcipher, key, strlen(key))) {
25		printk("key could not be set\n");
26		ret = -EAGAIN;
27		goto out;
28	}
29
30	ivsize = crypto_blkcipher_ivsize(blkcipher);
31	if (ivsize) {
32		if (ivsize != strlen(iv))
33			printk("IV length differs from expected length\n");
34		crypto_blkcipher_set_iv(blkcipher, iv, ivsize);
35	}
36
37	scratchpad = kmalloc(crypto_blkcipher_blocksize(blkcipher), GFP_KERNEL);
38	if (!scratchpad) {
39		printk("could not allocate scratchpad for %s\n", cipher);
40		goto out;
41	}
42	/* get some random data that we want to encrypt */
43	get_random_bytes(scratchpad, crypto_blkcipher_blocksize(blkcipher));
44
45	desc.flags = 0;
46	desc.tfm = blkcipher;
47	sg_init_one(&amp;sg, scratchpad, crypto_blkcipher_blocksize(blkcipher));
48
49	/* encrypt data in place */
50	crypto_blkcipher_encrypt(&amp;desc, &amp;sg, &amp;sg,
51				 crypto_blkcipher_blocksize(blkcipher));
52
53	/* decrypt data in place
54	 * crypto_blkcipher_decrypt(&amp;desc, &amp;sg, &amp;sg,
55	 */			 crypto_blkcipher_blocksize(blkcipher));
56
57
58	printk("Cipher operation completed\n");
59	return 0;
60
61out:
62	if (blkcipher)
63		crypto_free_blkcipher(blkcipher);
64	if (scratchpad)
65		kzfree(scratchpad);
66	return ret;
67}
68    </pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="Code.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="Code.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="ch06s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&#160;6.&#160;Code Examples&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Code Example For Use of Operational State Memory With SHASH</td></tr></table></div></body></html>
69