RadioShuttle Lib for the STM32 L4 Heltec Board

Dependents:   Turtle_RadioShuttle

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RadioSecurity.cpp Source File

RadioSecurity.cpp

00001 /*
00002  * The file is licensed under the Apache License, Version 2.0
00003  * (c) 2019 Helmut Tschemernjak
00004  * 30826 Garbsen (Hannover) Germany
00005  */
00006 
00007 #ifdef ARDUINO
00008 #include <Arduino.h>
00009 #define FEATURE_LORA    1
00010 #include "arduino-util.h"
00011 #endif
00012 
00013 #ifdef __MBED__
00014 #include "mbed.h"
00015 #include "main.h"
00016 #include "PinMap.h"
00017 #endif
00018 
00019 #include "RadioSecurityInterface.h"
00020 #include "RadioSecurity.h"
00021 
00022 #ifdef FEATURE_LORA
00023 RadioSecurity::RadioSecurity(void)
00024 {
00025     
00026 }
00027 
00028 RadioSecurity::~RadioSecurity(void)
00029 {
00030 }
00031 
00032 int
00033 RadioSecurity::GetSecurityVersion(void)
00034 {
00035     return _securityVers;
00036 }
00037 
00038 int
00039 RadioSecurity::GetHashBlockSize(void)
00040 {
00041     return SHA256_BLOCK_SIZE;
00042 }
00043 
00044 void
00045 RadioSecurity::HashPassword(void *seed, int seedLen, void *password, int pwLen, void *hashResult)
00046 {
00047     SHA256_CTX *shactx = (SHA256_CTX *)new uint8_t[sizeof(SHA256_CTX)];
00048     if (!shactx)
00049         return;
00050     
00051     sha256_init(shactx);
00052     if (seedLen)
00053         sha256_update(shactx, (BYTE *)seed, seedLen);
00054     if (password)
00055         sha256_update(shactx, (BYTE *)password, pwLen);
00056     sha256_final(shactx, (BYTE *)hashResult);
00057     
00058     delete[] shactx;
00059 }
00060 
00061 
00062 int
00063 RadioSecurity::GetEncryptionBlockSize(void)
00064 {
00065     return AES128_KEYLEN;
00066 }
00067 
00068 
00069 
00070 void *
00071 RadioSecurity::CreateEncryptionContext(void *key, int keyLen, void *seed, int seedlen)
00072 {
00073     AES_CTX *aesctx = (AES_CTX *)new uint8_t[sizeof(AES_CTX)];
00074     
00075     uint8_t mykey[AES128_KEYLEN];
00076     uint8_t myseed[AES128_KEYLEN];
00077     
00078     if (seed) {
00079         memset(myseed, 0, sizeof(myseed));
00080         memcpy(myseed, seed, seedlen > AES128_KEYLEN ? AES128_KEYLEN : seedlen);
00081     }
00082     memset(mykey, 0, sizeof(mykey));
00083     memcpy(mykey, key, keyLen  > AES128_KEYLEN ? AES128_KEYLEN : keyLen);
00084 
00085     AES128_InitContext(aesctx, mykey, seed ? myseed : NULL);
00086 
00087     return aesctx;
00088 }
00089 
00090 void
00091 RadioSecurity::DestroyEncryptionContext(void *context)
00092 {
00093     delete[] (AES_CTX *)context;
00094 }
00095 
00096 
00097 void
00098 RadioSecurity::EncryptMessage(void *context, const void *input, void *output, int len)
00099 {
00100     uint8_t *in = (uint8_t *)input;
00101     uint8_t *out = (uint8_t *)output;
00102     int off = 0;
00103     
00104     while (off < len) {
00105         AES128_ECB_encrypt((AES_CTX *)context, in + off, out + off);
00106         off += AES128_KEYLEN;
00107     }
00108 }
00109 
00110 
00111 void
00112 RadioSecurity::DecryptMessage(void *context, const void *input, void *output, int len)
00113 {
00114     uint8_t *in = (uint8_t *)input;
00115     uint8_t *out = (uint8_t *)output;
00116     int off = 0;
00117     
00118     while (off < len) {
00119         AES128_ECB_decrypt((AES_CTX *)context, in + off, out + off);
00120         off += AES128_KEYLEN;
00121     }
00122 }
00123 
00124 void
00125 RadioSecurity::EncryptTest(void)
00126 {
00127     uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
00128     uint8_t iv[]  = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
00129     
00130     {
00131         
00132         dprintf("ECB encrypt: ");
00133         void *context = CreateEncryptionContext(key, sizeof(key));
00134         
00135         // static void test_encrypt_ecb(void)
00136         uint8_t in[]  = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
00137         uint8_t out[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
00138         uint8_t buffer[16];
00139         
00140         
00141         AES128_ECB_encrypt((AES_CTX *)context, in, buffer);
00142         
00143         
00144         if (memcmp((char*) out, (char*) buffer, 16) == 0)
00145         {
00146             dprintf("SUCCESS!");
00147         } else {
00148             dprintf("FAILURE!");
00149         }
00150         DestroyEncryptionContext(context);
00151     }
00152     
00153     {
00154         dprintf("ECB decrypt: ");
00155         void *context = CreateEncryptionContext(key, sizeof(key));
00156 
00157         uint8_t in[]  = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
00158         uint8_t out[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
00159         uint8_t buffer[16];
00160         
00161         AES128_ECB_decrypt((AES_CTX *)context, in, buffer);
00162         
00163         if(memcmp((char*) out, (char*) buffer, 16) == 0) {
00164             dprintf("SUCCESS!");
00165         } else {
00166             dprintf("FAILURE!");
00167         }
00168         DestroyEncryptionContext(context);
00169     }
00170 
00171     
00172     {
00173         dprintf("CBC encrypt: ");
00174         void *context = CreateEncryptionContext(key, sizeof(key), iv, sizeof(iv));
00175 
00176         uint8_t in[]  = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
00177             0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
00178             0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
00179             0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
00180         uint8_t out[] = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
00181             0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
00182             0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
00183             0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
00184     
00185         uint8_t buffer[64];
00186 
00187     
00188         AES128_CBC_encrypt_buffer((AES_CTX  *)context, buffer, in, 64);
00189     
00190     
00191         if(memcmp((char*) out, (char*) buffer, 64) == 0)     {
00192             dprintf("SUCCESS!");
00193         } else     {
00194             dprintf("FAILURE!");
00195         }
00196         DestroyEncryptionContext(context);
00197     }
00198     
00199     {
00200         dprintf("CBC decrypt: ");
00201         void *context = CreateEncryptionContext(key, sizeof(key), iv, sizeof(iv));
00202 
00203         uint8_t in[]  = { 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
00204             0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
00205             0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
00206             0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 };
00207         uint8_t out[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
00208             0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
00209             0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
00210             0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
00211         uint8_t buffer[64];
00212         
00213         
00214         AES128_CBC_decrypt_buffer((AES_CTX  *)context, buffer+0, in+0,  16);
00215         AES128_CBC_decrypt_buffer((AES_CTX  *)context, buffer+16, in+16, 16);
00216         AES128_CBC_decrypt_buffer((AES_CTX  *)context, buffer+32, in+32, 16);
00217         AES128_CBC_decrypt_buffer((AES_CTX  *)context, buffer+48, in+48, 16);
00218         
00219         if (memcmp((char*) out, (char*) buffer, 64) == 0) {
00220             dprintf("SUCCESS!");
00221         } else {
00222             dprintf("FAILURE!");
00223         }
00224         DestroyEncryptionContext(context);
00225     }
00226 }
00227 
00228 
00229 #endif // FEATURE_LORA