Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Heimdal crypto library: example_evp_cipher.c</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<p>
<a href="http://www.h5l.org/"><img src="http://www.h5l.org/keyhole-heimdal.png" alt="keyhole logo"/></a>
</p>
<!-- end of header marker -->
<!-- Generated by Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>example_evp_cipher.c</h1>This is an example how to use <a class="el" href="group__hcrypto__evp.html#g98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex()</a>, <a class="el" href="group__hcrypto__evp.html#g863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate()</a> and <a class="el" href="group__hcrypto__evp.html#g714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex()</a>.<p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment"> * Copyright (c) 2008 Kungliga Tekniska Högskolan</span>
<span class="comment"> * (Royal Institute of Technology, Stockholm, Sweden).</span>
<span class="comment"> * All rights reserved.</span>
<span class="comment"> *</span>
<span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<span class="comment"> * modification, are permitted provided that the following conditions</span>
<span class="comment"> * are met:</span>
<span class="comment"> *</span>
<span class="comment"> * 1. Redistributions of source code must retain the above copyright</span>
<span class="comment"> *    notice, this list of conditions and the following disclaimer.</span>
<span class="comment"> *</span>
<span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<span class="comment"> *    notice, this list of conditions and the following disclaimer in the</span>
<span class="comment"> *    documentation and/or other materials provided with the distribution.</span>
<span class="comment"> *</span>
<span class="comment"> * 3. Neither the name of the Institute nor the names of its contributors</span>
<span class="comment"> *    may be used to endorse or promote products derived from this software</span>
<span class="comment"> *    without specific prior written permission.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND</span>
<span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span>
<span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>
<span class="comment"> * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE</span>
<span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span>
<span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span>
<span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span>
<span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span>
<span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span>
<span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span>
<span class="comment"> * SUCH DAMAGE.</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &lt;krb5-types.h&gt;</span> <span class="comment">/* should really be stdint.h */</span>
<span class="preprocessor">#include &lt;hcrypto/evp.h&gt;</span>

<span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<span class="preprocessor">#include &lt;string.h&gt;</span>
<span class="preprocessor">#include &lt;err.h&gt;</span>
<span class="preprocessor">#include &lt;assert.h&gt;</span>

<span class="preprocessor">#include "roken.h"</span>

<span class="comment">/* key and initial vector */</span>
<span class="keyword">static</span> <span class="keywordtype">char</span> key[16] =
    <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>
    <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>;
<span class="keyword">static</span> <span class="keywordtype">char</span> ivec[16] =
    <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>
    <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>;

<span class="keyword">static</span> <span class="keywordtype">void</span>
usage(<span class="keywordtype">int</span> exit_code) __attribute__((noreturn));

<span class="keyword">static</span> <span class="keywordtype">void</span>
usage(<span class="keywordtype">int</span> exit_code)
{
    printf(<span class="stringliteral">"usage: %s in out\n"</span>, getprogname());
    exit(exit_code);
}


<span class="keywordtype">int</span>
main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
{
    <span class="keywordtype">int</span> encryptp = 1;
    <span class="keyword">const</span> <span class="keywordtype">char</span> *ifn = NULL, *ofn = NULL;
    FILE *in, *out;
    <span class="keywordtype">void</span> *ibuf, *obuf;
    <span class="keywordtype">int</span> ilen, olen;
    <span class="keywordtype">size_t</span> block_size = 0;
    <span class="keyword">const</span> EVP_CIPHER *c = <a name="a0"></a><a class="code" href="group__hcrypto__evp.html#g2747bac943db15c97167ac37fdc2af43">EVP_aes_128_cbc</a>();
    EVP_CIPHER_CTX ctx;
    <span class="keywordtype">int</span> ret;

    setprogname(argv[0]);

    <span class="keywordflow">if</span> (argc == 2) {
        <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--version"</span>) == 0) {
            printf(<span class="stringliteral">"version"</span>);
            exit(0);
        }
        <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--help"</span>) == 0)
            usage(0);
        usage(1);
    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argc == 4) {
        block_size = atoi(argv[1]);
        <span class="keywordflow">if</span> (block_size == 0)
            errx(1, <span class="stringliteral">"invalid blocksize %s"</span>, argv[1]);
        ifn = argv[2];
        ofn = argv[3];
    } <span class="keywordflow">else</span>
        usage(1);

    in = fopen(ifn, <span class="stringliteral">"r"</span>);
    <span class="keywordflow">if</span> (in == NULL)
        errx(1, <span class="stringliteral">"failed to open input file"</span>);
    out = fopen(ofn, <span class="stringliteral">"w+"</span>);
    <span class="keywordflow">if</span> (out == NULL)
        errx(1, <span class="stringliteral">"failed to open output file"</span>);

    <span class="comment">/* Check that key and ivec are long enough */</span>
    assert(<a name="a1"></a><a class="code" href="group__hcrypto__evp.html#g4793a9e130da86ac42c497b19395b748">EVP_CIPHER_key_length</a>(c) &lt;= <span class="keyword">sizeof</span>(key));
    assert(<a name="a2"></a><a class="code" href="group__hcrypto__evp.html#g7edebe76d19dee11686a6698d24f19f5">EVP_CIPHER_iv_length</a>(c) &lt;= <span class="keyword">sizeof</span>(ivec));

    <span class="comment">/*</span>
<span class="comment">     * Allocate buffer, the output buffer is at least</span>
<span class="comment">     * EVP_CIPHER_block_size() longer</span>
<span class="comment">     */</span>
    ibuf = malloc(block_size);
    obuf = malloc(block_size + <a name="a3"></a><a class="code" href="group__hcrypto__evp.html#g3123ff4b426783dd972538249b9e9d4d">EVP_CIPHER_block_size</a>(c));

    <span class="comment">/*</span>
<span class="comment">     * Init the memory used for EVP_CIPHER_CTX and set the key and</span>
<span class="comment">     * ivec.</span>
<span class="comment">     */</span>
    <a name="a4"></a><a class="code" href="group__hcrypto__evp.html#g13d0ff0e87312566cb6b3591157f9754">EVP_CIPHER_CTX_init</a>(&amp;ctx);
    <a name="a5"></a><a class="code" href="group__hcrypto__evp.html#g98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex</a>(&amp;ctx, c, NULL, key, ivec, encryptp);

    <span class="comment">/* read in buffer */</span>
    <span class="keywordflow">while</span> ((ilen = fread(ibuf, 1, block_size, in)) &gt; 0) {
        <span class="comment">/* encrypto/decrypt */</span>
        ret = <a name="a6"></a><a class="code" href="group__hcrypto__evp.html#g863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate</a>(&amp;ctx, obuf, &amp;olen, ibuf, ilen);
        <span class="keywordflow">if</span> (ret != 1) {
            <a name="a7"></a><a class="code" href="group__hcrypto__evp.html#gc16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&amp;ctx);
            errx(1, <span class="stringliteral">"EVP_CipherUpdate failed"</span>);
        }
        <span class="comment">/* write out to output file */</span>
        fwrite(obuf, 1, olen, out);
    }
    <span class="comment">/* done reading */</span>
    fclose(in);

    <span class="comment">/* clear up any last bytes left in the output buffer */</span>
    ret = <a name="a8"></a><a class="code" href="group__hcrypto__evp.html#g714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex</a>(&amp;ctx, obuf, &amp;olen);
    <a class="code" href="group__hcrypto__evp.html#gc16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&amp;ctx);
    <span class="keywordflow">if</span> (ret != 1)
        errx(1, <span class="stringliteral">"EVP_CipherFinal_ex failed"</span>);

    <span class="comment">/* write the last bytes out and close */</span>
    fwrite(obuf, 1, olen, out);
    fclose(out);

    <span class="keywordflow">return</span> 0;
}
</pre></div> </div>
<hr size="1"><address style="text-align: right;"><small>
Generated on Wed Jan 11 14:07:38 2012 for Heimdal crypto library by&nbsp;<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6</small></address>
</body>
</html>