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

nrf_ecb_crypt() transmits waste chars

Hi,

I'm trying using AES ECB encryption of peripheral drivers on development board with nRF52832 and PCA10040.

I inserted the following code before main loop of ble_app_uart sample in nRF5 SDK version 12.2.

#define SRC ((uint8_t *)"sample 16B text.")
#define AES_KEY ((uint8_t *)"abcdefgh12345678")

int main (void) {
    ...
    uart_init();
    ret = nrf_ecb_init();

    ...
    uint8_t crypt[16];
    nrf_ecb_set_key(AES_KEY);
    ret = nrf_ecb_crypt(crypt, SRC);

    for(;;)
    {
        ...

In case that there isn't the last line of nrf_ecb_crypt() before for(;;), it works well. But add it and only waste characters comes on to terminal emulator.

Why it behaves so?

Thanks

Parents
  • The text you have in your string is 16 bytes long. This means the null terminator is the 17th byte. Thus this 17th byte is not in the encrypted string. If you decrypt it, you need to be aware the result is not null-terminated. So, you must only print 16 characters. If you would like to just use a printf, you should have the string 15 characters, such that the 16th byte is the null terminator.

Reply
  • The text you have in your string is 16 bytes long. This means the null terminator is the 17th byte. Thus this 17th byte is not in the encrypted string. If you decrypt it, you need to be aware the result is not null-terminated. So, you must only print 16 characters. If you would like to just use a printf, you should have the string 15 characters, such that the 16th byte is the null terminator.

Children
Related