RadioShuttle Lib for the STM32 L4 Heltec Board

Dependents:   Turtle_RadioShuttle

Committer:
Helmut Tschemernjak
Date:
Mon Mar 04 09:41:41 2019 +0100
Revision:
11:91bc7ef20f21
Parent:
0:0c31756924a2
Updated lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Helmut64 0:0c31756924a2 1 /*
Helmut64 0:0c31756924a2 2 * The file is licensed under the Apache License, Version 2.0
Helmut64 0:0c31756924a2 3 * (c) 2019 Helmut Tschemernjak
Helmut64 0:0c31756924a2 4 * 30826 Garbsen (Hannover) Germany
Helmut64 0:0c31756924a2 5 */
Helmut64 0:0c31756924a2 6
Helmut64 0:0c31756924a2 7 #ifdef ARDUINO
Helmut64 0:0c31756924a2 8 #include <Arduino.h>
Helmut64 0:0c31756924a2 9 #define FEATURE_LORA 1
Helmut64 0:0c31756924a2 10 #include "arduino-util.h"
Helmut64 0:0c31756924a2 11 #endif
Helmut64 0:0c31756924a2 12
Helmut64 0:0c31756924a2 13 #ifdef __MBED__
Helmut64 0:0c31756924a2 14 #include "mbed.h"
Helmut64 0:0c31756924a2 15 #include "main.h"
Helmut64 0:0c31756924a2 16 #include "PinMap.h"
Helmut64 0:0c31756924a2 17 #endif
Helmut64 0:0c31756924a2 18
Helmut64 0:0c31756924a2 19 #include "RadioSecurityInterface.h"
Helmut64 0:0c31756924a2 20 #include "RadioSecurity.h"
Helmut64 0:0c31756924a2 21
Helmut64 0:0c31756924a2 22 #ifdef FEATURE_LORA
Helmut64 0:0c31756924a2 23 RadioSecurity::RadioSecurity(void)
Helmut64 0:0c31756924a2 24 {
Helmut64 0:0c31756924a2 25
Helmut64 0:0c31756924a2 26 }
Helmut64 0:0c31756924a2 27
Helmut64 0:0c31756924a2 28 RadioSecurity::~RadioSecurity(void)
Helmut64 0:0c31756924a2 29 {
Helmut64 0:0c31756924a2 30 }
Helmut64 0:0c31756924a2 31
Helmut64 0:0c31756924a2 32 int
Helmut64 0:0c31756924a2 33 RadioSecurity::GetSecurityVersion(void)
Helmut64 0:0c31756924a2 34 {
Helmut64 0:0c31756924a2 35 return _securityVers;
Helmut64 0:0c31756924a2 36 }
Helmut64 0:0c31756924a2 37
Helmut64 0:0c31756924a2 38 int
Helmut64 0:0c31756924a2 39 RadioSecurity::GetHashBlockSize(void)
Helmut64 0:0c31756924a2 40 {
Helmut64 0:0c31756924a2 41 return SHA256_BLOCK_SIZE;
Helmut64 0:0c31756924a2 42 }
Helmut64 0:0c31756924a2 43
Helmut64 0:0c31756924a2 44 void
Helmut64 0:0c31756924a2 45 RadioSecurity::HashPassword(void *seed, int seedLen, void *password, int pwLen, void *hashResult)
Helmut64 0:0c31756924a2 46 {
Helmut64 0:0c31756924a2 47 SHA256_CTX *shactx = (SHA256_CTX *)new uint8_t[sizeof(SHA256_CTX)];
Helmut64 0:0c31756924a2 48 if (!shactx)
Helmut64 0:0c31756924a2 49 return;
Helmut64 0:0c31756924a2 50
Helmut64 0:0c31756924a2 51 sha256_init(shactx);
Helmut64 0:0c31756924a2 52 if (seedLen)
Helmut64 0:0c31756924a2 53 sha256_update(shactx, (BYTE *)seed, seedLen);
Helmut64 0:0c31756924a2 54 if (password)
Helmut64 0:0c31756924a2 55 sha256_update(shactx, (BYTE *)password, pwLen);
Helmut64 0:0c31756924a2 56 sha256_final(shactx, (BYTE *)hashResult);
Helmut64 0:0c31756924a2 57
Helmut64 0:0c31756924a2 58 delete[] shactx;
Helmut64 0:0c31756924a2 59 }
Helmut64 0:0c31756924a2 60
Helmut64 0:0c31756924a2 61
Helmut64 0:0c31756924a2 62 int
Helmut64 0:0c31756924a2 63 RadioSecurity::GetEncryptionBlockSize(void)
Helmut64 0:0c31756924a2 64 {
Helmut64 0:0c31756924a2 65 return AES128_KEYLEN;
Helmut64 0:0c31756924a2 66 }
Helmut64 0:0c31756924a2 67
Helmut64 0:0c31756924a2 68
Helmut64 0:0c31756924a2 69
Helmut64 0:0c31756924a2 70 void *
Helmut64 0:0c31756924a2 71 RadioSecurity::CreateEncryptionContext(void *key, int keyLen, void *seed, int seedlen)
Helmut64 0:0c31756924a2 72 {
Helmut64 0:0c31756924a2 73 AES_CTX *aesctx = (AES_CTX *)new uint8_t[sizeof(AES_CTX)];
Helmut64 0:0c31756924a2 74
Helmut64 0:0c31756924a2 75 uint8_t mykey[AES128_KEYLEN];
Helmut64 0:0c31756924a2 76 uint8_t myseed[AES128_KEYLEN];
Helmut64 0:0c31756924a2 77
Helmut64 0:0c31756924a2 78 if (seed) {
Helmut64 0:0c31756924a2 79 memset(myseed, 0, sizeof(myseed));
Helmut64 0:0c31756924a2 80 memcpy(myseed, seed, seedlen > AES128_KEYLEN ? AES128_KEYLEN : seedlen);
Helmut64 0:0c31756924a2 81 }
Helmut64 0:0c31756924a2 82 memset(mykey, 0, sizeof(mykey));
Helmut64 0:0c31756924a2 83 memcpy(mykey, key, keyLen > AES128_KEYLEN ? AES128_KEYLEN : keyLen);
Helmut64 0:0c31756924a2 84
Helmut64 0:0c31756924a2 85 AES128_InitContext(aesctx, mykey, seed ? myseed : NULL);
Helmut64 0:0c31756924a2 86
Helmut64 0:0c31756924a2 87 return aesctx;
Helmut64 0:0c31756924a2 88 }
Helmut64 0:0c31756924a2 89
Helmut64 0:0c31756924a2 90 void
Helmut64 0:0c31756924a2 91 RadioSecurity::DestroyEncryptionContext(void *context)
Helmut64 0:0c31756924a2 92 {
Helmut64 0:0c31756924a2 93 delete[] (AES_CTX *)context;
Helmut64 0:0c31756924a2 94 }
Helmut64 0:0c31756924a2 95
Helmut64 0:0c31756924a2 96
Helmut64 0:0c31756924a2 97 void
Helmut64 0:0c31756924a2 98 RadioSecurity::EncryptMessage(void *context, const void *input, void *output, int len)
Helmut64 0:0c31756924a2 99 {
Helmut64 0:0c31756924a2 100 uint8_t *in = (uint8_t *)input;
Helmut64 0:0c31756924a2 101 uint8_t *out = (uint8_t *)output;
Helmut64 0:0c31756924a2 102 int off = 0;
Helmut64 0:0c31756924a2 103
Helmut64 0:0c31756924a2 104 while (off < len) {
Helmut64 0:0c31756924a2 105 AES128_ECB_encrypt((AES_CTX *)context, in + off, out + off);
Helmut64 0:0c31756924a2 106 off += AES128_KEYLEN;
Helmut64 0:0c31756924a2 107 }
Helmut64 0:0c31756924a2 108 }
Helmut64 0:0c31756924a2 109
Helmut64 0:0c31756924a2 110
Helmut64 0:0c31756924a2 111 void
Helmut64 0:0c31756924a2 112 RadioSecurity::DecryptMessage(void *context, const void *input, void *output, int len)
Helmut64 0:0c31756924a2 113 {
Helmut64 0:0c31756924a2 114 uint8_t *in = (uint8_t *)input;
Helmut64 0:0c31756924a2 115 uint8_t *out = (uint8_t *)output;
Helmut64 0:0c31756924a2 116 int off = 0;
Helmut64 0:0c31756924a2 117
Helmut64 0:0c31756924a2 118 while (off < len) {
Helmut64 0:0c31756924a2 119 AES128_ECB_decrypt((AES_CTX *)context, in + off, out + off);
Helmut64 0:0c31756924a2 120 off += AES128_KEYLEN;
Helmut64 0:0c31756924a2 121 }
Helmut64 0:0c31756924a2 122 }
Helmut64 0:0c31756924a2 123
Helmut64 0:0c31756924a2 124 void
Helmut64 0:0c31756924a2 125 RadioSecurity::EncryptTest(void)
Helmut64 0:0c31756924a2 126 {
Helmut64 0:0c31756924a2 127 uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
Helmut64 0:0c31756924a2 128 uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
Helmut64 0:0c31756924a2 129
Helmut64 0:0c31756924a2 130 {
Helmut64 0:0c31756924a2 131
Helmut64 0:0c31756924a2 132 dprintf("ECB encrypt: ");
Helmut64 0:0c31756924a2 133 void *context = CreateEncryptionContext(key, sizeof(key));
Helmut64 0:0c31756924a2 134
Helmut64 0:0c31756924a2 135 // static void test_encrypt_ecb(void)
Helmut64 0:0c31756924a2 136 uint8_t in[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
Helmut64 0:0c31756924a2 137 uint8_t out[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
Helmut64 0:0c31756924a2 138 uint8_t buffer[16];
Helmut64 0:0c31756924a2 139
Helmut64 0:0c31756924a2 140
Helmut64 0:0c31756924a2 141 AES128_ECB_encrypt((AES_CTX *)context, in, buffer);
Helmut64 0:0c31756924a2 142
Helmut64 0:0c31756924a2 143
Helmut64 0:0c31756924a2 144 if (memcmp((char*) out, (char*) buffer, 16) == 0)
Helmut64 0:0c31756924a2 145 {
Helmut64 0:0c31756924a2 146 dprintf("SUCCESS!");
Helmut64 0:0c31756924a2 147 } else {
Helmut64 0:0c31756924a2 148 dprintf("FAILURE!");
Helmut64 0:0c31756924a2 149 }
Helmut64 0:0c31756924a2 150 DestroyEncryptionContext(context);
Helmut64 0:0c31756924a2 151 }
Helmut64 0:0c31756924a2 152
Helmut64 0:0c31756924a2 153 {
Helmut64 0:0c31756924a2 154 dprintf("ECB decrypt: ");
Helmut64 0:0c31756924a2 155 void *context = CreateEncryptionContext(key, sizeof(key));
Helmut64 0:0c31756924a2 156
Helmut64 0:0c31756924a2 157 uint8_t in[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
Helmut64 0:0c31756924a2 158 uint8_t out[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
Helmut64 0:0c31756924a2 159 uint8_t buffer[16];
Helmut64 0:0c31756924a2 160
Helmut64 0:0c31756924a2 161 AES128_ECB_decrypt((AES_CTX *)context, in, buffer);
Helmut64 0:0c31756924a2 162
Helmut64 0:0c31756924a2 163 if(memcmp((char*) out, (char*) buffer, 16) == 0) {
Helmut64 0:0c31756924a2 164 dprintf("SUCCESS!");
Helmut64 0:0c31756924a2 165 } else {
Helmut64 0:0c31756924a2 166 dprintf("FAILURE!");
Helmut64 0:0c31756924a2 167 }
Helmut64 0:0c31756924a2 168 DestroyEncryptionContext(context);
Helmut64 0:0c31756924a2 169 }
Helmut64 0:0c31756924a2 170
Helmut64 0:0c31756924a2 171
Helmut64 0:0c31756924a2 172 {
Helmut64 0:0c31756924a2 173 dprintf("CBC encrypt: ");
Helmut64 0:0c31756924a2 174 void *context = CreateEncryptionContext(key, sizeof(key), iv, sizeof(iv));
Helmut64 0:0c31756924a2 175
Helmut64 0:0c31756924a2 176 uint8_t in[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
Helmut64 0:0c31756924a2 177 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
Helmut64 0:0c31756924a2 178 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
Helmut64 0:0c31756924a2 179 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
Helmut64 0:0c31756924a2 180 uint8_t out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
Helmut64 0:0c31756924a2 181 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
Helmut64 0:0c31756924a2 182 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
Helmut64 0:0c31756924a2 183 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
Helmut64 0:0c31756924a2 184
Helmut64 0:0c31756924a2 185 uint8_t buffer[64];
Helmut64 0:0c31756924a2 186
Helmut64 0:0c31756924a2 187
Helmut64 0:0c31756924a2 188 AES128_CBC_encrypt_buffer((AES_CTX *)context, buffer, in, 64);
Helmut64 0:0c31756924a2 189
Helmut64 0:0c31756924a2 190
Helmut64 0:0c31756924a2 191 if(memcmp((char*) out, (char*) buffer, 64) == 0) {
Helmut64 0:0c31756924a2 192 dprintf("SUCCESS!");
Helmut64 0:0c31756924a2 193 } else {
Helmut64 0:0c31756924a2 194 dprintf("FAILURE!");
Helmut64 0:0c31756924a2 195 }
Helmut64 0:0c31756924a2 196 DestroyEncryptionContext(context);
Helmut64 0:0c31756924a2 197 }
Helmut64 0:0c31756924a2 198
Helmut64 0:0c31756924a2 199 {
Helmut64 0:0c31756924a2 200 dprintf("CBC decrypt: ");
Helmut64 0:0c31756924a2 201 void *context = CreateEncryptionContext(key, sizeof(key), iv, sizeof(iv));
Helmut64 0:0c31756924a2 202
Helmut64 0:0c31756924a2 203 uint8_t in[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
Helmut64 0:0c31756924a2 204 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
Helmut64 0:0c31756924a2 205 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
Helmut64 0:0c31756924a2 206 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
Helmut64 0:0c31756924a2 207 uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
Helmut64 0:0c31756924a2 208 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
Helmut64 0:0c31756924a2 209 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
Helmut64 0:0c31756924a2 210 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
Helmut64 0:0c31756924a2 211 uint8_t buffer[64];
Helmut64 0:0c31756924a2 212
Helmut64 0:0c31756924a2 213
Helmut64 0:0c31756924a2 214 AES128_CBC_decrypt_buffer((AES_CTX *)context, buffer+0, in+0, 16);
Helmut64 0:0c31756924a2 215 AES128_CBC_decrypt_buffer((AES_CTX *)context, buffer+16, in+16, 16);
Helmut64 0:0c31756924a2 216 AES128_CBC_decrypt_buffer((AES_CTX *)context, buffer+32, in+32, 16);
Helmut64 0:0c31756924a2 217 AES128_CBC_decrypt_buffer((AES_CTX *)context, buffer+48, in+48, 16);
Helmut64 0:0c31756924a2 218
Helmut64 0:0c31756924a2 219 if (memcmp((char*) out, (char*) buffer, 64) == 0) {
Helmut64 0:0c31756924a2 220 dprintf("SUCCESS!");
Helmut64 0:0c31756924a2 221 } else {
Helmut64 0:0c31756924a2 222 dprintf("FAILURE!");
Helmut64 0:0c31756924a2 223 }
Helmut64 0:0c31756924a2 224 DestroyEncryptionContext(context);
Helmut64 0:0c31756924a2 225 }
Helmut64 0:0c31756924a2 226 }
Helmut64 0:0c31756924a2 227
Helmut64 0:0c31756924a2 228
Helmut64 0:0c31756924a2 229 #endif // FEATURE_LORA