diff options
Diffstat (limited to 'utils/imxtools/sbtools/crypto.cpp')
-rw-r--r-- | utils/imxtools/sbtools/crypto.cpp | 51 |
1 files changed, 14 insertions, 37 deletions
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) |