1
0
Fork 0
forked from len0rd/rockbox

imxtools: Replace use of "byte" with its underlying uint8_t.

libtomcrypt uses a macro "byte" which conflicts with this type. Since
the underlying type is uint8_t and there's no real benefit from using a
custom type use the actual underlying type.

Change-Id: I982c9b8bdcb657b99fa645a5235303af7afda25b
This commit is contained in:
Dominik Riebeling 2020-08-08 21:25:50 +02:00
parent 387a45923c
commit 815b289cb3
8 changed files with 69 additions and 70 deletions

View file

@ -655,7 +655,7 @@ static enum imx_error_t find_model_by_md5sum(uint8_t file_md5sum[16], int *md5_i
} }
for(int j = 0; j < 16; j++) for(int j = 0; j < 16; j++)
{ {
byte a, b; uint8_t a, b;
if(convxdigit(imx_sums[i].md5sum[2 * j], &a) || convxdigit(imx_sums[i].md5sum[2 * j + 1], &b)) if(convxdigit(imx_sums[i].md5sum[2 * j], &a) || convxdigit(imx_sums[i].md5sum[2 * j + 1], &b))
{ {
printf("[ERR][INTERNAL] Bad checksum format: %s\n", imx_sums[i].md5sum); printf("[ERR][INTERNAL] Bad checksum format: %s\n", imx_sums[i].md5sum);

View file

@ -68,12 +68,12 @@ static uint32_t crc_table[256] = {
0x0B8757BDA, 0x0B5365D03, 0x0B1F740B4 0x0B8757BDA, 0x0B5365D03, 0x0B1F740B4
}; };
uint32_t crc(byte *data, int size) uint32_t crc(uint8_t *data, int size)
{ {
return crc_continue(0xffffffff, data, size); return crc_continue(0xffffffff, data, size);
} }
uint32_t crc_continue(uint32_t previous_crc, byte *data, int size) uint32_t crc_continue(uint32_t previous_crc, uint8_t *data, int size)
{ {
uint32_t c = previous_crc; uint32_t c = previous_crc;
/* normal CRC */ /* normal CRC */

View file

@ -30,19 +30,19 @@ namespace
{ {
enum crypto_method_t g_cur_method = CRYPTO_NONE; enum crypto_method_t g_cur_method = CRYPTO_NONE;
byte g_key[16]; uint8_t g_key[16];
CBC_Mode<AES>::Encryption g_aes_enc; CBC_Mode<AES>::Encryption g_aes_enc;
CBC_Mode<AES>::Decryption g_aes_dec; CBC_Mode<AES>::Decryption g_aes_dec;
bool g_aes_enc_key_dirty; /* true of g_aes_enc key needs to be updated */ bool g_aes_enc_key_dirty; /* true of g_aes_enc key needs to be updated */
bool g_aes_dec_key_dirty; /* same for g_aes_dec */ bool g_aes_dec_key_dirty; /* same for g_aes_dec */
int cbc_mac2( int cbc_mac2(
const byte *in_data, /* Input data */ const uint8_t *in_data, /* Input data */
byte *out_data, /* Output data (or NULL) */ uint8_t *out_data, /* Output data (or NULL) */
int nr_blocks, /* Number of blocks to encrypt/decrypt (one block=16 bytes) */ int nr_blocks, /* Number of blocks to encrypt/decrypt (one block=16 bytes) */
byte key[16], /* Key */ uint8_t key[16], /* Key */
byte iv[16], /* Initialisation Vector */ uint8_t iv[16], /* Initialisation Vector */
byte (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */
bool encrypt /* 1 to encrypt, 0 to decrypt */ bool encrypt /* 1 to encrypt, 0 to decrypt */
) )
{ {
@ -58,10 +58,10 @@ int cbc_mac2(
g_aes_enc_key_dirty = false; g_aes_enc_key_dirty = false;
} }
g_aes_enc.Resynchronize(iv, 16); g_aes_enc.Resynchronize(iv, 16);
byte tmp[16]; uint8_t tmp[16];
/* we need some output buffer, either a temporary one if we are CBC-MACing /* we need some output buffer, either a temporary one if we are CBC-MACing
* only, or use output buffer if available */ * only, or use output buffer if available */
byte *out_ptr = (out_data == NULL) ? tmp : out_data; uint8_t *out_ptr = (out_data == NULL) ? tmp : out_data;
while(nr_blocks-- > 0) while(nr_blocks-- > 0)
{ {
g_aes_enc.ProcessData(out_ptr, in_data, 16); g_aes_enc.ProcessData(out_ptr, in_data, 16);
@ -113,11 +113,11 @@ int crypto_setup(struct crypto_key_t *key)
} }
int crypto_apply( int crypto_apply(
byte *in_data, /* Input data */ uint8_t *in_data, /* Input data */
byte *out_data, /* Output data (or NULL) */ uint8_t *out_data, /* Output data (or NULL) */
int nr_blocks, /* Number of blocks (one block=16 bytes) */ int nr_blocks, /* Number of blocks (one block=16 bytes) */
byte iv[16], /* Key */ uint8_t iv[16], /* Key */
byte (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */
bool encrypt) bool encrypt)
{ {
if(g_cur_method == CRYPTO_KEY) if(g_cur_method == CRYPTO_KEY)
@ -131,7 +131,7 @@ void sha_1_init(struct sha_1_params_t *params)
params->object = new SHA1; params->object = new SHA1;
} }
void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size) void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size)
{ {
reinterpret_cast<SHA1 *>(params->object)->Update(buffer, size); reinterpret_cast<SHA1 *>(params->object)->Update(buffer, size);
} }
@ -143,7 +143,7 @@ void sha_1_finish(struct sha_1_params_t *params)
delete obj; delete obj;
} }
void sha_1_output(struct sha_1_params_t *params, byte *out) void sha_1_output(struct sha_1_params_t *params, uint8_t *out)
{ {
memcpy(out, params->hash, 20); memcpy(out, params->hash, 20);
} }

View file

@ -30,8 +30,6 @@
extern "C" { extern "C" {
#endif #endif
typedef uint8_t byte;
/* crypto.cpp */ /* crypto.cpp */
enum crypto_method_t enum crypto_method_t
{ {
@ -52,7 +50,7 @@ struct crypto_key_t
enum crypto_method_t method; enum crypto_method_t method;
union union
{ {
byte key[16]; uint8_t key[16];
union xorcrypt_key_t xor_key[2]; union xorcrypt_key_t xor_key[2];
}u; }u;
}; };
@ -68,28 +66,28 @@ int crypto_setup(struct crypto_key_t *key);
/* return 0 on success, <0 on error */ /* return 0 on success, <0 on error */
int crypto_apply( int crypto_apply(
byte *in_data, /* Input data */ uint8_t *in_data, /* Input data */
byte *out_data, /* Output data (or NULL) */ uint8_t *out_data, /* Output data (or NULL) */
int nr_blocks, /* Number of blocks (one block=16 bytes) */ int nr_blocks, /* Number of blocks (one block=16 bytes) */
byte iv[16], /* IV */ uint8_t iv[16], /* IV */
byte (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */ uint8_t (*out_cbc_mac)[16], /* CBC-MAC of the result (or NULL) */
bool encrypt); bool encrypt);
/* crc.c */ /* crc.c */
uint32_t crc(byte *data, int size); uint32_t crc(uint8_t *data, int size);
uint32_t crc_continue(uint32_t previous_crc, byte *data, int size); uint32_t crc_continue(uint32_t previous_crc, uint8_t *data, int size);
/* sha1.c */ /* sha1.c */
struct sha_1_params_t struct sha_1_params_t
{ {
byte hash[20]; /* final hash */ uint8_t hash[20]; /* final hash */
void *object; /* pointer to CryptoPP::SHA1 object */ void *object; /* pointer to CryptoPP::SHA1 object */
}; };
void sha_1_init(struct sha_1_params_t *params); void sha_1_init(struct sha_1_params_t *params);
void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size); void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size);
void sha_1_finish(struct sha_1_params_t *params); void sha_1_finish(struct sha_1_params_t *params);
void sha_1_output(struct sha_1_params_t *params, byte *out); void sha_1_output(struct sha_1_params_t *params, uint8_t *out);
/* xorcrypt.c */ /* xorcrypt.c */

View file

@ -207,7 +207,7 @@ static void parse_number(struct context_t *ctx, struct lexem_t *lexem)
{ {
if(base == 10 && !isdigit(cur_char(ctx))) if(base == 10 && !isdigit(cur_char(ctx)))
break; break;
byte v; uint8_t v;
if(convxdigit(cur_char(ctx), &v)) if(convxdigit(cur_char(ctx), &v))
break; break;
lexem->num = base * lexem->num + v; lexem->num = base * lexem->num + v;

View file

@ -58,7 +58,7 @@ void *xmalloc(size_t s)
return r; return r;
} }
int convxdigit(char digit, byte *val) int convxdigit(char digit, uint8_t *val)
{ {
if(digit >= '0' && digit <= '9') if(digit >= '0' && digit <= '9')
{ {
@ -127,7 +127,7 @@ bool parse_key(char **pstr, struct crypto_key_t *key)
{ {
for(int j = 0; j < 128; j++) for(int j = 0; j < 128; j++)
{ {
byte a, b; uint8_t a, b;
if(convxdigit(str[2 * j], &a) || convxdigit(str[2 * j + 1], &b)) if(convxdigit(str[2 * j], &a) || convxdigit(str[2 * j + 1], &b))
return false; return false;
key->u.xor_key[j / 64].key[j % 64] = (a << 4) | b; key->u.xor_key[j / 64].key[j % 64] = (a << 4) | b;
@ -143,7 +143,7 @@ bool parse_key(char **pstr, struct crypto_key_t *key)
return false; return false;
for(int j = 0; j < 16; j++) for(int j = 0; j < 16; j++)
{ {
byte a, b; uint8_t a, b;
if(convxdigit(str[2 * j], &a) || convxdigit(str[2 * j + 1], &b)) if(convxdigit(str[2 * j], &a) || convxdigit(str[2 * j + 1], &b))
return false; return false;
key->u.key[j] = (a << 4) | b; key->u.key[j] = (a << 4) | b;
@ -243,7 +243,7 @@ bool add_keys_from_file(const char *key_file)
return true; return true;
} }
void print_hex(void *user, misc_printf_t printf, byte *data, int len, bool newline) void print_hex(void *user, misc_printf_t printf, uint8_t *data, int len, bool newline)
{ {
for(int i = 0; i < len; i++) for(int i = 0; i < len; i++)
printf(user, "%02X ", data[i]); printf(user, "%02X ", data[i]);

View file

@ -22,6 +22,7 @@
#define __MISC_H__ #define __MISC_H__
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include "crypto.h" #include "crypto.h"
#define _STR(a) #a #define _STR(a) #a
@ -53,8 +54,8 @@ void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity,
void *aug, int aug_cnt); void *aug, int aug_cnt);
void generate_random_data(void *buf, size_t sz); void generate_random_data(void *buf, size_t sz);
void *xmalloc(size_t s); void *xmalloc(size_t s);
int convxdigit(char digit, byte *val); int convxdigit(char digit, uint8_t *val);
void print_hex(void *user, misc_printf_t printf, byte *data, int len, bool newline); void print_hex(void *user, misc_printf_t printf, uint8_t *data, int len, bool newline);
void add_keys(key_array_t ka, int kac); void add_keys(key_array_t ka, int kac);
bool parse_key(char **str, struct crypto_key_t *key); bool parse_key(char **str, struct crypto_key_t *key);
bool add_keys_from_file(const char *key_file); bool add_keys_from_file(const char *key_file);

View file

@ -223,7 +223,7 @@ static void produce_sb_section_header(struct sb_section_t *sec,
static uint8_t instruction_checksum(struct sb_instruction_header_t *hdr) static uint8_t instruction_checksum(struct sb_instruction_header_t *hdr)
{ {
uint8_t sum = 90; uint8_t sum = 90;
byte *ptr = (byte *)hdr; uint8_t *ptr = (uint8_t *)hdr;
for(int i = 1; i < 16; i++) for(int i = 1; i < 16; i++)
sum += ptr[i]; sum += ptr[i];
return sum; return sum;
@ -287,8 +287,8 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
#define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
struct crypto_key_t real_key; struct crypto_key_t real_key;
real_key.method = CRYPTO_KEY; real_key.method = CRYPTO_KEY;
byte crypto_iv[16]; uint8_t crypto_iv[16];
byte (*cbc_macs)[16] = xmalloc(16 * g_nr_keys); uint8_t (*cbc_macs)[16] = xmalloc(16 * g_nr_keys);
/* init CBC-MACs */ /* init CBC-MACs */
for(int i = 0; i < g_nr_keys; i++) for(int i = 0; i < g_nr_keys; i++)
memset(cbc_macs[i], 0, 16); memset(cbc_macs[i], 0, 16);
@ -319,8 +319,8 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
struct sb_header_t sb_hdr; struct sb_header_t sb_hdr;
produce_sb_header(sb, &sb_hdr); produce_sb_header(sb, &sb_hdr);
/* allocate image */ /* allocate image */
byte *buf = xmalloc(sb_hdr.image_size * BLOCK_SIZE); uint8_t *buf = xmalloc(sb_hdr.image_size * BLOCK_SIZE);
byte *buf_p = buf; uint8_t *buf_p = buf;
#define write(p, sz) do { memcpy(buf_p, p, sz); buf_p += sz; } while(0) #define write(p, sz) do { memcpy(buf_p, p, sz); buf_p += sz; } while(0)
#define check_crypto(expr) \ #define check_crypto(expr) \
do { int err = expr; \ do { int err = expr; \
@ -329,7 +329,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
cprintf(u, true, GREY, "Crypto error: %d\n", err); \ cprintf(u, true, GREY, "Crypto error: %d\n", err); \
return SB_CRYPTO_ERROR; } } while(0) return SB_CRYPTO_ERROR; } } while(0)
sha_1_update(&file_sha1, (byte *)&sb_hdr, sizeof(sb_hdr)); sha_1_update(&file_sha1, (uint8_t *)&sb_hdr, sizeof(sb_hdr));
write(&sb_hdr, sizeof(sb_hdr)); write(&sb_hdr, sizeof(sb_hdr));
memcpy(crypto_iv, &sb_hdr, 16); memcpy(crypto_iv, &sb_hdr, 16);
@ -338,7 +338,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
for(int i = 0; i < g_nr_keys; i++) for(int i = 0; i < g_nr_keys; i++)
{ {
check_crypto(crypto_setup(&g_key_array[i])); check_crypto(crypto_setup(&g_key_array[i]));
check_crypto(crypto_apply((byte *)&sb_hdr, NULL, sizeof(sb_hdr) / BLOCK_SIZE, check_crypto(crypto_apply((uint8_t *)&sb_hdr, NULL, sizeof(sb_hdr) / BLOCK_SIZE,
cbc_macs[i], &cbc_macs[i], true)); cbc_macs[i], &cbc_macs[i], true));
} }
@ -347,13 +347,13 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
{ {
struct sb_section_header_t sb_sec_hdr; struct sb_section_header_t sb_sec_hdr;
produce_sb_section_header(&sb->sections[i], &sb_sec_hdr); produce_sb_section_header(&sb->sections[i], &sb_sec_hdr);
sha_1_update(&file_sha1, (byte *)&sb_sec_hdr, sizeof(sb_sec_hdr)); sha_1_update(&file_sha1, (uint8_t *)&sb_sec_hdr, sizeof(sb_sec_hdr));
write(&sb_sec_hdr, sizeof(sb_sec_hdr)); write(&sb_sec_hdr, sizeof(sb_sec_hdr));
/* update CBC-MACs */ /* update CBC-MACs */
for(int j = 0; j < g_nr_keys; j++) for(int j = 0; j < g_nr_keys; j++)
{ {
check_crypto(crypto_setup(&g_key_array[j])); check_crypto(crypto_setup(&g_key_array[j]));
check_crypto(crypto_apply((byte *)&sb_sec_hdr, NULL, check_crypto(crypto_apply((uint8_t *)&sb_sec_hdr, NULL,
sizeof(sb_sec_hdr) / BLOCK_SIZE, cbc_macs[j], &cbc_macs[j], true)); sizeof(sb_sec_hdr) / BLOCK_SIZE, cbc_macs[j], &cbc_macs[j], true));
} }
} }
@ -365,7 +365,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
check_crypto(crypto_setup(&g_key_array[i])); check_crypto(crypto_setup(&g_key_array[i]));
check_crypto(crypto_apply(real_key.u.key, entry.key, 1, crypto_iv, NULL, true)); check_crypto(crypto_apply(real_key.u.key, entry.key, 1, crypto_iv, NULL, true));
write(&entry, sizeof(entry)); write(&entry, sizeof(entry));
sha_1_update(&file_sha1, (byte *)&entry, sizeof(entry)); sha_1_update(&file_sha1, (uint8_t *)&entry, sizeof(entry));
} }
/* HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK */ /* HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK */
@ -391,7 +391,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
unsigned init_gap = (sb->sections[0].file_offset - 1) * BLOCK_SIZE - (buf_p - buf); unsigned init_gap = (sb->sections[0].file_offset - 1) * BLOCK_SIZE - (buf_p - buf);
if(init_gap > 0) if(init_gap > 0)
{ {
byte *data = xmalloc(init_gap); uint8_t *data = xmalloc(init_gap);
generate_random_data(data, init_gap); generate_random_data(data, init_gap);
sha_1_update(&file_sha1, data, init_gap); sha_1_update(&file_sha1, data, init_gap);
write(data, init_gap); write(data, init_gap);
@ -407,13 +407,13 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
produce_section_tag_cmd(&sb->sections[i], &tag_cmd, (i + 1) == sb_hdr.nr_sections); produce_section_tag_cmd(&sb->sections[i], &tag_cmd, (i + 1) == sb_hdr.nr_sections);
if(g_nr_keys > 0) if(g_nr_keys > 0)
{ {
check_crypto(crypto_apply((byte *)&tag_cmd, (byte *)&tag_cmd, check_crypto(crypto_apply((uint8_t *)&tag_cmd, (uint8_t *)&tag_cmd,
sizeof(tag_cmd) / BLOCK_SIZE, crypto_iv, NULL, true)); sizeof(tag_cmd) / BLOCK_SIZE, crypto_iv, NULL, true));
} }
sha_1_update(&file_sha1, (byte *)&tag_cmd, sizeof(tag_cmd)); sha_1_update(&file_sha1, (uint8_t *)&tag_cmd, sizeof(tag_cmd));
write(&tag_cmd, sizeof(tag_cmd)); write(&tag_cmd, sizeof(tag_cmd));
/* produce other commands */ /* produce other commands */
byte cur_cbc_mac[16]; uint8_t cur_cbc_mac[16];
memcpy(cur_cbc_mac, crypto_iv, 16); memcpy(cur_cbc_mac, crypto_iv, 16);
for(int j = 0; j < sb->sections[i].nr_insts; j++) for(int j = 0; j < sb->sections[i].nr_insts; j++)
{ {
@ -425,17 +425,17 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
produce_sb_instruction(inst, &cmd, u, cprintf); produce_sb_instruction(inst, &cmd, u, cprintf);
if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
{ {
check_crypto(crypto_apply((byte *)&cmd, (byte *)&cmd, check_crypto(crypto_apply((uint8_t *)&cmd, (uint8_t *)&cmd,
sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true)); sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true));
} }
sha_1_update(&file_sha1, (byte *)&cmd, sizeof(cmd)); sha_1_update(&file_sha1, (uint8_t *)&cmd, sizeof(cmd));
write(&cmd, sizeof(cmd)); write(&cmd, sizeof(cmd));
} }
/* data */ /* data */
if(inst->inst == SB_INST_LOAD || inst->inst == SB_INST_DATA) if(inst->inst == SB_INST_LOAD || inst->inst == SB_INST_DATA)
{ {
uint32_t sz = inst->size + inst->padding_size; uint32_t sz = inst->size + inst->padding_size;
byte *data = xmalloc(sz); uint8_t *data = xmalloc(sz);
memcpy(data, inst->data, inst->size); memcpy(data, inst->data, inst->size);
memcpy(data + inst->size, inst->padding, inst->padding_size); memcpy(data + inst->size, inst->padding, inst->padding_size);
if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
@ -452,7 +452,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
uint32_t pad_size = sb->sections[i].pad_size; uint32_t pad_size = sb->sections[i].pad_size;
if(sb->sections[i].is_data) if(sb->sections[i].is_data)
{ {
byte *data = xmalloc(pad_size * BLOCK_SIZE); uint8_t *data = xmalloc(pad_size * BLOCK_SIZE);
generate_random_data(data, pad_size * BLOCK_SIZE); generate_random_data(data, pad_size * BLOCK_SIZE);
sha_1_update(&file_sha1, data, pad_size * BLOCK_SIZE); sha_1_update(&file_sha1, data, pad_size * BLOCK_SIZE);
write(data, pad_size * BLOCK_SIZE); write(data, pad_size * BLOCK_SIZE);
@ -468,16 +468,16 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
cmd.hdr.checksum = instruction_checksum(&cmd.hdr); cmd.hdr.checksum = instruction_checksum(&cmd.hdr);
if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
{ {
check_crypto(crypto_apply((byte *)&cmd, (byte *)&cmd, check_crypto(crypto_apply((uint8_t *)&cmd, (uint8_t *)&cmd,
sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true)); sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true));
} }
sha_1_update(&file_sha1, (byte *)&cmd, sizeof(cmd)); sha_1_update(&file_sha1, (uint8_t *)&cmd, sizeof(cmd));
write(&cmd, sizeof(cmd)); write(&cmd, sizeof(cmd));
} }
} }
} }
/* write file SHA-1 */ /* write file SHA-1 */
byte final_sig[32]; uint8_t final_sig[32];
sha_1_finish(&file_sha1); sha_1_finish(&file_sha1);
sha_1_output(&file_sha1, final_sig); sha_1_output(&file_sha1, final_sig);
generate_random_data(final_sig + 20, 12); generate_random_data(final_sig + 20, 12);
@ -513,7 +513,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
#undef printf #undef printf
} }
static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, static struct sb_section_t *read_section(bool data_sec, uint32_t id, uint8_t *buf,
int size, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err) int size, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err)
{ {
#define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
@ -758,7 +758,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
fatal(SB_CRYPTO_ERROR, "Crypto error: %d\n", err); } while(0) fatal(SB_CRYPTO_ERROR, "Crypto error: %d\n", err); } while(0)
struct sha_1_params_t sha_1_params; struct sha_1_params_t sha_1_params;
byte (*cbcmacs)[16] = xmalloc(16 * g_nr_keys); uint8_t (*cbcmacs)[16] = xmalloc(16 * g_nr_keys);
sb_file = xmalloc(sizeof(struct sb_file_t)); sb_file = xmalloc(sizeof(struct sb_file_t));
memset(sb_file, 0, sizeof(struct sb_file_t)); memset(sb_file, 0, sizeof(struct sb_file_t));
struct sb_header_t *sb_header = (struct sb_header_t *)buf; struct sb_header_t *sb_header = (struct sb_header_t *)buf;
@ -790,10 +790,10 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
printf(GREEN, " SB version: "); printf(GREEN, " SB version: ");
printf(YELLOW, "%d.%d\n", sb_header->major_ver, sb_header->minor_ver); printf(YELLOW, "%d.%d\n", sb_header->major_ver, sb_header->minor_ver);
printf(GREEN, " Header SHA-1: "); printf(GREEN, " Header SHA-1: ");
byte *hdr_sha1 = sb_header->sha1_header; uint8_t *hdr_sha1 = sb_header->sha1_header;
print_hex(YELLOW, hdr_sha1, 20, false); print_hex(YELLOW, hdr_sha1, 20, false);
/* Check SHA1 sum */ /* Check SHA1 sum */
byte computed_sha1[20]; uint8_t computed_sha1[20];
sha_1_init(&sha_1_params); sha_1_init(&sha_1_params);
sha_1_update(&sha_1_params, &sb_header->signature[0], sha_1_update(&sha_1_params, &sb_header->signature[0],
sizeof(struct sb_header_t) - sizeof(sb_header->sha1_header)); sizeof(struct sb_header_t) - sizeof(sb_header->sha1_header));
@ -876,7 +876,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
print_key(&printer, sb_printer, &g_key_array[i], true); print_key(&printer, sb_printer, &g_key_array[i], true);
printf(GREEN, " CBC-MAC: "); printf(GREEN, " CBC-MAC: ");
/* check it */ /* check it */
byte zero[16]; uint8_t zero[16];
memset(zero, 0, 16); memset(zero, 0, 16);
check_crypto(crypto_setup(&g_key_array[i])); check_crypto(crypto_setup(&g_key_array[i]));
check_crypto(crypto_apply(buf, NULL, sb_header->header_size + check_crypto(crypto_apply(buf, NULL, sb_header->header_size +
@ -906,8 +906,8 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
{ {
printf(RED, " Match\n"); printf(RED, " Match\n");
/* decrypt */ /* decrypt */
byte decrypted_key[16]; uint8_t decrypted_key[16];
byte iv[16]; uint8_t iv[16];
memcpy(iv, buf, 16); /* uses the first 16-bytes of SHA-1 sig as IV */ memcpy(iv, buf, 16); /* uses the first 16-bytes of SHA-1 sig as IV */
check_crypto(crypto_setup(&g_key_array[idx])); check_crypto(crypto_setup(&g_key_array[idx]));
check_crypto(crypto_apply(dict_entry->key, decrypted_key, 1, iv, NULL, false)); check_crypto(crypto_apply(dict_entry->key, decrypted_key, 1, iv, NULL, false));
@ -1008,7 +1008,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
} }
/* save it */ /* save it */
byte *sec = xmalloc(size); uint8_t *sec = xmalloc(size);
if(encrypted) if(encrypted)
check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false)); check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false));
else else
@ -1034,13 +1034,13 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
/* advanced raw mode */ /* advanced raw mode */
printf(BLUE, "Commands\n"); printf(BLUE, "Commands\n");
uint32_t offset = sb_header->first_boot_tag_off * BLOCK_SIZE; uint32_t offset = sb_header->first_boot_tag_off * BLOCK_SIZE;
byte iv[16]; uint8_t iv[16];
const char *indent = " "; const char *indent = " ";
while(true) while(true)
{ {
/* restart with IV */ /* restart with IV */
memcpy(iv, buf, 16); memcpy(iv, buf, 16);
byte cmd[BLOCK_SIZE]; uint8_t cmd[BLOCK_SIZE];
if(sb_header->nr_keys > 0) if(sb_header->nr_keys > 0)
check_crypto(crypto_apply(buf + offset, cmd, 1, iv, &iv, false)); check_crypto(crypto_apply(buf + offset, cmd, 1, iv, &iv, false));
else else
@ -1106,7 +1106,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
} }
/* save it */ /* save it */
byte *sec = xmalloc(size); uint8_t *sec = xmalloc(size);
if(encrypted) if(encrypted)
check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false)); check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false));
else else
@ -1147,11 +1147,11 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
/* final signature */ /* final signature */
printf(BLUE, "Final signature:\n"); printf(BLUE, "Final signature:\n");
byte decrypted_block[32]; uint8_t decrypted_block[32];
if(sb_header->nr_keys > 0) if(sb_header->nr_keys > 0)
{ {
printf(GREEN, " Encrypted SHA-1:\n"); printf(GREEN, " Encrypted SHA-1:\n");
byte *encrypted_block = &buf[filesize - 32]; uint8_t *encrypted_block = &buf[filesize - 32];
printf(OFF, " "); printf(OFF, " ");
print_hex(YELLOW, encrypted_block, 16, true); print_hex(YELLOW, encrypted_block, 16, true);
printf(OFF, " "); printf(OFF, " ");