mirror of
https://github.com/emmansun/gmsm.git
synced 2025-05-10 19:16:18 +08:00
Updated SM4 with AESENCLAST (markdown)
parent
db6d37063d
commit
6c97ff23b5
@ -114,6 +114,43 @@ func gen_lookup_table(m [8]byte, c byte) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Below python code is more intuitive:
|
||||||
|
```
|
||||||
|
from pyfinite import genericmatrix
|
||||||
|
|
||||||
|
XOR = lambda x,y:x^y
|
||||||
|
AND = lambda x,y:x&y
|
||||||
|
DIV = lambda x,y:x
|
||||||
|
|
||||||
|
|
||||||
|
def to_matrix(x):
|
||||||
|
m = genericmatrix.GenericMatrix(size=(8,8), zeroElement=0, identityElement=1, add=XOR, mul=AND, sub=XOR, div=DIV)
|
||||||
|
for i in range(8):
|
||||||
|
m.SetRow(i, [(x[i] & 0x80) >> 7, (x[i] & 0x40) >> 6, (x[i] & 0x20) >> 5, (x[i] & 0x10) >> 4, (x[i] & 0x08) >> 3, (x[i] & 0x04) >> 2, (x[i] & 0x02) >> 1, (x[i] & 0x01) >> 0])
|
||||||
|
return m
|
||||||
|
|
||||||
|
def matrix_col_byte(c):
|
||||||
|
return (c[0] << 7) ^ (c[1] << 6) ^ (c[2] << 5) ^ (c[3] << 4) ^ (c[4] << 3) ^ (c[5] << 2) ^ (c[6] << 1) ^ (c[7] << 0)
|
||||||
|
|
||||||
|
def gen_lookup(m, c):
|
||||||
|
Mmatrix = to_matrix(m)
|
||||||
|
table = []
|
||||||
|
for i in range(256):
|
||||||
|
Imatrix = genericmatrix.GenericMatrix(size=(8, 1), zeroElement=0, identityElement=1, add=XOR, mul=AND, sub=XOR, div=DIV)
|
||||||
|
for j in range (8):
|
||||||
|
Imatrix.SetRow(j, [(i >> (7 - j)) & 1])
|
||||||
|
tmp = Mmatrix * Imatrix
|
||||||
|
table.append(matrix_col_byte(tmp.GetColumn(0)) ^ c)
|
||||||
|
return table
|
||||||
|
|
||||||
|
def print_table(table):
|
||||||
|
for i, s in enumerate(table):
|
||||||
|
print(f'0x%02X'%s,',', end='')
|
||||||
|
if (i+1) % 16 == 0:
|
||||||
|
print()
|
||||||
|
|
||||||
|
print_table(gen_lookup([0xfe, 0x54, 0xaf, 0xdd, 0xf7, 0xf9, 0xac, 0xe2], 0x34))
|
||||||
|
```
|
||||||
|
|
||||||
**How to calculate M, C from lookup table?**
|
**How to calculate M, C from lookup table?**
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user