forked from SeattleTestbed/seattlelib_v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa.repy
826 lines (603 loc) · 21.7 KB
/
rsa.repy
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
"""
<Program Name>
rsa.repy
<Started>
2008-04-23
<Author>
Modified by Anthony Honstain from the following code:
PyCrypto which is authored by Dwayne C. Litzenberger
Seattle's origional rsa.repy which is Authored by:
Adapted by Justin Cappos from the version by:
author = "Sybren Stuvel, Marloes de Boer and Ivo Tamboer"
<Purpose>
This is the main interface for using the ported RSA implementation.
<Notes on port>
The random function can not be defined with the initial construction of
the RSAImplementation object, it is hard coded into number_* functions.
"""
include random.repy
include pycryptorsa.repy
def rsa_gen_pubpriv_keys(bitsize):
"""
<Purpose>
Will generate a new key object with a key size of the argument
bitsize and return it. A recommended value would be 1024.
<Arguments>
bitsize:
The number of bits that the key should have. This
means the modulus (publickey - n) will be in the range
[2**(bitsize - 1), 2**(bitsize) - 1]
<Exceptions>
None
<Side Effects>
Key generation will result in call to metered function for
random data.
<Return>
Will return a key object that rsa_encrypt, rsa_decrypt, rsa_sign,
rsa_validate can use to preform their tasts.
"""
rsa_implementation = RSA_RSAImplementation()
# The key returned is of type RSA_RSAobj which is derived
# from pubkey_pubkey, and wraps a _slowmath_RSAKey object
rsa_key = rsa_implementation.generate(bitsize)
return ({'e':rsa_key.e, 'n':rsa_key.n },
{'d':rsa_key.d, 'p':rsa_key.p, 'q':rsa_key.q })
def _rsa_keydict_to_keyobj(publickey = None, privatekey = None):
"""
<Purpose>
Will generate a new key object using the data from the dictionary
passed. This is used because the pycrypto scheme uses
a key object but we want backwards compatibility which
requires a dictionary.
<Arguments>
publickey:
Must be a valid publickey dictionary of
the form {'n': 1.., 'e': 6..} with the keys
'n' and 'e'.
privatekey:
Must be a valid privatekey dictionary of
the form {'d':1.., 'p':1.. 'q': 1..} with
the keys 'd', 'p', and 'q'
<Exceptions>
TypeError if neither argument is provided.
ValueError if public or private key is invalid.
<Side Effects>
None
<Return>
Will return a key object that rsa_encrypt, rsa_decrypt, rsa_sign,
rsa_validate will use.
"""
if publickey is not None:
if not rsa_is_valid_publickey(publickey):
raise ValueError, "Invalid public key"
if privatekey is not None:
if not rsa_is_valid_privatekey(privatekey):
raise ValueError, "Invalid private key"
if publickey is None and privatekey is None:
raise TypeError("Must provide either private or public key dictionary")
if publickey is None:
publickey = {}
if privatekey is None:
privatekey = {}
n = None
e = None
d = None
p = None
q = None
if 'd' in privatekey:
d = long(privatekey['d'])
if 'p' in privatekey:
p = long(privatekey['p'])
if 'q' in privatekey:
q = long(privatekey['q'])
if 'n' in publickey:
n = long(publickey['n'])
# n is needed for a private key even thought it is not
# part of the standard public key dictionary.
else: n = p*q
if 'e' in publickey:
e = long(publickey['e'])
rsa_implementation = RSA_RSAImplementation()
rsa_key = rsa_implementation.construct((n,e,d,p,q))
return rsa_key
def rsa_encrypt(message, publickey):
"""
<Purpose>
Will use the key to encrypt the message string.
If the string is to large to be encrypted it will be broken
into chunks that are suitable for the keys modulus,
by the _rsa_chopstring function.
<Arguments>
message:
A string to be encrypted, there is no restriction on size.
publickey:
Must be a valid publickey dictionary of
the form {'n': 1.., 'e': 6..} with the keys
'n' and 'e'.
<Exceptions>
ValueError if public key is invalid.
_slowmath_error if the key object lacks a public key
(elements 'e' and 'n').
<Side Effects>
None
<Return>
Will return a string with the cypher text broken up and stored
in the seperate integer representation. For example it might
be similar to the string " 1422470 3031373 65044827" but with
much larger integers.
"""
# A key object is created to interact with the PyCrypto
# encryption suite. The object contains key data and
# the necessary rsa functions.
temp_key_obj = _rsa_keydict_to_keyobj(publickey)
return _rsa_chopstring(message, temp_key_obj, temp_key_obj.encrypt)
def rsa_decrypt(cypher, privatekey):
"""
<Purpose>
Will use the private key to decrypt the cypher string.
If the plaintext string was to large to be encrypted it will
use _rsa_gluechops and _rsa_unpicklechops to reassemble the
origional plaintext after the individual peices are decrypted.
<Arguments>
cypher:
The encrypted string that was returned by rsa_encrypt.
Example:
Should have the form " 142247030 31373650 44827705"
privatekey:
Must be a valid privatekey dictionary of
the form {'d':1.., 'p':1.. 'q': 1..} with
the keys 'd', 'p', and 'q'
<Exceptions>
ValueError if private key is invalid.
_slowmath_error if the key object lacks a private key
(elements 'd' and 'n').
<Return>
This will return the plaintext string that was encrypted
with rsa_encrypt.
"""
# A key object is created to interact with the PyCrypto
# encryption suite. The object contains key data and
# the necessary rsa functions.
temp_key_obj = _rsa_keydict_to_keyobj(privatekey = privatekey)
return _rsa_gluechops(cypher, temp_key_obj, temp_key_obj.decrypt)
def rsa_sign(message, privatekey):
"""
<Purpose>
Will use the key to sign the plaintext string.
<None>
If the string is to large to be encrypted it will be broken
into chunks that are suitable for the keys modulus,
by the _rsa_chopstring function.
<Arguments>
message:
A string to be signed, there is no restriction on size.
privatekey:
Must be a valid privatekey dictionary of
the form {'d':1.., 'p':1.. 'q': 1..} with
the keys 'd', 'p', and 'q'
<Exceptions>
ValueError if private key is invalid.
_slowmath_error if the key object lacks a private key
(elements 'd' and 'n').
<Side Effects>
None
<Return>
Will return a string with the cypher text broken up and stored
in the seperate integer representation. For example it might
be similar to the string " 1422470 3031373 65044827" but with
much larger integers.
"""
# A key object is created to interact with the PyCrypto
# encryption suite. The object contains key data and
# the necessary rsa functions.
temp_key_obj = _rsa_keydict_to_keyobj(privatekey = privatekey)
return _rsa_chopstring(message, temp_key_obj, temp_key_obj.sign)
def rsa_verify(cypher, publickey):
"""
<Purpose>
Will use the private key to decrypt the cypher string.
If the plaintext string was to large to be encrypted it will
use _rsa_gluechops and _rsa_unpicklechops to reassemble the
origional plaintext after the individual peices are decrypted.
<Arguments>
cypher:
The signed string that was returned by rsa_sign.
Example:
Should have the form " 1422470 3031373 65044827"
publickey:
Must be a valid publickey dictionary of
the form {'n': 1.., 'e': 6..} with the keys
'n' and 'e'.
<Exceptions>
ValueError if public key is invalid.
_slowmath_error if the key object lacks a public key
(elements 'e' and 'n').
<Side Effects>
None
<Return>
This will return the plaintext string that was signed by
rsa_sign.
"""
# A key object is created to interact with the PyCrypto
# encryption suite. The object contains key data and
# the necessary rsa functions.
temp_key_obj = _rsa_keydict_to_keyobj(publickey)
return _rsa_gluechops(cypher, temp_key_obj, temp_key_obj.verify)
def _rsa_chopstring(message, key, function):
"""
<Purpose>
Splits 'message' into chops that are at most as long as
(key.size() / 8 - 1 )bytes.
Used by 'encrypt' and 'sign'.
<Notes on chopping>
If a 1024 bit key was used, then the message would be
broken into length x, where 1<= x <= 126.
(1023 / 8) - 1 = 126
After being converted to a long, the result would
be at most 1009 bits and at least 9 bits.
maxstring = chr(1) + chr(255)*126
minstring = chr(1) + chr(0)
number_bytes_to_long(maxstring)
=> 2**1009 - 1
number_bytes_to_long(minstring)
=> 256
Given the large exponent used by default (65537)
this will ensure that small message are okay and that
large messages do not overflow and cause pycrypto to
silently fail (since its behavior is undefined for a
message greater then n-1 (where n in the key modulus).
WARNING: key.encrypt could have undefined behavior
in the event a larger message is encrypted.
"""
msglen = len(message)
# the size of the key in bits, minus one
# so if the key was a 1024 bits, key.size() returns 1023
nbits = key.size()
# JAC: subtract a byte because we're going to add an extra char on the front
# to properly handle leading \000 bytes and ensure no loss of information.
nbytes = int(nbits / 8) - 1
blocks = int(msglen / nbytes)
if msglen % nbytes > 0:
blocks += 1
# cypher will contain the integers returned from either
# sign or encrypt.
cypher = []
for bindex in range(blocks):
offset = bindex * nbytes
block = message[offset:offset+nbytes]
# key.encrypt will return a bytestring
# IMPORTANT: The block is padded with a '\x01' to ensure
# that no information is lost when the key transforms the block
# into its long representation prior to encryption. It is striped
# off in _rsa_gluechops.
# IMPORTANT: both rsa_encrypt and rsa_sign which use _rsa_chopstring
# will pass the argument 'function' a reference to encrypt or
# sign from the baseclass publickey.publickey, they will return
# the cypher as a tuple, with the first element being the desired
# integer result.
# Example result : ( 1023422341232124123212 , )
# IMPORTANT: the second arguement to function is ignored
# by PyCrypto but required for different algorithms.
cypher.append( function(chr(1) + block, '0')[0])
return _rsa_picklechops(cypher)
def _rsa_gluechops(chops, key, function):
"""
Glues chops back together into a string. Uses _rsa_unpicklechops to
get a list of cipher text blocks in byte form, then key.decrypt
is used and the '\x01' pad is striped.
Example
chops=" 126864321546531240600979768267740190820"
after _rsa_unpicklechops(chops)
chops=['\x0b\xed\xf5\x0b;G\x80\xf4\x06+\xff\xd3\xf8\x1b\x8f\x9f']
Used by 'decrypt' and 'verify'.
"""
message = ""
# _rsa_unpicklechops returns a list of the choped encrypted text
# Will be a list with elements of type long
chops = _rsa_unpicklechops(chops)
for cpart in chops:
# decrypt will return the plaintext message as a bytestring
message += function(cpart)[1:] # Remove the '\x01'
return message
def _rsa_picklechops(chops):
"""previously used to pickles and base64encodes it's argument chops"""
retstring = ''
for item in chops:
# the elements of chops will be of type long
retstring = retstring + ' ' + str(item)
return retstring
def _rsa_unpicklechops(string):
"""previously used to base64decode and unpickle it's argument string"""
retchops = []
for item in string.split():
retchops.append(long(item))
return retchops
def rsa_is_valid_privatekey(key):
"""
<Purpose>
This tries to determine if a key is valid. If it returns False, the
key is definitely invalid. If True, the key is almost certainly valid
<Arguments>
key:
A dictionary of the form {'d':1.., 'p':1.. 'q': 1..}
with the keys 'd', 'p', and 'q'
<Exceptions>
None
<Side Effects>
None
<Return>
If the key is valid, True will be returned. Otherwise False will
be returned.
"""
# must be a dict
if type(key) is not dict:
return False
# missing the right keys
if 'd' not in key or 'p' not in key or 'q' not in key:
return False
# has extra data in the key
if len(key) != 3:
return False
for item in ['d', 'p', 'q']:
# must have integer or long types for the key components...
if type(key[item]) is not int and type(key[item]) is not long:
return False
if number_isPrime(key['p']) and number_isPrime(key['q']):
# Seems valid...
return True
else:
return False
def rsa_is_valid_publickey(key):
"""
<Purpose>
This tries to determine if a key is valid. If it returns False, the
key is definitely invalid. If True, the key is almost certainly valid
<Arguments>
key:
A dictionary of the form {'n': 1.., 'e': 6..} with the
keys 'n' and 'e'.
<Exceptions>
None
<Side Effects>
None
<Return>
If the key is valid, True will be returned. Otherwise False will
be returned.
"""
# must be a dict
if type(key) is not dict:
return False
# missing the right keys
if 'e' not in key or 'n' not in key:
return False
# has extra data in the key
if len(key) != 2:
return False
for item in ['e', 'n']:
# must have integer or long types for the key components...
if type(key[item]) is not int and type(key[item]) is not long:
return False
if key['e'] < key['n']:
# Seems valid...
return True
else:
return False
def rsa_publickey_to_string(publickey):
"""
<Purpose>
To convert a publickey to a string. It will read the
publickey which should a dictionary, and return it in
the appropriate string format.
<Arguments>
publickey:
Must be a valid publickey dictionary of
the form {'n': 1.., 'e': 6..} with the keys
'n' and 'e'.
<Exceptions>
ValueError if the publickey is invalid.
<Side Effects>
None
<Return>
A string containing the publickey.
Example: if the publickey was {'n':21, 'e':3} then returned
string would be "3 21"
"""
if not rsa_is_valid_publickey(publickey):
raise ValueError, "Invalid public key"
return str(publickey['e'])+" "+str(publickey['n'])
def rsa_string_to_publickey(mystr):
"""
<Purpose>
To read a private key string and return a dictionary in
the appropriate format: {'n': 1.., 'e': 6..}
with the keys 'n' and 'e'.
<Arguments>
mystr:
A string containing the publickey, should be in the format
created by the function rsa_publickey_to_string.
Example if e=3 and n=21, mystr = "3 21"
<Exceptions>
ValueError if the string containing the privateky is
in a invalid format.
<Side Effects>
None
<Return>
Returns a publickey dictionary of the form
{'n': 1.., 'e': 6..} with the keys 'n' and 'e'.
"""
if len(mystr.split()) != 2:
raise ValueError, "Invalid public key string"
return {'e':long(mystr.split()[0]), 'n':long(mystr.split()[1])}
def rsa_privatekey_to_string(privatekey):
"""
<Purpose>
To convert a privatekey to a string. It will read the
privatekey which should a dictionary, and return it in
the appropriate string format.
<Arguments>
privatekey:
Must be a valid privatekey dictionary of
the form {'d':1.., 'p':1.. 'q': 1..} with
the keys 'd', 'p', and 'q'
<Exceptions>
ValueError if the privatekey is invalid.
<Side Effects>
None
<Return>
A string containing the privatekey.
Example: if the privatekey was {'d':21, 'p':3, 'q':7} then returned
string would be "21 3 7"
"""
if not rsa_is_valid_privatekey(privatekey):
raise ValueError, "Invalid private key"
return str(privatekey['d'])+" "+str(privatekey['p'])+" "+str(privatekey['q'])
def rsa_string_to_privatekey(mystr):
"""
<Purpose>
To read a private key string and return a dictionary in
the appropriate format: {'d':1.., 'p':1.. 'q': 1..}
with the keys 'd', 'p', and 'q'
<Arguments>
mystr:
A string containing the privatekey, should be in the format
created by the function rsa_privatekey_to_string.
Example mystr = "21 7 3"
<Exceptions>
ValueError if the string containing the privateky is
in a invalid format.
<Side Effects>
None
<Return>
Returns a privatekey dictionary of the form
{'d':1.., 'p':1.. 'q': 1..} with the keys 'd', 'p', and 'q'.
"""
if len(mystr.split()) != 3:
raise ValueError, "Invalid private key string"
return {'d':long(mystr.split()[0]), 'p':long(mystr.split()[1]), 'q':long(mystr.split()[2])}
def rsa_privatekey_to_file(key,filename):
"""
<Purpose>
To write a privatekey to a file. It will convert the
privatekey which should a dictionary, to the appropriate format
and write it to a file, so that it can be read by
rsa_file_to_privatekey.
<Arguments>
privatekey:
Must be a valid privatekey dictionary of
the form {'d':1.., 'p':1.. 'q': 1..} with
the keys 'd', 'p', and 'q'
filename:
The string containing the name for the desired
publickey file.
<Exceptions>
ValueError if the privatekey is invalid.
IOError if the file cannot be opened.
<Side Effects>
file(filename, "w") will be written to.
<Return>
None
"""
if not rsa_is_valid_privatekey(key):
raise ValueError, "Invalid private key"
fileobject = file(filename,"w")
fileobject.write(rsa_privatekey_to_string(key))
fileobject.close()
def rsa_file_to_privatekey(filename):
"""
<Purpose>
To read a file containing a key that was created with
rsa_privatekey_to_file and return it in the appropriate
format: {'d':1.., 'p':1.. 'q': 1..} with the keys 'd', 'p', and 'q'
<Arguments>
filename:
The name of the file containing the privatekey.
<Exceptions>
ValueError if the file contains an invalid private key string.
IOError if the file cannot be opened.
<Side Effects>
None
<Return>
Returns a privatekey dictionary of the form
{'d':1.., 'p':1.. 'q': 1..} with the keys 'd', 'p', and 'q'.
"""
fileobject = file(filename,'r')
privatekeystring = fileobject.read()
fileobject.close()
return rsa_string_to_privatekey(privatekeystring)
def rsa_publickey_to_file(publickey, filename):
"""
<Purpose>
To write a publickey to a file. It will convert the
publickey which should a dictionary, to the appropriate format
and write it to a file, so that it can be read by
rsa_file_to_publickey.
<Arguments>
publickey:
Must be a valid publickey dictionary of
the form {'n': 1.., 'e': 6..} with the keys
'n' and 'e'.
filename:
The string containing the name for the desired
publickey file.
<Exceptions>
ValueError if the publickey is invalid.
IOError if the file cannot be opened.
<Side Effects>
file(filename, "w") will be written to.
<Return>
None
"""
if not rsa_is_valid_publickey(publickey):
raise ValueError, "Invalid public key"
fileobject = file(filename,"w")
fileobject.write(rsa_publickey_to_string(publickey))
fileobject.close()
def rsa_file_to_publickey(filename):
"""
<Purpose>
To read a file containing a key that was created with
rsa_publickey_to_file and return it in the appropriate
format: {'n': 1.., 'e': 6..} with the keys 'n' and 'e'.
<Arguments>
filename:
The name of the file containing the publickey.
<Exceptions>
ValueError if the file contains an invalid public key string.
IOError if the file cannot be opened.
<Side Effects>
None
<Return>
Returns a publickey dictionary of the form
{'n': 1.., 'e': 6..} with the keys 'n' and 'e'.
"""
fileobject = file(filename,'r')
publickeystring = fileobject.read()
fileobject.close()
return rsa_string_to_publickey(publickeystring)
def rsa_matching_keys(privatekey, publickey):
"""
<Purpose>
Determines if a pair of public and private keys match and allow
for encryption/decryption.
<Arguments>
privatekey: The private key*
publickey: The public key*
* The dictionary structure, not the string or file name
<Returns>
True, if they can be used. False otherwise.
"""
# We will attempt to encrypt then decrypt and check that the message matches
testmessage = "A quick brown fox."
# Encrypt with the public key
encryptedmessage = rsa_encrypt(testmessage, publickey)
# Decrypt with the private key
try:
decryptedmessage = rsa_decrypt(encryptedmessage, privatekey)
except TypeError:
# If there was an exception, assume the keys are to blame
return False
except OverflowError:
# There was an overflow while decrypting, blame the keys
return False
# Test for a match
return (testmessage == decryptedmessage)