1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (C) 2023 Eneas Ulir de Queiroz
13 static void check_enc_dec(const int enc
)
17 fprintf(stderr
, "Error: both -d and -e were specified.\n");
21 static void show_usage(const char* name
)
23 fprintf(stderr
, "Usage: %s: [-d | -e] [-n] -k key [-i iv] [-c cipher]\n"
24 "-d = decrypt; -e = encrypt; -n = no padding\n", name
);
27 static void uencrypt_clear_free(void *ptr
, size_t len
)
35 int main(int argc
, char *argv
[])
38 unsigned char *iv
= NULL
;
39 unsigned char *key
= NULL
;
40 long keylen
= 0, ivlen
= 0;
43 const cipher_t
*cipher
= get_default_cipher();
45 int ret
= EXIT_FAILURE
;
47 while ((opt
= getopt(argc
, argv
, "c:dei:k:n")) != -1) {
50 if (!(cipher
= get_cipher_or_print_error(optarg
)))
62 iv
= hexstr2buf(optarg
, &ivlen
);
64 fprintf(stderr
, "Error setting IV to %s. The IV should be encoded in hex.\n",
68 memset(optarg
, '*', strlen(optarg
));
71 key
= hexstr2buf(optarg
, &keylen
);
73 fprintf(stderr
, "Error setting key to %s. The key should be encoded in hex.\n",
77 memset(optarg
, '*', strlen(optarg
));
87 if (ivlen
!= get_cipher_ivsize(cipher
)) {
88 fprintf(stderr
, "Error: IV must be %d bytes; given IV is %zd bytes.\n",
89 get_cipher_ivsize(cipher
), ivlen
);
92 if (keylen
!= get_cipher_keysize(cipher
)) {
93 fprintf(stderr
, "Error: key must be %d bytes; given key is %zd bytes.\n",
94 get_cipher_keysize(cipher
), keylen
);
97 ctx
= create_ctx(cipher
, key
, iv
, !!enc
, padding
);
99 ret
= do_crypt(stdin
, stdout
, ctx
);
102 uencrypt_clear_free(iv
, ivlen
);
103 uencrypt_clear_free(key
, keylen
);