44
44
#include "aes.h"
45
45
#include "aes_cmac.h"
46
46
47
- static void xor_128 (const uint8_t * a , const uint8_t * b , uint8_t * out )
47
+ static void xor_128 (const uint8_t * a , const uint8_t * b , uint8_t * out )
48
48
{
49
49
uint8_t count ;
50
- for (count = 0 ; count < 16 ; count ++ )
51
- {
50
+ for (count = 0 ; count < 16 ; count ++ ) {
52
51
out [count ] = a [count ] ^ b [count ];
53
52
}
54
53
}
55
54
56
- static void leftshift_onebit (const uint8_t * input , uint8_t * output )
55
+ static void leftshift_onebit (const uint8_t * input , uint8_t * output )
57
56
{
58
57
int8_t count ;
59
58
uint8_t overflow = 0 ;
60
59
61
- for (count = 15 ; count >= 0 ; count -- )
62
- {
60
+ for (count = 15 ; count >= 0 ; count -- ) {
63
61
output [count ] = input [count ] << 1 ;
64
62
output [count ] |= overflow ;
65
63
overflow = (input [count ] & 0x80 ) ? 1 : 0 ;
@@ -72,76 +70,90 @@ static void leftshift_onebit(const uint8_t * input, uint8_t * output)
72
70
* @param K1 128-bit first sub key.
73
71
* @param K2 128-bit second sub key.
74
72
*/
75
- static void generate_subkey (const uint8_t * key , uint8_t * K1 , uint8_t * K2 )
73
+ static void generate_subkey (const uint8_t * key , uint8_t * K1 , uint8_t * K2 )
76
74
{
77
75
uint8_t L [16 ];
78
76
uint8_t tmp [16 ];
79
77
80
- const uint8_t const_Zero [16 ] = {
81
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
82
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
83
- };
84
-
85
- const uint8_t const_Rb [16 ] = {
86
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
87
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x87
88
- };
78
+ const uint8_t const_Zero [16 ] = {0x00 ,
79
+ 0x00 ,
80
+ 0x00 ,
81
+ 0x00 ,
82
+ 0x00 ,
83
+ 0x00 ,
84
+ 0x00 ,
85
+ 0x00 ,
86
+ 0x00 ,
87
+ 0x00 ,
88
+ 0x00 ,
89
+ 0x00 ,
90
+ 0x00 ,
91
+ 0x00 ,
92
+ 0x00 ,
93
+ 0x00 };
94
+
95
+ const uint8_t const_Rb [16 ] = {0x00 ,
96
+ 0x00 ,
97
+ 0x00 ,
98
+ 0x00 ,
99
+ 0x00 ,
100
+ 0x00 ,
101
+ 0x00 ,
102
+ 0x00 ,
103
+ 0x00 ,
104
+ 0x00 ,
105
+ 0x00 ,
106
+ 0x00 ,
107
+ 0x00 ,
108
+ 0x00 ,
109
+ 0x00 ,
110
+ 0x87 };
89
111
90
112
/*
91
113
* L := AES-128(Key, const_Zero);
92
114
*/
93
115
AES128_ECB_encrypt ((uint8_t * )const_Zero , key , L );
94
116
95
- if (0 == (L [0 ] & 0x80 )) // if MSB(L) is equal to 0 then K1 := L << 1;
117
+ if (0 == (L [0 ] & 0x80 )) // if MSB(L) is equal to 0 then K1 := L << 1;
96
118
{
97
119
leftshift_onebit (L , K1 );
98
- }
99
- else
100
- { // else K1 := (L << 1) XOR const_Rb;
120
+ } else { // else K1 := (L << 1) XOR const_Rb;
101
121
leftshift_onebit (L , tmp );
102
- xor_128 (tmp ,const_Rb , K1 );
122
+ xor_128 (tmp , const_Rb , K1 );
103
123
}
104
124
105
- if (0 == (K1 [0 ] & 0x80 )) // if MSB(K1) is equal to 0 then K2 := K1 << 1;
125
+ if (0 == (K1 [0 ] & 0x80 )) // if MSB(K1) is equal to 0 then K2 := K1 << 1;
106
126
{
107
127
leftshift_onebit (K1 , K2 );
108
- }
109
- else // else K2 := (K1 << 1) XOR const_Rb;
128
+ } else // else K2 := (K1 << 1) XOR const_Rb;
110
129
{
111
130
leftshift_onebit (K1 , tmp );
112
- xor_128 (tmp ,const_Rb , K2 );
131
+ xor_128 (tmp , const_Rb , K2 );
113
132
}
114
133
}
115
134
116
135
//void padding(unsigned char *lastb, unsigned char *pad, int length)
117
- static void padding (const uint8_t * lastb , uint8_t * pad , const uint8_t length )
136
+ static void padding (const uint8_t * lastb , uint8_t * pad , const uint8_t length )
118
137
{
119
138
int j ;
120
139
121
140
/* original last block */
122
- for (j = 0 ; j < 16 ; j ++ )
123
- {
124
- if (j < length )
125
- {
141
+ for (j = 0 ; j < 16 ; j ++ ) {
142
+ if (j < length ) {
126
143
pad [j ] = lastb [j ];
127
- }
128
- else if (j == length )
129
- {
144
+ } else if (j == length ) {
130
145
pad [j ] = 0x80 ;
131
- }
132
- else
133
- {
146
+ } else {
134
147
pad [j ] = 0x00 ;
135
148
}
136
149
}
137
150
}
138
151
139
152
//void AES_CMAC ( unsigned char *key, unsigned char *input, int length, unsigned char *mac )
140
- void aes_cmac_calculate (
141
- const uint8_t * key ,
142
- const uint8_t * message ,
143
- const uint16_t message_length ,
144
- uint8_t * mac )
153
+ void aes_cmac_calculate (const uint8_t * key ,
154
+ const uint8_t * message ,
155
+ const uint16_t message_length ,
156
+ uint8_t * mac )
145
157
{
146
158
uint8_t X [16 ];
147
159
uint8_t Y [16 ];
@@ -150,75 +162,62 @@ void aes_cmac_calculate(
150
162
uint8_t K1 [16 ];
151
163
uint8_t K2 [16 ];
152
164
uint8_t flag ;
153
- uint8_t n ; //int n;
154
- uint8_t i ; //int i;
165
+ uint8_t n ; //int n;
166
+ uint8_t i ; //int i;
155
167
156
168
generate_subkey (key , K1 , K2 );
157
169
158
- n = (message_length + 15 ) / 16 ; // n is number of rounds
170
+ n = (message_length + 15 ) / 16 ; // n is number of rounds
159
171
160
- if (0 == n )
161
- {
162
- n = 1 ;
172
+ if (0 == n ) {
173
+ n = 1 ;
163
174
flag = 0 ;
164
- }
165
- else
166
- {
167
- if (0 == (message_length % 16 )) /* last block is a complete block */
175
+ } else {
176
+ if (0 == (message_length % 16 )) /* last block is a complete block */
168
177
{
169
178
flag = 1 ;
170
- }
171
- else
172
- { /* last block is not complete block */
179
+ } else { /* last block is not complete block */
173
180
flag = 0 ;
174
181
}
175
182
}
176
183
177
184
if (flag ) /* last block is complete block */
178
185
{
179
186
xor_128 (& message [16 * (n - 1 )], K1 , M_last );
180
- }
181
- else
182
- {
187
+ } else {
183
188
padding (& message [16 * (n - 1 )], padded , message_length % 16 );
184
189
xor_128 (padded , K2 , M_last );
185
190
}
186
191
187
- for (i = 0 ; i < 16 ; i ++ )
188
- {
192
+ for (i = 0 ; i < 16 ; i ++ ) {
189
193
X [i ] = 0 ;
190
194
}
191
195
192
- for (i = 0 ; i < (n - 1 ); i ++ )
193
- {
196
+ for (i = 0 ; i < (n - 1 ); i ++ ) {
194
197
xor_128 (X , & message [16 * i ], Y ); /* Y := Mi (+) X */
195
- AES128_ECB_encrypt (Y , key , X ); // X := AES-128(key, Y);
198
+ AES128_ECB_encrypt (Y , key , X ); // X := AES-128(key, Y);
196
199
}
197
200
198
201
xor_128 (X , M_last , Y );
199
- AES128_ECB_encrypt (Y , key , X ); // X := AES-128(key, Y);
202
+ AES128_ECB_encrypt (Y , key , X ); // X := AES-128(key, Y);
200
203
201
- for (i = 0 ; i < 16 ; i ++ )
202
- {
204
+ for (i = 0 ; i < 16 ; i ++ ) {
203
205
mac [i ] = X [i ];
204
206
}
205
207
}
206
208
207
- CMAC_VERIFY_T aes_cmac_verify (
208
- const uint8_t * key ,
209
- const uint8_t * message ,
210
- const uint16_t message_length ,
211
- const uint8_t * mac )
209
+ CMAC_VERIFY_T aes_cmac_verify (const uint8_t * key ,
210
+ const uint8_t * message ,
211
+ const uint16_t message_length ,
212
+ const uint8_t * mac )
212
213
{
213
214
uint8_t calculated_mac [16 ];
214
215
uint8_t count ;
215
216
216
217
aes_cmac_calculate (key , message , message_length , calculated_mac );
217
218
218
- for (count = 0 ; count < 16 ; count ++ )
219
- {
220
- if (mac [count ] != calculated_mac [count ])
221
- {
219
+ for (count = 0 ; count < 16 ; count ++ ) {
220
+ if (mac [count ] != calculated_mac [count ]) {
222
221
return CMAC_INVALID ;
223
222
}
224
223
}
0 commit comments