diff --git a/app/src/main/cpp/des.cpp b/app/src/main/cpp/des.cpp index bc2b29b..0093dfc 100644 --- a/app/src/main/cpp/des.cpp +++ b/app/src/main/cpp/des.cpp @@ -177,9 +177,9 @@ bytes des_encrypt(bytes data, uint64_t key) { r.insert(r.end(), x.begin(), x.end()); cbc = input; } - LOGI("des encrypt"); - logBytes(" input", data); - logBytes(" output", r); + LOGI("des encrypt: %llx", key); + logBytes(" input %s", data); + logBytes(" output %s", r); return r; } @@ -203,9 +203,9 @@ bytes tripledes_cbc_encrypt(bytes data, uint64_t key1, uint64_t key2, uint64_t k } cbc = input; } - LOGI("3des encrypt"); - logBytes(" input", data); - logBytes(" output", r); + LOGI("3des encrypt: %llx %llx %llx", key1, key2, key3); + logBytes(" input %s", data); + logBytes(" output %s", r); return r; } @@ -230,15 +230,15 @@ bytes tripledes_cbc_decrypt(bytes data, uint64_t key1, uint64_t key2, uint64_t k } cbc = before_transform; } - LOGI("3des decrypt"); - logBytes(" input", data); - logBytes(" output", r); + LOGI("3des decrypt: %llx %llx %llx", key1, key2, key3); + logBytes(" input %s", data); + logBytes(" output %s", r); return r; } -uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2, uint64_t key3) { +uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2, uint64_t key3, bool pad) { bytes msg = data; - iso9797_pad(msg); + if (pad) iso9797_pad(msg); bytes mac = des_encrypt(msg, key1); mac.erase(mac.begin(), mac.end() - 8); @@ -249,6 +249,10 @@ uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2, uint64_t key3) { return mac_value; } +uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2) { + return iso9797_mac(data, key1, key2, key1, true); +} + bytes KDF(bytes keyseed, int32_t count) { bytes hash(20); bytes msg; diff --git a/app/src/main/cpp/des.h b/app/src/main/cpp/des.h index f850505..7cf9c26 100644 --- a/app/src/main/cpp/des.h +++ b/app/src/main/cpp/des.h @@ -158,12 +158,21 @@ uint64_t des(uint64_t input, uint64_t key, char mode); bytes des_encrypt(bytes data, uint64_t key); bytes tripledes_cbc_encrypt(bytes data, uint64_t key1, uint64_t key2, uint64_t key3); bytes tripledes_cbc_decrypt(bytes data, uint64_t key1, uint64_t key2, uint64_t key3); -uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2, uint64_t key3); +uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2, uint64_t key3, bool pad); +uint64_t iso9797_mac(bytes data, uint64_t key1, uint64_t key2); inline void iso9797_pad(bytes& data) { bytes pad = {0x80, 0,0,0,0,0,0,0}; size_t padsize = 8 - data.size() % 8; data.insert(data.end(), pad.begin(), pad.begin() + padsize); } +inline void iso9797_unpad(bytes& data) { + auto end = data.rbegin(); + while (*end != 0x80) { + end++; + } + end++; + data.erase(end.base(), data.rbegin().base()); +} bytes deriveKeyISO9797(bytes keyseed); bytes deriveKeyDesEDE(bytes keyseed);