Skip to content

Commit cd36011

Browse files
author
Roland Hedberg
committed
Changed from pycrypto to pycryptodome.
1 parent e81afc3 commit cd36011

File tree

8 files changed

+35
-27
lines changed

8 files changed

+35
-27
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"Topic :: Software Development :: Libraries :: Python Modules",
4242
"Programming Language :: Python :: 2.7",
4343
"Programming Language :: Python :: 3.4"],
44-
install_requires=["pycrypto >= 2.6.1", "requests", "six", "future"],
44+
install_requires=["pycryptodome", "requests", "six", "future"],
4545
tests_require=['pytest'],
4646
zip_safe=False,
4747
scripts=glob.glob('script/*.py'),

src/jwkest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from binascii import unhexlify
1616

17-
__version__ = "1.0.11b0"
17+
__version__ = "1.1.0"
1818

1919
logger = logging.getLogger(__name__)
2020

src/jwkest/aes_key_wrap.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
PyCrypto's AES.
1717
"""
1818
from __future__ import division
19+
from Crypto.Cipher.AES import MODE_ECB
1920

2021
try:
2122
from builtins import hex
@@ -28,12 +29,12 @@
2829
QUAD = struct.Struct('>Q')
2930

3031

31-
def aes_unwrap_key_and_iv(kek, wrapped):
32+
def aes_unwrap_key_and_iv(kek, wrapped, mode=MODE_ECB):
3233
n = (len(wrapped) // 8) - 1
3334
#NOTE: R[0] is never accessed, left in for consistency with RFC indices
3435
r = [None] + [wrapped[i * 8:i * 8 + 8] for i in range(1, n + 1)]
3536
a = QUAD.unpack(wrapped[:8])[0]
36-
decrypt = AES.new(kek).decrypt
37+
decrypt = AES.new(kek, mode).decrypt
3738
for j in range(5, -1, -1): #counting down
3839
for i in range(n, 0, -1): #(n, n-1, ..., 1)
3940
ciphertext = QUAD.pack(a ^ (n * j + i)) + r[i]
@@ -62,11 +63,11 @@ def aes_unwrap_key_withpad(kek, wrapped):
6263
return key[:key_len]
6364

6465

65-
def aes_wrap_key(kek, plaintext, iv=0xa6a6a6a6a6a6a6a6):
66+
def aes_wrap_key(kek, plaintext, iv=0xa6a6a6a6a6a6a6a6, mode=MODE_ECB):
6667
n = len(plaintext) // 8
6768
r = [None] + [plaintext[i * 8:i * 8 + 8] for i in range(0, n)]
6869
a = iv
69-
encrypt = AES.new(kek).encrypt
70+
encrypt = AES.new(kek, mode).encrypt
7071
for j in range(6):
7172
for i in range(1, n + 1):
7273
b = encrypt(QUAD.pack(a) + r[i])

src/jwkest/elliptic.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,19 @@ def y_from_x(x, p, q, n, sign):
349349

350350

351351
if __name__ == "__main__":
352-
import rsa
352+
from Crypto.Random.random import randint
353+
from Crypto.Util.number import getPrime
353354
import time
354355

355356
t = time.time()
356-
n = rsa.get_prime(256 / 8, 20)
357+
n = getPrime(int(256/8))
358+
#n = rsa.get_prime(256 / 8, 20)
357359
tp = time.time() - t
358-
p = rsa.random.randint(1, n)
359-
p1 = (rsa.random.randint(1, n), rsa.random.randint(1, n))
360+
p = randint(1, n)
361+
p1 = (randint(1, n), randint(1, n))
360362
q = curve_q(p1[0], p1[1], p, n)
361-
r1 = rsa.random.randint(1, n)
362-
r2 = rsa.random.randint(1, n)
363+
r1 = randint(1, n)
364+
r2 = randint(1, n)
363365
q1 = mulp(p, q, n, p1, r1)
364366
q2 = mulp(p, q, n, p1, r2)
365367
s1 = mulp(p, q, n, q1, r2)
@@ -368,14 +370,15 @@ def y_from_x(x, p, q, n, sign):
368370
tt = time.time() - t
369371

370372
def test(tcount, bits=256):
371-
n = rsa.get_prime(bits / 8, 20)
372-
p = rsa.random.randint(1, n)
373-
p1 = (rsa.random.randint(1, n), rsa.random.randint(1, n))
373+
n = getPrime(int(bits/8))
374+
#n = rsa.get_prime(bits / 8, 20)
375+
p = randint(1, n)
376+
p1 = (randint(1, n), randint(1, n))
374377
q = curve_q(p1[0], p1[1], p, n)
375-
p2 = mulp(p, q, n, p1, rsa.random.randint(1, n))
378+
p2 = mulp(p, q, n, p1, randint(1, n))
376379

377-
c1 = [rsa.random.randint(1, n) for i in range(tcount)]
378-
c2 = [rsa.random.randint(1, n) for i in range(tcount)]
380+
c1 = [randint(1, n) for i in range(tcount)]
381+
c2 = [randint(1, n) for i in range(tcount)]
379382
c = list(zip(c1, c2))
380383

381384
t = time.time()

src/jwkest/jwk.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from Crypto.PublicKey import RSA
1212
from Crypto.PublicKey.RSA import importKey
13-
from Crypto.PublicKey.RSA import _RSAobj
13+
from Crypto.PublicKey.RSA import RsaKey
1414
from Crypto.Util.asn1 import DerSequence
1515

1616
from requests import request
@@ -110,6 +110,7 @@ def der2rsa(der):
110110
def pem_cert2rsa(pem_file):
111111
# Convert from PEM to DER
112112
pem = open(pem_file).read()
113+
_rsa = RSA.importKey(pem)
113114
lines = pem.replace(" ", '').split()
114115
return der2rsa(a2b_base64(''.join(lines[1:-1])))
115116

@@ -648,7 +649,7 @@ def jwk_wrap(key, use="", kid=""):
648649
:param kid: A key id
649650
:return: The Key instance
650651
"""
651-
if isinstance(key, _RSAobj):
652+
if isinstance(key, RsaKey):
652653
kspec = RSAKey(use=use, kid=kid).load_key(key)
653654
elif isinstance(key, str):
654655
kspec = SYMKey(key=key, use=use, kid=kid)

tests/test_0_jwkest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import base64
22
import os
33
import struct
4-
from jwkest import long2intarr, b64d, b64e
4+
from jwkest import long2intarr
5+
from jwkest import b64d
6+
from jwkest import b64e
57
from jwkest import intarr2long
68
from jwkest import base64_to_long
79
from jwkest import long_to_base64

tests/test_2_jwk.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import Counter
44
import json
55
from Crypto.PublicKey import RSA
6-
from Crypto.PublicKey.RSA import _RSAobj
6+
from Crypto.PublicKey.RSA import RsaKey
77
import struct
88
import six
99
from jwkest.ecc import P256
@@ -65,7 +65,7 @@ def test_urlsafe_base64decode():
6565

6666
def test_pem_cert2rsa():
6767
_ckey = pem_cert2rsa(CERT)
68-
assert isinstance(_ckey, _RSAobj)
68+
assert isinstance(_ckey, RsaKey)
6969

7070

7171
def test_extract_rsa_from_cert_2():
@@ -160,12 +160,12 @@ def test_load_jwk():
160160
assert len(keylist1) == 1
161161
key = keylist1["rsa"][0]
162162
assert key.kty == 'RSA'
163-
assert isinstance(key.key, _RSAobj)
163+
assert isinstance(key.key, RsaKey)
164164

165165

166166
def test_import_rsa_key():
167167
_ckey = RSA.importKey(open(full_path(KEY), 'r').read())
168-
assert isinstance(_ckey, _RSAobj)
168+
assert isinstance(_ckey, RsaKey)
169169
djwk = jwk_wrap(_ckey).to_dict()
170170
print(djwk)
171171
assert _eq(djwk.keys(), ["kty", "e", "n", "p", "q", "d"])

tests/test_4_jwe.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from __future__ import print_function
2-
import array
1+
#from __future__ import print_function
32
import hashlib
43
import os
54
import sys
5+
import array
6+
67
from Crypto.PublicKey import RSA
78

89
from jwkest.aes_gcm import AES_GCM

0 commit comments

Comments
 (0)