Rishin Amin / LoRaWAN-lib-2

Fork of LoRaWAN-lib_publishing_testing_UART_bug by Rishin Amin

Committer:
Rishin
Date:
Tue Dec 12 11:19:44 2017 +0000
Revision:
13:2337cfa0bea3
Seemingly working code

Who changed what in which revision?

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