2011-01-21 10:32:12 +01:00
|
|
|
/*
|
|
|
|
* \brief Generic file encryption program using generic wrappers for configured
|
|
|
|
* security.
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-01-21 10:32:12 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-01-21 10:32:12 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-01-21 10:32:12 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2011-01-21 10:32:12 +01:00
|
|
|
*/
|
|
|
|
|
2017-12-05 13:08:15 +01:00
|
|
|
/* Enable definition of fileno() even when compiling with -std=c99. Must be
|
2021-05-28 09:42:25 +02:00
|
|
|
* set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
|
2017-12-05 13:08:15 +01:00
|
|
|
* Harmless on other platforms. */
|
2020-06-11 13:03:45 +02:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2017-12-05 13:08:15 +01:00
|
|
|
|
2021-05-27 11:25:03 +02:00
|
|
|
#include "mbedtls/build_info.h"
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2015-02-11 15:06:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_FS_IO)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/cipher.h"
|
|
|
|
#include "mbedtls/md.h"
|
2018-10-12 17:46:37 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2015-02-11 15:06:19 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-01-19 15:26:37 +01:00
|
|
|
#endif
|
|
|
|
|
2011-04-24 17:30:07 +02:00
|
|
|
#if defined(_WIN32)
|
2011-01-21 10:32:12 +01:00
|
|
|
#include <windows.h>
|
2011-11-18 15:26:47 +01:00
|
|
|
#if !defined(_WIN32_WCE)
|
2011-01-21 10:32:12 +01:00
|
|
|
#include <io.h>
|
2011-11-18 15:26:47 +01:00
|
|
|
#endif
|
2011-01-21 10:32:12 +01:00
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MODE_ENCRYPT 0
|
|
|
|
#define MODE_DECRYPT 1
|
|
|
|
|
|
|
|
#define USAGE \
|
2015-04-08 12:49:31 +02:00
|
|
|
"\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
|
2011-01-21 10:32:12 +01:00
|
|
|
"\n <mode>: 0 = encrypt, 1 = decrypt\n" \
|
|
|
|
"\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
|
|
|
|
"\n"
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
|
|
|
|
!defined(MBEDTLS_FS_IO)
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(void)
|
2011-05-26 15:16:06 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(0);
|
2011-05-26 15:16:06 +02:00
|
|
|
}
|
|
|
|
#else
|
2018-12-06 18:43:31 +01:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2011-01-21 10:32:12 +01:00
|
|
|
{
|
2020-04-14 19:34:19 +02:00
|
|
|
int ret = 1, i;
|
|
|
|
unsigned n;
|
2018-04-29 20:11:26 +02:00
|
|
|
int exit_code = MBEDTLS_EXIT_FAILURE;
|
2016-09-02 22:44:09 +02:00
|
|
|
int mode;
|
2011-05-26 16:02:58 +02:00
|
|
|
size_t keylen, ilen, olen;
|
2011-01-21 10:32:12 +01:00
|
|
|
FILE *fkey, *fin = NULL, *fout = NULL;
|
|
|
|
|
|
|
|
char *p;
|
|
|
|
unsigned char IV[16];
|
|
|
|
unsigned char key[512];
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char digest[MBEDTLS_MD_MAX_SIZE];
|
2011-01-21 10:32:12 +01:00
|
|
|
unsigned char buffer[1024];
|
|
|
|
unsigned char output[1024];
|
2013-10-31 14:22:08 +01:00
|
|
|
unsigned char diff;
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_cipher_context_t cipher_ctx;
|
|
|
|
mbedtls_md_context_t md_ctx;
|
2023-06-12 15:53:02 +02:00
|
|
|
mbedtls_cipher_mode_t cipher_mode;
|
|
|
|
unsigned int cipher_block_size;
|
|
|
|
unsigned char md_size;
|
2011-11-18 15:26:47 +01:00
|
|
|
#if defined(_WIN32_WCE)
|
|
|
|
long filesize, offset;
|
|
|
|
#elif defined(_WIN32)
|
2023-01-11 14:50:10 +01:00
|
|
|
LARGE_INTEGER li_size;
|
2011-01-21 10:32:12 +01:00
|
|
|
__int64 filesize, offset;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
off_t filesize, offset;
|
2011-01-21 10:32:12 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_init(&cipher_ctx);
|
|
|
|
mbedtls_md_init(&md_ctx);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the command-line arguments.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (argc != 7) {
|
2011-01-21 10:32:12 +01:00
|
|
|
const int *list;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf(USAGE);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("Available ciphers:\n");
|
2015-04-08 12:49:31 +02:00
|
|
|
list = mbedtls_cipher_list();
|
2023-01-11 14:50:10 +01:00
|
|
|
while (*list) {
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type(*list);
|
|
|
|
mbedtls_printf(" %s\n", mbedtls_cipher_info_get_name(cipher_info));
|
2011-01-21 10:32:12 +01:00
|
|
|
list++;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("\nAvailable message digests:\n");
|
2015-04-08 12:49:31 +02:00
|
|
|
list = mbedtls_md_list();
|
2023-01-11 14:50:10 +01:00
|
|
|
while (*list) {
|
|
|
|
md_info = mbedtls_md_info_from_type(*list);
|
|
|
|
mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
|
2011-01-21 10:32:12 +01:00
|
|
|
list++;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mode = atoi(argv[1]);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
|
|
|
|
mbedtls_fprintf(stderr, "invalid operation mode\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (strcmp(argv[2], argv[3]) == 0) {
|
|
|
|
mbedtls_fprintf(stderr, "input and output filenames must differ\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((fin = fopen(argv[2], "rb")) == NULL) {
|
|
|
|
mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((fout = fopen(argv[3], "wb+")) == NULL) {
|
|
|
|
mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2022-06-30 17:06:11 +02:00
|
|
|
/* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_setbuf(fin, NULL);
|
|
|
|
mbedtls_setbuf(fout, NULL);
|
2022-06-30 17:06:11 +02:00
|
|
|
|
2011-01-21 10:32:12 +01:00
|
|
|
/*
|
|
|
|
* Read the Cipher and MD from the command line
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_string(argv[4]);
|
|
|
|
if (cipher_info == NULL) {
|
|
|
|
mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
|
2014-04-17 16:00:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
md_info = mbedtls_md_info_from_string(argv[5]);
|
|
|
|
if (md_info == NULL) {
|
|
|
|
mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2016-06-03 16:40:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
|
2016-06-03 16:40:57 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
2017-06-27 12:36:21 +02:00
|
|
|
* Read the secret key from file or command line
|
2011-01-21 10:32:12 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((fkey = fopen(argv[6], "rb")) != NULL) {
|
|
|
|
keylen = fread(key, 1, sizeof(key), fkey);
|
|
|
|
fclose(fkey);
|
|
|
|
} else {
|
|
|
|
if (memcmp(argv[6], "hex:", 4) == 0) {
|
2011-01-21 10:32:12 +01:00
|
|
|
p = &argv[6][4];
|
|
|
|
keylen = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
|
|
|
|
keylen < (int) sizeof(key)) {
|
2011-01-21 10:32:12 +01:00
|
|
|
key[keylen++] = (unsigned char) n;
|
|
|
|
p += 2;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
keylen = strlen(argv[6]);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (keylen > (int) sizeof(key)) {
|
|
|
|
keylen = (int) sizeof(key);
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(key, argv[6], keylen);
|
2011-01-21 10:32:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-18 15:26:47 +01:00
|
|
|
#if defined(_WIN32_WCE)
|
2023-01-11 14:50:10 +01:00
|
|
|
filesize = fseek(fin, 0L, SEEK_END);
|
2011-11-18 15:26:47 +01:00
|
|
|
#else
|
|
|
|
#if defined(_WIN32)
|
2011-01-21 10:32:12 +01:00
|
|
|
/*
|
|
|
|
* Support large files (> 2Gb) on Win32
|
|
|
|
*/
|
|
|
|
li_size.QuadPart = 0;
|
|
|
|
li_size.LowPart =
|
2023-01-11 14:50:10 +01:00
|
|
|
SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
|
|
|
|
li_size.LowPart, &li_size.HighPart, FILE_END);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
|
|
|
|
mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
filesize = li_size.QuadPart;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
|
|
|
|
perror("lseek");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif
|
2011-11-18 15:26:47 +01:00
|
|
|
#endif
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fseek(fin, 0, SEEK_SET) < 0) {
|
|
|
|
mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:53:02 +02:00
|
|
|
md_size = mbedtls_md_get_size(md_info);
|
|
|
|
cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mode == MODE_ENCRYPT) {
|
2011-01-21 10:32:12 +01:00
|
|
|
/*
|
|
|
|
* Generate the initialization vector as:
|
2016-09-02 22:44:09 +02:00
|
|
|
* IV = MD( filesize || filename )[0..15]
|
2011-01-21 10:32:12 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
buffer[i] = (unsigned char) (filesize >> (i << 3));
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
p = argv[2];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_starts(&md_ctx) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
|
|
|
|
!= 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_finish(&md_ctx, digest) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(IV, digest, 16);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Append the IV at the beginning of the output.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(IV, 1, 16, fout) != 16) {
|
|
|
|
mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash the IV and the secret key together 8192 times
|
|
|
|
* using the result to setup the AES context and HMAC.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(digest, 0, 32);
|
|
|
|
memcpy(digest, IV, 16);
|
|
|
|
|
|
|
|
for (i = 0; i < 8192; i++) {
|
|
|
|
if (mbedtls_md_starts(&md_ctx) != 0) {
|
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"mbedtls_md_starts() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
|
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"mbedtls_md_update() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"mbedtls_md_update() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_finish(&md_ctx, digest) != 0) {
|
|
|
|
mbedtls_fprintf(stderr,
|
|
|
|
"mbedtls_md_finish() returned error\n");
|
2021-12-09 18:18:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_setkey(&cipher_ctx,
|
|
|
|
digest,
|
|
|
|
(int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
|
|
|
|
MBEDTLS_ENCRYPT) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
|
2012-07-04 19:08:33 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
|
2013-09-03 13:04:44 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
|
2012-07-04 19:08:33 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Encrypt and write the ciphertext.
|
|
|
|
*/
|
2023-06-12 15:53:02 +02:00
|
|
|
for (offset = 0; offset < filesize; offset += cipher_block_size) {
|
|
|
|
ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
|
|
|
|
cipher_block_size : (unsigned int) (filesize - offset);
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
if (fread(buffer, 1, ilen, fin) != ilen) {
|
|
|
|
mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
|
2014-04-17 16:00:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(output, 1, olen, fout) != olen) {
|
|
|
|
mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
|
2012-07-04 19:08:33 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(output, 1, olen, fout) != olen) {
|
|
|
|
mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2012-07-04 19:08:33 +02:00
|
|
|
|
2011-01-21 10:32:12 +01:00
|
|
|
/*
|
|
|
|
* Finally write the HMAC.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-06-12 15:53:02 +02:00
|
|
|
if (fwrite(digest, 1, md_size, fout) != md_size) {
|
|
|
|
mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mode == MODE_DECRYPT) {
|
2011-01-21 10:32:12 +01:00
|
|
|
/*
|
|
|
|
* The encrypted file must be structured as follows:
|
|
|
|
*
|
|
|
|
* 00 .. 15 Initialization Vector
|
2016-09-02 22:44:09 +02:00
|
|
|
* 16 .. 31 Encrypted Block #1
|
2011-01-21 10:32:12 +01:00
|
|
|
* ..
|
2016-09-02 22:44:09 +02:00
|
|
|
* N*16 .. (N+1)*16 - 1 Encrypted Block #N
|
|
|
|
* (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
|
2011-01-21 10:32:12 +01:00
|
|
|
*/
|
2023-06-12 15:53:02 +02:00
|
|
|
if (filesize < 16 + md_size) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-06-12 15:53:02 +02:00
|
|
|
if (cipher_block_size == 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
|
2016-06-03 16:40:57 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the file size.
|
|
|
|
*/
|
2023-06-12 15:53:02 +02:00
|
|
|
cipher_mode = mbedtls_cipher_info_get_mode(cipher_info);
|
|
|
|
if (cipher_mode != MBEDTLS_MODE_GCM &&
|
|
|
|
cipher_mode != MBEDTLS_MODE_CTR &&
|
|
|
|
cipher_mode != MBEDTLS_MODE_CFB &&
|
|
|
|
cipher_mode != MBEDTLS_MODE_OFB &&
|
|
|
|
((filesize - md_size) % cipher_block_size) != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
|
2023-06-12 15:53:02 +02:00
|
|
|
cipher_block_size);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-10-29 10:02:51 +01:00
|
|
|
* Subtract the IV + HMAC length.
|
2011-01-21 10:32:12 +01:00
|
|
|
*/
|
2023-06-12 15:53:02 +02:00
|
|
|
filesize -= (16 + md_size);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the IV and original filesize modulo 16.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fread(buffer, 1, 16, fin) != 16) {
|
|
|
|
mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(IV, buffer, 16);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Hash the IV and the secret key together 8192 times
|
|
|
|
* using the result to setup the AES context and HMAC.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(digest, 0, 32);
|
|
|
|
memcpy(digest, IV, 16);
|
|
|
|
|
|
|
|
for (i = 0; i < 8192; i++) {
|
|
|
|
if (mbedtls_md_starts(&md_ctx) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_finish(&md_ctx, digest) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_setkey(&cipher_ctx,
|
|
|
|
digest,
|
|
|
|
(int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
|
|
|
|
MBEDTLS_DECRYPT) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
|
2014-04-17 16:00:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
|
2014-04-17 16:00:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
|
2014-04-17 16:00:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrypt and write the plaintext.
|
|
|
|
*/
|
2023-06-12 15:53:02 +02:00
|
|
|
for (offset = 0; offset < filesize; offset += cipher_block_size) {
|
|
|
|
ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
|
|
|
|
cipher_block_size : (unsigned int) (filesize - offset);
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
if (fread(buffer, 1, ilen, fin) != ilen) {
|
|
|
|
mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
|
2023-06-12 15:53:02 +02:00
|
|
|
cipher_block_size);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
|
|
|
|
&olen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
|
2014-04-17 16:00:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(output, 1, olen, fout) != olen) {
|
|
|
|
mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the message authentication code.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-06-12 15:53:02 +02:00
|
|
|
if (fread(buffer, 1, md_size, fin) != md_size) {
|
|
|
|
mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size);
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2013-10-31 14:22:08 +01:00
|
|
|
/* Use constant-time buffer comparison */
|
|
|
|
diff = 0;
|
2023-06-12 15:53:02 +02:00
|
|
|
for (i = 0; i < md_size; i++) {
|
2013-10-31 14:22:08 +01:00
|
|
|
diff |= digest[i] ^ buffer[i];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-10-31 14:22:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (diff != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
|
|
|
|
"or file corrupted.\n");
|
2011-01-21 10:32:12 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2013-11-25 17:55:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the final block of data
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
|
|
|
|
mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
|
2021-12-10 14:25:45 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2013-11-25 17:55:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fwrite(output, 1, olen, fout) != olen) {
|
|
|
|
mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
|
2013-11-25 17:55:17 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
}
|
|
|
|
|
2018-04-29 20:11:26 +02:00
|
|
|
exit_code = MBEDTLS_EXIT_SUCCESS;
|
2011-01-21 10:32:12 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fin) {
|
|
|
|
fclose(fin);
|
|
|
|
}
|
|
|
|
if (fout) {
|
|
|
|
fclose(fout);
|
|
|
|
}
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2017-06-27 09:22:17 +02:00
|
|
|
/* Zeroize all command line arguments to also cover
|
|
|
|
the case when the user has missed or reordered some,
|
|
|
|
in which case the key might not be in argv[6]. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
|
|
|
|
}
|
2017-06-27 09:22:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(IV, sizeof(IV));
|
|
|
|
mbedtls_platform_zeroize(key, sizeof(key));
|
|
|
|
mbedtls_platform_zeroize(buffer, sizeof(buffer));
|
|
|
|
mbedtls_platform_zeroize(output, sizeof(output));
|
|
|
|
mbedtls_platform_zeroize(digest, sizeof(digest));
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_free(&cipher_ctx);
|
|
|
|
mbedtls_md_free(&md_ctx);
|
2011-01-21 10:32:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_exit(exit_code);
|
2011-01-21 10:32:12 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */
|