UpdatedDecryp

Dependencies:   BahlDecrypModified CyaSSL mbed nRF51822

Fork of Decryptulator by Mobius IoT

Committer:
vbahl2
Date:
Tue May 09 03:06:55 2017 +0000
Revision:
13:8b706583610a
Parent:
12:dbbf0ddc9b12
UpdatedDecryp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vbahl2 13:8b706583610a 1 #include <stdio.h>
rgrover1 0:332983584a9c 2 #include "mbed.h"
budoguyiii 12:dbbf0ddc9b12 3 //#include "toolchain.h"
rgrover1 9:69a2ad0bcdb7 4 #include "ble/BLE.h"
vbahl2 13:8b706583610a 5 #include "ble/BLEProtocol.h"
sunsmile2015 7:91324daa3bfa 6 #include "TMP_nrf51/TMP_nrf51.h"
vbahl2 13:8b706583610a 7 #include "newSha256.h"
vbahl2 13:8b706583610a 8 #include "UARTService.h"
sunsmile2015 7:91324daa3bfa 9
vbahl2 13:8b706583610a 10 #include "Hashes/sfh_mbed.h"
budoguyiii 12:dbbf0ddc9b12 11
budoguyiii 12:dbbf0ddc9b12 12 #include "ctc_aes.h"
budoguyiii 12:dbbf0ddc9b12 13
budoguyiii 12:dbbf0ddc9b12 14 #define UART_TX p9
budoguyiii 12:dbbf0ddc9b12 15 #define UART_RX p11
budoguyiii 12:dbbf0ddc9b12 16
budoguyiii 12:dbbf0ddc9b12 17 #define LOG(...) { pc.printf(__VA_ARGS__); }
budoguyiii 12:dbbf0ddc9b12 18
vbahl2 13:8b706583610a 19 //#define DEBUG 1
vbahl2 13:8b706583610a 20
vbahl2 13:8b706583610a 21
vbahl2 13:8b706583610a 22 #define TABLE_SIZE 3000
vbahl2 13:8b706583610a 23 #define SLIDING_WINDOW_SIZE 300
vbahl2 13:8b706583610a 24
rgrover1 9:69a2ad0bcdb7 25 DigitalOut alivenessLED(LED1, 1);
andresag 11:16f67d5752e1 26 Ticker ticker;
rgrover1 0:332983584a9c 27
budoguyiii 12:dbbf0ddc9b12 28 Serial pc(UART_TX, UART_RX);
budoguyiii 12:dbbf0ddc9b12 29
budoguyiii 12:dbbf0ddc9b12 30 UARTService *uartServicePtr;
budoguyiii 12:dbbf0ddc9b12 31
budoguyiii 12:dbbf0ddc9b12 32 //stuff for encryption
budoguyiii 12:dbbf0ddc9b12 33 uint8_t payload[31];
budoguyiii 12:dbbf0ddc9b12 34 #define BLOCK_SIZE 16
budoguyiii 12:dbbf0ddc9b12 35 #define KEYLEN 256 //128, 192, 256
budoguyiii 12:dbbf0ddc9b12 36 int j;
budoguyiii 12:dbbf0ddc9b12 37 unsigned char nonce_counter[BLOCK_SIZE];
budoguyiii 12:dbbf0ddc9b12 38 unsigned char plain[BLOCK_SIZE];
budoguyiii 12:dbbf0ddc9b12 39 unsigned char cipher[BLOCK_SIZE];
budoguyiii 12:dbbf0ddc9b12 40 unsigned char* counter_bytes = nonce_counter+BLOCK_SIZE/2;
budoguyiii 12:dbbf0ddc9b12 41 size_t counter_tx_len = 3;
budoguyiii 12:dbbf0ddc9b12 42 unsigned char key[KEYLEN/8];
budoguyiii 12:dbbf0ddc9b12 43 unsigned char iv[BLOCK_SIZE];//not used for ctr mode but required by setKey
budoguyiii 12:dbbf0ddc9b12 44 Aes ctx;
budoguyiii 12:dbbf0ddc9b12 45
vbahl2 13:8b706583610a 46 #define DECRYP_HASH_SIZE 6
vbahl2 13:8b706583610a 47 #define HASH_CTR_SIZE 255
vbahl2 13:8b706583610a 48
vbahl2 13:8b706583610a 49 bool isSameMAC(const Gap::AdvertisementCallbackParams_t *params){
vbahl2 13:8b706583610a 50
vbahl2 13:8b706583610a 51 for(int i=(params->advertisingDataLen)-counter_tx_len; i < params->advertisingDataLen; i++){
vbahl2 13:8b706583610a 52 nonce_counter[BLOCK_SIZE-3+(i-((params->advertisingDataLen)-counter_tx_len))] = params->advertisingData[i];
vbahl2 13:8b706583610a 53 printf("params->advertisingData[%d]: %02x\n", i,params->advertisingData[i]);
vbahl2 13:8b706583610a 54 }
vbahl2 13:8b706583610a 55 printf("\n");
vbahl2 13:8b706583610a 56 printf("nonce_counter done\n");
vbahl2 13:8b706583610a 57 #ifdef DEBUG
vbahl2 13:8b706583610a 58 printf("\n\n");
vbahl2 13:8b706583610a 59 #endif
vbahl2 13:8b706583610a 60
vbahl2 13:8b706583610a 61 /*Dummy MAC address with NoOPS*/
vbahl2 13:8b706583610a 62 BLEProtocol::AddressBytes_t alpha = {0x90,0x90,0x90,0x90,0x90,0x90};
vbahl2 13:8b706583610a 63
vbahl2 13:8b706583610a 64 for(int k = 0; k< DECRYP_HASH_SIZE; k ++){
vbahl2 13:8b706583610a 65 alpha[k] = params->peerAddr[k];
vbahl2 13:8b706583610a 66 #ifdef DEBUG
vbahl2 13:8b706583610a 67 printf("alpha[%d]: %02x peerAddr[%d]: %02x",k,alpha[k],k,params->peerAddr[k]);
vbahl2 13:8b706583610a 68 #endif
vbahl2 13:8b706583610a 69 }
vbahl2 13:8b706583610a 70
vbahl2 13:8b706583610a 71 bool encryptedMac = true;
vbahl2 13:8b706583610a 72
vbahl2 13:8b706583610a 73 unsigned char pre_out[32];
vbahl2 13:8b706583610a 74 unsigned char out[32];
vbahl2 13:8b706583610a 75
vbahl2 13:8b706583610a 76 do{
vbahl2 13:8b706583610a 77 mbedtls_sha256(nonce_counter, sizeof(nonce_counter), out, 0);
vbahl2 13:8b706583610a 78 /*rehash*/
vbahl2 13:8b706583610a 79
vbahl2 13:8b706583610a 80 for(int idx = 0; idx < DECRYP_HASH_SIZE; idx++){
vbahl2 13:8b706583610a 81 //#ifdef DEBUG
vbahl2 13:8b706583610a 82 printf("out[%d]: %02x - alpha[%d]: %02x\n",idx,out[idx],idx,alpha[idx]);
vbahl2 13:8b706583610a 83 //#endif
vbahl2 13:8b706583610a 84 if(out[idx] == alpha[idx]){
vbahl2 13:8b706583610a 85 /*check all bytes*/
vbahl2 13:8b706583610a 86 continue;
vbahl2 13:8b706583610a 87 }
vbahl2 13:8b706583610a 88 else{
vbahl2 13:8b706583610a 89 printf("NOT ENCRYPTULATOR! EXITING DECRYPTION\n\n");
vbahl2 13:8b706583610a 90 encryptedMac = false;
vbahl2 13:8b706583610a 91 break;
vbahl2 13:8b706583610a 92 }
vbahl2 13:8b706583610a 93 }
vbahl2 13:8b706583610a 94 }while(0);
vbahl2 13:8b706583610a 95
vbahl2 13:8b706583610a 96
vbahl2 13:8b706583610a 97 return encryptedMac;
vbahl2 13:8b706583610a 98
vbahl2 13:8b706583610a 99 }
budoguyiii 12:dbbf0ddc9b12 100
rgrover1 0:332983584a9c 101 void periodicCallback(void)
rgrover1 0:332983584a9c 102 {
rgrover1 9:69a2ad0bcdb7 103 alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */
rgrover1 0:332983584a9c 104 }
budoguyiii 12:dbbf0ddc9b12 105 void initAES(void)
budoguyiii 12:dbbf0ddc9b12 106 {
budoguyiii 12:dbbf0ddc9b12 107 for(int i=0;i<KEYLEN/8;i++)
budoguyiii 12:dbbf0ddc9b12 108 key[i] = i;
budoguyiii 12:dbbf0ddc9b12 109 for(int i=0; i<BLOCK_SIZE-3; i++)
budoguyiii 12:dbbf0ddc9b12 110 nonce_counter[i]=i<8?i:0;
budoguyiii 12:dbbf0ddc9b12 111 for(int i=0; i<BLOCK_SIZE;i++)
budoguyiii 12:dbbf0ddc9b12 112 iv[i]=0;
budoguyiii 12:dbbf0ddc9b12 113 AesSetKey(&ctx, key, KEYLEN/8, iv, AES_ENCRYPTION);
budoguyiii 12:dbbf0ddc9b12 114 }
budoguyiii 12:dbbf0ddc9b12 115
vbahl2 13:8b706583610a 116
vbahl2 13:8b706583610a 117
vbahl2 13:8b706583610a 118
vbahl2 13:8b706583610a 119
vbahl2 13:8b706583610a 120 /*
vbahl2 13:8b706583610a 121 void buildLookupTable(unsigned char * nonce){
vbahl2 13:8b706583610a 122 unsigned char nonce_ctr_cpy[BLOCK_SIZE];
vbahl2 13:8b706583610a 123 unsigned char ctr_cpy = nonce_ctr_cpy + BLOCK_SIZE
vbahl2 13:8b706583610a 124 for(int i = 0; i < BLOCK_SIZE; i++)
vbahl2 13:8b706583610a 125 nonce_ctr_cpy = i;
vbahl2 13:8b706583610a 126 int ** hashLookup = new int[HASH_CTR_SIZE];
vbahl2 13:8b706583610a 127 mbedtls_sha256(nonce_counter, sizeof(nonce_counter), out, 0);
vbahl2 13:8b706583610a 128
vbahl2 13:8b706583610a 129 for(int i = 0; i < HASH_CTR_SIZE; i++){
vbahl2 13:8b706583610a 130 hashLookup[i] = out[i % 32];
vbahl2 13:8b706583610a 131 }
vbahl2 13:8b706583610a 132 }*/
vbahl2 13:8b706583610a 133
budoguyiii 12:dbbf0ddc9b12 134 void decrypt(const Gap::AdvertisementCallbackParams_t *params)
budoguyiii 12:dbbf0ddc9b12 135 {
budoguyiii 12:dbbf0ddc9b12 136 //puts decrypted data into GLOBAL plain variable.
budoguyiii 12:dbbf0ddc9b12 137
vbahl2 13:8b706583610a 138 // pair<uint32_t, uint32_t> * lookupTable = new pair<uint32_t,uint32_t> [300000];
vbahl2 13:8b706583610a 139 pair<BLEProtocol::AddressBytes_t,unsigned char *>;
budoguyiii 12:dbbf0ddc9b12 140 //get coutner
vbahl2 13:8b706583610a 141 /*for(int i=(params->advertisingDataLen)-counter_tx_len; i < params->advertisingDataLen; i++)
budoguyiii 12:dbbf0ddc9b12 142 nonce_counter[BLOCK_SIZE-3+(i-((params->advertisingDataLen)-counter_tx_len))] = params->advertisingData[i];
vbahl2 13:8b706583610a 143 */
budoguyiii 12:dbbf0ddc9b12 144
budoguyiii 12:dbbf0ddc9b12 145 //print nonce_counter
vbahl2 13:8b706583610a 146 printf("\nNonceCtr: ");
budoguyiii 12:dbbf0ddc9b12 147 for(int i=0;i<BLOCK_SIZE;i++)
vbahl2 13:8b706583610a 148 printf("%02x ", nonce_counter[i]);
budoguyiii 12:dbbf0ddc9b12 149
budoguyiii 12:dbbf0ddc9b12 150 //get cipher text
budoguyiii 12:dbbf0ddc9b12 151 for(int i=0; i < (params->advertisingDataLen) - (counter_tx_len + 2); i++)
budoguyiii 12:dbbf0ddc9b12 152 cipher[i] = params->advertisingData[i+2];
budoguyiii 12:dbbf0ddc9b12 153
vbahl2 13:8b706583610a 154
vbahl2 13:8b706583610a 155 //AddressBytes ble_mac = params->peerAddr;
vbahl2 13:8b706583610a 156
vbahl2 13:8b706583610a 157
budoguyiii 12:dbbf0ddc9b12 158 //print cipher
vbahl2 13:8b706583610a 159 printf("\nCiphertxt: ");
budoguyiii 12:dbbf0ddc9b12 160 for(int i=0; i < BLOCK_SIZE; i++)
vbahl2 13:8b706583610a 161 printf("%02x ", cipher[i]);
budoguyiii 12:dbbf0ddc9b12 162
budoguyiii 12:dbbf0ddc9b12 163
budoguyiii 12:dbbf0ddc9b12 164 //build key stream
budoguyiii 12:dbbf0ddc9b12 165 AesEncrypt(&ctx, nonce_counter, plain);
budoguyiii 12:dbbf0ddc9b12 166 //print key
vbahl2 13:8b706583610a 167 printf("\nKey: ");
budoguyiii 12:dbbf0ddc9b12 168 for(int i=0; i<BLOCK_SIZE; i++)
vbahl2 13:8b706583610a 169 printf("%02x ", plain[i]);
budoguyiii 12:dbbf0ddc9b12 170
budoguyiii 12:dbbf0ddc9b12 171 //decrypt into plain (destroying key)
budoguyiii 12:dbbf0ddc9b12 172 for(int i=0;i<BLOCK_SIZE;i++)
budoguyiii 12:dbbf0ddc9b12 173 plain[i]^=cipher[i];
budoguyiii 12:dbbf0ddc9b12 174 }
rgrover1 0:332983584a9c 175
rgrover1 9:69a2ad0bcdb7 176 /*
rgrover1 9:69a2ad0bcdb7 177 * This function is called every time we scan an advertisement.
rgrover1 9:69a2ad0bcdb7 178 */
sunsmile2015 6:850f44146c9f 179 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
sunsmile2015 6:850f44146c9f 180 {
budoguyiii 12:dbbf0ddc9b12 181
rgrover1 9:69a2ad0bcdb7 182 /* Search for the manufacturer specific data with matching application-ID */
budoguyiii 12:dbbf0ddc9b12 183 int addr_length = 6;
vbahl2 13:8b706583610a 184 //int * cachedMAC = new int[3];
budoguyiii 12:dbbf0ddc9b12 185 //print from addr
budoguyiii 12:dbbf0ddc9b12 186 //if(params->peerAddr[addr_length-1] == 0xfc)
budoguyiii 12:dbbf0ddc9b12 187 //{
vbahl2 13:8b706583610a 188 BLEDevice ble;
vbahl2 13:8b706583610a 189 unsigned char address[addr_length];
vbahl2 13:8b706583610a 190 Gap::addr_type_t gap_type = Gap::ADDR_TYPE_PUBLIC;
vbahl2 13:8b706583610a 191 ble.getAddress(&gap_type,address);
vbahl2 13:8b706583610a 192 printf("\nDecryptor MAC: ");
vbahl2 13:8b706583610a 193 for(int jj = 0; jj < addr_length; jj++)
vbahl2 13:8b706583610a 194 printf("%02x:",address[jj]);
vbahl2 13:8b706583610a 195
vbahl2 13:8b706583610a 196
vbahl2 13:8b706583610a 197
vbahl2 13:8b706583610a 198
vbahl2 13:8b706583610a 199 printf("\nFrom: ");
budoguyiii 12:dbbf0ddc9b12 200 for(int i=0; i<addr_length; i++)
vbahl2 13:8b706583610a 201 printf("%02x:", params->peerAddr[addr_length-i-1]);
budoguyiii 12:dbbf0ddc9b12 202 //print payload
vbahl2 13:8b706583610a 203 printf("\nPayload: ");
budoguyiii 12:dbbf0ddc9b12 204 for(int i=0; i < params->advertisingDataLen; i++)
vbahl2 13:8b706583610a 205 printf(" %02x", params->advertisingData[i]);
budoguyiii 12:dbbf0ddc9b12 206
vbahl2 13:8b706583610a 207 //cache the beginning MAC address
vbahl2 13:8b706583610a 208
vbahl2 13:8b706583610a 209 /*
vbahl2 13:8b706583610a 210 for(int c = 0; c < 3; c++){
vbahl2 13:8b706583610a 211 cachedMAC[c] = params->peerAddr[addr_length-c-1];
vbahl2 13:8b706583610a 212 }
vbahl2 13:8b706583610a 213 */
vbahl2 13:8b706583610a 214
vbahl2 13:8b706583610a 215
vbahl2 13:8b706583610a 216 if(isSameMAC(params)){
vbahl2 13:8b706583610a 217 printf("MAC ADDRESS IDENTIFIED!!!!\n");
vbahl2 13:8b706583610a 218 exit(0);
vbahl2 13:8b706583610a 219 decrypt(params);
vbahl2 13:8b706583610a 220 }
vbahl2 13:8b706583610a 221
vbahl2 13:8b706583610a 222
budoguyiii 12:dbbf0ddc9b12 223
budoguyiii 12:dbbf0ddc9b12 224 //print plaintext
vbahl2 13:8b706583610a 225 printf("\nPlaintext: ");
budoguyiii 12:dbbf0ddc9b12 226 for(int i=0; i<BLOCK_SIZE; i++)
vbahl2 13:8b706583610a 227 printf("%02x ", plain[i]);
budoguyiii 12:dbbf0ddc9b12 228
budoguyiii 12:dbbf0ddc9b12 229 //print close of round
vbahl2 13:8b706583610a 230 printf("\n\n");
budoguyiii 12:dbbf0ddc9b12 231 //}
rgrover1 0:332983584a9c 232 }
rgrover1 0:332983584a9c 233
vbahl2 13:8b706583610a 234
vbahl2 13:8b706583610a 235
andresag 11:16f67d5752e1 236 /**
andresag 11:16f67d5752e1 237 * This function is called when the ble initialization process has failed
andresag 11:16f67d5752e1 238 */
andresag 11:16f67d5752e1 239 void onBleInitError(BLE &ble, ble_error_t error)
andresag 11:16f67d5752e1 240 {
andresag 11:16f67d5752e1 241 /* Initialization error handling should go here */
vbahl2 13:8b706583610a 242 printf("Crap, the BLE radio is broken\n");
andresag 11:16f67d5752e1 243 }
andresag 11:16f67d5752e1 244
andresag 11:16f67d5752e1 245 /**
andresag 11:16f67d5752e1 246 * Callback triggered when the ble initialization process has finished
andresag 11:16f67d5752e1 247 */
andresag 11:16f67d5752e1 248 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 11:16f67d5752e1 249 {
andresag 11:16f67d5752e1 250 BLE& ble = params->ble;
andresag 11:16f67d5752e1 251 ble_error_t error = params->error;
andresag 11:16f67d5752e1 252
andresag 11:16f67d5752e1 253 if (error != BLE_ERROR_NONE) {
andresag 11:16f67d5752e1 254 /* In case of error, forward the error handling to onBleInitError */
andresag 11:16f67d5752e1 255 onBleInitError(ble, error);
andresag 11:16f67d5752e1 256 return;
andresag 11:16f67d5752e1 257 }
andresag 11:16f67d5752e1 258
andresag 11:16f67d5752e1 259 /* Ensure that it is the default instance of BLE */
andresag 11:16f67d5752e1 260 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 11:16f67d5752e1 261 return;
andresag 11:16f67d5752e1 262 }
andresag 11:16f67d5752e1 263
andresag 11:16f67d5752e1 264 /* Setup and start scanning */
vbahl2 13:8b706583610a 265 ble.gap().setScanParams(50 /* scan interval */, 50 /* scan window */);
andresag 11:16f67d5752e1 266 ble.gap().startScan(advertisementCallback);
andresag 11:16f67d5752e1 267 }
andresag 11:16f67d5752e1 268
rgrover1 0:332983584a9c 269 int main(void)
rgrover1 0:332983584a9c 270 {
vbahl2 13:8b706583610a 271
budoguyiii 12:dbbf0ddc9b12 272 //use 115200 for term 4M for energy
vbahl2 13:8b706583610a 273 //pc.baud(115200);
budoguyiii 12:dbbf0ddc9b12 274
vbahl2 13:8b706583610a 275
vbahl2 13:8b706583610a 276
vbahl2 13:8b706583610a 277 printf("---- DECRYPTULATOR ACTIVIZE ----\n");
budoguyiii 12:dbbf0ddc9b12 278 initAES();
budoguyiii 12:dbbf0ddc9b12 279
budoguyiii 12:dbbf0ddc9b12 280 ticker.attach(periodicCallback, 1); /* flash the LED because reasons */
rgrover1 0:332983584a9c 281
vbahl2 13:8b706583610a 282 printf("Bring up the BLE radio\n");
andresag 11:16f67d5752e1 283 BLE &ble = BLE::Instance();
andresag 11:16f67d5752e1 284 ble.init(bleInitComplete);
rgrover1 0:332983584a9c 285
budoguyiii 12:dbbf0ddc9b12 286 UARTService uartService(ble);
budoguyiii 12:dbbf0ddc9b12 287 uartServicePtr = &uartService;
budoguyiii 12:dbbf0ddc9b12 288 //uartService.retargetStdout();
budoguyiii 12:dbbf0ddc9b12 289
rgrover1 0:332983584a9c 290 while (true) {
rgrover1 0:332983584a9c 291 ble.waitForEvent();
rgrover1 0:332983584a9c 292 }
rgrover1 0:332983584a9c 293 }