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>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.9.1 --> 12<script type="text/javascript" src="menudata.js"></script> 13<script type="text/javascript" src="menu.js"></script> 14<script type="text/javascript"> 15/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ 16$(function() { 17 initMenu('',false,false,'search.php','Search'); 18}); 19/* @license-end */</script> 20<div id="main-nav"></div> 21</div><!-- top --> 22<div class="header"> 23 <div class="headertitle"> 24<div class="title">example_evp_cipher.c</div> </div> 25</div><!--header--> 26<div class="contents"> 27<p>This is an example how to use <a class="el" href="group__hcrypto__evp.html#ga98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex()</a>, <a class="el" href="group__hcrypto__evp.html#ga863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate()</a> and <a class="el" href="group__hcrypto__evp.html#ga714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex()</a>.</p> 28<div class="fragment"><div class="line"><span class="comment">/*</span></div> 29<div class="line"><span class="comment"> * Copyright (c) 2008 Kungliga Tekniska Högskolan</span></div> 30<div class="line"><span class="comment"> * (Royal Institute of Technology, Stockholm, Sweden).</span></div> 31<div class="line"><span class="comment"> * All rights reserved.</span></div> 32<div class="line"><span class="comment"> *</span></div> 33<div class="line"><span class="comment"> * Redistribution and use in source and binary forms, with or without</span></div> 34<div class="line"><span class="comment"> * modification, are permitted provided that the following conditions</span></div> 35<div class="line"><span class="comment"> * are met:</span></div> 36<div class="line"><span class="comment"> *</span></div> 37<div class="line"><span class="comment"> * 1. Redistributions of source code must retain the above copyright</span></div> 38<div class="line"><span class="comment"> * notice, this list of conditions and the following disclaimer.</span></div> 39<div class="line"><span class="comment"> *</span></div> 40<div class="line"><span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span></div> 41<div class="line"><span class="comment"> * notice, this list of conditions and the following disclaimer in the</span></div> 42<div class="line"><span class="comment"> * documentation and/or other materials provided with the distribution.</span></div> 43<div class="line"><span class="comment"> *</span></div> 44<div class="line"><span class="comment"> * 3. Neither the name of the Institute nor the names of its contributors</span></div> 45<div class="line"><span class="comment"> * may be used to endorse or promote products derived from this software</span></div> 46<div class="line"><span class="comment"> * without specific prior written permission.</span></div> 47<div class="line"><span class="comment"> *</span></div> 48<div class="line"><span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND</span></div> 49<div class="line"><span class="comment"> * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span></div> 50<div class="line"><span class="comment"> * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span></div> 51<div class="line"><span class="comment"> * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE</span></div> 52<div class="line"><span class="comment"> * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span></div> 53<div class="line"><span class="comment"> * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span></div> 54<div class="line"><span class="comment"> * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span></div> 55<div class="line"><span class="comment"> * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span></div> 56<div class="line"><span class="comment"> * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span></div> 57<div class="line"><span class="comment"> * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span></div> 58<div class="line"><span class="comment"> * SUCH DAMAGE.</span></div> 59<div class="line"><span class="comment"> */</span></div> 60<div class="line"> </div> 61<div class="line"><span class="preprocessor">#include <config.h></span></div> 62<div class="line"><span class="preprocessor">#include <roken.h></span></div> 63<div class="line"> </div> 64<div class="line"><span class="preprocessor">#include <krb5-types.h></span> <span class="comment">/* should really be stdint.h */</span></div> 65<div class="line"><span class="preprocessor">#include <hcrypto/evp.h></span></div> 66<div class="line"><span class="preprocessor">#include <hcrypto/evp-pkcs11.h></span></div> 67<div class="line"><span class="preprocessor">#ifdef __APPLE__</span></div> 68<div class="line"><span class="preprocessor">#include <hcrypto/evp-cc.h></span></div> 69<div class="line"><span class="preprocessor">#endif</span></div> 70<div class="line"><span class="preprocessor">#ifdef _WIN32</span></div> 71<div class="line"><span class="preprocessor">#include <hcrypto/evp-w32.h></span></div> 72<div class="line"><span class="preprocessor">#endif</span></div> 73<div class="line"> </div> 74<div class="line"><span class="preprocessor">#include <err.h></span></div> 75<div class="line"><span class="preprocessor">#include <assert.h></span></div> 76<div class="line"> </div> 77<div class="line"><span class="comment">/* key and initial vector */</span></div> 78<div class="line"><span class="keyword">static</span> <span class="keywordtype">char</span> key[16] =</div> 79<div class="line"> <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span></div> 80<div class="line"> <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>;</div> 81<div class="line"><span class="keyword">static</span> <span class="keywordtype">char</span> ivec[16] =</div> 82<div class="line"> <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span></div> 83<div class="line"> <span class="stringliteral">"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"</span>;</div> 84<div class="line"> </div> 85<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span></div> 86<div class="line">usage(<span class="keywordtype">int</span> exit_code) __attribute__((noreturn));</div> 87<div class="line"> </div> 88<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span></div> 89<div class="line">usage(<span class="keywordtype">int</span> exit_code)</div> 90<div class="line">{</div> 91<div class="line"> printf(<span class="stringliteral">"usage: %s in out [pkcs11 | cc | w32]\n"</span>, getprogname());</div> 92<div class="line"> exit(exit_code);</div> 93<div class="line">}</div> 94<div class="line"> </div> 95<div class="line"> </div> 96<div class="line"><span class="keywordtype">int</span></div> 97<div class="line">main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div> 98<div class="line">{</div> 99<div class="line"> <span class="keywordtype">int</span> encryptp = 1;</div> 100<div class="line"> <span class="keyword">const</span> <span class="keywordtype">char</span> *ifn = NULL, *ofn = NULL;</div> 101<div class="line"> FILE *in, *out;</div> 102<div class="line"> <span class="keywordtype">void</span> *ibuf, *obuf;</div> 103<div class="line"> <span class="keywordtype">int</span> ilen, olen;</div> 104<div class="line"> <span class="keywordtype">size_t</span> block_size = 0;</div> 105<div class="line"> <span class="keyword">const</span> EVP_CIPHER *c = <a name="a0"></a><a class="code" href="group__hcrypto__evp.html#ga2747bac943db15c97167ac37fdc2af43">EVP_aes_128_cbc</a>();</div> 106<div class="line"> EVP_CIPHER_CTX ctx;</div> 107<div class="line"> <span class="keywordtype">int</span> ret;</div> 108<div class="line"> </div> 109<div class="line"> setprogname(argv[0]);</div> 110<div class="line"> </div> 111<div class="line"> <span class="keywordflow">if</span> (argc == 2) {</div> 112<div class="line"> <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--version"</span>) == 0) {</div> 113<div class="line"> printf(<span class="stringliteral">"version"</span>);</div> 114<div class="line"> exit(0);</div> 115<div class="line"> }</div> 116<div class="line"> <span class="keywordflow">if</span> (strcmp(argv[1], <span class="stringliteral">"--help"</span>) == 0)</div> 117<div class="line"> usage(0);</div> 118<div class="line"> usage(1);</div> 119<div class="line"> } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argc == 4 || argc == 5) {</div> 120<div class="line"> block_size = atoi(argv[1]);</div> 121<div class="line"> <span class="keywordflow">if</span> (block_size == 0)</div> 122<div class="line"> errx(1, <span class="stringliteral">"invalid blocksize %s"</span>, argv[1]);</div> 123<div class="line"> ifn = argv[2];</div> 124<div class="line"> ofn = argv[3];</div> 125<div class="line"> <span class="keywordflow">if</span> (argc == 5) {</div> 126<div class="line"> <span class="keywordflow">if</span> (strcmp(argv[4], <span class="stringliteral">"pkcs11"</span>) == 0)</div> 127<div class="line"> c = hc_EVP_pkcs11_aes_128_cbc();</div> 128<div class="line"><span class="preprocessor">#ifdef __APPLE__</span></div> 129<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[4], <span class="stringliteral">"cc"</span>) == 0)</div> 130<div class="line"> c = hc_EVP_cc_aes_128_cbc();</div> 131<div class="line"><span class="preprocessor">#endif</span></div> 132<div class="line"><span class="preprocessor">#ifdef _WIN32</span></div> 133<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[4], <span class="stringliteral">"w32"</span>) == 0)</div> 134<div class="line"> c = hc_EVP_w32crypto_aes_128_cbc();</div> 135<div class="line"><span class="preprocessor">#endif</span></div> 136<div class="line"> <span class="keywordflow">else</span></div> 137<div class="line"> usage(1);</div> 138<div class="line"> }</div> 139<div class="line"> } <span class="keywordflow">else</span></div> 140<div class="line"> usage(1);</div> 141<div class="line"> </div> 142<div class="line"> in = fopen(ifn, <span class="stringliteral">"r"</span>);</div> 143<div class="line"> <span class="keywordflow">if</span> (in == NULL)</div> 144<div class="line"> errx(1, <span class="stringliteral">"failed to open input file"</span>);</div> 145<div class="line"> out = fopen(ofn, <span class="stringliteral">"w+"</span>);</div> 146<div class="line"> <span class="keywordflow">if</span> (out == NULL)</div> 147<div class="line"> errx(1, <span class="stringliteral">"failed to open output file"</span>);</div> 148<div class="line"> </div> 149<div class="line"> <span class="comment">/* Check that key and ivec are long enough */</span></div> 150<div class="line"> assert(<a name="a1"></a><a class="code" href="group__hcrypto__evp.html#ga4793a9e130da86ac42c497b19395b748">EVP_CIPHER_key_length</a>(c) <= <span class="keyword">sizeof</span>(key));</div> 151<div class="line"> assert(<a name="a2"></a><a class="code" href="group__hcrypto__evp.html#ga7edebe76d19dee11686a6698d24f19f5">EVP_CIPHER_iv_length</a>(c) <= <span class="keyword">sizeof</span>(ivec));</div> 152<div class="line"> </div> 153<div class="line"> <span class="comment">/*</span></div> 154<div class="line"><span class="comment"> * Allocate buffer, the output buffer is at least</span></div> 155<div class="line"><span class="comment"> * EVP_CIPHER_block_size() longer</span></div> 156<div class="line"><span class="comment"> */</span></div> 157<div class="line"> ibuf = malloc(block_size);</div> 158<div class="line"> obuf = malloc(block_size + <a name="a3"></a><a class="code" href="group__hcrypto__evp.html#ga3123ff4b426783dd972538249b9e9d4d">EVP_CIPHER_block_size</a>(c));</div> 159<div class="line"> </div> 160<div class="line"> <span class="comment">/*</span></div> 161<div class="line"><span class="comment"> * Init the memory used for EVP_CIPHER_CTX and set the key and</span></div> 162<div class="line"><span class="comment"> * ivec.</span></div> 163<div class="line"><span class="comment"> */</span></div> 164<div class="line"> <a name="a4"></a><a class="code" href="group__hcrypto__evp.html#ga13d0ff0e87312566cb6b3591157f9754">EVP_CIPHER_CTX_init</a>(&ctx);</div> 165<div class="line"> <a name="a5"></a><a class="code" href="group__hcrypto__evp.html#ga98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex</a>(&ctx, c, NULL, key, ivec, encryptp);</div> 166<div class="line"> </div> 167<div class="line"> <span class="comment">/* read in buffer */</span></div> 168<div class="line"> <span class="keywordflow">while</span> ((ilen = fread(ibuf, 1, block_size, in)) > 0) {</div> 169<div class="line"> <span class="comment">/* encrypto/decrypt */</span></div> 170<div class="line"> ret = <a name="a6"></a><a class="code" href="group__hcrypto__evp.html#ga863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate</a>(&ctx, obuf, &olen, ibuf, ilen);</div> 171<div class="line"> <span class="keywordflow">if</span> (ret != 1) {</div> 172<div class="line"> <a name="a7"></a><a class="code" href="group__hcrypto__evp.html#gac16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&ctx);</div> 173<div class="line"> errx(1, <span class="stringliteral">"EVP_CipherUpdate failed"</span>);</div> 174<div class="line"> }</div> 175<div class="line"> <span class="comment">/* write out to output file */</span></div> 176<div class="line"> fwrite(obuf, 1, olen, out);</div> 177<div class="line"> }</div> 178<div class="line"> <span class="comment">/* done reading */</span></div> 179<div class="line"> fclose(in);</div> 180<div class="line"> </div> 181<div class="line"> <span class="comment">/* clear up any last bytes left in the output buffer */</span></div> 182<div class="line"> ret = <a name="a8"></a><a class="code" href="group__hcrypto__evp.html#ga714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex</a>(&ctx, obuf, &olen);</div> 183<div class="line"> <a class="code" href="group__hcrypto__evp.html#gac16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a>(&ctx);</div> 184<div class="line"> <span class="keywordflow">if</span> (ret != 1)</div> 185<div class="line"> errx(1, <span class="stringliteral">"EVP_CipherFinal_ex failed"</span>);</div> 186<div class="line"> </div> 187<div class="line"> <span class="comment">/* write the last bytes out and close */</span></div> 188<div class="line"> fwrite(obuf, 1, olen, out);</div> 189<div class="line"> fclose(out);</div> 190<div class="line"> </div> 191<div class="line"> <span class="keywordflow">return</span> 0;</div> 192<div class="line">}</div> 193<div class="ttc" id="agroup__hcrypto__evp_html_ga13d0ff0e87312566cb6b3591157f9754"><div class="ttname"><a href="group__hcrypto__evp.html#ga13d0ff0e87312566cb6b3591157f9754">EVP_CIPHER_CTX_init</a></div><div class="ttdeci">void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)</div><div class="ttdef"><b>Definition:</b> evp.c:570</div></div> 194<div class="ttc" id="agroup__hcrypto__evp_html_ga2747bac943db15c97167ac37fdc2af43"><div class="ttname"><a href="group__hcrypto__evp.html#ga2747bac943db15c97167ac37fdc2af43">EVP_aes_128_cbc</a></div><div class="ttdeci">const EVP_CIPHER * EVP_aes_128_cbc(void)</div><div class="ttdef"><b>Definition:</b> evp.c:1153</div></div> 195<div class="ttc" id="agroup__hcrypto__evp_html_ga3123ff4b426783dd972538249b9e9d4d"><div class="ttname"><a href="group__hcrypto__evp.html#ga3123ff4b426783dd972538249b9e9d4d">EVP_CIPHER_block_size</a></div><div class="ttdeci">size_t EVP_CIPHER_block_size(const EVP_CIPHER *c)</div><div class="ttdef"><b>Definition:</b> evp.c:523</div></div> 196<div class="ttc" id="agroup__hcrypto__evp_html_ga4793a9e130da86ac42c497b19395b748"><div class="ttname"><a href="group__hcrypto__evp.html#ga4793a9e130da86ac42c497b19395b748">EVP_CIPHER_key_length</a></div><div class="ttdeci">size_t EVP_CIPHER_key_length(const EVP_CIPHER *c)</div><div class="ttdef"><b>Definition:</b> evp.c:539</div></div> 197<div class="ttc" id="agroup__hcrypto__evp_html_ga714eef7d737fd68171d852043a4995de"><div class="ttname"><a href="group__hcrypto__evp.html#ga714eef7d737fd68171d852043a4995de">EVP_CipherFinal_ex</a></div><div class="ttdeci">int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)</div><div class="ttdef"><b>Definition:</b> evp.c:939</div></div> 198<div class="ttc" id="agroup__hcrypto__evp_html_ga7edebe76d19dee11686a6698d24f19f5"><div class="ttname"><a href="group__hcrypto__evp.html#ga7edebe76d19dee11686a6698d24f19f5">EVP_CIPHER_iv_length</a></div><div class="ttdeci">size_t EVP_CIPHER_iv_length(const EVP_CIPHER *c)</div><div class="ttdef"><b>Definition:</b> evp.c:555</div></div> 199<div class="ttc" id="agroup__hcrypto__evp_html_ga863349e1001b36cfd6c4afedddd12862"><div class="ttname"><a href="group__hcrypto__evp.html#ga863349e1001b36cfd6c4afedddd12862">EVP_CipherUpdate</a></div><div class="ttdeci">int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen, void *in, size_t inlen)</div><div class="ttdef"><b>Definition:</b> evp.c:853</div></div> 200<div class="ttc" id="agroup__hcrypto__evp_html_ga98da5a5c1aa25d9cb2e4717fa11314bd"><div class="ttname"><a href="group__hcrypto__evp.html#ga98da5a5c1aa25d9cb2e4717fa11314bd">EVP_CipherInit_ex</a></div><div class="ttdeci">int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine, const void *key, const void *iv, int encp)</div><div class="ttdef"><b>Definition:</b> evp.c:775</div></div> 201<div class="ttc" id="agroup__hcrypto__evp_html_gac16dd3987cd90c8bcdbad5290c558359"><div class="ttname"><a href="group__hcrypto__evp.html#gac16dd3987cd90c8bcdbad5290c558359">EVP_CIPHER_CTX_cleanup</a></div><div class="ttdeci">int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)</div><div class="ttdef"><b>Definition:</b> evp.c:586</div></div> 202</div><!-- fragment --> </div><!-- contents --> 203<hr size="1"><address style="text-align: right;"><small> 204Generated on Tue Nov 15 2022 14:04:26 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.9.1</small></address> 205</body> 206</html> 207