forked from GmSSL/GmSSL-Python
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheasy_sm4_test.py
More file actions
86 lines (74 loc) · 3.5 KB
/
easy_sm4_test.py
File metadata and controls
86 lines (74 loc) · 3.5 KB
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
import unittest
from .easy_sm4_key import EasySm4CBC, EasySm4GCM
from .gmssl import SM4_BLOCK_SIZE, SM4_CBC_IV_SIZE, SM4_GCM_DEFAULT_TAG_SIZE, Sm4Cbc
class MyTestCase(unittest.TestCase):
def test_sm4_cbc(self):
key = 'x' * SM4_BLOCK_SIZE
iv = 'y' * SM4_CBC_IV_SIZE
test_enc = Sm4Cbc(key.encode('utf-8'), iv.encode('utf-8'), True)
plain1 = 'hello,world'
plain2 = '1234567890'
cipher1 = test_enc.update(plain1.encode('utf-8'))
cipher2 = test_enc.update(plain2.encode('utf-8'))
ciphers = cipher1 + cipher2 + test_enc.finish()
self.assertTrue(len(ciphers) % SM4_BLOCK_SIZE == 0)
test_dec = Sm4Cbc(key.encode('utf-8'), iv.encode('utf-8'), False)
decrypted_plain1 = test_dec.update(ciphers)
decrypted_plain = decrypted_plain1 + test_dec.finish()
self.assertEqual(decrypted_plain, (plain1 + plain2).encode('utf-8'))
def test_sm4_cbc_invalid_key_len(self):
key = 'x' * (SM4_BLOCK_SIZE - 1)
iv = 'y' * SM4_CBC_IV_SIZE
try:
test_enc = Sm4Cbc(key.encode('utf-8'), iv.encode('utf-8'), True)
except Exception as e:
print(e)
self.assertTrue(True)
else:
self.assertTrue(False)
def test_sm4_cbc_invalid_iv_len(self):
key = 'x' * SM4_BLOCK_SIZE
iv = 'y' * (SM4_CBC_IV_SIZE - 1)
try:
test_enc = Sm4Cbc(key.encode('utf-8'), iv.encode('utf-8'), True)
except Exception as e:
print(e)
self.assertTrue(True)
else:
self.assertTrue(False)
def test_easy_sm4_cbc(self):
key = 'x' * SM4_BLOCK_SIZE
iv = 'y' * SM4_CBC_IV_SIZE
test_cbc_enc = EasySm4CBC(key.encode('utf-8'), iv.encode('utf-8'), True)
plain1 = 'hello,world'
plain2 = '1234567890'
cipher1 = test_cbc_enc.Update(plain1.encode('utf-8'))
cipher2 = test_cbc_enc.Update(plain2.encode('utf-8'))
ciphers = cipher1 + cipher2 + test_cbc_enc.Finish()
self.assertTrue(len(ciphers) % SM4_BLOCK_SIZE == 0)
print('ciphers len:', len(ciphers))
test_dec = EasySm4CBC(key.encode('utf-8'), iv.encode('utf-8'), False)
decrypted_plain1 = test_dec.Update(ciphers)
decrypted_plain = decrypted_plain1 + test_dec.Finish()
self.assertEqual(decrypted_plain, (plain1 + plain2).encode('utf-8'))
def test_easy_sm4_gcm(self):
key = 'x' * SM4_BLOCK_SIZE
iv = 'y' * SM4_CBC_IV_SIZE
aad = 'a' * (SM4_BLOCK_SIZE + SM4_CBC_IV_SIZE)
tag_len = int(SM4_GCM_DEFAULT_TAG_SIZE / 2)
test_gcm_enc = EasySm4GCM(key.encode('utf-8'), iv.encode('utf-8'), aad, tag_len, True)
plain1 = 'hello,world'
plain2 = '1234567890'
cipher1 = test_gcm_enc.Update(plain1.encode('utf-8'))
cipher2 = test_gcm_enc.Update(plain2.encode('utf-8'))
ciphers = cipher1 + cipher2 + test_gcm_enc.Finish()
# GCM模式下的密文长度与明文长度等长
# 返回的密文中包含了 tag 长度
self.assertTrue((len(ciphers) - tag_len) == len(plain1 + plain2))
print('ciphers len:', len(ciphers), 'tag_len=', tag_len, 'plain len:', len(plain1 + plain2))
test_dec = EasySm4GCM(key.encode('utf-8'), iv.encode('utf-8'), aad, tag_len, False)
decrypted_plain1 = test_dec.Update(ciphers)
decrypted_plain = decrypted_plain1 + test_dec.Finish()
self.assertEqual(decrypted_plain, (plain1 + plain2).encode('utf-8'))
if __name__ == '__main__':
unittest.main()