| 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 <config.h> |
| 35 |
#include <roken.h> |
| 36 |
|
| 37 |
#include <krb5-types.h> /* should really be stdint.h */ |
| 38 |
#include <hcrypto/evp.h> |
| 39 |
#include <hcrypto/evp-pkcs11.h> |
| 40 |
#ifdef __APPLE__ |
| 41 |
#include <hcrypto/evp-cc.h> |
| 42 |
#endif |
| 43 |
#ifdef _WIN32 |
| 44 |
#include <hcrypto/evp-w32.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include <err.h> |
| 48 |
#include <assert.h> |
| 49 |
|
| 50 |
/* key and initial vector */ |
| 51 |
static char key[16] = |
| 52 |
"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4" |
| 53 |
"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"; |
| 54 |
static char ivec[16] = |
| 55 |
"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4" |
| 56 |
"\xaa\xbb\x45\xd4\xaa\xbb\x45\xd4"; |
| 57 |
|
| 58 |
static void |
| 59 |
usage(int exit_code) __attribute__((noreturn)); |
| 60 |
|
| 61 |
static void |
| 62 |
usage(int exit_code) |
| 63 |
{ |
| 64 |
printf("usage: %s in out [pkcs11 | cc | w32]\n", getprogname()); |
| 65 |
exit(exit_code); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
int |
| 70 |
main(int argc, char **argv) |
| 71 |
{ |
| 72 |
int encryptp = 1; |
| 73 |
const char *ifn = NULL, *ofn = NULL; |
| 74 |
FILE *in, *out; |
| 75 |
void *ibuf, *obuf; |
| 76 |
int ilen, olen; |
| 77 |
size_t block_size = 0; |
| 78 |
const EVP_CIPHER *c = EVP_aes_128_cbc(); |
| 79 |
EVP_CIPHER_CTX ctx; |
| 80 |
int ret; |
| 81 |
|
| 82 |
setprogname(argv[0]); |
| 83 |
|
| 84 |
if (argc == 2) { |
| 85 |
if (strcmp(argv[1], "--version") == 0) { |
| 86 |
printf("version"); |
| 87 |
exit(0); |
| 88 |
} |
| 89 |
if (strcmp(argv[1], "--help") == 0) |
| 90 |
usage(0); |
| 91 |
usage(1); |
| 92 |
} else if (argc == 4 || argc == 5) { |
| 93 |
block_size = atoi(argv[1]); |
| 94 |
if (block_size == 0) |
| 95 |
errx(1, "invalid blocksize %s", argv[1]); |
| 96 |
ifn = argv[2]; |
| 97 |
ofn = argv[3]; |
| 98 |
if (argc == 5) { |
| 99 |
if (strcmp(argv[4], "pkcs11") == 0) |
| 100 |
c = hc_EVP_pkcs11_aes_128_cbc(); |
| 101 |
#ifdef __APPLE__ |
| 102 |
else if (strcmp(argv[4], "cc") == 0) |
| 103 |
c = hc_EVP_cc_aes_128_cbc(); |
| 104 |
#endif |
| 105 |
#ifdef _WIN32 |
| 106 |
else if (strcmp(argv[4], "w32") == 0) |
| 107 |
c = hc_EVP_w32crypto_aes_128_cbc(); |
| 108 |
#endif |
| 109 |
else |
| 110 |
usage(1); |
| 111 |
} |
| 112 |
} else |
| 113 |
usage(1); |
| 114 |
|
| 115 |
in = fopen(ifn, "r"); |
| 116 |
if (in == NULL) |
| 117 |
errx(1, "failed to open input file"); |
| 118 |
out = fopen(ofn, "w+"); |
| 119 |
if (out == NULL) |
| 120 |
errx(1, "failed to open output file"); |
| 121 |
|
| 122 |
/* Check that key and ivec are long enough */ |
| 123 |
assert(EVP_CIPHER_key_length(c) <= sizeof(key)); |
| 124 |
assert(EVP_CIPHER_iv_length(c) <= sizeof(ivec)); |
| 125 |
|
| 126 |
/* |
| 127 |
* Allocate buffer, the output buffer is at least |
| 128 |
* EVP_CIPHER_block_size() longer |
| 129 |
*/ |
| 130 |
ibuf = malloc(block_size); |
| 131 |
obuf = malloc(block_size + EVP_CIPHER_block_size(c)); |
| 132 |
|
| 133 |
/* |
| 134 |
* Init the memory used for EVP_CIPHER_CTX and set the key and |
| 135 |
* ivec. |
| 136 |
*/ |
| 137 |
EVP_CIPHER_CTX_init(&ctx); |
| 138 |
EVP_CipherInit_ex(&ctx, c, NULL, key, ivec, encryptp); |
| 139 |
|
| 140 |
/* read in buffer */ |
| 141 |
while ((ilen = fread(ibuf, 1, block_size, in)) > 0) { |
| 142 |
/* encrypto/decrypt */ |
| 143 |
ret = EVP_CipherUpdate(&ctx, obuf, &olen, ibuf, ilen); |
| 144 |
if (ret != 1) { |
| 145 |
EVP_CIPHER_CTX_cleanup(&ctx); |
| 146 |
errx(1, "EVP_CipherUpdate failed"); |
| 147 |
} |
| 148 |
/* write out to output file */ |
| 149 |
fwrite(obuf, 1, olen, out); |
| 150 |
} |
| 151 |
/* done reading */ |
| 152 |
fclose(in); |
| 153 |
|
| 154 |
/* clear up any last bytes left in the output buffer */ |
| 155 |
ret = EVP_CipherFinal_ex(&ctx, obuf, &olen); |
| 156 |
EVP_CIPHER_CTX_cleanup(&ctx); |
| 157 |
if (ret != 1) |
| 158 |
errx(1, "EVP_CipherFinal_ex failed"); |
| 159 |
|
| 160 |
/* write the last bytes out and close */ |
| 161 |
fwrite(obuf, 1, olen, out); |
| 162 |
fclose(out); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |