1 /* SPDX-License-Identifier: GPL-2.0-or-later
2 * Copyright (C) 2023 Eneas Ulir de Queiroz
12 unsigned char *hexstr2buf(const char *str
, long *len
)
15 long inlen
= strlen(str
);
23 for (long x
= 0; x
< *len
; x
++)
24 sscanf(str
+ x
* 2, "%2hhx", buf
+ x
);
28 const cipher_t
*get_default_cipher(void)
30 return mbedtls_cipher_info_from_type (MBEDTLS_CIPHER_AES_128_CBC
);
33 static char* upperstr(char *str
) {
34 for (char *s
= str
; *s
; s
++)
35 *s
= toupper((unsigned char) *s
);
39 const cipher_t
*get_cipher_or_print_error(char *name
)
41 const mbedtls_cipher_info_t
*cipher
;
43 cipher
= mbedtls_cipher_info_from_string(upperstr(name
));
47 fprintf(stderr
, "Error: invalid cipher: %s.\n", name
);
48 fprintf(stderr
, "Supported ciphers: \n");
49 for (const int *list
= mbedtls_cipher_list(); *list
; list
++) {
50 cipher
= mbedtls_cipher_info_from_type(*list
);
53 fprintf(stderr
, "\t%s\n", cipher
->name
);
58 int get_cipher_ivsize(const cipher_t
*cipher
)
60 const mbedtls_cipher_info_t
*c
= cipher
;
65 int get_cipher_keysize(const cipher_t
*cipher
)
67 const mbedtls_cipher_info_t
*c
= cipher
;
69 return c
->key_bitlen
>> 3;
72 ctx_t
*create_ctx(const cipher_t
*cipher
, const unsigned char *key
,
73 const unsigned char *iv
, int enc
, int padding
)
75 mbedtls_cipher_context_t
*ctx
;
76 const mbedtls_cipher_info_t
*cipher_info
=cipher
;
79 ctx
= malloc(sizeof (mbedtls_cipher_context_t
));
81 fprintf (stderr
, "Error: create_ctx: out of memory.\n");
85 mbedtls_cipher_init(ctx
);
86 ret
= mbedtls_cipher_setup(ctx
, cipher_info
);
88 fprintf(stderr
, "Error: mbedtls_cipher_setup: %d\n", ret
);
91 ret
= mbedtls_cipher_setkey(ctx
, key
,
92 (int) mbedtls_cipher_get_key_bitlen(ctx
),
93 enc
? MBEDTLS_ENCRYPT
: MBEDTLS_DECRYPT
);
95 fprintf(stderr
, "Error: mbedtls_cipher_setkey: %d\n", ret
);
99 ret
= mbedtls_cipher_set_iv(ctx
, iv
, mbedtls_cipher_get_iv_size(ctx
));
101 fprintf(stderr
, "Error: mbedtls_cipher_set_iv: %d\n", ret
);
106 if (cipher_info
->mode
== MBEDTLS_MODE_CBC
) {
107 ret
= mbedtls_cipher_set_padding_mode(ctx
, padding
?
108 MBEDTLS_PADDING_PKCS7
:
109 MBEDTLS_PADDING_NONE
);
111 fprintf(stderr
, "Error: mbedtls_cipher_set_padding_mode: %d\n",
116 if (cipher_info
->block_size
> 1 && padding
) {
118 "Error: mbedTLS only allows padding with CBC ciphers.\n");
123 ret
= mbedtls_cipher_reset(ctx
);
125 fprintf(stderr
, "Error: mbedtls_cipher_reset: %d\n", ret
);
135 int do_crypt(FILE *infile
, FILE *outfile
, ctx_t
*ctx
)
137 unsigned char inbuf
[CRYPT_BUF_SIZE
];
138 unsigned char outbuf
[CRYPT_BUF_SIZE
+ MBEDTLS_MAX_BLOCK_LENGTH
];
139 size_t inlen
, outlen
, step
;
142 if (mbedtls_cipher_get_cipher_mode(ctx
) == MBEDTLS_MODE_ECB
) {
143 step
= mbedtls_cipher_get_block_size(ctx
);
144 if (step
> CRYPT_BUF_SIZE
) {
145 step
= CRYPT_BUF_SIZE
;
148 step
= CRYPT_BUF_SIZE
;
152 inlen
= fread(inbuf
, 1, step
, infile
);
155 ret
= mbedtls_cipher_update(ctx
, inbuf
, inlen
, outbuf
, &outlen
);
157 fprintf(stderr
, "Error: mbedtls_cipher_update: %d\n", ret
);
160 ret
= fwrite(outbuf
, 1, outlen
, outfile
);
162 fprintf(stderr
, "Error: cipher_update short write.\n");
166 ret
= mbedtls_cipher_finish(ctx
, outbuf
, &outlen
);
168 fprintf(stderr
, "Error: mbedtls_cipher_finish: %d\n", ret
);
171 ret
= fwrite(outbuf
, 1, outlen
, outfile
);
173 fprintf(stderr
, "Error: cipher_finish short write.\n");
180 void free_ctx(ctx_t
*ctx
)
183 mbedtls_cipher_free(ctx
);