UpdatedDecryp
Dependencies: BahlDecrypModified CyaSSL mbed nRF51822
Fork of Decryptulator by
main.cpp@12:dbbf0ddc9b12, 2016-05-28 (annotated)
- Committer:
- budoguyiii
- Date:
- Sat May 28 22:07:19 2016 +0000
- Revision:
- 12:dbbf0ddc9b12
- Parent:
- 11:16f67d5752e1
- Child:
- 13:8b706583610a
CryptoCoP Decryptulator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:332983584a9c | 1 | |
rgrover1 | 0:332983584a9c | 2 | #include "mbed.h" |
budoguyiii | 12:dbbf0ddc9b12 | 3 | //#include "toolchain.h" |
rgrover1 | 9:69a2ad0bcdb7 | 4 | #include "ble/BLE.h" |
sunsmile2015 | 7:91324daa3bfa | 5 | #include "TMP_nrf51/TMP_nrf51.h" |
sunsmile2015 | 7:91324daa3bfa | 6 | |
budoguyiii | 12:dbbf0ddc9b12 | 7 | |
budoguyiii | 12:dbbf0ddc9b12 | 8 | #include "UARTService.h" |
budoguyiii | 12:dbbf0ddc9b12 | 9 | |
budoguyiii | 12:dbbf0ddc9b12 | 10 | #include "ctc_aes.h" |
budoguyiii | 12:dbbf0ddc9b12 | 11 | |
budoguyiii | 12:dbbf0ddc9b12 | 12 | #define UART_TX p9 |
budoguyiii | 12:dbbf0ddc9b12 | 13 | #define UART_RX p11 |
budoguyiii | 12:dbbf0ddc9b12 | 14 | |
budoguyiii | 12:dbbf0ddc9b12 | 15 | #define LOG(...) { pc.printf(__VA_ARGS__); } |
budoguyiii | 12:dbbf0ddc9b12 | 16 | |
rgrover1 | 9:69a2ad0bcdb7 | 17 | DigitalOut alivenessLED(LED1, 1); |
andresag | 11:16f67d5752e1 | 18 | Ticker ticker; |
rgrover1 | 0:332983584a9c | 19 | |
budoguyiii | 12:dbbf0ddc9b12 | 20 | Serial pc(UART_TX, UART_RX); |
budoguyiii | 12:dbbf0ddc9b12 | 21 | |
budoguyiii | 12:dbbf0ddc9b12 | 22 | UARTService *uartServicePtr; |
budoguyiii | 12:dbbf0ddc9b12 | 23 | |
budoguyiii | 12:dbbf0ddc9b12 | 24 | //stuff for encryption |
budoguyiii | 12:dbbf0ddc9b12 | 25 | uint8_t payload[31]; |
budoguyiii | 12:dbbf0ddc9b12 | 26 | #define BLOCK_SIZE 16 |
budoguyiii | 12:dbbf0ddc9b12 | 27 | #define KEYLEN 256 //128, 192, 256 |
budoguyiii | 12:dbbf0ddc9b12 | 28 | int j; |
budoguyiii | 12:dbbf0ddc9b12 | 29 | unsigned char nonce_counter[BLOCK_SIZE]; |
budoguyiii | 12:dbbf0ddc9b12 | 30 | unsigned char plain[BLOCK_SIZE]; |
budoguyiii | 12:dbbf0ddc9b12 | 31 | unsigned char cipher[BLOCK_SIZE]; |
budoguyiii | 12:dbbf0ddc9b12 | 32 | unsigned char* counter_bytes = nonce_counter+BLOCK_SIZE/2; |
budoguyiii | 12:dbbf0ddc9b12 | 33 | size_t counter_tx_len = 3; |
budoguyiii | 12:dbbf0ddc9b12 | 34 | unsigned char key[KEYLEN/8]; |
budoguyiii | 12:dbbf0ddc9b12 | 35 | unsigned char iv[BLOCK_SIZE];//not used for ctr mode but required by setKey |
budoguyiii | 12:dbbf0ddc9b12 | 36 | Aes ctx; |
budoguyiii | 12:dbbf0ddc9b12 | 37 | |
budoguyiii | 12:dbbf0ddc9b12 | 38 | |
rgrover1 | 0:332983584a9c | 39 | void periodicCallback(void) |
rgrover1 | 0:332983584a9c | 40 | { |
rgrover1 | 9:69a2ad0bcdb7 | 41 | alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */ |
rgrover1 | 0:332983584a9c | 42 | } |
budoguyiii | 12:dbbf0ddc9b12 | 43 | void initAES(void) |
budoguyiii | 12:dbbf0ddc9b12 | 44 | { |
budoguyiii | 12:dbbf0ddc9b12 | 45 | for(int i=0;i<KEYLEN/8;i++) |
budoguyiii | 12:dbbf0ddc9b12 | 46 | key[i] = i; |
budoguyiii | 12:dbbf0ddc9b12 | 47 | for(int i=0; i<BLOCK_SIZE-3; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 48 | nonce_counter[i]=i<8?i:0; |
budoguyiii | 12:dbbf0ddc9b12 | 49 | for(int i=0; i<BLOCK_SIZE;i++) |
budoguyiii | 12:dbbf0ddc9b12 | 50 | iv[i]=0; |
budoguyiii | 12:dbbf0ddc9b12 | 51 | AesSetKey(&ctx, key, KEYLEN/8, iv, AES_ENCRYPTION); |
budoguyiii | 12:dbbf0ddc9b12 | 52 | } |
budoguyiii | 12:dbbf0ddc9b12 | 53 | |
budoguyiii | 12:dbbf0ddc9b12 | 54 | void decrypt(const Gap::AdvertisementCallbackParams_t *params) |
budoguyiii | 12:dbbf0ddc9b12 | 55 | { |
budoguyiii | 12:dbbf0ddc9b12 | 56 | //puts decrypted data into GLOBAL plain variable. |
budoguyiii | 12:dbbf0ddc9b12 | 57 | |
budoguyiii | 12:dbbf0ddc9b12 | 58 | |
budoguyiii | 12:dbbf0ddc9b12 | 59 | //get coutner |
budoguyiii | 12:dbbf0ddc9b12 | 60 | for(int i=(params->advertisingDataLen)-counter_tx_len; i < params->advertisingDataLen; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 61 | nonce_counter[BLOCK_SIZE-3+(i-((params->advertisingDataLen)-counter_tx_len))] = params->advertisingData[i]; |
budoguyiii | 12:dbbf0ddc9b12 | 62 | |
budoguyiii | 12:dbbf0ddc9b12 | 63 | //print nonce_counter |
budoguyiii | 12:dbbf0ddc9b12 | 64 | LOG("\nNonceCtr: "); |
budoguyiii | 12:dbbf0ddc9b12 | 65 | for(int i=0;i<BLOCK_SIZE;i++) |
budoguyiii | 12:dbbf0ddc9b12 | 66 | LOG("%02x ", nonce_counter[i]); |
budoguyiii | 12:dbbf0ddc9b12 | 67 | |
budoguyiii | 12:dbbf0ddc9b12 | 68 | //get cipher text |
budoguyiii | 12:dbbf0ddc9b12 | 69 | for(int i=0; i < (params->advertisingDataLen) - (counter_tx_len + 2); i++) |
budoguyiii | 12:dbbf0ddc9b12 | 70 | cipher[i] = params->advertisingData[i+2]; |
budoguyiii | 12:dbbf0ddc9b12 | 71 | |
budoguyiii | 12:dbbf0ddc9b12 | 72 | //print cipher |
budoguyiii | 12:dbbf0ddc9b12 | 73 | LOG("\nCiphertxt: "); |
budoguyiii | 12:dbbf0ddc9b12 | 74 | for(int i=0; i < BLOCK_SIZE; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 75 | LOG("%02x ", cipher[i]); |
budoguyiii | 12:dbbf0ddc9b12 | 76 | |
budoguyiii | 12:dbbf0ddc9b12 | 77 | |
budoguyiii | 12:dbbf0ddc9b12 | 78 | //build key stream |
budoguyiii | 12:dbbf0ddc9b12 | 79 | AesEncrypt(&ctx, nonce_counter, plain); |
budoguyiii | 12:dbbf0ddc9b12 | 80 | //print key |
budoguyiii | 12:dbbf0ddc9b12 | 81 | LOG("\nKey: "); |
budoguyiii | 12:dbbf0ddc9b12 | 82 | for(int i=0; i<BLOCK_SIZE; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 83 | LOG("%02x ", plain[i]); |
budoguyiii | 12:dbbf0ddc9b12 | 84 | |
budoguyiii | 12:dbbf0ddc9b12 | 85 | //decrypt into plain (destroying key) |
budoguyiii | 12:dbbf0ddc9b12 | 86 | for(int i=0;i<BLOCK_SIZE;i++) |
budoguyiii | 12:dbbf0ddc9b12 | 87 | plain[i]^=cipher[i]; |
budoguyiii | 12:dbbf0ddc9b12 | 88 | } |
rgrover1 | 0:332983584a9c | 89 | |
rgrover1 | 9:69a2ad0bcdb7 | 90 | /* |
rgrover1 | 9:69a2ad0bcdb7 | 91 | * This function is called every time we scan an advertisement. |
rgrover1 | 9:69a2ad0bcdb7 | 92 | */ |
sunsmile2015 | 6:850f44146c9f | 93 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) |
sunsmile2015 | 6:850f44146c9f | 94 | { |
budoguyiii | 12:dbbf0ddc9b12 | 95 | |
rgrover1 | 9:69a2ad0bcdb7 | 96 | /* Search for the manufacturer specific data with matching application-ID */ |
budoguyiii | 12:dbbf0ddc9b12 | 97 | int addr_length = 6; |
budoguyiii | 12:dbbf0ddc9b12 | 98 | |
budoguyiii | 12:dbbf0ddc9b12 | 99 | //print from addr |
budoguyiii | 12:dbbf0ddc9b12 | 100 | //if(params->peerAddr[addr_length-1] == 0xfc) |
budoguyiii | 12:dbbf0ddc9b12 | 101 | //{ |
budoguyiii | 12:dbbf0ddc9b12 | 102 | LOG("\nFrom: "); |
budoguyiii | 12:dbbf0ddc9b12 | 103 | for(int i=0; i<addr_length; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 104 | LOG("%02x:", params->peerAddr[addr_length-i-1]); |
budoguyiii | 12:dbbf0ddc9b12 | 105 | //print payload |
budoguyiii | 12:dbbf0ddc9b12 | 106 | LOG("\nPayload: "); |
budoguyiii | 12:dbbf0ddc9b12 | 107 | for(int i=0; i < params->advertisingDataLen; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 108 | LOG(" %02x", params->advertisingData[i]); |
budoguyiii | 12:dbbf0ddc9b12 | 109 | |
budoguyiii | 12:dbbf0ddc9b12 | 110 | |
budoguyiii | 12:dbbf0ddc9b12 | 111 | decrypt(params); |
budoguyiii | 12:dbbf0ddc9b12 | 112 | |
budoguyiii | 12:dbbf0ddc9b12 | 113 | //print plaintext |
budoguyiii | 12:dbbf0ddc9b12 | 114 | LOG("\nPlaintext: "); |
budoguyiii | 12:dbbf0ddc9b12 | 115 | for(int i=0; i<BLOCK_SIZE; i++) |
budoguyiii | 12:dbbf0ddc9b12 | 116 | LOG("%02x ", plain[i]); |
budoguyiii | 12:dbbf0ddc9b12 | 117 | |
budoguyiii | 12:dbbf0ddc9b12 | 118 | //print close of round |
budoguyiii | 12:dbbf0ddc9b12 | 119 | LOG("\n\n"); |
budoguyiii | 12:dbbf0ddc9b12 | 120 | //} |
rgrover1 | 0:332983584a9c | 121 | } |
rgrover1 | 0:332983584a9c | 122 | |
andresag | 11:16f67d5752e1 | 123 | /** |
andresag | 11:16f67d5752e1 | 124 | * This function is called when the ble initialization process has failed |
andresag | 11:16f67d5752e1 | 125 | */ |
andresag | 11:16f67d5752e1 | 126 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 11:16f67d5752e1 | 127 | { |
andresag | 11:16f67d5752e1 | 128 | /* Initialization error handling should go here */ |
budoguyiii | 12:dbbf0ddc9b12 | 129 | LOG("Crap, the BLE radio is broken\n"); |
andresag | 11:16f67d5752e1 | 130 | } |
andresag | 11:16f67d5752e1 | 131 | |
andresag | 11:16f67d5752e1 | 132 | /** |
andresag | 11:16f67d5752e1 | 133 | * Callback triggered when the ble initialization process has finished |
andresag | 11:16f67d5752e1 | 134 | */ |
andresag | 11:16f67d5752e1 | 135 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 11:16f67d5752e1 | 136 | { |
andresag | 11:16f67d5752e1 | 137 | BLE& ble = params->ble; |
andresag | 11:16f67d5752e1 | 138 | ble_error_t error = params->error; |
andresag | 11:16f67d5752e1 | 139 | |
andresag | 11:16f67d5752e1 | 140 | if (error != BLE_ERROR_NONE) { |
andresag | 11:16f67d5752e1 | 141 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 11:16f67d5752e1 | 142 | onBleInitError(ble, error); |
andresag | 11:16f67d5752e1 | 143 | return; |
andresag | 11:16f67d5752e1 | 144 | } |
andresag | 11:16f67d5752e1 | 145 | |
andresag | 11:16f67d5752e1 | 146 | /* Ensure that it is the default instance of BLE */ |
andresag | 11:16f67d5752e1 | 147 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 11:16f67d5752e1 | 148 | return; |
andresag | 11:16f67d5752e1 | 149 | } |
andresag | 11:16f67d5752e1 | 150 | |
andresag | 11:16f67d5752e1 | 151 | /* Setup and start scanning */ |
budoguyiii | 12:dbbf0ddc9b12 | 152 | ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */); |
andresag | 11:16f67d5752e1 | 153 | ble.gap().startScan(advertisementCallback); |
andresag | 11:16f67d5752e1 | 154 | } |
andresag | 11:16f67d5752e1 | 155 | |
rgrover1 | 0:332983584a9c | 156 | int main(void) |
rgrover1 | 0:332983584a9c | 157 | { |
budoguyiii | 12:dbbf0ddc9b12 | 158 | //use 115200 for term 4M for energy |
budoguyiii | 12:dbbf0ddc9b12 | 159 | pc.baud(115200); |
budoguyiii | 12:dbbf0ddc9b12 | 160 | |
budoguyiii | 12:dbbf0ddc9b12 | 161 | LOG("---- DECRYPTULATOR ACTIVIZE ----\n"); |
budoguyiii | 12:dbbf0ddc9b12 | 162 | initAES(); |
budoguyiii | 12:dbbf0ddc9b12 | 163 | |
budoguyiii | 12:dbbf0ddc9b12 | 164 | ticker.attach(periodicCallback, 1); /* flash the LED because reasons */ |
rgrover1 | 0:332983584a9c | 165 | |
budoguyiii | 12:dbbf0ddc9b12 | 166 | LOG("Bring up the BLE radio\n"); |
andresag | 11:16f67d5752e1 | 167 | BLE &ble = BLE::Instance(); |
andresag | 11:16f67d5752e1 | 168 | ble.init(bleInitComplete); |
rgrover1 | 0:332983584a9c | 169 | |
budoguyiii | 12:dbbf0ddc9b12 | 170 | UARTService uartService(ble); |
budoguyiii | 12:dbbf0ddc9b12 | 171 | uartServicePtr = &uartService; |
budoguyiii | 12:dbbf0ddc9b12 | 172 | //uartService.retargetStdout(); |
budoguyiii | 12:dbbf0ddc9b12 | 173 | |
rgrover1 | 0:332983584a9c | 174 | while (true) { |
rgrover1 | 0:332983584a9c | 175 | ble.waitForEvent(); |
rgrover1 | 0:332983584a9c | 176 | } |
rgrover1 | 0:332983584a9c | 177 | } |