pycryptodome icon indicating copy to clipboard operation
pycryptodome copied to clipboard

suggest to add sm4 encrypt and decrypt

Open l1t1 opened this issue 5 years ago • 7 comments

the pure python version(pysm4 on github) is very slow, i cannot port the c version into python by myself.

l1t1 avatar Oct 03 '20 13:10 l1t1

def test_SM4_ECB(n):
 #from Crypto.Cipher import AES
 from pysm4 import encrypt_ecb, decrypt_ecb
 import time
 #obj = AES.new('This is a key123', AES.MODE_ECB, 'This is an IV456')
 message = 'a' * 160
 t1 = time.time()
 key='This is a key123'
 for i in xrange(10000*n):
     ciphertext = encrypt_ecb(message,key)
     #obj2 = AES.new('This is a key123', AES.MODE_ECB, 'This is an IV456')
     text = decrypt_ecb(ciphertext,key)
     #print text
 t2 = time.time()
 print (t2 - t1)

def test_AES_ECB(n):
 from Crypto.Cipher import AES
 import time
 obj = AES.new('This is a key123', AES.MODE_ECB, 'This is an IV456')
 message = 'a' * 160
 t1 = time.time()
 for i in xrange(10000*n):
     ciphertext = obj.encrypt(message)
     obj2 = AES.new('This is a key123', AES.MODE_ECB, 'This is an IV456')
     text = obj2.decrypt(ciphertext)
     #print text
 t2 = time.time()
 print (t2 - t1)

test_SM4_ECB(1) costs 600 s, while test_ASE_ECB(1) costs 0.1 s

l1t1 avatar Oct 03 '20 13:10 l1t1

the c code https://github.com/NEWPLAN/SMx/tree/master/SM4/Linux

l1t1 avatar Oct 04 '20 04:10 l1t1

my test of sm4 vs aes(https://github.com/zhouyangchao/AES) encrpt 1000000 times

aes
real    0m20.359s
user    0m6.748s
sys     0m0.008s

sm4
real    0m0.796s
user    0m0.266s
sys     0m0.001s

l1t1 avatar Oct 04 '20 05:10 l1t1

i run this code on pypy, 3 time faster than the original python

def test_SM4_ECB(n):
  #from pysm4 import encrypt_ecb, decrypt_ecb
  import time
  message = 'a' * 16
  t1 = time.time()
  key='This is a key123'
  for i in xrange(10000*n):ciphertext = encrypt_ecb(message,key)
  t2 = time.time()
  print (t2 - t1)

>>>> test_SM4_ECB(1)
9.86400008202
>>>> test_SM4_ECB(10)
84.7309999466
#python 3.5
>>> def test_SM4_ECB(n):
...   #from pysm4 import encrypt_ecb, decrypt_ecb
...   import time
...   message = 'a' * 16
...   t1 = time.time()
...   key='This is a key123'
...   for i in range(10000*n):ciphertext = encrypt_ecb(message,key)
...   t2 = time.time()
...   print (t2 - t1)
...
>>> test_SM4_ECB(1)
29.570343494415283

l1t1 avatar Oct 08 '20 06:10 l1t1

@l1t1

I am guessing that you need to interface to a China system that is employing SM4, Otherwise, you should be using an international standard like AES.

You could clone one of the github SM4 implementations and adapt numba (JIT compilation) or cython (conventional compilation) to it. Try this search on github: https://github.com/search?o=desc&q=sm4&s=updated&type=Repositories

texadactyl avatar Nov 03 '20 21:11 texadactyl

@texadactyl thanks, I will try numba

l1t1 avatar Nov 17 '20 11:11 l1t1

i found this https://github.com/pyca/pyopenssl docs https://www.pyopenssl.org/en/stable/

l1t1 avatar Nov 18 '20 04:11 l1t1