uencrypt: Fix compile warnings
[openwrt/staging/nbd.git] / package / utils / uencrypt / src / uencrypt.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (C) 2023 Eneas Ulir de Queiroz
3 */
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include "uencrypt.h"
12
13 static void check_enc_dec(const int enc)
14 {
15 if (enc == -1)
16 return;
17 fprintf(stderr, "Error: both -d and -e were specified.\n");
18 exit(EXIT_FAILURE);
19 }
20
21 static void show_usage(const char* name)
22 {
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);
25 }
26
27 static void uencrypt_clear_free(void *ptr, size_t len)
28 {
29 if (ptr) {
30 memset(ptr, 0, len);
31 free(ptr);
32 }
33 }
34
35 int main(int argc, char *argv[])
36 {
37 int enc = -1;
38 unsigned char *iv = NULL;
39 unsigned char *key = NULL;
40 long keylen = 0, ivlen = 0;
41 int opt;
42 int padding = 1;
43 const cipher_t *cipher = get_default_cipher();
44 ctx_t* ctx;
45 int ret = EXIT_FAILURE;
46
47 while ((opt = getopt(argc, argv, "c:dei:k:n")) != -1) {
48 switch (opt) {
49 case 'c':
50 if (!(cipher = get_cipher_or_print_error(optarg)))
51 exit(EXIT_FAILURE);
52 break;
53 case 'd':
54 check_enc_dec(enc);
55 enc = 0;
56 break;
57 case 'e':
58 check_enc_dec(enc);
59 enc = 1;
60 break;
61 case 'i':
62 iv = hexstr2buf(optarg, &ivlen);
63 if (iv == NULL) {
64 fprintf(stderr, "Error setting IV to %s. The IV should be encoded in hex.\n",
65 optarg);
66 exit(EINVAL);
67 }
68 memset(optarg, '*', strlen(optarg));
69 break;
70 case 'k':
71 key = hexstr2buf(optarg, &keylen);
72 if (key == NULL) {
73 fprintf(stderr, "Error setting key to %s. The key should be encoded in hex.\n",
74 optarg);
75 exit(EINVAL);
76 }
77 memset(optarg, '*', strlen(optarg));
78 break;
79 case 'n':
80 padding = 0;
81 break;
82 default:
83 show_usage(argv[0]);
84 exit(EINVAL);
85 }
86 }
87 if (ivlen != get_cipher_ivsize(cipher)) {
88 fprintf(stderr, "Error: IV must be %d bytes; given IV is %ld bytes.\n",
89 get_cipher_ivsize(cipher), ivlen);
90 exit(EXIT_FAILURE);
91 }
92 if (keylen != get_cipher_keysize(cipher)) {
93 fprintf(stderr, "Error: key must be %d bytes; given key is %ld bytes.\n",
94 get_cipher_keysize(cipher), keylen);
95 exit(EXIT_FAILURE);
96 }
97 ctx = create_ctx(cipher, key, iv, !!enc, padding);
98 if (ctx) {
99 ret = do_crypt(stdin, stdout, ctx);
100 free_ctx(ctx);
101 }
102 uencrypt_clear_free(iv, ivlen);
103 uencrypt_clear_free(key, keylen);
104 return ret;
105 }