4444#include "aes.h"
4545#include "aes_cmac.h"
4646
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 )
4848{
4949 uint8_t count ;
50- for (count = 0 ; count < 16 ; count ++ )
51- {
50+ for (count = 0 ; count < 16 ; count ++ ) {
5251 out [count ] = a [count ] ^ b [count ];
5352 }
5453}
5554
56- static void leftshift_onebit (const uint8_t * input , uint8_t * output )
55+ static void leftshift_onebit (const uint8_t * input , uint8_t * output )
5756{
5857 int8_t count ;
5958 uint8_t overflow = 0 ;
6059
61- for (count = 15 ; count >= 0 ; count -- )
62- {
60+ for (count = 15 ; count >= 0 ; count -- ) {
6361 output [count ] = input [count ] << 1 ;
6462 output [count ] |= overflow ;
6563 overflow = (input [count ] & 0x80 ) ? 1 : 0 ;
@@ -72,76 +70,90 @@ static void leftshift_onebit(const uint8_t * input, uint8_t * output)
7270 * @param K1 128-bit first sub key.
7371 * @param K2 128-bit second sub key.
7472 */
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 )
7674{
7775 uint8_t L [16 ];
7876 uint8_t tmp [16 ];
7977
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 };
89111
90112 /*
91113 * L := AES-128(Key, const_Zero);
92114 */
93115 AES128_ECB_encrypt ((uint8_t * )const_Zero , key , L );
94116
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;
96118 {
97119 leftshift_onebit (L , K1 );
98- }
99- else
100- { // else K1 := (L << 1) XOR const_Rb;
120+ } else { // else K1 := (L << 1) XOR const_Rb;
101121 leftshift_onebit (L , tmp );
102- xor_128 (tmp ,const_Rb , K1 );
122+ xor_128 (tmp , const_Rb , K1 );
103123 }
104124
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;
106126 {
107127 leftshift_onebit (K1 , K2 );
108- }
109- else // else K2 := (K1 << 1) XOR const_Rb;
128+ } else // else K2 := (K1 << 1) XOR const_Rb;
110129 {
111130 leftshift_onebit (K1 , tmp );
112- xor_128 (tmp ,const_Rb , K2 );
131+ xor_128 (tmp , const_Rb , K2 );
113132 }
114133}
115134
116135//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 )
118137{
119138 int j ;
120139
121140 /* 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 ) {
126143 pad [j ] = lastb [j ];
127- }
128- else if (j == length )
129- {
144+ } else if (j == length ) {
130145 pad [j ] = 0x80 ;
131- }
132- else
133- {
146+ } else {
134147 pad [j ] = 0x00 ;
135148 }
136149 }
137150}
138151
139152//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 )
145157{
146158 uint8_t X [16 ];
147159 uint8_t Y [16 ];
@@ -150,75 +162,62 @@ void aes_cmac_calculate(
150162 uint8_t K1 [16 ];
151163 uint8_t K2 [16 ];
152164 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;
155167
156168 generate_subkey (key , K1 , K2 );
157169
158- n = (message_length + 15 ) / 16 ; // n is number of rounds
170+ n = (message_length + 15 ) / 16 ; // n is number of rounds
159171
160- if (0 == n )
161- {
162- n = 1 ;
172+ if (0 == n ) {
173+ n = 1 ;
163174 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 */
168177 {
169178 flag = 1 ;
170- }
171- else
172- { /* last block is not complete block */
179+ } else { /* last block is not complete block */
173180 flag = 0 ;
174181 }
175182 }
176183
177184 if (flag ) /* last block is complete block */
178185 {
179186 xor_128 (& message [16 * (n - 1 )], K1 , M_last );
180- }
181- else
182- {
187+ } else {
183188 padding (& message [16 * (n - 1 )], padded , message_length % 16 );
184189 xor_128 (padded , K2 , M_last );
185190 }
186191
187- for (i = 0 ; i < 16 ; i ++ )
188- {
192+ for (i = 0 ; i < 16 ; i ++ ) {
189193 X [i ] = 0 ;
190194 }
191195
192- for (i = 0 ; i < (n - 1 ); i ++ )
193- {
196+ for (i = 0 ; i < (n - 1 ); i ++ ) {
194197 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);
196199 }
197200
198201 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);
200203
201- for (i = 0 ; i < 16 ; i ++ )
202- {
204+ for (i = 0 ; i < 16 ; i ++ ) {
203205 mac [i ] = X [i ];
204206 }
205207}
206208
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 )
212213{
213214 uint8_t calculated_mac [16 ];
214215 uint8_t count ;
215216
216217 aes_cmac_calculate (key , message , message_length , calculated_mac );
217218
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 ]) {
222221 return CMAC_INVALID ;
223222 }
224223 }
0 commit comments