diff options
Diffstat (limited to 'utils/imxtools/sbtools')
-rw-r--r-- | utils/imxtools/sbtools/Makefile | 17 | ||||
-rw-r--r-- | utils/imxtools/sbtools/crypto.cpp | 51 | ||||
-rw-r--r-- | utils/imxtools/sbtools/crypto.h | 3 |
3 files changed, 27 insertions, 44 deletions
diff --git a/utils/imxtools/sbtools/Makefile b/utils/imxtools/sbtools/Makefile index 3e284e20dd..b1d0313f77 100644 --- a/utils/imxtools/sbtools/Makefile +++ b/utils/imxtools/sbtools/Makefile | |||
@@ -3,10 +3,11 @@ CC := gcc | |||
3 | CXX := g++ | 3 | CXX := g++ |
4 | LD := g++ | 4 | LD := g++ |
5 | 5 | ||
6 | LIBUSB := $(shell pkg-config --cflags libusb-1.0) | 6 | TOMCRYPT_DIR := ../../tomcrypt/ |
7 | CFLAGS := -O3 -g -std=c99 -Wall $(shell pkg-config --cflags libusb-1.0) $(DEFINES) | 7 | |
8 | CXXFLAGS := -O3 -g -Wall $(pkg-config --cflags libcrypto++) $(DEFINES) | 8 | CFLAGS := -O3 -g -std=c99 -Wall $(shell pkg-config --cflags libusb-1.0) $(DEFINES) -I$(TOMCRYPT_DIR)/src/headers |
9 | LDFLAGS := $(shell pkg-config --libs libusb-1.0) $(shell pkg-config --libs libcrypto++) | 9 | CXXFLAGS := -O3 -g -Wall $(DEFINES) -I$(TOMCRYPT_DIR)/src/headers |
10 | LDFLAGS := $(shell pkg-config --libs libusb-1.0) | ||
10 | BINS := elftosb sbtoelf sbloader rsrctool elftosb1 | 11 | BINS := elftosb sbtoelf sbloader rsrctool elftosb1 |
11 | 12 | ||
12 | all: $(BINS) | 13 | all: $(BINS) |
@@ -17,10 +18,10 @@ all: $(BINS) | |||
17 | %.o: %.cpp | 18 | %.o: %.cpp |
18 | $(CXX) $(CXXFLAGS) -c -o $@ $< | 19 | $(CXX) $(CXXFLAGS) -c -o $@ $< |
19 | 20 | ||
20 | sbtoelf: sbtoelf.o crc.o crypto.o xorcrypt.o dbparser.o elf.o misc.o sb.o sb1.o | 21 | sbtoelf: sbtoelf.o crc.o crypto.o xorcrypt.o dbparser.o elf.o misc.o sb.o sb1.o $(TOMCRYPT_DIR)/librbtomcrypt.a |
21 | $(LD) -o $@ $^ $(LDFLAGS) | 22 | $(LD) -o $@ $^ $(LDFLAGS) |
22 | 23 | ||
23 | elftosb: elftosb.o crc.o crypto.o elf.o dbparser.o misc.o sb.o | 24 | elftosb: elftosb.o crc.o crypto.o elf.o dbparser.o misc.o sb.o $(TOMCRYPT_DIR)/librbtomcrypt.a |
24 | $(LD) -o $@ $^ $(LDFLAGS) | 25 | $(LD) -o $@ $^ $(LDFLAGS) |
25 | 26 | ||
26 | elftosb1: elftosb1.o xorcrypt.o elf.o misc.o sb1.o | 27 | elftosb1: elftosb1.o xorcrypt.o elf.o misc.o sb1.o |
@@ -37,3 +38,7 @@ clean: | |||
37 | 38 | ||
38 | veryclean: | 39 | veryclean: |
39 | rm -rf $(BINS) | 40 | rm -rf $(BINS) |
41 | |||
42 | $(TOMCRYPT_DIR)/librbtomcrypt.a: | ||
43 | $(MAKE) -C $(TOMCRYPT_DIR) | ||
44 | |||
diff --git a/utils/imxtools/sbtools/crypto.cpp b/utils/imxtools/sbtools/crypto.cpp index 5563fcfd3b..d3ec18bd11 100644 --- a/utils/imxtools/sbtools/crypto.cpp +++ b/utils/imxtools/sbtools/crypto.cpp | |||
@@ -20,21 +20,13 @@ | |||
20 | ****************************************************************************/ | 20 | ****************************************************************************/ |
21 | #include "crypto.h" | 21 | #include "crypto.h" |
22 | #include "misc.h" | 22 | #include "misc.h" |
23 | #include <cryptopp/modes.h> | ||
24 | #include <cryptopp/aes.h> | ||
25 | #include <cryptopp/sha.h> | ||
26 | 23 | ||
27 | using namespace CryptoPP; | 24 | #include "tomcrypt.h" |
25 | |||
28 | 26 | ||
29 | namespace | ||
30 | { | ||
31 | 27 | ||
32 | enum crypto_method_t g_cur_method = CRYPTO_NONE; | 28 | enum crypto_method_t g_cur_method = CRYPTO_NONE; |
33 | uint8_t g_key[16]; | 29 | uint8_t g_key[16]; |
34 | CBC_Mode<AES>::Encryption g_aes_enc; | ||
35 | CBC_Mode<AES>::Decryption g_aes_dec; | ||
36 | bool g_aes_enc_key_dirty; /* true of g_aes_enc key needs to be updated */ | ||
37 | bool g_aes_dec_key_dirty; /* same for g_aes_dec */ | ||
38 | 30 | ||
39 | int cbc_mac2( | 31 | int cbc_mac2( |
40 | const uint8_t *in_data, /* Input data */ | 32 | const uint8_t *in_data, /* Input data */ |
@@ -46,25 +38,20 @@ int cbc_mac2( | |||
46 | bool encrypt /* 1 to encrypt, 0 to decrypt */ | 38 | bool encrypt /* 1 to encrypt, 0 to decrypt */ |
47 | ) | 39 | ) |
48 | { | 40 | { |
41 | int cipher = register_cipher(&aes_desc); | ||
42 | symmetric_CBC cbc; | ||
43 | cbc_start(cipher, iv, key, 16, 0, &cbc); | ||
44 | |||
49 | /* encrypt */ | 45 | /* encrypt */ |
50 | if(encrypt) | 46 | if(encrypt) |
51 | { | 47 | { |
52 | /* update keys if neeeded */ | ||
53 | if(g_aes_enc_key_dirty) | ||
54 | { | ||
55 | /* we need to provide an IV with the key, although we change it | ||
56 | * everytime we run the cipher anyway */ | ||
57 | g_aes_enc.SetKeyWithIV(g_key, 16, iv, 16); | ||
58 | g_aes_enc_key_dirty = false; | ||
59 | } | ||
60 | g_aes_enc.Resynchronize(iv, 16); | ||
61 | uint8_t tmp[16]; | 48 | uint8_t tmp[16]; |
62 | /* we need some output buffer, either a temporary one if we are CBC-MACing | 49 | /* we need some output buffer, either a temporary one if we are CBC-MACing |
63 | * only, or use output buffer if available */ | 50 | * only, or use output buffer if available */ |
64 | uint8_t *out_ptr = (out_data == NULL) ? tmp : out_data; | 51 | uint8_t *out_ptr = (out_data == NULL) ? tmp : out_data; |
65 | while(nr_blocks-- > 0) | 52 | while(nr_blocks-- > 0) |
66 | { | 53 | { |
67 | g_aes_enc.ProcessData(out_ptr, in_data, 16); | 54 | cbc_encrypt(in_data, out_ptr, 16, &cbc); |
68 | /* if this is the last block, copy CBC-MAC */ | 55 | /* if this is the last block, copy CBC-MAC */ |
69 | if(nr_blocks == 0 && out_cbc_mac) | 56 | if(nr_blocks == 0 && out_cbc_mac) |
70 | memcpy(out_cbc_mac, out_ptr, 16); | 57 | memcpy(out_cbc_mac, out_ptr, 16); |
@@ -78,24 +65,17 @@ int cbc_mac2( | |||
78 | /* decrypt */ | 65 | /* decrypt */ |
79 | else | 66 | else |
80 | { | 67 | { |
68 | cbc_decrypt(in_data, out_data, nr_blocks * 16, &cbc); | ||
69 | |||
81 | /* update keys if neeeded */ | 70 | /* update keys if neeeded */ |
82 | if(g_aes_dec_key_dirty) | ||
83 | { | ||
84 | /* we need to provide an IV with the key, although we change it | ||
85 | * everytime we run the cipher anyway */ | ||
86 | g_aes_dec.SetKeyWithIV(g_key, 16, iv, 16); | ||
87 | g_aes_dec_key_dirty = false; | ||
88 | } | ||
89 | /* we cannot produce a CBC-MAC in decrypt mode, output buffer exists */ | 71 | /* we cannot produce a CBC-MAC in decrypt mode, output buffer exists */ |
90 | if(out_cbc_mac || out_data == NULL) | 72 | if(out_cbc_mac || out_data == NULL) |
91 | return CRYPTO_ERROR_INVALID_OP; | 73 | return CRYPTO_ERROR_INVALID_OP; |
92 | g_aes_dec.Resynchronize(iv, 16); | 74 | |
93 | g_aes_dec.ProcessData(out_data, in_data, nr_blocks * 16); | ||
94 | return CRYPTO_ERROR_SUCCESS; | 75 | return CRYPTO_ERROR_SUCCESS; |
95 | } | 76 | } |
96 | } | 77 | } |
97 | 78 | ||
98 | } | ||
99 | 79 | ||
100 | int crypto_setup(struct crypto_key_t *key) | 80 | int crypto_setup(struct crypto_key_t *key) |
101 | { | 81 | { |
@@ -104,8 +84,7 @@ int crypto_setup(struct crypto_key_t *key) | |||
104 | { | 84 | { |
105 | case CRYPTO_KEY: | 85 | case CRYPTO_KEY: |
106 | memcpy(g_key, key->u.key, 16); | 86 | memcpy(g_key, key->u.key, 16); |
107 | g_aes_dec_key_dirty = true; | 87 | |
108 | g_aes_enc_key_dirty = true; | ||
109 | return CRYPTO_ERROR_SUCCESS; | 88 | return CRYPTO_ERROR_SUCCESS; |
110 | default: | 89 | default: |
111 | return CRYPTO_ERROR_BADSETUP; | 90 | return CRYPTO_ERROR_BADSETUP; |
@@ -128,19 +107,17 @@ int crypto_apply( | |||
128 | 107 | ||
129 | void sha_1_init(struct sha_1_params_t *params) | 108 | void sha_1_init(struct sha_1_params_t *params) |
130 | { | 109 | { |
131 | params->object = new SHA1; | 110 | sha1_init(¶ms->state); |
132 | } | 111 | } |
133 | 112 | ||
134 | void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size) | 113 | void sha_1_update(struct sha_1_params_t *params, uint8_t *buffer, int size) |
135 | { | 114 | { |
136 | reinterpret_cast<SHA1 *>(params->object)->Update(buffer, size); | 115 | sha1_process(¶ms->state, buffer, size); |
137 | } | 116 | } |
138 | 117 | ||
139 | void sha_1_finish(struct sha_1_params_t *params) | 118 | void sha_1_finish(struct sha_1_params_t *params) |
140 | { | 119 | { |
141 | SHA1 *obj = reinterpret_cast<SHA1 *>(params->object); | 120 | sha1_done(¶ms->state, params->hash); |
142 | obj->Final(params->hash); | ||
143 | delete obj; | ||
144 | } | 121 | } |
145 | 122 | ||
146 | void sha_1_output(struct sha_1_params_t *params, uint8_t *out) | 123 | void sha_1_output(struct sha_1_params_t *params, uint8_t *out) |
diff --git a/utils/imxtools/sbtools/crypto.h b/utils/imxtools/sbtools/crypto.h index bdb94bb881..446891b7f2 100644 --- a/utils/imxtools/sbtools/crypto.h +++ b/utils/imxtools/sbtools/crypto.h | |||
@@ -77,11 +77,12 @@ int crypto_apply( | |||
77 | uint32_t crc(uint8_t *data, int size); | 77 | uint32_t crc(uint8_t *data, int size); |
78 | uint32_t crc_continue(uint32_t previous_crc, uint8_t *data, int size); | 78 | uint32_t crc_continue(uint32_t previous_crc, uint8_t *data, int size); |
79 | 79 | ||
80 | #include "tomcrypt.h" | ||
80 | /* sha1.c */ | 81 | /* sha1.c */ |
81 | struct sha_1_params_t | 82 | struct sha_1_params_t |
82 | { | 83 | { |
83 | uint8_t hash[20]; /* final hash */ | 84 | uint8_t hash[20]; /* final hash */ |
84 | void *object; /* pointer to CryptoPP::SHA1 object */ | 85 | hash_state state; /* libtomcrypt state */ |
85 | }; | 86 | }; |
86 | 87 | ||
87 | void sha_1_init(struct sha_1_params_t *params); | 88 | void sha_1_init(struct sha_1_params_t *params); |