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

Function to encrypt all data to be sent

Hi,

Based on other discussions of encryption etc , I created a function to encrypt all the data, so that whenever I call the function It encrypts.

static   nrf_ecb_hal_data_t aes_struct;

static  uint8_t aes_data[16];

static   uint8_t aes_data_decrypted[16];

//Initializing arrays

memset (&aes_struct, 0, sizeof(aes_struct));

memset (aes_data, 0, sizeof(aes_data));

memset (aes_data_decrypted, 0, sizeof(aes_data_decrypted));

uint8_t* encrypt(uint8_t* p_data)
 {



        //Initializing key

        for (int i = 0; i < 16; i++)

        {
            aes_struct.key [i] = i;
        }

        //Initializing nouncence

        memset (aes_struct.cleartext, 0xaa, sizeof(aes_struct.cleartext)); //todo: use more random data and add counter

        //Creating chipertext

        sd_ecb_block_encrypt(&aes_struct); 

        //Encrypt -> XOR chipertext with p_data:



        for (int i = 0; i < length; i++)

        {  
            aes_data[i] = p_data [i] ^ aes_struct.ciphertext[i];
        }


        //decrypt -> XOR chipertext with Encrypted data:

        return (aes_data);

 }

 uint8_t* decrypt(uint8_t* p_data)
 {
    for (int i = 0; i < length; i++)

        {  
            aes_data[i] = p_data [i] ^ aes_struct.ciphertext[i];
        }


        //decrypt -> XOR chipertext with Encrypted data:

        return (aes_data);
 }

But I am not able to call the function, there's always an error

uint8_t vib_cnt_buf[5];
Convert_to_ascii(right,(&vib_cnt_buf[0]),8);
&vib_cnt_buf = encrypt(&vib_cnt_buf); //Encrypting this data

Can you please help

  • Hi

    Are you able to provide some more information on the error you are seeing?

    Is it the Convert_to_ascii(..) function that returns the error?

    Are you able to share the implementation of this function with me?

    Best regards
    Torbjørn

  • Hi again,

    Error : lvalue required as left operand of assignment

    this error is at this lilne : &vib_cnt_buf = encrypt(&vib_cnt_buf);

    I can't seem to get rid of it at all

    the convert_to_ascii() function works well and converts the same variable to ascii values 

  • Hi 

    Sorry, I didn't notice that line earlier, it is clearly an illegal operation. 

    You are taking a pointer to a pointer to the vib_cnt_buf array and trying to assign a pointer to it, this will not work. 

    Instead I would suggest doing something like this:

    uint8_t *encrypted_data_ptr = encrypt(&vib_cnt_buf);
    memcpy(vib_cnt_buf, encrypted_data_ptr, length);

    Best regards
    Torbjørn

  • Hello,

    There is another error in the above mentioned code at line 9 : memset (&aes_struct, 0, sizeof(aes_struct));

    Error : expected declaration specifiers or '...' before '&' token.

    Thanks for the support

  • Hello,

    I am now using ECB mode of AES, there were lot of errors in previous one.

    Now in the below code for ECB mode I have just one error : which I can't figure out the meaning,

    static void nus_data_handler(ble_nus_evt_t * p_evt)
    {   
        //some codes over here
    
        Convert_to_ascii(Battery_voltage,(&vib_cnt_buf[0]),4);
        uint32_t *encrypted_data_ptr = ctr_encrypt(&vib_cnt_buf);
        memcpy(vib_cnt_buf, encrypted_data_ptr, length);
        err_code = ble_nus_data_send(&m_nus, (&vib_cnt_buf), &thi, m_conn_handle);
    }
    /* 
    .
    .
    .
    .   
    .
    .
    .
     */
    static uint32_t crypt(uint8_t * buf)
    {
        uint8_t  i;
        uint32_t err_code;
    
        if (!m_initialized)
        {
    	    return NRF_ERROR_INVALID_STATE;
        }
    
        err_code = sd_ecb_block_encrypt(&m_ecb_data);
        if (NRF_SUCCESS != err_code)
        {
    	    return err_code;
        }
    
        for (i=0; i < ECB_KEY_LEN; i++)
        {
    	    buf[i] ^= m_ecb_data.ciphertext[i];
        }
    
        // Increment the counter.
        (*((uint32_t*) m_ecb_data.cleartext))++;
    
        return NRF_SUCCESS;
    }
    
    
    uint32_t ctr_encrypt(uint8_t * p_clear_text)
    {
        return crypt(p_clear_text);
    }
    
    
    uint32_t ctr_decrypt(uint8_t * p_cipher_text)
    {
        return crypt(p_cipher_text);
    }
    
    

    The following line has error (line 47 in above snippet) : 

    uint32_t ctr_encrypt(uint8_t * p_clear_text)

    {

    }

    Error : conflicting types for 'ctr_encrypt'

Related