1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
52
53
54
58
59
60
61
62
63
64
65
66
67
68
69
70
74
75
76
81
82
83
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
124
125
126
127
128
129
134
135
136
137
138
139
140
145
146
147
148
149
150
155
156
161
162
163
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
217
218
219
220
221
222
223
224
225
230
239
240
241
242
243
244
245
246
247
248
258
259
260
263
264
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
CRYP_HandleTypeDef hcryp;
uint32_t AES128Key[4] = {0xfeffe992,0x8665731c,0x6d6a8f94,0x67308308};
uint32_t InitVector[4] = {0xcafebabe,0xfacedbad,0xdecaf888 , 0x00000002};
uint32_t HeaderMessage[5] = {0xfeedface, 0xdeadbeef, 0xfeedface, 0xdeadbeef,0xabaddad2};
uint32_t Plaintext[15] = {0xd9313225,0xf88406e5,0xa55909c5,0xaff5269a
,0x86a7a953,0x1534f7da,0x2e4c303d,0x8a318a72
,0x1c3c0c95,0x95680953,0x2fcf0e24,0x49a6b525
,0xb16aedf5,0xaa0de657,0xba637b39...};
uint32_t Ciphertext[15] = {0x42831ec2,0x21777424,0x4b7221b7,0x84d0d49c
,0xe3aa212f,0x2c02a4e0,0x35c17e23,0x29aca12e
,0x21d514b2,0x5466931c,0x7d8f6a5a,0xac84aa05
,0x1ba30b39,0x6a0aac97,0x3d58e091...};
uint32_t ExpectedTAG[4]={0x5bc94fbc,0x3221a5db,0x94fae95a,0xe7121a47};
uint32_t Encryptedtext[16]={0};
uint32_t Decryptedtext[16]={0};
uint32_t TAG[4]={0};
Private variables
static void SystemClock_Config(void);
static void Error_Handler(void);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED1);
BSP_LED_Init(LED3);
hcryp.Instance = CRYP;
hcryp.Init.DataType = CRYP_DATATYPE_32B;
hcryp.Init.KeySize = CRYP_KEYSIZE_128B;
hcryp.Init.pKey = AES128Key;
hcryp.Init.Algorithm = CRYP_AES_GCM;
hcryp.Init.KeyIVConfigSkip = CRYP_KEYIVCONFIG_ALWAYS;
hcryp.Init.pInitVect = InitVector;
hcryp.Init.Header = HeaderMessage;
hcryp.Init.HeaderSize = 5;
HAL_CRYP_Init(&hcryp);
HAL_CRYP_Encrypt(&hcryp, Plaintext, 15, Encryptedtext, TIMEOUT_VALUE);
if(memcmp(Encryptedtext, Ciphertext, 60) != 0)
{
Error_Handler();
}if (memcmp(Encryptedtext, Ciphertext, 60) != 0) { ... }
HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp,TAG, TIMEOUT_VALUE);
if(memcmp(TAG, ExpectedTAG, 16) != 0)
{
Error_Handler();
}if (memcmp(TAG, ExpectedTAG, 16) != 0) { ... }
HAL_CRYP_Decrypt(&hcryp,Ciphertext , 15, Decryptedtext, TIMEOUT_VALUE);
if(memcmp(Decryptedtext, Plaintext, 60) != 0)
{
Error_Handler();
}if (memcmp(Decryptedtext, Plaintext, 60) != 0) { ... }
HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp,TAG, TIMEOUT_VALUE);
if(memcmp(TAG, ExpectedTAG, 16) != 0)
{
Error_Handler();
}if (memcmp(TAG, ExpectedTAG, 16) != 0) { ... }
else
{
BSP_LED_On(LED1);
}else { ... }
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
HAL_PWREx_EnableOverDrive();
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED3);
while (1)
{
}while (1) { ... }
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */