-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycrypto.cpp
196 lines (171 loc) · 4.98 KB
/
mycrypto.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdexcept>
#include <sstream>
using std::stringstream;
#include <string>
using std::string;
using std::getline;
#include <iostream>
using std::cerr;
using std::endl;
#include <vector>
using std::vector;
#include <cstring>
#include "mycrypto.hpp"
ByteBlock::ByteBlock(size_t size_, BYTE init_value) :
amount_of_bytes(size_)
{
if(!size_) pBlocks = nullptr;
else {
pBlocks = new BYTE [size_];
memset(pBlocks, init_value, size_);
}
}
ByteBlock::ByteBlock(BYTE * pBlocks_, size_t size_) :
amount_of_bytes(size_)
{
pBlocks = new BYTE [size_];
memcpy(pBlocks, pBlocks_, size_);
}
ByteBlock::ByteBlock(ByteBlock && rhs) :
pBlocks(rhs.pBlocks), amount_of_bytes(rhs.amount_of_bytes)
{
rhs.pBlocks = nullptr;
rhs.amount_of_bytes = 0;
}
ByteBlock::~ByteBlock() {
if(pBlocks) {
memset(pBlocks, 0, amount_of_bytes);
delete [] pBlocks;
}
}
void ByteBlock::operator = (ByteBlock && rhs) {
if(this == &rhs) return;
if(pBlocks) {
memset(pBlocks, 0, amount_of_bytes);
delete [] pBlocks;
}
pBlocks = rhs.pBlocks;
amount_of_bytes = rhs.amount_of_bytes;
rhs.pBlocks = nullptr;
rhs.amount_of_bytes = 0;
}
BYTE * ByteBlock::byte_ptr() {
return pBlocks;
}
const BYTE * ByteBlock::byte_ptr() const {
return pBlocks;
}
BYTE & ByteBlock::operator [] (size_t index) {
return *(pBlocks + index);
}
BYTE ByteBlock::operator [] (size_t index) const {
return *(pBlocks + index);
}
bool ByteBlock::operator == (const ByteBlock & lhs) const {
return pBlocks == lhs.pBlocks;
}
bool ByteBlock::operator != (const ByteBlock & lhs) const {
return !(*this == lhs);
}
void ByteBlock::reset(const BYTE * pBlocks_, size_t size_) {
if(pBlocks) {
memset(pBlocks, 0, amount_of_bytes);
delete [] pBlocks;
}
if(size_ && pBlocks_) {
pBlocks = new BYTE [size_];
memcpy(pBlocks, pBlocks_, size_);
amount_of_bytes = size_;
} else {
pBlocks = nullptr;
amount_of_bytes = 0;
}
}
size_t ByteBlock::size() const {
return amount_of_bytes;
};
ByteBlock ByteBlock::deep_copy() const {
return ByteBlock(pBlocks, amount_of_bytes);
}
ByteBlock ByteBlock::operator () (size_t begin, size_t length) const {
ByteBlock tmp;
tmp.reset(pBlocks + begin, length);
return tmp;
}
void swap(ByteBlock & lhs, ByteBlock & rhs) {
BYTE * p = lhs.pBlocks;
size_t s = lhs.amount_of_bytes;
lhs.pBlocks = rhs.pBlocks;
lhs.amount_of_bytes = rhs.amount_of_bytes;
rhs.pBlocks = p;
rhs.amount_of_bytes = s;
}
vector<ByteBlock> split_blocks(const ByteBlock & src, size_t length) {
vector<ByteBlock> tmp;
int amount = src.size() / length;
int tail = src.size() % length;
for(int i = 0; i < amount; i++)
tmp.push_back(src(i * length, length));
if(tail)
tmp.push_back(src(amount * length, tail));
return tmp;
}
ByteBlock join_blocks(const vector<ByteBlock> & blocks) {
if(blocks.empty()) return ByteBlock();
size_t size_vector = blocks.size();
size_t size_block = blocks[0].size();
size_t size_last = blocks[size_vector - 1].size();
size_t size_byteblock = (size_vector - 1) * size_block + size_last;
ByteBlock tmp(size_byteblock);
for(int i = 0; i < size_vector - 1; i++) {
memcpy(
tmp.byte_ptr() + i * size_block,
blocks[i].byte_ptr(),
size_block
);
}
memcpy(
tmp.byte_ptr() + (size_vector - 1) * size_block,
blocks[size_vector - 1].byte_ptr(),
size_last
);
return tmp;
}
void xor_blocks(ByteBlock & to_assign, const ByteBlock & lhs, const ByteBlock & rhs) {
size_t result_size = lhs.size() > rhs.size() ? rhs.size() : lhs.size();
ByteBlock tmp(result_size);
for(size_t i = 0; i < result_size; i++)
tmp[i] = lhs[i] ^ rhs[i];
to_assign = std::move(tmp);
}
inline char to_hex_literal(BYTE number) {
if(number < 10) return '0' + number;
if(number < 16) return 'a' + number - 10;
throw std::invalid_argument("to_hex_literal: " + std::to_string(number));
}
inline BYTE from_hex_literal(char symbol) {
if(isdigit(symbol)) return symbol - '0';
if(symbol >= 'a' && symbol <= 'f') return symbol - 'a' + 10;
if(symbol >= 'A' && symbol <= 'F') return symbol - 'A' + 10;
throw std::invalid_argument("from_hex_literal: " + std::to_string(symbol));
}
string hex_representation(const ByteBlock & bb) {
stringstream ss;
for(int i = 0; i < bb.size(); i++) {
ss << to_hex_literal(bb[i] >> 4);
ss << to_hex_literal(bb[i] & 0xF);
}
string result;
getline(ss, result);
return result;
}
ByteBlock hex_to_bytes(const string & s) {
if(s.size() % 2) throw std::invalid_argument("length of hex-string must be even number");
int size = s.size() / 2;
ByteBlock result(size);
for(int i = 0; i < size; i++) {
result[i] = from_hex_literal(s[2 * i]) << 4;
result[i] += from_hex_literal(s[2 * i + 1]);
}
return result;
}