From d097742155873c8597f1b5adcce95fa17f135002 Mon Sep 17 00:00:00 2001 From: Dominik Riebeling Date: Sat, 8 Aug 2020 22:05:44 +0200 Subject: sbtools: Change crypto wrapper implementation to C. There's nothing C++ left. Change-Id: I98d8406215287c02b56029ed7c0e2b0e645bbcf1 --- rbutil/mkimxboot/Makefile | 4 +- utils/imxtools/sbtools/Makefile | 3 - utils/imxtools/sbtools/crypto.c | 126 ++++++++++++++++++++++++++++++++++++++ utils/imxtools/sbtools/crypto.cpp | 126 -------------------------------------- 4 files changed, 127 insertions(+), 132 deletions(-) create mode 100644 utils/imxtools/sbtools/crypto.c delete mode 100644 utils/imxtools/sbtools/crypto.cpp diff --git a/rbutil/mkimxboot/Makefile b/rbutil/mkimxboot/Makefile index d2c487c475..966423159a 100644 --- a/rbutil/mkimxboot/Makefile +++ b/rbutil/mkimxboot/Makefile @@ -13,18 +13,16 @@ COMPILEFLAGS := -Wall -g -O3 -I$(IMXTOOLS_DIR) # std=gnu99 is required by MinGW on Windows (c99 is sufficient for Linux / MXE) CFLAGS += -std=gnu99 $(COMPILEFLAGS) -CXXFLAGS += $(COMPILEFLAGS) TOMCRYPT_DIR := ../../utils/tomcrypt -CXXFLAGS += -I$(TOMCRYPT_DIR)/src/headers CFLAGS += -I$(TOMCRYPT_DIR)/src/headers LDOPTS += -lpthread $(TOMCRYPT_DIR)/librbtomcrypt.a OUTPUT = mkimxboot # inputs for lib -IMXTOOLS_SOURCES = misc.c sb.c crypto.cpp crc.c elf.c +IMXTOOLS_SOURCES = misc.c sb.c crypto.c crc.c elf.c LIBSOURCES := dualboot.c mkimxboot.c md5.c \ $(addprefix $(IMXTOOLS_DIR),$(IMXTOOLS_SOURCES)) diff --git a/utils/imxtools/sbtools/Makefile b/utils/imxtools/sbtools/Makefile index b1d0313f77..64a9651f4a 100644 --- a/utils/imxtools/sbtools/Makefile +++ b/utils/imxtools/sbtools/Makefile @@ -15,9 +15,6 @@ all: $(BINS) %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -%.o: %.cpp - $(CXX) $(CXXFLAGS) -c -o $@ $< - sbtoelf: sbtoelf.o crc.o crypto.o xorcrypt.o dbparser.o elf.o misc.o sb.o sb1.o $(TOMCRYPT_DIR)/librbtomcrypt.a $(LD) -o $@ $^ $(LDFLAGS) diff --git a/utils/imxtools/sbtools/crypto.c b/utils/imxtools/sbtools/crypto.c new file mode 100644 index 0000000000..d3ec18bd11 --- /dev/null +++ b/utils/imxtools/sbtools/crypto.c @@ -0,0 +1,126 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2016 Amaury Pouly + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "crypto.h" +#include "misc.h" + +#include "tomcrypt.h" + + + +enum crypto_method_t g_cur_method = CRYPTO_NONE; +uint8_t g_key[16]; + +int cbc_mac2( + const uint8_t *in_data, /* Input data */ + uint8_t *out_data, /* Output data (or NULL) */ + int nr_blocks, /* Number of blocks to encrypt/decrypt (one block=16 bytes) */ + uint8_t key[16], /* Key */ + uint8_t iv[16], /* Initialisation Vector */ + uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ + bool encrypt /* 1 to encrypt, 0 to decrypt */ + ) +{ + int cipher = register_cipher(&aes_desc); + symmetric_CBC cbc; + cbc_start(cipher, iv, key, 16, 0, &cbc); + + /* encrypt */ + if(encrypt) + { + uint8_t tmp[16]; + /* we need some output buffer, either a temporary one if we are CBC-MACing + * only, or use output buffer if available */ + uint8_t *out_ptr = (out_data == NULL) ? tmp : out_data; + while(nr_blocks-- > 0) + { + cbc_encrypt(in_data, out_ptr, 16, &cbc); + /* if this is the last block, copy CBC-MAC */ + if(nr_blocks == 0 && out_cbc_mac) + memcpy(out_cbc_mac, out_ptr, 16); + /* if we are writing data to the output buffer, advance output pointer */ + if(out_data != NULL) + out_ptr += 16; + in_data += 16; + } + return CRYPTO_ERROR_SUCCESS; + } + /* decrypt */ + else + { + cbc_decrypt(in_data, out_data, nr_blocks * 16, &cbc); + + /* update keys if neeeded */ + /* we cannot produce a CBC-MAC in decrypt mode, output buffer exists */ + if(out_cbc_mac || out_data == NULL) + return CRYPTO_ERROR_INVALID_OP; + + return CRYPTO_ERROR_SUCCESS; + } +} + + +int crypto_setup(struct crypto_key_t *key) +{ + g_cur_method = key->method; + switch(g_cur_method) + { + case CRYPTO_KEY: + memcpy(g_key, key->u.key, 16); + + return CRYPTO_ERROR_SUCCESS; + default: + return CRYPTO_ERROR_BADSETUP; + } +} + +int crypto_apply( + uint8_t *in_data, /* Input data */ + uint8_t *out_data, /* Output data (or NULL) */ + int nr_blocks, /* Number of blocks (one block=16 bytes) */ + uint8_t iv[16], /* Key */ + uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ + bool encrypt) +{ + if(g_cur_method == CRYPTO_KEY) + return cbc_mac2(in_data, out_data, nr_blocks, g_key, iv, out_cbc_mac, encrypt); + else + return CRYPTO_ERROR_BADSETUP; +} + +void sha_1_init(struct sha_1_params_t *params) +{ + sha1_init(¶ms->state); +} + +void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size) +{ + sha1_process(¶ms->state, buffer, size); +} + +void sha_1_finish(struct sha_1_params_t *params) +{ + sha1_done(¶ms->state, params->hash); +} + +void sha_1_output(struct sha_1_params_t *params, uint8_t *out) +{ + memcpy(out, params->hash, 20); +} diff --git a/utils/imxtools/sbtools/crypto.cpp b/utils/imxtools/sbtools/crypto.cpp deleted file mode 100644 index d3ec18bd11..0000000000 --- a/utils/imxtools/sbtools/crypto.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2016 Amaury Pouly - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ -#include "crypto.h" -#include "misc.h" - -#include "tomcrypt.h" - - - -enum crypto_method_t g_cur_method = CRYPTO_NONE; -uint8_t g_key[16]; - -int cbc_mac2( - const uint8_t *in_data, /* Input data */ - uint8_t *out_data, /* Output data (or NULL) */ - int nr_blocks, /* Number of blocks to encrypt/decrypt (one block=16 bytes) */ - uint8_t key[16], /* Key */ - uint8_t iv[16], /* Initialisation Vector */ - uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ - bool encrypt /* 1 to encrypt, 0 to decrypt */ - ) -{ - int cipher = register_cipher(&aes_desc); - symmetric_CBC cbc; - cbc_start(cipher, iv, key, 16, 0, &cbc); - - /* encrypt */ - if(encrypt) - { - uint8_t tmp[16]; - /* we need some output buffer, either a temporary one if we are CBC-MACing - * only, or use output buffer if available */ - uint8_t *out_ptr = (out_data == NULL) ? tmp : out_data; - while(nr_blocks-- > 0) - { - cbc_encrypt(in_data, out_ptr, 16, &cbc); - /* if this is the last block, copy CBC-MAC */ - if(nr_blocks == 0 && out_cbc_mac) - memcpy(out_cbc_mac, out_ptr, 16); - /* if we are writing data to the output buffer, advance output pointer */ - if(out_data != NULL) - out_ptr += 16; - in_data += 16; - } - return CRYPTO_ERROR_SUCCESS; - } - /* decrypt */ - else - { - cbc_decrypt(in_data, out_data, nr_blocks * 16, &cbc); - - /* update keys if neeeded */ - /* we cannot produce a CBC-MAC in decrypt mode, output buffer exists */ - if(out_cbc_mac || out_data == NULL) - return CRYPTO_ERROR_INVALID_OP; - - return CRYPTO_ERROR_SUCCESS; - } -} - - -int crypto_setup(struct crypto_key_t *key) -{ - g_cur_method = key->method; - switch(g_cur_method) - { - case CRYPTO_KEY: - memcpy(g_key, key->u.key, 16); - - return CRYPTO_ERROR_SUCCESS; - default: - return CRYPTO_ERROR_BADSETUP; - } -} - -int crypto_apply( - uint8_t *in_data, /* Input data */ - uint8_t *out_data, /* Output data (or NULL) */ - int nr_blocks, /* Number of blocks (one block=16 bytes) */ - uint8_t iv[16], /* Key */ - uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ - bool encrypt) -{ - if(g_cur_method == CRYPTO_KEY) - return cbc_mac2(in_data, out_data, nr_blocks, g_key, iv, out_cbc_mac, encrypt); - else - return CRYPTO_ERROR_BADSETUP; -} - -void sha_1_init(struct sha_1_params_t *params) -{ - sha1_init(¶ms->state); -} - -void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size) -{ - sha1_process(¶ms->state, buffer, size); -} - -void sha_1_finish(struct sha_1_params_t *params) -{ - sha1_done(¶ms->state, params->hash); -} - -void sha_1_output(struct sha_1_params_t *params, uint8_t *out) -{ - memcpy(out, params->hash, 20); -} -- cgit v1.2.3