1 /**************************************************************************
2 Copyright (C) 2009 Lander Casado, Philippas Tsigas
3
4 All rights reserved.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files
8 (the "Software"), to deal with the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimers. Redistributions in
16 binary form must reproduce the above copyright notice, this list of
17 conditions and the following disclaimers in the documentation and/or
18 other materials provided with the distribution.
19
20 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,
24 arising out of or in connection with the use or performance of this software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 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
32 DEALINGS WITH THE SOFTWARE
33
34 *****************************************************************************/
35 #include <stdint.h>
36 #include "aes.h"
37 #include "cmac.h"
38 #include "utilities.h"
39
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
AES_CMAC_Init(AES_CMAC_CTX * ctx)59 void AES_CMAC_Init( AES_CMAC_CTX* ctx )
60 {
61 memset1( ctx->X, 0, sizeof ctx->X );
62 ctx->M_n = 0;
63 memset1( ctx->rijndael.ksch, '\0', 240 );
64 }
65
AES_CMAC_SetKey(AES_CMAC_CTX * ctx,const uint8_t key[AES_CMAC_KEY_LENGTH])66 void AES_CMAC_SetKey( AES_CMAC_CTX* ctx, const uint8_t key[AES_CMAC_KEY_LENGTH] )
67 {
68 aes_set_key( key, AES_CMAC_KEY_LENGTH, &ctx->rijndael );
69 }
70
AES_CMAC_Update(AES_CMAC_CTX * ctx,const uint8_t * data,uint32_t len)71 void AES_CMAC_Update( AES_CMAC_CTX* ctx, const uint8_t* data, uint32_t len )
72 {
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;
107 }
108
AES_CMAC_Final(uint8_t digest[AES_CMAC_DIGEST_LENGTH],AES_CMAC_CTX * ctx)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 );
154 }
155