先日、SHAのアクセラレータが使えるようになりましたので、AESも試してみました。
ただし、Serial.printなどを追記すると、レジスタの出力が反映されていないなどの不具合も含みます。
↓
とりあえず不具合を修正したバージョンが出来上がりました
スケッチ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define DPORT_PERI_CLK_EN_REG *((volatile uint32_t *)0x3FF0001C) | |
#define DPORT_PERI_RST_EN_REG *((volatile uint32_t *)0x3FF00020) | |
#define AES_MODE_REG *((volatile uint32_t *)0x3FF01008) | |
#define AES_ENDIAN_REG *((volatile uint32_t *)0x3FF01040) | |
#define AES_KEY_0_REG *((volatile uint32_t *)0x3FF01010) | |
#define AES_KEY_1_REG *((volatile uint32_t *)0x3FF01014) | |
#define AES_KEY_2_REG *((volatile uint32_t *)0x3FF01018) | |
#define AES_KEY_3_REG *((volatile uint32_t *)0x3FF0101C) | |
#define AES_KEY_4_REG *((volatile uint32_t *)0x3FF01020) | |
#define AES_KEY_5_REG *((volatile uint32_t *)0x3FF01024) | |
#define AES_KEY_6_REG *((volatile uint32_t *)0x3FF01028) | |
#define AES_KEY_7_REG *((volatile uint32_t *)0x3FF0102C) | |
#define AES_TEXT_0_REG *((volatile uint32_t *)0x3FF01030) | |
#define AES_TEXT_1_REG *((volatile uint32_t *)0x3FF01034) | |
#define AES_TEXT_2_REG *((volatile uint32_t *)0x3FF01038) | |
#define AES_TEXT_3_REG *((volatile uint32_t *)0x3FF0103C) | |
#define AES_START_REG *((volatile uint32_t *)0x3FF01000) | |
#define AES_IDLE_REG *((volatile uint32_t *)0x3FF01004) | |
#define AES_MODE_AES128ENCRYPTION 0x00000000 | |
#define AES_MODE_AES192ENCRYPTION 0x00000001 | |
#define AES_MODE_AES256ENCRYPTION 0x00000002 | |
#define AES_MODE_AES128DECRYPTION 0x00000004 | |
#define AES_MODE_AES192DECRYPTION 0x00000005 | |
#define AES_MODE_AES256DECRYPTION 0x00000006 | |
#define AES_KEY_ENDIAN0 0x00000000 | |
#define AES_KEY_ENDIAN1 0x00000001 | |
#define AES_KEY_ENDIAN2 0x00000002 | |
#define AES_KEY_ENDIAN3 0x00000003 | |
#define AES_TEXT_ENDIAN0 0x00000000 | |
#define AES_TEXT_ENDIAN1 0x00000014 | |
#define AES_TEXT_ENDIAN2 0x00000028 | |
#define AES_TEXT_ENDIAN3 0x0000003C | |
void setup() { | |
uint32_t encrypt_text[4]; | |
uint32_t decrypt_text[4]; | |
Serial.begin(115200); | |
delay(1000); | |
uint32_t plane_text[] = {0x3243f6a8, 0x885a308d, 0x313198a2, 0xe0370734}; | |
uint32_t cipher_key[] = {0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c}; | |
uint32_t crypto_sample_text[] = {0x3925841d, 0x02dc09fb, 0xdc118597, 0x196a0b32}; | |
//Enable AES module | |
DPORT_PERI_CLK_EN_REG = DPORT_PERI_CLK_EN_REG | 0x00000001; // peripheral clock enable | |
DPORT_PERI_RST_EN_REG = DPORT_PERI_RST_EN_REG & (~(0x00000001 | 0x00000008 | 0x00000010)); // peripheral reset | |
Serial.println("encrypt"); | |
//1. Initialize AES_MODE_REG, AES_KEY_n_REG, AES_TEXT_m_REG and AES_ENDIAN_REG. | |
AES_MODE_REG = AES_MODE_AES128ENCRYPTION; | |
AES_ENDIAN_REG = AES_KEY_ENDIAN2 | AES_TEXT_ENDIAN2; | |
AES_KEY_0_REG = cipher_key[0]; | |
AES_KEY_1_REG = cipher_key[1]; | |
AES_KEY_2_REG = cipher_key[2]; | |
AES_KEY_3_REG = cipher_key[3]; | |
AES_TEXT_0_REG = plane_text[0]; | |
AES_TEXT_1_REG = plane_text[1]; | |
AES_TEXT_2_REG = plane_text[2]; | |
AES_TEXT_3_REG = plane_text[3]; | |
//2. Write 1 to AES_START_REG. | |
AES_START_REG = 0x00000001; | |
//3. Wait until AES_IDLE_REG reads 1. | |
while (AES_IDLE_REG) {} | |
while (AES_IDLE_REG) {} | |
//4. Read results from AES_TEXT_m_REG | |
encrypt_text[0] = AES_TEXT_0_REG; | |
encrypt_text[1] = AES_TEXT_1_REG; | |
encrypt_text[2] = AES_TEXT_2_REG; | |
encrypt_text[3] = AES_TEXT_3_REG; | |
//Serial.println("encrypt"); | |
Serial.print("ENC : "); | |
showText(encrypt_text); | |
Serial.print("SAMPL: "); | |
showText(crypto_sample_text); | |
//DeCypher | |
Serial.println("decrypt"); | |
AES_MODE_REG = AES_MODE_AES128DECRYPTION; | |
AES_ENDIAN_REG = AES_KEY_ENDIAN2 | AES_TEXT_ENDIAN2; | |
AES_TEXT_0_REG = crypto_sample_text[0]; | |
AES_TEXT_1_REG = crypto_sample_text[1]; | |
AES_TEXT_2_REG = crypto_sample_text[2]; | |
AES_TEXT_3_REG = crypto_sample_text[3]; | |
AES_KEY_0_REG = cipher_key[0]; | |
AES_KEY_1_REG = cipher_key[1]; | |
AES_KEY_2_REG = cipher_key[2]; | |
AES_KEY_3_REG = cipher_key[3]; | |
AES_START_REG = 0x00000001; | |
while (AES_IDLE_REG) {} | |
decrypt_text[0] = AES_TEXT_0_REG; | |
decrypt_text[1] = AES_TEXT_1_REG; | |
decrypt_text[2] = AES_TEXT_2_REG; | |
decrypt_text[3] = AES_TEXT_3_REG; | |
Serial.print("DEC : "); | |
showText(decrypt_text); | |
Serial.print("SAMPL: "); | |
showText(plane_text); | |
//Disable SHA module | |
DPORT_PERI_CLK_EN_REG = 0x00000000; // peripheral clock enable | |
DPORT_PERI_RST_EN_REG = 0x0000000F; // peripheral reset | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} | |
void showText(uint32_t * text) { | |
for (int i = 0; i < 4; i++) | |
{ | |
char str[10]; | |
sprintf(str, "%08x ", text[i]); | |
Serial.print(str); | |
} | |
Serial.println(); | |
} |
結果
encrypt
ENC : 3925841d 02dc09fb dc118597 196a0b32
SAMPL: 3925841d 02dc09fb dc118597 196a0b32
decrypt
DEC : 3243f6a8 885a308d 313198a2 e0370734
SAMPL: 3243f6a8 885a308d 313198a2 e0370734
ENC : 3925841d 02dc09fb dc118597 196a0b32
SAMPL: 3925841d 02dc09fb dc118597 196a0b32
decrypt
DEC : 3243f6a8 885a308d 313198a2 e0370734
SAMPL: 3243f6a8 885a308d 313198a2 e0370734
いちおう動いてはいますが、最初に書いたとおり余計な記述を追加するとAES_TEXT_0_REGの値が実行前のままだったりするので注意が必要です。
0 件のコメント:
コメントを投稿