1185605Skmacy<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2185605Skmacy<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 3185605Skmacy<title>Heimdal crypto library: example_evp_cipher.c</title> 4185605Skmacy<link href="doxygen.css" rel="stylesheet" type="text/css"> 5185605Skmacy<link href="tabs.css" rel="stylesheet" type="text/css"> 6185605Skmacy</head><body> 7185605Skmacy<p> 8185605Skmacy<a href="http://www.h5l.org/"><img src="http://www.h5l.org/keyhole-heimdal.png" alt="keyhole logo"/></a> 9185605Skmacy</p> 10185605Skmacy<!-- end of header marker --> 11185605Skmacy<!-- Generated by Doxygen 1.5.6 --> 12185605Skmacy<div class="navigation" id="top"> 13185605Skmacy <div class="tabs"> 14185605Skmacy <ul> 15185605Skmacy <li><a href="index.html"><span>Main Page</span></a></li> 16185605Skmacy <li><a href="modules.html"><span>Modules</span></a></li> 17185605Skmacy <li><a href="examples.html"><span>Examples</span></a></li> 18185605Skmacy </ul> 19185605Skmacy </div> 20185605Skmacy</div> 21185605Skmacy<div class="contents"> 22185605Skmacy<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> 23185605Skmacy<div class="fragment"><pre class="fragment"><span class="comment">/*</span> 24185605Skmacy<span class="comment"> * Copyright (c) 2008 Kungliga Tekniska H��gskolan</span> 25185605Skmacy<span class="comment"> * (Royal Institute of Technology, Stockholm, Sweden).</span> 26185605Skmacy<span class="comment"> * All rights reserved.</span> 27185605Skmacy<span class="comment"> *</span> 28185605Skmacy<span class="comment"> * Redistribution and use in source and binary forms, with or without</span> 29185605Skmacy<span class="comment"> * modification, are permitted provided that the following conditions</span> 30185605Skmacy<span class="comment"> * are met:</span> 31185605Skmacy<span class="comment"> *</span> 32185605Skmacy<span class="comment"> * 1. Redistributions of source code must retain the above copyright</span> 33185605Skmacy<span class="comment"> * notice, this list of conditions and the following disclaimer.</span> 34185605Skmacy<span class="comment"> *</span> 35185605Skmacy<span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> 36185605Skmacy<span class="comment"> * notice, this list of conditions and the following disclaimer in the</span> 37185605Skmacy<span class="comment"> * documentation and/or other materials provided with the distribution.</span> 38185605Skmacy<span class="comment"> *</span> 39185605Skmacy<span class="comment"> * 3. Neither the name of the Institute nor the names of its contributors</span> 40185605Skmacy<span class="comment"> * may be used to endorse or promote products derived from this software</span> 41185605Skmacy<span class="comment"> * without specific prior written permission.</span> 42185605Skmacy<span class="comment"> *</span> 43185605Skmacy<span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND</span> 44185605Skmacy<span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> 45185605Skmacy<span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> 46185605Skmacy<span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE</span> 47185605Skmacy<span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span> 48185605Skmacy<span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span> 49185605Skmacy<span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span> 50185605Skmacy<span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span> 51185605Skmacy<span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span> 52185605Skmacy<span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span> 53185605Skmacy<span class="comment"> * SUCH DAMAGE.</span> 54185605Skmacy<span class="comment"> */</span> 55185605Skmacy 56185605Skmacy<span class="preprocessor">#include <krb5-types.h></span> <span class="comment">/* should really be stdint.h */</span> 57185605Skmacy<span class="preprocessor">#include <hcrypto/evp.h></span> 58185605Skmacy 59185605Skmacy<span class="preprocessor">#include <stdio.h></span> 60185605Skmacy<span class="preprocessor">#include <stdlib.h></span> 61185605Skmacy<span class="preprocessor">#include <string.h></span> 62185605Skmacy<span class="preprocessor">#include <err.h></span> 63185605Skmacy<span class="preprocessor">#include <assert.h></span> 64185605Skmacy 65185605Skmacy<span class="preprocessor">#include "roken.h"</span> 66185605Skmacy 67185605Skmacy<span class="comment">/* key and initial vector */</span> 68185605Skmacy<span class="keyword">static</span> <span class="keywordtype">char</span> key[16] = 69185605Skmacy <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span> 70185605Skmacy <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>; 71185605Skmacy<span class="keyword">static</span> <span class="keywordtype">char</span> ivec[16] = 72185605Skmacy <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span> 73185605Skmacy <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>; 74185605Skmacy 75185605Skmacy<span class="keyword">static</span> <span class="keywordtype">void</span> 76185605Skmacyusage(<span class="keywordtype">int</span> exit_code) __attribute__((noreturn)); 77185605Skmacy 78185605Skmacy<span class="keyword">static</span> <span class="keywordtype">void</span> 79185605Skmacyusage(<span class="keywordtype">int</span> exit_code) 80185605Skmacy{ 81185605Skmacy printf(<span class="stringliteral">"usage: %s in out\n"</span>, getprogname()); 82185605Skmacy exit(exit_code); 83185605Skmacy} 84185605Skmacy 85185605Skmacy 86185605Skmacy<span class="keywordtype">int</span> 87185605Skmacymain(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) 88185605Skmacy{ 89185605Skmacy <span class="keywordtype">int</span> encryptp = 1; 90185605Skmacy <span class="keyword">const</span> <span class="keywordtype">char</span> *ifn = NULL, *ofn = NULL; 91185605Skmacy FILE *in, *out; 92185605Skmacy <span class="keywordtype">void</span> *ibuf, *obuf; 93185605Skmacy <span class="keywordtype">int</span> ilen, olen; 94185605Skmacy <span class="keywordtype">size_t</span> block_size = 0; 95185605Skmacy <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>(); 96185605Skmacy EVP_CIPHER_CTX ctx; 97185605Skmacy <span class="keywordtype">int</span> ret; 98185605Skmacy 99185605Skmacy setprogname(argv[0]); 100185605Skmacy 101185605Skmacy <span class="keywordflow">if</span> (argc == 2) { 102185605Skmacy <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--version"</span>) == 0) { 103185605Skmacy printf(<span class="stringliteral">"version"</span>); 104185605Skmacy exit(0); 105185605Skmacy } 106185605Skmacy <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--help"</span>) == 0) 107185605Skmacy usage(0); 108185605Skmacy usage(1); 109185605Skmacy } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argc == 4) { 110185605Skmacy block_size = atoi(argv[1]); 111185605Skmacy <span class="keywordflow">if</span> (block_size == 0) 112185605Skmacy errx(1, <span class="stringliteral">"invalid blocksize %s"</span>, argv[1]); 113185605Skmacy ifn = argv[2]; 114185605Skmacy ofn = argv[3]; 115185605Skmacy } <span class="keywordflow">else</span> 116185605Skmacy usage(1); 117185605Skmacy 118185605Skmacy in = fopen(ifn, <span class="stringliteral">"r"</span>); 119185605Skmacy <span class="keywordflow">if</span> (in == NULL) 120185605Skmacy errx(1, <span class="stringliteral">"failed to open input file"</span>); 121185605Skmacy out = fopen(ofn, <span class="stringliteral">"w+"</span>); 122185605Skmacy <span class="keywordflow">if</span> (out == NULL) 123185605Skmacy errx(1, <span class="stringliteral">"failed to open output file"</span>); 124185605Skmacy 125185605Skmacy <span class="comment">/* Check that key and ivec are long enough */</span> 126185605Skmacy assert(<a name="a1"></a><a class="code" href="group__hcrypto__evp.html#g4793a9e130da86ac42c497b19395b748">EVP_CIPHER_key_length</a>(c) <= <span class="keyword">sizeof</span>(key)); 127185605Skmacy assert(<a name="a2"></a><a class="code" href="group__hcrypto__evp.html#g7edebe76d19dee11686a6698d24f19f5">EVP_CIPHER_iv_length</a>(c) <= <span class="keyword">sizeof</span>(ivec)); 128185605Skmacy 129185605Skmacy <span class="comment">/*</span> 130185605Skmacy<span class="comment"> * Allocate buffer, the output buffer is at least</span> 131185605Skmacy<span class="comment"> * EVP_CIPHER_block_size() longer</span> 132185605Skmacy<span class="comment"> */</span> 133185605Skmacy ibuf = malloc(block_size); 134185605Skmacy obuf = malloc(block_size + <a name="a3"></a><a class="code" href="group__hcrypto__evp.html#g3123ff4b426783dd972538249b9e9d4d">EVP_CIPHER_block_size</a>(c)); 135185605Skmacy 136185605Skmacy <span class="comment">/*</span> 137185605Skmacy<span class="comment"> * Init the memory used for EVP_CIPHER_CTX and set the key and</span> 138185605Skmacy<span class="comment"> * ivec.</span> 139185605Skmacy<span class="comment"> */</span> 140185605Skmacy <a name="a4"></a><a class="code" href="group__hcrypto__evp.html#g13d0ff0e87312566cb6b3591157f9754">EVP_CIPHER_CTX_init</a>(&ctx); 141185605Skmacy <a name="a5"></a><a class="code" href="group__hcrypto__evp.html#g98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex</a>(&ctx, c, NULL, key, ivec, encryptp); 142185605Skmacy 143185605Skmacy <span class="comment">/* read in buffer */</span> 144185605Skmacy <span class="keywordflow">while</span> ((ilen = fread(ibuf, 1, block_size, in)) > 0) { 145185605Skmacy <span class="comment">/* encrypto/decrypt */</span> 146185605Skmacy ret = <a name="a6"></a><a class="code" href="group__hcrypto__evp.html#g863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate</a>(&ctx, obuf, &olen, ibuf, ilen); 147185605Skmacy <span class="keywordflow">if</span> (ret != 1) { 148185605Skmacy <a name="a7"></a><a class="code" href="group__hcrypto__evp.html#gc16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&ctx); 149185605Skmacy errx(1, <span class="stringliteral">"EVP_CipherUpdate failed"</span>); 150185605Skmacy } 151185605Skmacy <span class="comment">/* write out to output file */</span> 152185605Skmacy fwrite(obuf, 1, olen, out); 153185605Skmacy } 154185605Skmacy <span class="comment">/* done reading */</span> 155185605Skmacy fclose(in); 156185605Skmacy 157185605Skmacy <span class="comment">/* clear up any last bytes left in the output buffer */</span> 158185605Skmacy ret = <a name="a8"></a><a class="code" href="group__hcrypto__evp.html#g714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex</a>(&ctx, obuf, &olen); 159185605Skmacy <a class="code" href="group__hcrypto__evp.html#gc16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&ctx); 160185605Skmacy <span class="keywordflow">if</span> (ret != 1) 161185605Skmacy errx(1, <span class="stringliteral">"EVP_CipherFinal_ex failed"</span>); 162185605Skmacy 163185605Skmacy <span class="comment">/* write the last bytes out and close */</span> 164185605Skmacy fwrite(obuf, 1, olen, out); 165185605Skmacy fclose(out); 166185605Skmacy 167185605Skmacy <span class="keywordflow">return</span> 0; 168185605Skmacy} 169185605Skmacy</pre></div> </div> 170185605Skmacy<hr size="1"><address style="text-align: right;"><small> 171185605SkmacyGenerated on Wed Jan 11 14:07:38 2012 for Heimdal crypto library by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6</small></address> 172185605Skmacy</body> 173185605Skmacy</html> 174185605Skmacy