example__evp__cipher_8c-example.html revision 285830
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> 3<title>Heimdal crypto library: example_evp_cipher.c</title> 4<link href="doxygen.css" rel="stylesheet" type="text/css"> 5<link href="tabs.css" rel="stylesheet" type="text/css"> 6</head><body> 7<p> 8<a href="http://www.h5l.org/"><img src="http://www.h5l.org/keyhole-heimdal.png" alt="keyhole logo"/></a> 9</p> 10<!-- end of header marker --> 11<!-- Generated by Doxygen 1.5.6 --> 12<div class="navigation" id="top"> 13 <div class="tabs"> 14 <ul> 15 <li><a href="index.html"><span>Main Page</span></a></li> 16 <li><a href="modules.html"><span>Modules</span></a></li> 17 <li><a href="examples.html"><span>Examples</span></a></li> 18 </ul> 19 </div> 20</div> 21<div class="contents"> 22<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> 23<div class="fragment"><pre class="fragment"><span class="comment">/*</span> 24<span class="comment"> * Copyright (c) 2008 Kungliga Tekniska H��gskolan</span> 25<span class="comment"> * (Royal Institute of Technology, Stockholm, Sweden).</span> 26<span class="comment"> * All rights reserved.</span> 27<span class="comment"> *</span> 28<span class="comment"> * Redistribution and use in source and binary forms, with or without</span> 29<span class="comment"> * modification, are permitted provided that the following conditions</span> 30<span class="comment"> * are met:</span> 31<span class="comment"> *</span> 32<span class="comment"> * 1. Redistributions of source code must retain the above copyright</span> 33<span class="comment"> * notice, this list of conditions and the following disclaimer.</span> 34<span class="comment"> *</span> 35<span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span> 36<span class="comment"> * notice, this list of conditions and the following disclaimer in the</span> 37<span class="comment"> * documentation and/or other materials provided with the distribution.</span> 38<span class="comment"> *</span> 39<span class="comment"> * 3. Neither the name of the Institute nor the names of its contributors</span> 40<span class="comment"> * may be used to endorse or promote products derived from this software</span> 41<span class="comment"> * without specific prior written permission.</span> 42<span class="comment"> *</span> 43<span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND</span> 44<span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> 45<span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> 46<span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE</span> 47<span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span> 48<span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span> 49<span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span> 50<span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span> 51<span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span> 52<span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span> 53<span class="comment"> * SUCH DAMAGE.</span> 54<span class="comment"> */</span> 55 56<span class="preprocessor">#include <krb5-types.h></span> <span class="comment">/* should really be stdint.h */</span> 57<span class="preprocessor">#include <hcrypto/evp.h></span> 58 59<span class="preprocessor">#include <stdio.h></span> 60<span class="preprocessor">#include <stdlib.h></span> 61<span class="preprocessor">#include <string.h></span> 62<span class="preprocessor">#include <err.h></span> 63<span class="preprocessor">#include <assert.h></span> 64 65<span class="preprocessor">#include "roken.h"</span> 66 67<span class="comment">/* key and initial vector */</span> 68<span class="keyword">static</span> <span class="keywordtype">char</span> key[16] = 69 <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span> 70 <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>; 71<span class="keyword">static</span> <span class="keywordtype">char</span> ivec[16] = 72 <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span> 73 <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>; 74 75<span class="keyword">static</span> <span class="keywordtype">void</span> 76usage(<span class="keywordtype">int</span> exit_code) __attribute__((noreturn)); 77 78<span class="keyword">static</span> <span class="keywordtype">void</span> 79usage(<span class="keywordtype">int</span> exit_code) 80{ 81 printf(<span class="stringliteral">"usage: %s in out\n"</span>, getprogname()); 82 exit(exit_code); 83} 84 85 86<span class="keywordtype">int</span> 87main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) 88{ 89 <span class="keywordtype">int</span> encryptp = 1; 90 <span class="keyword">const</span> <span class="keywordtype">char</span> *ifn = NULL, *ofn = NULL; 91 FILE *in, *out; 92 <span class="keywordtype">void</span> *ibuf, *obuf; 93 <span class="keywordtype">int</span> ilen, olen; 94 <span class="keywordtype">size_t</span> block_size = 0; 95 <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>(); 96 EVP_CIPHER_CTX ctx; 97 <span class="keywordtype">int</span> ret; 98 99 setprogname(argv[0]); 100 101 <span class="keywordflow">if</span> (argc == 2) { 102 <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--version"</span>) == 0) { 103 printf(<span class="stringliteral">"version"</span>); 104 exit(0); 105 } 106 <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--help"</span>) == 0) 107 usage(0); 108 usage(1); 109 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argc == 4) { 110 block_size = atoi(argv[1]); 111 <span class="keywordflow">if</span> (block_size == 0) 112 errx(1, <span class="stringliteral">"invalid blocksize %s"</span>, argv[1]); 113 ifn = argv[2]; 114 ofn = argv[3]; 115 } <span class="keywordflow">else</span> 116 usage(1); 117 118 in = fopen(ifn, <span class="stringliteral">"r"</span>); 119 <span class="keywordflow">if</span> (in == NULL) 120 errx(1, <span class="stringliteral">"failed to open input file"</span>); 121 out = fopen(ofn, <span class="stringliteral">"w+"</span>); 122 <span class="keywordflow">if</span> (out == NULL) 123 errx(1, <span class="stringliteral">"failed to open output file"</span>); 124 125 <span class="comment">/* Check that key and ivec are long enough */</span> 126 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)); 127 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)); 128 129 <span class="comment">/*</span> 130<span class="comment"> * Allocate buffer, the output buffer is at least</span> 131<span class="comment"> * EVP_CIPHER_block_size() longer</span> 132<span class="comment"> */</span> 133 ibuf = malloc(block_size); 134 obuf = malloc(block_size + <a name="a3"></a><a class="code" href="group__hcrypto__evp.html#g3123ff4b426783dd972538249b9e9d4d">EVP_CIPHER_block_size</a>(c)); 135 136 <span class="comment">/*</span> 137<span class="comment"> * Init the memory used for EVP_CIPHER_CTX and set the key and</span> 138<span class="comment"> * ivec.</span> 139<span class="comment"> */</span> 140 <a name="a4"></a><a class="code" href="group__hcrypto__evp.html#g13d0ff0e87312566cb6b3591157f9754">EVP_CIPHER_CTX_init</a>(&ctx); 141 <a name="a5"></a><a class="code" href="group__hcrypto__evp.html#g98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex</a>(&ctx, c, NULL, key, ivec, encryptp); 142 143 <span class="comment">/* read in buffer */</span> 144 <span class="keywordflow">while</span> ((ilen = fread(ibuf, 1, block_size, in)) > 0) { 145 <span class="comment">/* encrypto/decrypt */</span> 146 ret = <a name="a6"></a><a class="code" href="group__hcrypto__evp.html#g863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate</a>(&ctx, obuf, &olen, ibuf, ilen); 147 <span class="keywordflow">if</span> (ret != 1) { 148 <a name="a7"></a><a class="code" href="group__hcrypto__evp.html#gc16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&ctx); 149 errx(1, <span class="stringliteral">"EVP_CipherUpdate failed"</span>); 150 } 151 <span class="comment">/* write out to output file */</span> 152 fwrite(obuf, 1, olen, out); 153 } 154 <span class="comment">/* done reading */</span> 155 fclose(in); 156 157 <span class="comment">/* clear up any last bytes left in the output buffer */</span> 158 ret = <a name="a8"></a><a class="code" href="group__hcrypto__evp.html#g714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex</a>(&ctx, obuf, &olen); 159 <a class="code" href="group__hcrypto__evp.html#gc16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&ctx); 160 <span class="keywordflow">if</span> (ret != 1) 161 errx(1, <span class="stringliteral">"EVP_CipherFinal_ex failed"</span>); 162 163 <span class="comment">/* write the last bytes out and close */</span> 164 fwrite(obuf, 1, olen, out); 165 fclose(out); 166 167 <span class="keywordflow">return</span> 0; 168} 169</pre></div> </div> 170<hr size="1"><address style="text-align: right;"><small> 171Generated 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> 172</body> 173</html> 174