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