to be used with the DSPLoRa module (minor changes wrt transmit power)
Fork of LoRaWAN-lib by
crypto/cmac.cpp@0:91d1a7783bb9, 2015-10-20 (annotated)
- Committer:
- mluis
- Date:
- Tue Oct 20 13:21:26 2015 +0000
- Revision:
- 0:91d1a7783bb9
Library creation synchronized with GitHub LoRaMac-node v3.4 (https://github.com/Lora-net/LoRaMac-node)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:91d1a7783bb9 | 1 | /************************************************************************** |
mluis | 0:91d1a7783bb9 | 2 | Copyright (C) 2009 Lander Casado, Philippas Tsigas |
mluis | 0:91d1a7783bb9 | 3 | |
mluis | 0:91d1a7783bb9 | 4 | All rights reserved. |
mluis | 0:91d1a7783bb9 | 5 | |
mluis | 0:91d1a7783bb9 | 6 | Permission is hereby granted, free of charge, to any person obtaining |
mluis | 0:91d1a7783bb9 | 7 | a copy of this software and associated documentation files |
mluis | 0:91d1a7783bb9 | 8 | (the "Software"), to deal with the Software without restriction, including |
mluis | 0:91d1a7783bb9 | 9 | without limitation the rights to use, copy, modify, merge, publish, |
mluis | 0:91d1a7783bb9 | 10 | distribute, sublicense, and/or sell copies of the Software, and to |
mluis | 0:91d1a7783bb9 | 11 | permit persons to whom the Software is furnished to do so, subject to |
mluis | 0:91d1a7783bb9 | 12 | the following conditions: |
mluis | 0:91d1a7783bb9 | 13 | |
mluis | 0:91d1a7783bb9 | 14 | Redistributions of source code must retain the above copyright notice, |
mluis | 0:91d1a7783bb9 | 15 | this list of conditions and the following disclaimers. Redistributions in |
mluis | 0:91d1a7783bb9 | 16 | binary form must reproduce the above copyright notice, this list of |
mluis | 0:91d1a7783bb9 | 17 | conditions and the following disclaimers in the documentation and/or |
mluis | 0:91d1a7783bb9 | 18 | other materials provided with the distribution. |
mluis | 0:91d1a7783bb9 | 19 | |
mluis | 0:91d1a7783bb9 | 20 | In no event shall the authors or copyright holders be liable for any special, |
mluis | 0:91d1a7783bb9 | 21 | incidental, indirect or consequential damages of any kind, or any damages |
mluis | 0:91d1a7783bb9 | 22 | whatsoever resulting from loss of use, data or profits, whether or not |
mluis | 0:91d1a7783bb9 | 23 | advised of the possibility of damage, and on any theory of liability, |
mluis | 0:91d1a7783bb9 | 24 | arising out of or in connection with the use or performance of this software. |
mluis | 0:91d1a7783bb9 | 25 | |
mluis | 0:91d1a7783bb9 | 26 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
mluis | 0:91d1a7783bb9 | 27 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mluis | 0:91d1a7783bb9 | 28 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mluis | 0:91d1a7783bb9 | 29 | CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mluis | 0:91d1a7783bb9 | 30 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
mluis | 0:91d1a7783bb9 | 31 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
mluis | 0:91d1a7783bb9 | 32 | DEALINGS WITH THE SOFTWARE |
mluis | 0:91d1a7783bb9 | 33 | |
mluis | 0:91d1a7783bb9 | 34 | *****************************************************************************/ |
mluis | 0:91d1a7783bb9 | 35 | //#include <sys/param.h> |
mluis | 0:91d1a7783bb9 | 36 | //#include <sys/systm.h> |
mluis | 0:91d1a7783bb9 | 37 | #include "mbed.h" |
mluis | 0:91d1a7783bb9 | 38 | #include "aes.h" |
mluis | 0:91d1a7783bb9 | 39 | #include "cmac.h" |
mluis | 0:91d1a7783bb9 | 40 | #include "utilities.h" |
mluis | 0:91d1a7783bb9 | 41 | |
mluis | 0:91d1a7783bb9 | 42 | #define LSHIFT(v, r) do { \ |
mluis | 0:91d1a7783bb9 | 43 | int i; \ |
mluis | 0:91d1a7783bb9 | 44 | for (i = 0; i < 15; i++) \ |
mluis | 0:91d1a7783bb9 | 45 | (r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \ |
mluis | 0:91d1a7783bb9 | 46 | (r)[15] = (v)[15] << 1; \ |
mluis | 0:91d1a7783bb9 | 47 | } while (0) |
mluis | 0:91d1a7783bb9 | 48 | |
mluis | 0:91d1a7783bb9 | 49 | #define XOR(v, r) do { \ |
mluis | 0:91d1a7783bb9 | 50 | int i; \ |
mluis | 0:91d1a7783bb9 | 51 | for (i = 0; i < 16; i++) \ |
mluis | 0:91d1a7783bb9 | 52 | { \ |
mluis | 0:91d1a7783bb9 | 53 | (r)[i] = (r)[i] ^ (v)[i]; \ |
mluis | 0:91d1a7783bb9 | 54 | } \ |
mluis | 0:91d1a7783bb9 | 55 | } while (0) \ |
mluis | 0:91d1a7783bb9 | 56 | |
mluis | 0:91d1a7783bb9 | 57 | |
mluis | 0:91d1a7783bb9 | 58 | //#define MIN(a,b) (((a)<(b))?(a):(b)) |
mluis | 0:91d1a7783bb9 | 59 | |
mluis | 0:91d1a7783bb9 | 60 | /* |
mluis | 0:91d1a7783bb9 | 61 | void memcpy1( u_int8_t *dst, const u_int8_t *src, u_int size ); |
mluis | 0:91d1a7783bb9 | 62 | void memset1( u_int8_t *dst, u_int8_t value, u_int size ); |
mluis | 0:91d1a7783bb9 | 63 | */ |
mluis | 0:91d1a7783bb9 | 64 | |
mluis | 0:91d1a7783bb9 | 65 | /* |
mluis | 0:91d1a7783bb9 | 66 | static void memcpy1( uint_8t * d, const uint_8t *s, uint_8t nn ) |
mluis | 0:91d1a7783bb9 | 67 | { |
mluis | 0:91d1a7783bb9 | 68 | while( nn-- ) |
mluis | 0:91d1a7783bb9 | 69 | // *((uint_8t*)d)++ = *((uint_8t*)s)++; |
mluis | 0:91d1a7783bb9 | 70 | *d++ = *s++; |
mluis | 0:91d1a7783bb9 | 71 | } |
mluis | 0:91d1a7783bb9 | 72 | |
mluis | 0:91d1a7783bb9 | 73 | static void memset1( uint_8t * d, uint_8t a, uint_8t nn ) |
mluis | 0:91d1a7783bb9 | 74 | { |
mluis | 0:91d1a7783bb9 | 75 | while( nn-- ) |
mluis | 0:91d1a7783bb9 | 76 | // *((uint_8t*)d)++ = *((uint_8t*)s)++; |
mluis | 0:91d1a7783bb9 | 77 | *d++ = a; |
mluis | 0:91d1a7783bb9 | 78 | } |
mluis | 0:91d1a7783bb9 | 79 | */ |
mluis | 0:91d1a7783bb9 | 80 | |
mluis | 0:91d1a7783bb9 | 81 | void AES_CMAC_Init(AES_CMAC_CTX *ctx) |
mluis | 0:91d1a7783bb9 | 82 | { |
mluis | 0:91d1a7783bb9 | 83 | memset1(ctx->X, 0, sizeof ctx->X); |
mluis | 0:91d1a7783bb9 | 84 | ctx->M_n = 0; |
mluis | 0:91d1a7783bb9 | 85 | memset1(ctx->rijndael.ksch, '\0', 240); |
mluis | 0:91d1a7783bb9 | 86 | } |
mluis | 0:91d1a7783bb9 | 87 | |
mluis | 0:91d1a7783bb9 | 88 | void AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const u_int8_t key[AES_CMAC_KEY_LENGTH]) |
mluis | 0:91d1a7783bb9 | 89 | { |
mluis | 0:91d1a7783bb9 | 90 | //rijndael_set_key_enc_only(&ctx->rijndael, key, 128); |
mluis | 0:91d1a7783bb9 | 91 | aes_set_key( key, AES_CMAC_KEY_LENGTH, &ctx->rijndael); |
mluis | 0:91d1a7783bb9 | 92 | } |
mluis | 0:91d1a7783bb9 | 93 | |
mluis | 0:91d1a7783bb9 | 94 | void AES_CMAC_Update(AES_CMAC_CTX *ctx, const u_int8_t *data, u_int len) |
mluis | 0:91d1a7783bb9 | 95 | { |
mluis | 0:91d1a7783bb9 | 96 | u_int mlen; |
mluis | 0:91d1a7783bb9 | 97 | unsigned char in[16]; |
mluis | 0:91d1a7783bb9 | 98 | |
mluis | 0:91d1a7783bb9 | 99 | if (ctx->M_n > 0) { |
mluis | 0:91d1a7783bb9 | 100 | mlen = MIN(16 - ctx->M_n, len); |
mluis | 0:91d1a7783bb9 | 101 | memcpy1(ctx->M_last + ctx->M_n, ( uint8_t* )data, mlen); |
mluis | 0:91d1a7783bb9 | 102 | ctx->M_n += mlen; |
mluis | 0:91d1a7783bb9 | 103 | if (ctx->M_n < 16 || len == mlen) |
mluis | 0:91d1a7783bb9 | 104 | return; |
mluis | 0:91d1a7783bb9 | 105 | XOR(ctx->M_last, ctx->X); |
mluis | 0:91d1a7783bb9 | 106 | //rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X); |
mluis | 0:91d1a7783bb9 | 107 | aes_encrypt( ctx->X, ctx->X, &ctx->rijndael); |
mluis | 0:91d1a7783bb9 | 108 | data += mlen; |
mluis | 0:91d1a7783bb9 | 109 | len -= mlen; |
mluis | 0:91d1a7783bb9 | 110 | } |
mluis | 0:91d1a7783bb9 | 111 | while (len > 16) { /* not last block */ |
mluis | 0:91d1a7783bb9 | 112 | |
mluis | 0:91d1a7783bb9 | 113 | XOR(data, ctx->X); |
mluis | 0:91d1a7783bb9 | 114 | //rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X); |
mluis | 0:91d1a7783bb9 | 115 | |
mluis | 0:91d1a7783bb9 | 116 | memcpy1(in, &ctx->X[0], 16); //Bestela ez du ondo iten |
mluis | 0:91d1a7783bb9 | 117 | aes_encrypt( in, in, &ctx->rijndael); |
mluis | 0:91d1a7783bb9 | 118 | memcpy1(&ctx->X[0], in, 16); |
mluis | 0:91d1a7783bb9 | 119 | |
mluis | 0:91d1a7783bb9 | 120 | data += 16; |
mluis | 0:91d1a7783bb9 | 121 | len -= 16; |
mluis | 0:91d1a7783bb9 | 122 | } |
mluis | 0:91d1a7783bb9 | 123 | /* potential last block, save it */ |
mluis | 0:91d1a7783bb9 | 124 | memcpy1(ctx->M_last, ( uint8_t* )data, len); |
mluis | 0:91d1a7783bb9 | 125 | ctx->M_n = len; |
mluis | 0:91d1a7783bb9 | 126 | } |
mluis | 0:91d1a7783bb9 | 127 | |
mluis | 0:91d1a7783bb9 | 128 | void AES_CMAC_Final(u_int8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx) |
mluis | 0:91d1a7783bb9 | 129 | { |
mluis | 0:91d1a7783bb9 | 130 | u_int8_t K[16]; |
mluis | 0:91d1a7783bb9 | 131 | unsigned char in[16]; |
mluis | 0:91d1a7783bb9 | 132 | /* generate subkey K1 */ |
mluis | 0:91d1a7783bb9 | 133 | memset1(K, '\0', 16); |
mluis | 0:91d1a7783bb9 | 134 | |
mluis | 0:91d1a7783bb9 | 135 | //rijndael_encrypt(&ctx->rijndael, K, K); |
mluis | 0:91d1a7783bb9 | 136 | |
mluis | 0:91d1a7783bb9 | 137 | aes_encrypt( K, K, &ctx->rijndael); |
mluis | 0:91d1a7783bb9 | 138 | |
mluis | 0:91d1a7783bb9 | 139 | if (K[0] & 0x80) { |
mluis | 0:91d1a7783bb9 | 140 | LSHIFT(K, K); |
mluis | 0:91d1a7783bb9 | 141 | K[15] ^= 0x87; |
mluis | 0:91d1a7783bb9 | 142 | } else |
mluis | 0:91d1a7783bb9 | 143 | LSHIFT(K, K); |
mluis | 0:91d1a7783bb9 | 144 | |
mluis | 0:91d1a7783bb9 | 145 | |
mluis | 0:91d1a7783bb9 | 146 | if (ctx->M_n == 16) { |
mluis | 0:91d1a7783bb9 | 147 | /* last block was a complete block */ |
mluis | 0:91d1a7783bb9 | 148 | XOR(K, ctx->M_last); |
mluis | 0:91d1a7783bb9 | 149 | |
mluis | 0:91d1a7783bb9 | 150 | } else { |
mluis | 0:91d1a7783bb9 | 151 | /* generate subkey K2 */ |
mluis | 0:91d1a7783bb9 | 152 | if (K[0] & 0x80) { |
mluis | 0:91d1a7783bb9 | 153 | LSHIFT(K, K); |
mluis | 0:91d1a7783bb9 | 154 | K[15] ^= 0x87; |
mluis | 0:91d1a7783bb9 | 155 | } else |
mluis | 0:91d1a7783bb9 | 156 | LSHIFT(K, K); |
mluis | 0:91d1a7783bb9 | 157 | |
mluis | 0:91d1a7783bb9 | 158 | /* padding(M_last) */ |
mluis | 0:91d1a7783bb9 | 159 | ctx->M_last[ctx->M_n] = 0x80; |
mluis | 0:91d1a7783bb9 | 160 | while (++ctx->M_n < 16) |
mluis | 0:91d1a7783bb9 | 161 | ctx->M_last[ctx->M_n] = 0; |
mluis | 0:91d1a7783bb9 | 162 | |
mluis | 0:91d1a7783bb9 | 163 | XOR(K, ctx->M_last); |
mluis | 0:91d1a7783bb9 | 164 | |
mluis | 0:91d1a7783bb9 | 165 | |
mluis | 0:91d1a7783bb9 | 166 | } |
mluis | 0:91d1a7783bb9 | 167 | XOR(ctx->M_last, ctx->X); |
mluis | 0:91d1a7783bb9 | 168 | |
mluis | 0:91d1a7783bb9 | 169 | //rijndael_encrypt(&ctx->rijndael, ctx->X, digest); |
mluis | 0:91d1a7783bb9 | 170 | |
mluis | 0:91d1a7783bb9 | 171 | memcpy1(in, &ctx->X[0], 16); //Bestela ez du ondo iten |
mluis | 0:91d1a7783bb9 | 172 | aes_encrypt(in, digest, &ctx->rijndael); |
mluis | 0:91d1a7783bb9 | 173 | memset1(K, 0, sizeof K); |
mluis | 0:91d1a7783bb9 | 174 | |
mluis | 0:91d1a7783bb9 | 175 | } |
mluis | 0:91d1a7783bb9 | 176 |