1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (C) 2022 Eneas Ulir de Queiroz
11 # include <wolfssl/options.h>
12 # include <wolfssl/openssl/evp.h>
14 # include <openssl/evp.h>
17 int do_crypt(FILE *infile
, FILE *outfile
, const char *key
, const char *iv
,
21 unsigned char inbuf
[1024], outbuf
[1024 + EVP_MAX_BLOCK_LENGTH
];
24 ctx
= EVP_CIPHER_CTX_new();
25 EVP_CipherInit_ex(ctx
, EVP_aes_128_cbc(), NULL
, key
, iv
, enc
);
26 EVP_CIPHER_CTX_set_padding(ctx
, padding
);
29 inlen
= fread(inbuf
, 1, 1024, infile
);
32 if (!EVP_CipherUpdate(ctx
, outbuf
, &outlen
, inbuf
, inlen
)) {
33 EVP_CIPHER_CTX_free(ctx
);
36 fwrite(outbuf
, 1, outlen
, outfile
);
38 if (!EVP_CipherFinal_ex(ctx
, outbuf
, &outlen
)) {
39 EVP_CIPHER_CTX_free(ctx
);
42 fwrite(outbuf
, 1, outlen
, outfile
);
44 EVP_CIPHER_CTX_free(ctx
);
48 static void check_enc_dec(const int enc
)
52 fprintf(stderr
, "Error: both -d and -e were specified.\n");
56 static void show_usage(const char* name
)
58 fprintf(stderr
, "Usage: %s: [-d | -e] [-n] -k key -i iv\n"
59 "-d = decrypt; -e = encrypt; -n = no padding\n", name
);
62 int main(int argc
, char *argv
[])
65 unsigned char *iv
= NULL
;
66 unsigned char *key
= NULL
;
72 while ((opt
= getopt(argc
, argv
, "dei:k:n")) != -1) {
83 iv
= OPENSSL_hexstr2buf((const char *)optarg
, &len
);
84 if (iv
== NULL
|| len
!= 16) {
85 fprintf(stderr
, "Error setting IV to %s. The IV should be 16 bytes, encoded in hex.\n",
91 key
= OPENSSL_hexstr2buf((const char *)optarg
, &len
);
92 if (key
== NULL
|| len
!= 16) {
93 fprintf(stderr
, "Error setting key to %s. The key should be 16 bytes, encoded in hex.\n",
106 if (iv
== NULL
|| key
== NULL
) {
107 fprintf(stderr
, "Error: %s not set.\n", key
? "iv" : (iv
? "key" : "key and iv"));
111 ret
= do_crypt(stdin
, stdout
, key
, iv
, !!enc
, padding
);
113 fprintf(stderr
, "Error during crypt operation.\n");