for India band 30dBm LoRa Module, DSPLoRa based on code from Semtech and as modified by Sachin Pukale/ TCL/

Dependencies:   mbed Chainable_RGB_LED DigitDisplay LoRaWAN-lib SX1276Lib

Fork of DSP_LoRaWAN by Akshay Mishra

Committer:
akshaymishra
Date:
Thu Jan 05 03:47:15 2017 +0000
Revision:
7:5c0e1dfdf0c6
Parent:
0:cb80564f40e1
Address update

Who changed what in which revision?

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