Skip to content

Commit c2a1145

Browse files
committed
Update share as word-wise
1 parent 80cf4f7 commit c2a1145

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

bintool/mbedtls_wrapper.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,24 @@ typedef struct iv {
3838
uint8_t bytes[16];
3939
} iv_t; /**< Convenience typedef */
4040

41+
typedef struct aes_key {
42+
/** An array 32 bytes key data. */
43+
union {
44+
uint8_t bytes[32];
45+
uint32_t words[8];
46+
};
47+
} aes_key_t; /**< Convenience typedef */
48+
4149
typedef struct aes_key_share {
50+
/** An array 128 bytes key data, 1 word from each share at a time. */
4251
union {
43-
struct {
44-
/** A 4-way share of the 256-bit value. */
45-
uint8_t bytes_a[32];
46-
uint8_t bytes_b[32];
47-
uint8_t bytes_c[32];
48-
uint8_t bytes_d[32];
49-
};
5052
uint8_t bytes[128];
53+
uint32_t words[32];
5154
};
5255
} aes_key_share_t; /**< Convenience typedef */
5356

5457
typedef signature_t public_t;
5558
typedef message_digest_t private_t;
56-
typedef message_digest_t aes_key_t;
5759

5860
void mb_sha256_buffer(const uint8_t *data, size_t len, message_digest_t *digest_out);
5961
void mb_aes256_buffer(const uint8_t *data, size_t len, uint8_t *data_out, const aes_key_t *key, iv_t *iv);

main.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4724,13 +4724,16 @@ bool encrypt_command::execute(device_map &devices) {
47244724
auto aes_file = get_file_idx(ios::in|ios::binary, 2);
47254725
aes_file->exceptions(std::iostream::failbit | std::iostream::badbit);
47264726

4727-
// Key is stored as a 4-way share, ie X = A ^ B ^ C ^ D
47284727
aes_key_share_t aes_key_share;
47294728
aes_file->read((char*)aes_key_share.bytes, sizeof(aes_key_share.bytes));
47304729

47314730
aes_key_t aes_key;
4732-
for (int i=0; i < sizeof(aes_key); i++) {
4733-
aes_key.bytes[i] = aes_key_share.bytes_a[i] ^ aes_key_share.bytes_b[i] ^ aes_key_share.bytes_c[i] ^ aes_key_share.bytes_d[i];
4731+
// Key is stored as a 4-way share of each word, ie X[0] = A[0] ^ B[0] ^ C[0] ^ D[0], stored as A[0], B[0], C[0], D[0]
4732+
for (int i=0; i < count_of(aes_key.words); i++) {
4733+
aes_key.words[i] = aes_key_share.words[i*4]
4734+
^ aes_key_share.words[i*4 + 1]
4735+
^ aes_key_share.words[i*4 + 2]
4736+
^ aes_key_share.words[i*4 + 3];
47344737
}
47354738

47364739
private_t private_key = {};

0 commit comments

Comments
 (0)