Skip to content

Commit c94d7e3

Browse files
Sylvain Rochetmluis1
authored andcommitted
Pull-request #843 - Applied proposed solution.
Fixed file alignments.
1 parent 15fa96b commit c94d7e3

File tree

1 file changed

+118
-117
lines changed

1 file changed

+118
-117
lines changed

src/peripherals/soft-se/cmac.c

Lines changed: 118 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -4,150 +4,151 @@ Copyright (C) 2009 Lander Casado, Philippas Tsigas
44
All rights reserved.
55
66
Permission is hereby granted, free of charge, to any person obtaining
7-
a copy of this software and associated documentation files
7+
a copy of this software and associated documentation files
88
(the "Software"), to deal with the Software without restriction, including
9-
without limitation the rights to use, copy, modify, merge, publish,
9+
without limitation the rights to use, copy, modify, merge, publish,
1010
distribute, sublicense, and/or sell copies of the Software, and to
1111
permit persons to whom the Software is furnished to do so, subject to
12-
the following conditions:
12+
the following conditions:
1313
14-
Redistributions of source code must retain the above copyright notice,
14+
Redistributions of source code must retain the above copyright notice,
1515
this list of conditions and the following disclaimers. Redistributions in
1616
binary form must reproduce the above copyright notice, this list of
17-
conditions and the following disclaimers in the documentation and/or
17+
conditions and the following disclaimers in the documentation and/or
1818
other materials provided with the distribution.
1919
2020
In no event shall the authors or copyright holders be liable for any special,
21-
incidental, indirect or consequential damages of any kind, or any damages
22-
whatsoever resulting from loss of use, data or profits, whether or not
23-
advised of the possibility of damage, and on any theory of liability,
21+
incidental, indirect or consequential damages of any kind, or any damages
22+
whatsoever resulting from loss of use, data or profits, whether or not
23+
advised of the possibility of damage, and on any theory of liability,
2424
arising out of or in connection with the use or performance of this software.
25-
25+
2626
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
2727
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2828
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2929
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
3232
DEALINGS WITH THE SOFTWARE
3333
3434
*****************************************************************************/
35-
//#include <sys/param.h>
36-
//#include <sys/systm.h>
3735
#include <stdint.h>
3836
#include "aes.h"
3937
#include "cmac.h"
4038
#include "utilities.h"
4139

42-
#define LSHIFT(v, r) do { \
43-
int32_t i; \
44-
for (i = 0; i < 15; i++) \
45-
(r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \
46-
(r)[15] = (v)[15] << 1; \
47-
} while (0)
48-
49-
#define XOR(v, r) do { \
50-
int32_t i; \
51-
for (i = 0; i < 16; i++) \
52-
{ \
53-
(r)[i] = (r)[i] ^ (v)[i]; \
54-
} \
55-
} while (0) \
56-
57-
58-
void AES_CMAC_Init(AES_CMAC_CTX *ctx)
40+
#define LSHIFT( v, r ) \
41+
do \
42+
{ \
43+
int32_t i; \
44+
for( i = 0; i < 15; i++ ) \
45+
( r )[i] = ( v )[i] << 1 | ( v )[i + 1] >> 7; \
46+
( r )[15] = ( v )[15] << 1; \
47+
} while( 0 )
48+
49+
#define XOR( v, r ) \
50+
do \
51+
{ \
52+
int32_t i; \
53+
for( i = 0; i < 16; i++ ) \
54+
{ \
55+
( r )[i] = ( r )[i] ^ ( v )[i]; \
56+
} \
57+
} while( 0 )
58+
59+
void AES_CMAC_Init( AES_CMAC_CTX* ctx )
5960
{
60-
memset1(ctx->X, 0, sizeof ctx->X);
61-
ctx->M_n = 0;
62-
memset1(ctx->rijndael.ksch, '\0', 240);
61+
memset1( ctx->X, 0, sizeof ctx->X );
62+
ctx->M_n = 0;
63+
memset1( ctx->rijndael.ksch, '\0', 240 );
6364
}
64-
65-
void AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const uint8_t key[AES_CMAC_KEY_LENGTH])
65+
66+
void AES_CMAC_SetKey( AES_CMAC_CTX* ctx, const uint8_t key[AES_CMAC_KEY_LENGTH] )
6667
{
67-
//rijndael_set_key_enc_only(&ctx->rijndael, key, 128);
68-
aes_set_key( key, AES_CMAC_KEY_LENGTH, &ctx->rijndael);
68+
aes_set_key( key, AES_CMAC_KEY_LENGTH, &ctx->rijndael );
6969
}
70-
71-
void AES_CMAC_Update(AES_CMAC_CTX *ctx, const uint8_t *data, uint32_t len)
70+
71+
void AES_CMAC_Update( AES_CMAC_CTX* ctx, const uint8_t* data, uint32_t len )
7272
{
73-
uint32_t mlen;
74-
uint8_t in[16];
75-
76-
if (ctx->M_n > 0) {
77-
mlen = MIN(16 - ctx->M_n, len);
78-
memcpy1(ctx->M_last + ctx->M_n, data, mlen);
79-
ctx->M_n += mlen;
80-
if (ctx->M_n < 16 || len == mlen)
81-
return;
82-
XOR(ctx->M_last, ctx->X);
83-
//rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X);
84-
aes_encrypt( ctx->X, ctx->X, &ctx->rijndael);
85-
data += mlen;
86-
len -= mlen;
87-
}
88-
while (len > 16) { /* not last block */
89-
90-
XOR(data, ctx->X);
91-
//rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X);
92-
93-
memcpy1(in, &ctx->X[0], 16); //Bestela ez du ondo iten
94-
aes_encrypt( in, in, &ctx->rijndael);
95-
memcpy1(&ctx->X[0], in, 16);
96-
97-
data += 16;
98-
len -= 16;
99-
}
100-
/* potential last block, save it */
101-
memcpy1(ctx->M_last, data, len);
102-
ctx->M_n = len;
73+
uint32_t mlen;
74+
uint8_t in[16];
75+
76+
if( ctx->M_n > 0 )
77+
{
78+
mlen = MIN( 16 - ctx->M_n, len );
79+
memcpy1( ctx->M_last + ctx->M_n, data, mlen );
80+
ctx->M_n += mlen;
81+
if( ctx->M_n < 16 || len == mlen )
82+
return;
83+
XOR( ctx->M_last, ctx->X );
84+
85+
memcpy1( in, &ctx->X[0], 16 ); // Otherwise it does not look good
86+
aes_encrypt( in, in, &ctx->rijndael );
87+
memcpy1( &ctx->X[0], in, 16 );
88+
89+
data += mlen;
90+
len -= mlen;
91+
}
92+
while( len > 16 )
93+
{ /* not last block */
94+
95+
XOR( data, ctx->X );
96+
97+
memcpy1( in, &ctx->X[0], 16 ); // Otherwise it does not look good
98+
aes_encrypt( in, in, &ctx->rijndael );
99+
memcpy1( &ctx->X[0], in, 16 );
100+
101+
data += 16;
102+
len -= 16;
103+
}
104+
/* potential last block, save it */
105+
memcpy1( ctx->M_last, data, len );
106+
ctx->M_n = len;
103107
}
104-
105-
void AES_CMAC_Final(uint8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx)
106-
{
107-
uint8_t K[16];
108-
uint8_t in[16];
109-
/* generate subkey K1 */
110-
memset1(K, '\0', 16);
111-
112-
//rijndael_encrypt(&ctx->rijndael, K, K);
113-
114-
aes_encrypt( K, K, &ctx->rijndael);
115-
116-
if (K[0] & 0x80) {
117-
LSHIFT(K, K);
118-
K[15] ^= 0x87;
119-
} else
120-
LSHIFT(K, K);
121-
122-
123-
if (ctx->M_n == 16) {
124-
/* last block was a complete block */
125-
XOR(K, ctx->M_last);
126-
127-
} else {
128-
/* generate subkey K2 */
129-
if (K[0] & 0x80) {
130-
LSHIFT(K, K);
131-
K[15] ^= 0x87;
132-
} else
133-
LSHIFT(K, K);
134-
135-
/* padding(M_last) */
136-
ctx->M_last[ctx->M_n] = 0x80;
137-
while (++ctx->M_n < 16)
138-
ctx->M_last[ctx->M_n] = 0;
139-
140-
XOR(K, ctx->M_last);
141-
142-
143-
}
144-
XOR(ctx->M_last, ctx->X);
145-
146-
//rijndael_encrypt(&ctx->rijndael, ctx->X, digest);
147-
148-
memcpy1(in, &ctx->X[0], 16); //Bestela ez du ondo iten
149-
aes_encrypt(in, digest, &ctx->rijndael);
150-
memset1(K, 0, sizeof K);
151108

109+
void AES_CMAC_Final( uint8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX* ctx )
110+
{
111+
uint8_t K[16];
112+
uint8_t in[16];
113+
/* generate subkey K1 */
114+
memset1( K, '\0', 16 );
115+
116+
aes_encrypt( K, K, &ctx->rijndael );
117+
118+
if( K[0] & 0x80 )
119+
{
120+
LSHIFT( K, K );
121+
K[15] ^= 0x87;
122+
}
123+
else
124+
LSHIFT( K, K );
125+
126+
if( ctx->M_n == 16 )
127+
{
128+
/* last block was a complete block */
129+
XOR( K, ctx->M_last );
130+
}
131+
else
132+
{
133+
/* generate subkey K2 */
134+
if( K[0] & 0x80 )
135+
{
136+
LSHIFT( K, K );
137+
K[15] ^= 0x87;
138+
}
139+
else
140+
LSHIFT( K, K );
141+
142+
/* padding(M_last) */
143+
ctx->M_last[ctx->M_n] = 0x80;
144+
while( ++ctx->M_n < 16 )
145+
ctx->M_last[ctx->M_n] = 0;
146+
147+
XOR( K, ctx->M_last );
148+
}
149+
XOR( ctx->M_last, ctx->X );
150+
151+
memcpy1( in, &ctx->X[0], 16 ); // Otherwise it does not look good
152+
aes_encrypt( in, digest, &ctx->rijndael );
153+
memset1( K, 0, sizeof K );
152154
}
153-

0 commit comments

Comments
 (0)