1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Code Example For Use of Operational State Memory With SHASH</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="ch06s02.html" title="Code Example For Synchronous Block Cipher Operation"><link rel="next" href="ch06s04.html" title="Code Example For Random Number Generator Usage"></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 Use of Operational State Memory With SHASH</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s02.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="ch06s04.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idp1098429564"></a>Code Example For Use of Operational State Memory With SHASH</h2></div></div></div><pre class="programlisting">
2
3struct sdesc {
4	struct shash_desc shash;
5	char ctx[];
6};
7
8static struct sdescinit_sdesc(struct crypto_shash *alg)
9{
10	struct sdescsdesc;
11	int size;
12
13	size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
14	sdesc = kmalloc(size, GFP_KERNEL);
15	if (!sdesc)
16		return ERR_PTR(-ENOMEM);
17	sdesc-&gt;shash.tfm = alg;
18	sdesc-&gt;shash.flags = 0x0;
19	return sdesc;
20}
21
22static int calc_hash(struct crypto_shashalg,
23		     const unsigned chardata, unsigned int datalen,
24		     unsigned chardigest) {
25	struct sdescsdesc;
26	int ret;
27
28	sdesc = init_sdesc(alg);
29	if (IS_ERR(sdesc)) {
30		pr_info("trusted_key: can't alloc %s\n", hash_alg);
31		return PTR_ERR(sdesc);
32	}
33
34	ret = crypto_shash_digest(&amp;sdesc-&gt;shash, data, datalen, digest);
35	kfree(sdesc);
36	return ret;
37}
38    </pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s02.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="ch06s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Code Example For Synchronous Block Cipher Operation&#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 Random Number Generator Usage</td></tr></table></div></body></html>
39