source: heimdal/trunk/lib/hcrypto/example_evp_cipher.c

Last change on this file was 1, checked in by Paul Smedley, 10 years ago

Initial commit of Heimdal 1.5.3

File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <krb5-types.h> /* should really be stdint.h */
35#include <hcrypto/evp.h>
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <err.h>
41#include <assert.h>
42
43#include "roken.h"
44
45/* key and initial vector */
46static char key[16] =
47 "\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"
48 "\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4";
49static char ivec[16] =
50 "\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"
51 "\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4";
52
53static void
54usage(int exit_code) __attribute__((noreturn));
55
56static void
57usage(int exit_code)
58{
59 printf("usage: %s in out\n", getprogname());
60 exit(exit_code);
61}
62
63
64int
65main(int argc, char **argv)
66{
67 int encryptp = 1;
68 const char *ifn = NULL, *ofn = NULL;
69 FILE *in, *out;
70 void *ibuf, *obuf;
71 int ilen, olen;
72 size_t block_size = 0;
73 const EVP_CIPHER *c = EVP_aes_128_cbc();
74 EVP_CIPHER_CTX ctx;
75 int ret;
76
77 setprogname(argv[0]);
78
79 if (argc == 2) {
80 if (strcmp(argv[1], "--version") == 0) {
81 printf("version");
82 exit(0);
83 }
84 if (strcmp(argv[1], "--help") == 0)
85 usage(0);
86 usage(1);
87 } else if (argc == 4) {
88 block_size = atoi(argv[1]);
89 if (block_size == 0)
90 errx(1, "invalid blocksize %s", argv[1]);
91 ifn = argv[2];
92 ofn = argv[3];
93 } else
94 usage(1);
95
96 in = fopen(ifn, "r");
97 if (in == NULL)
98 errx(1, "failed to open input file");
99 out = fopen(ofn, "w+");
100 if (out == NULL)
101 errx(1, "failed to open output file");
102
103 /* Check that key and ivec are long enough */
104 assert(EVP_CIPHER_key_length(c) <= sizeof(key));
105 assert(EVP_CIPHER_iv_length(c) <= sizeof(ivec));
106
107 /*
108 * Allocate buffer, the output buffer is at least
109 * EVP_CIPHER_block_size() longer
110 */
111 ibuf = malloc(block_size);
112 obuf = malloc(block_size + EVP_CIPHER_block_size(c));
113
114 /*
115 * Init the memory used for EVP_CIPHER_CTX and set the key and
116 * ivec.
117 */
118 EVP_CIPHER_CTX_init(&ctx);
119 EVP_CipherInit_ex(&ctx, c, NULL, key, ivec, encryptp);
120
121 /* read in buffer */
122 while ((ilen = fread(ibuf, 1, block_size, in)) > 0) {
123 /* encrypto/decrypt */
124 ret = EVP_CipherUpdate(&ctx, obuf, &olen, ibuf, ilen);
125 if (ret != 1) {
126 EVP_CIPHER_CTX_cleanup(&ctx);
127 errx(1, "EVP_CipherUpdate failed");
128 }
129 /* write out to output file */
130 fwrite(obuf, 1, olen, out);
131 }
132 /* done reading */
133 fclose(in);
134
135 /* clear up any last bytes left in the output buffer */
136 ret = EVP_CipherFinal_ex(&ctx, obuf, &olen);
137 EVP_CIPHER_CTX_cleanup(&ctx);
138 if (ret != 1)
139 errx(1, "EVP_CipherFinal_ex failed");
140
141 /* write the last bytes out and close */
142 fwrite(obuf, 1, olen, out);
143 fclose(out);
144
145 return 0;
146}
Note: See TracBrowser for help on using the repository browser.