Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
cmac.cpp
00001 /************************************************************************** 00002 Copyright (C) 2009 Lander Casado, Philippas Tsigas 00003 All rights reserved. 00004 Permission is hereby granted, free of charge, to any person obtaining 00005 a copy of this software and associated documentation files 00006 (the "Software"), to deal with the Software without restriction, including 00007 without limitation the rights to use, copy, modify, merge, publish, 00008 distribute, sublicense, and/or sell copies of the Software, and to 00009 permit persons to whom the Software is furnished to do so, subject to 00010 the following conditions: 00011 Redistributions of source code must retain the above copyright notice, 00012 this list of conditions and the following disclaimers. Redistributions in 00013 binary form must reproduce the above copyright notice, this list of 00014 conditions and the following disclaimers in the documentation and/or 00015 other materials provided with the distribution. 00016 In no event shall the authors or copyright holders be liable for any special, 00017 incidental, indirect or consequential damages of any kind, or any damages 00018 whatsoever resulting from loss of use, data or profits, whether or not 00019 advised of the possibility of damage, and on any theory of liability, 00020 arising out of or in connection with the use or performance of this software. 00021 00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00023 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00025 CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00028 DEALINGS WITH THE SOFTWARE 00029 *****************************************************************************/ 00030 //#include <sys/param.h> 00031 //#include <sys/systm.h> 00032 #include <stdint.h> 00033 #include "aes.h" 00034 #include "cmac.h" 00035 #include "utilities.h" 00036 00037 #define LSHIFT(v, r) do { \ 00038 int32_t i; \ 00039 for (i = 0; i < 15; i++) \ 00040 (r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \ 00041 (r)[15] = (v)[15] << 1; \ 00042 } while (0) 00043 00044 #define XOR(v, r) do { \ 00045 int32_t i; \ 00046 for (i = 0; i < 16; i++) \ 00047 { \ 00048 (r)[i] = (r)[i] ^ (v)[i]; \ 00049 } \ 00050 } while (0) \ 00051 00052 00053 void AES_CMAC_Init(AES_CMAC_CTX *ctx) 00054 { 00055 memset1(ctx->X, 0, sizeof ctx->X); 00056 ctx->M_n = 0; 00057 memset1(ctx->rijndael.ksch, '\0', 240); 00058 } 00059 00060 void AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const uint8_t key[AES_CMAC_KEY_LENGTH]) 00061 { 00062 //rijndael_set_key_enc_only(&ctx->rijndael, key, 128); 00063 aes_set_key( key, AES_CMAC_KEY_LENGTH, &ctx->rijndael); 00064 } 00065 00066 void AES_CMAC_Update(AES_CMAC_CTX *ctx, const uint8_t *data, uint32_t len) 00067 { 00068 uint32_t mlen; 00069 uint8_t in[16]; 00070 00071 if (ctx->M_n > 0) { 00072 mlen = MIN(16 - ctx->M_n, len); 00073 memcpy1(ctx->M_last + ctx->M_n, data, mlen); 00074 ctx->M_n += mlen; 00075 if (ctx->M_n < 16 || len == mlen) 00076 return; 00077 XOR(ctx->M_last, ctx->X); 00078 //rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X); 00079 aes_encrypt( ctx->X, ctx->X, &ctx->rijndael); 00080 data += mlen; 00081 len -= mlen; 00082 } 00083 while (len > 16) { /* not last block */ 00084 00085 XOR(data, ctx->X); 00086 //rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X); 00087 00088 memcpy1(in, &ctx->X[0], 16); //Bestela ez du ondo iten 00089 aes_encrypt( in, in, &ctx->rijndael); 00090 memcpy1(&ctx->X[0], in, 16); 00091 00092 data += 16; 00093 len -= 16; 00094 } 00095 /* potential last block, save it */ 00096 memcpy1(ctx->M_last, data, len); 00097 ctx->M_n = len; 00098 } 00099 00100 void AES_CMAC_Final(uint8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx) 00101 { 00102 uint8_t K[16]; 00103 uint8_t in[16]; 00104 /* generate subkey K1 */ 00105 memset1(K, '\0', 16); 00106 00107 //rijndael_encrypt(&ctx->rijndael, K, K); 00108 00109 aes_encrypt( K, K, &ctx->rijndael); 00110 00111 if (K[0] & 0x80) { 00112 LSHIFT(K, K); 00113 K[15] ^= 0x87; 00114 } else 00115 LSHIFT(K, K); 00116 00117 00118 if (ctx->M_n == 16) { 00119 /* last block was a complete block */ 00120 XOR(K, ctx->M_last); 00121 00122 } else { 00123 /* generate subkey K2 */ 00124 if (K[0] & 0x80) { 00125 LSHIFT(K, K); 00126 K[15] ^= 0x87; 00127 } else 00128 LSHIFT(K, K); 00129 00130 /* padding(M_last) */ 00131 ctx->M_last[ctx->M_n] = 0x80; 00132 while (++ctx->M_n < 16) 00133 ctx->M_last[ctx->M_n] = 0; 00134 00135 XOR(K, ctx->M_last); 00136 00137 00138 } 00139 XOR(ctx->M_last, ctx->X); 00140 00141 //rijndael_encrypt(&ctx->rijndael, ctx->X, digest); 00142 00143 memcpy1(in, &ctx->X[0], 16); //Bestela ez du ondo iten 00144 aes_encrypt(in, digest, &ctx->rijndael); 00145 memset1(K, 0, sizeof K); 00146 00147 }
Generated on Wed Jul 13 2022 23:48:00 by
