Updated SM4 with AESENCLAST (markdown)

Sun Yimin 2023-10-10 15:06:31 +08:00
parent a83c7d6e3e
commit c06e4573b4

@ -398,6 +398,54 @@ def matrix_from_cols(cols):
def gen_matrix_based_table(table):
return matrix_from_cols([table[0x80] ^ table[0], table[0x40] ^ table[0], table[0x20] ^ table[0], table[0x10] ^ table[0], table[0x08] ^ table[0], table[0x04] ^ table[0], table[0x02] ^ table[0], table[0x01] ^ table[0]])
```
# AES ShiftRows
![image](https://github.com/emmansun/gmsm/assets/7235232/12b2527c-84d7-4831-81dc-031275d49cb7)
![image](https://github.com/emmansun/gmsm/assets/7235232/dbbef92a-5e4c-469f-83ef-bf61e0e8f6d0)
16字节State是这样存储的
$in_0 \ in_1 \ in_2 \ in_3 \ in_4\ in_5\ in_6\ in_7\ in_8\ in_9\ in_{10}\ in_{11}\ in_{12}\ in_{13}\ in_{14}\ in_{15}$
ShiftRows操作
|<!-- --> |<!-- --> | <!-- --> |<!-- --> |
|------|------|------|------|
|0 | 4 | 8 | c |
|1 | 5 | 9 | d |
|2 | 6 | a | e |
|3 | 7 | b | f |
ShiftRows后变成
|<!-- --> |<!-- --> | <!-- --> |<!-- --> |
|------|------|------|------|
|0 | 4 | 8 | c |
|5 | 9 | d | 1 |
|a | e | 2 | 6 |
|f | 3 | 7 | b |
再来看STATE 先逆ShiftRows, 再ShiftRows
|<!-- --> |<!-- --> | <!-- --> |<!-- --> |
|------|------|------|------|
|0 | 4 | 8 | c |
|1 | 5 | 9 | d |
|2 | 6 | a | e |
|3 | 7 | b | f |
STATE逆ShiftRows后
|<!-- --> |<!-- --> | <!-- --> |<!-- --> |
|------|------|------|------|
|0 | 4 | 8 | c |
|d | 1 | 5 | 9 |
|a | e | 2 | 6 |
|7 | b | f | 3 |
再ShiftRows后
|<!-- --> |<!-- --> | <!-- --> |<!-- --> |
|------|------|------|------|
|0 | 4 | 8 | c |
|1 | 5 | 9 | d |
|2 | 6 | a | e |
|3 | 7 | b | f |
回到初始STATE。
# Reference
* [AES-NI used in SM4](https://github.com/mjosaarinen/sm4ni)
* [Advanced Encryption Standard (AES)](https://www.nist.gov/publications/advanced-encryption-standard-aes)