-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrsa_module.py
258 lines (197 loc) · 7.56 KB
/
rsa_module.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
# Author: Euaggelos Mouroutsos
# Secure Communication Project
# Implementation of a cryptographic utility
# Copyright (c) 2015, Euaggelos Mouroutsos
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met: 1. Redistributions
# of source code must retain the above copyright notice, this list of conditions and
# the following disclaimer. 2. Redistributions in binary form must reproduce the
# above copyright notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from binascii import hexlify
from Crypto.Util import strxor
import random
RSA_BLOCK_SIZE = 128
def pad(s):
return s + (RSA_BLOCK_SIZE - len(s) % RSA_BLOCK_SIZE) * chr(RSA_BLOCK_SIZE - len(s) % RSA_BLOCK_SIZE)
def unpad(s):
return s[0:-ord(s[-1])]
def msg_db(m, desc, mode):
# print debugging messages
print "$ %s -> length %d" % (desc, len(m))
print "\t****"
if mode == "hex":
print("\t: %s" % hexlify(m))
else:
print("\t: %s" % m)
print "\t****\n"
def rsa_key(bits=2048):
# generate rsa key default size 2048 bit
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=bits,
backend=default_backend()
)
return private_key
def load_priv_key_rsa(keypath):
# load rsa key from file in pem format
with open(keypath, "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
return private_key
def load_pub_key_rsa(keypath):
# load rsa key from file in pem format
with open(keypath, "rb") as key_file:
public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)
return public_key
def serialize_private_key(private_key):
# serialize a private key before saving to file
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
return pem
def serialize_public_key(private_key):
# serialize a public key before saving to file
public_key = private_key.public_key()
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
return pem
def save_to_file(data, file):
# save some data to some file
with open(file, "wb") as f:
f.write(data)
f.close()
def sign_data(message, private_key):
# sign data with a private key
signer = private_key.signer(
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()), # change to sha 512 in the future
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
# message = b"A message I want to sign"
signer.update(message)
signature = signer.finalize()
return signature
def public_key(private_key):
# return the public key from a private key
return private_key.public_key()
def verify_data(message, signature, public_key):
verifier = public_key.verifier(
signature,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH
),
hashes.SHA256()
)
verifier.update(message)
verifier.verify()
def rsa_encrypt(message, public_key):
# ecnrypt a message with rsa oaep padding
ciphertext = public_key.encrypt(
message,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
return ciphertext
# dectrypt encrypted message (aka ciphertext)
def rsa_decrypt(ciphertext, private_key):
plaintext = private_key.decrypt(ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
return plaintext
def rsa_cbc_encrypt(data, key):
# apply rsa encryption with cipber block chaining
# before enryption pad data to rsa block size 128 bits
data_padded = pad(data)
# split input data in chuncks of block size
c = []
for i in xrange(0, len(data_padded), RSA_BLOCK_SIZE):
x = data_padded[i:i + RSA_BLOCK_SIZE]
c.append(x)
ciphers_list = []
# prepair IV vector for the first encryption round
# Random.get_random_bytes(128)
IV = ''.join(chr(random.randint(0, 0xFF)) for i in range(128))
IV2 = IV
# encrypt each chunk with rsa
for d in c:
# xor data with iv
dx = strxor.strxor(d, IV)
# encrypt xored data
cipher_text = rsa_encrypt(dx, key)
# update iv from cipher text
IV = cipher_text[0:128]
ciphers_list.append(cipher_text)
# last block save initial IV for decryption
ciphers_list.append(IV2)
return "".join(ciphers_list)
def rsa_cbc_dencrypt(data, key):
# extract iv from encrypted data
IV = data[-128:]
cipher_text = data[:-128]
# split cipher text in chunks of 2*RSA Block size
c = []
for i in xrange(0, len(cipher_text), 2 * RSA_BLOCK_SIZE):
x = cipher_text[i:i + 2 * RSA_BLOCK_SIZE]
c.append(x)
# decrypt each chunk
text = []
for d in c:
dx = rsa_decrypt(d, key)
# clear_text=sxor(dx, IV)
clear_text = strxor.strxor(dx, IV)
IV = d[0:128]
text.append(clear_text)
big_data = "".join(text)
r = unpad(big_data)
return r
# generate keys
# server
#key_server = rsa_key(2048)
#pem__private_server = serialize_private_key(key_server)
#pem_public_server = serialize_public_key(key_server)
#save_to_file(pem__private_server, "server_pri.pem")
#save_to_file(pem_public_server, "server_pub.pem")
# client
#key_client = rsa_key(2048)
#pem__private_client = serialize_private_key(key_client)
#pem_public_client = serialize_public_key(key_client)
#save_to_file(pem__private_client, "client_pri.pem")
#save_to_file(pem_public_client, "client_pub.pem")