This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Timestamp encrypted with AES in nRF51822 BLUETOOTH SMART BEACON KIT

Hello,

I have been trying to implement AES in order to encrypt a time stamtamp to be sent in the advert of the iBeacon ( 128 bits, using Minor and Major bits, (16+16)*4=128 bits to use the 128bits AES encryption using 4 complete adverts). Here is the snippet of the main where I'm testing this features:

//main snippet. I only add the code related to the AES implementation 
        //key I'm going to use 
        uint8_t key[16]={0x0f,0x15,0x71,0xc9,0x47,0xd9,0xe8,0xf3}; 
        //date or timestamp I want to encrypt and send 
    uint8_t text[16]={0x00,0x00,0x00,0x00,0x24,0x07,0x20,0x14}; 
nrf_ecb_hal_data_t aesstruct; 
uint8_t aes_data[16]; 
//Initializing arrays
        memset (aes_data, 0, sizeof(aes_data)); 
        memset (&aesstruct, 0, sizeof(aesstruct)); 
        //for I use to fill the aesstruct.key with the key I want to use 
        for (int i = 0; i < 16; i++) {   
                aesstruct.key [i] = key[i]; } 
memset (aesstruct.cleartext, 0xAA, sizeof(aesstruct.cleartext));  
//Creating ciphertext 
        sd_ecb_block_encrypt(&aesstruct);  
        //XOR ciphertext with text in order to get the aes_data encrypted:
                for (int i = 0; i < 16; i++)
                {   
                    aes_data[i] =  text[i] ^ aesstruct.ciphertext[i];
                } 
        //Now in the infinite for I want to send that encrypted data 
        // Start execution.
                advertising_start();
                // Enter main loop.
                for (;;)
                { if(cont<2) 
        { flash_db_t tmp; tmp.data.major_value[0] = clbeacon_info[18] = 0x00; 
        tmp.data.major_value[1] = clbeacon_info[19] = 0x01; 
        tmp.data.minor_value[0] = clbeacon_info[20] = 0x00; 
        if(cont==0) 
        { 
        tmp.data.minor_value[1] = clbeacon_info[21] = retanio; 
        } 
        else 
        { 
        //It's always 0. Why?????? How can I solve this. There seems to be no encryption      tmp.data.minor_value[1] = clbeacon_info[21] = aes_data[1]; 
        } 
        err_code = pstorage_clear(&pstorage_block_id, sizeof(flash_db_t));
                    APP_ERROR_CHECK(err_code);
                    
                    err_code = pstorage_store(&pstorage_block_id, (uint8_t *)&tmp, sizeof(flash_db_t), 0);
                    APP_ERROR_CHECK(err_code);
                    
                    err_code = pstorage_access_wait();
                    APP_ERROR_CHECK(err_code); advertising_init(beacon_mode_normal);
                    s_leds_bit_mask |= BEACON_MODE_LED_MSK; 
        //Solo esto estaba dentro del for(;;)
                    app_sched_execute();
                    power_manage();  cont++; 
        } 
        else 
        cont=0; 
            
            } }

One of the Minor's values appear well (I alternate between to values to check the rotation of different values), but the encrypted one doesn't. Any advice will be very appreciated.

Regards,

Iván

Related