This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKey.h
147 lines (130 loc) · 3.64 KB
/
Key.h
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
#pragma once
#include <vector>
#include "Permutation.h"
#include "Joiner.h"
class Key
{
public:
Key()
{
// Òàáëèöà ïåðåñòàíîâîê ëåâîé ÷àñòè êëþ÷à
uint8_t k1Table[cntBitsPartKey] =
{
57, 49, 41, 33, 25, 17, 9 , 1 , 58, 50,
42, 34, 26, 18, 10, 2 , 59, 51, 43, 35,
27, 19, 11, 3 , 60, 52, 44, 36,
};
// Òàáëèöà ïåðåñòàíîâîê ïðàâîé ÷àñòè êëþ÷à
uint8_t k2Table[cntBitsPartKey] =
{
63, 55, 47, 39, 31, 23, 15, 7 , 62, 54,
46, 38, 30, 22, 14, 6 , 61, 53, 45, 37,
29, 21, 13, 5 , 28, 20, 12, 4 ,
};
const uint8_t cntBitsKeyComp = 48;
// Ñæèìàþùàÿ òàáëèöà ïåðåñòàíîâîê êëþ÷à
uint8_t keyCompTable[cntBitsKeyComp] =
{
14, 17, 11, 24, 1 , 5 , 3 , 28, 15, 6 , 21, 10,
23, 19, 12, 4 , 26, 8 , 16, 7 , 27, 20, 13, 2 ,
41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32,
};
_leftp = new Permutation(k1Table, cntBitsPartKey);
_rightp = new Permutation(k2Table, cntBitsPartKey);
_keyp = new Permutation(keyCompTable,
cntBitsKeyComp);
}
//------------------------------------------------------
~Key()
{
delete _leftp;
delete _rightp;
delete _keyp;
}
//------------------------------------------------------
/*Ïîëó÷åíèå êëþ÷åé èç îäíîãî êëþ÷à
*
* Ïðèíèìàåò êëþ÷ key
*
* Âîçâðàùàåò âåêòîð êëþ÷åé*/
std::vector<uint64_t> KeyExpansion(uint64_t key)
{
std::vector<uint64_t> keys;
uint32_t left = 0, right = 0;
// Îñóùåñòâëÿåì ïåðåñòàíîâêè â ÷àñòÿõ êëþ÷à
left = _leftp->Process(key);
right = _rightp->Process(key);
// Ïîëó÷àåì êëþ÷è äëÿ ðàóíäîâ ñåòè Ôåéñòåëÿ
for (size_t i = 0; i < cntRounds; i++)
{
left = leftCycleShift(left, _table[i]);
right = leftCycleShift(right, _table[i]);
uint64_t joinKey = joiner.
Join28bitsTo56bits(left, right);
keys.push_back(_keyp->Process(joinKey));
}
return keys;
}
//------------------------------------------------------
/*Ãåíåðèðóåò êëþ÷ äëèíîé â 8 áàéò
*
* Íè÷åãî íå ïðèíèìàåò
*
* Âîçâðàùàåò êëþ÷*/
std::string GenerationKey()
{
const size_t maxBorder = 255, minBorder = 32;
std::string key;
for (size_t i = 0; i < byte; i++)
{
unsigned char ch = minBorder + rand() %
maxBorder;
if (checkEven(ch))
{
ch += 1;
}
key += ch;
}
return key;
}
//------------------------------------------------------
private:
Permutation* _leftp, * _rightp, * _keyp;
Joiner joiner;
static const size_t byte = 8;
static const size_t cntRounds = 16;
static const uint8_t cntBitsPartKey = 28;
const uint8_t _table[cntRounds] = {
1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
};
/*Ïðîâåðêà ÷åòíîñòè êîëè÷åñòâà áèò ñèìâîëà
*
* Ïðèíèìàåò ñèìâîë ch
*
* Âîçâðàùàåò true, åñëè êîëè÷åñòâî áèò ÷åòíî*/
bool checkEven(unsigned char ch)
{
uint8_t cntOnes = 0;
for (size_t i = 0; i < byte; i++)
{
if ((ch >> i) & 1)
{
cntOnes++;
}
}
return cntOnes % 2 == 0;
}
//------------------------------------------------------
/*Öèêëè÷åñêèé ñâäèã ÷èñëà âëåâî
*
* Ïðèíèìàåò ÷èñëî num è êîëè÷åñòâî øàãîâ ñäâèãà step
*
* Âîçâðàùàåò ñìåùåííîå ÷èñëî */
uint32_t leftCycleShift(uint32_t num, uint8_t step)
{
uint32_t part1 = num << step;
uint32_t part2 = num >> (cntBitsPartKey - step);
return part1 | part2;
}
};