forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypt.cpp
109 lines (94 loc) · 3.79 KB
/
Encrypt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Encrypt.h"
#include "Data.h"
#include <TrezorCrypto/aes.h>
#include <cassert>
#include <cstring>
#include <stdexcept>
namespace TW::Encrypt {
size_t paddingSize(size_t origSize, size_t blockSize, TWAESPaddingMode paddingMode) {
if (origSize % blockSize == 0) {
// even blocks
if (paddingMode == TWAESPaddingModePKCS7) {
return blockSize;
}
return 0;
}
// non-even
return blockSize - origSize % blockSize;
}
Data AESCBCEncrypt(const Data& key, const Data& data, Data& iv, TWAESPaddingMode paddingMode) {
aes_encrypt_ctx ctx;
if (aes_encrypt_key(key.data(), static_cast<int>(key.size()), &ctx) == EXIT_FAILURE) {
throw std::invalid_argument("Invalid key");
}
// Message is padded to round block size, or by a full padding block if even
const size_t blockSize = AES_BLOCK_SIZE;
const auto padding = paddingSize(data.size(), blockSize, paddingMode);
const auto resultSize = data.size() + padding;
Data result(resultSize);
size_t idx;
for (idx = 0; idx < resultSize - blockSize; idx += blockSize) {
aes_cbc_encrypt(data.data() + idx, result.data() + idx, blockSize, iv.data(), &ctx);
}
// last block
if (idx < resultSize) {
uint8_t padded[blockSize] = {0};
if (paddingMode == TWAESPaddingModePKCS7) {
std::memset(padded, static_cast<int>(padding), blockSize);
}
std::memcpy(padded, data.data() + idx, data.size() - idx);
aes_cbc_encrypt(padded, result.data() + idx, blockSize, iv.data(), &ctx);
}
return result;
}
Data AESCBCDecrypt(const Data& key, const Data& data, Data& iv, TWAESPaddingMode paddingMode) {
const size_t blockSize = AES_BLOCK_SIZE;
if (data.size() % blockSize != 0) {
throw std::invalid_argument("Invalid data size");
}
assert((data.size() % blockSize) == 0);
aes_decrypt_ctx ctx;
if (aes_decrypt_key(key.data(), static_cast<int>(key.size()), &ctx) != EXIT_SUCCESS) {
throw std::invalid_argument("Invalid key");
}
Data result(data.size());
for (std::size_t i = 0; i < data.size(); i += blockSize) {
aes_cbc_decrypt(data.data() + i, result.data() + i, blockSize, iv.data(), &ctx);
}
if (paddingMode == TWAESPaddingModePKCS7 && result.size() > 0) {
// need to remove padding
assert(result.size() > 0);
const byte paddingSize = result[result.size() - 1];
if (paddingSize <= result.size()) {
// remove last paddingSize number of bytes
const size_t unpaddedSize = result.size() - paddingSize;
Data resultUnpadded = TW::data(result.data(), unpaddedSize);
return resultUnpadded;
}
}
return result;
}
Data AESCTREncrypt(const Data& key, const Data& data, Data& iv) {
aes_encrypt_ctx ctx;
if (aes_encrypt_key(key.data(), static_cast<int>(key.size()), &ctx) != EXIT_SUCCESS) {
throw std::invalid_argument("Invalid key");
}
Data result(data.size());
aes_ctr_encrypt(data.data(), result.data(), static_cast<int>(data.size()), iv.data(), aes_ctr_cbuf_inc, &ctx);
return result;
}
Data AESCTRDecrypt(const Data& key, const Data& data, Data& iv) {
aes_encrypt_ctx ctx;
if (aes_encrypt_key(key.data(), static_cast<int>(key.size()), &ctx) != EXIT_SUCCESS) {
throw std::invalid_argument("Invalid key");
}
Data result(data.size());
aes_ctr_decrypt(data.data(), result.data(), static_cast<int>(data.size()), iv.data(), aes_ctr_cbuf_inc, &ctx);
return result;
}
} // namespace TW::Encrypt