-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencFuncs.py
162 lines (107 loc) · 3.66 KB
/
encFuncs.py
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
from Funcs import *
def subBytes(inValue):
subbedBytes = []
for i in range(len(inValue)):
subbedBytes += lookupSBox(inValue[i])
return transposeMtrx(mtrx4x4(subbedBytes))
def prepEncValues(inValue, mode, iv):
if (mode == "CFB" or mode == "OFB" or mode == "CTR"):
inValue = strToHex(inValue)
else:
inValue = mtrx4x4(padHex(strToHex(inValue)))
if(mode == "CFB" or mode == "OFB" or mode == "CTR"):
res = []
for value in divInBlocks(inValue, 16):
res.append(value)
return res
if(iv and mode == "CBC"):
res = []
for idx, value in enumerate(divInBlocks(inValue, 4)):
if (idx == 0):
res.append(xorMatrices(
value, (mtrx4x4((strToHex(iv))))))
else:
res.append(value)
return res
else:
blocks = divInBlocks(inValue, 4)
return blocks
def initEncRound(inValue, key):
return(addRoundKey((inValue), mtrx4x4((key))))
def encRounds(inValue, roundKeys):
for i in range(1, len(roundKeys)-1):
inValue = addRoundKey(
mixCols(shiftRows(subBytes(inValue))), mtrx4x4(roundKeys[i]))
return inValue
def finalEncRound(inValue, key):
return addRoundKey(shiftRows(subBytes(inValue)), mtrx4x4(key))
def encBlock(block, roundKeys):
return finalEncRound(encRounds(initEncRound(
block, roundKeys[0]), roundKeys), roundKeys[-1])
def encECB(inValue, roundkeys):
res = []
for block in inValue:
res.append(encBlock(block, roundkeys))
return res
def encCBC(inValue, iv, roundKeys):
res = []
cipherText = ""
for block in inValue:
if (iv and cipherText != ""):
block = xorMatrices(block, cipherText)
cipherText = encBlock(block, roundKeys)
res.append(cipherText)
return res
def encCFB(inValue, iv, roundKeys):
res = []
for block in inValue:
if res != []:
iv = (res[-4:])
encIV = encBlock(iv, roundKeys)
for blockIdx, block4x4 in enumerate(divInBlocks(block, 4)):
cipCFB = xorMatrices([block4x4], [encIV[blockIdx]])
res.extend(cipCFB)
return res
def encOFB(inValue, iv, roundKeys):
res = []
oldIV = []
for block in inValue:
if oldIV != []:
iv = oldIV
encIV = encBlock(iv, roundKeys)
for blockIdx, block4x4 in enumerate(divInBlocks(block, 4)):
cipOFB = xorMatrices([block4x4], [encIV[blockIdx]])
res.extend(cipOFB)
oldIV = encIV
return res
def increaseCTR(nonce):
nonce = int(prettify(nonce), 16)
nonce += 1
nonce = hex(nonce)[2:]
return mtrx4x4(divInBlocks(nonce, 2))
def encCTR(inValue, nonce, roundKeys):
res = []
oldIV = []
for block in inValue:
if oldIV != []:
nonce = increaseCTR(nonce)
encIV = encBlock(nonce, roundKeys)
for blockIdx, block4x4 in enumerate(divInBlocks(block, 4)):
cipCTR = xorMatrices([block4x4], [encIV[blockIdx % 4]])
res.extend(cipCTR)
oldIV = encIV
return res
def encrypt(inValue, secretKey, rounds, mode="ECB", iv=None):
roundKeys = genRoundKeys(strToHex(secretKey), rounds)
inValue = prepEncValues(inValue, mode, iv)
iv = mtrx4x4(strToHex(iv)) if iv else None
if(mode == "CBC"):
return encCBC(inValue, iv, roundKeys)
elif(mode == "CFB"):
return encCFB(inValue, iv, roundKeys)
elif (mode == "CTR"):
return encCTR(inValue, iv, roundKeys)
elif(mode == "OFB"):
return encOFB(inValue, iv, roundKeys)
elif(mode == "ECB"):
return encECB(inValue, roundKeys)