Bitwise (OR, AND, XOR) Operations and Plaintext encryption in RioT

I have to implement a group key encryption scheme involving plain text encryption, bitwise XOR operations. I have run the following code. Now my question is how can I encrypt some string consisting of node ids, timestamp, and a random nonce. I have encrypted the above in other libraries but not sure how to do them for embedded and RIOT. Also, I have to implement the bitwise XOR operation. Can someone help me solve this?

#include <stdio.h>
#include <string.h>
#include <crypto/aes.h>

static uint8_t TEST_1_KEY[] = {
    0x23, 0xA0, 0x18, 0x53, 0xFA, 0xB3, 0x89, 0x23,
    0x65, 0x89, 0x2A, 0xBC, 0x43, 0x99, 0xCC, 0x00
};

static uint8_t TEST_1_INP[] = {
    0x11, 0x53, 0x81, 0xE2, 0x5F, 0x33, 0xE7, 0x41,
    0xBB, 0x12, 0x67, 0x38, 0xE9, 0x12, 0x54, 0x23
};


int main(void)
{
    uint8_t data_enc[16]={0};
    uint8_t data_dec[16]={0};

    cipher_context_t ctx_riot;

    aes_init(&ctx_riot, TEST_1_KEY, sizeof(TEST_1_KEY));
  		
    aes_encrypt(&ctx_riot, TEST_1_INP, data_enc);

    aes_decrypt(&ctx_riot, data_enc, data_dec);	 		 
   	
    if (memcmp(TEST_1_INP, data_dec, sizeof(data_dec)) != 0) {
        printf("ERROR\n"); }
    else
    {
	printf("SUCCESS \n");  
    }
    return 0;
}

I have also looked at the following code

#include "crypto/ciphers.h"
cipher_t cipher;
uint8_t key[AES_KEY_SIZE] = {0},
plain_text[AES_BLOCK_SIZE] = {0},
cipher_text[AES_BLOCK_SIZE] = {0};
    if (cipher_init(&cipher, CIPHER_AES_128, key, AES_KEY_SIZE) < 0)
        printf("Cipher init failed!\n");
    if (cipher_encrypt(&cipher, plain_text, cipher_text) < 0)
        printf("Cipher encryption failed!\n");
    else
        od_hex_dump(cipher_text, AES_BLOCK_SIZE, 0);

Can you post the make file for the code? I am unable to build the code that you provided…

I want to encrypt some custom string like str = “NID001SRVID001281512346”;
Here is the make file

name of your application

APPLICATION = aes-Test

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../../..

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# includes the crypto_aes module:
USEMODULE += crypto_aes
USEMODULE += cipher_modes

include $(RIOTBASE)/Makefile.include

Try this code…

#include <stdio.h>

#include "shell.h" // comes with riot
#include <string.h>
#include <crypto/aes.h>

static uint8_t TEST_1_KEY[] = {
    0x23, 0xA0, 0x18, 0x53, 0xFA, 0xB3, 0x89, 0x23,
    0x65, 0x89, 0x2A, 0xBC, 0x43, 0x99, 0xCC, 0x00
};
static uint8_t data16[]="This-is-data----"; // 16 bytes data
int main(void)
{
    uint8_t data_enc[16]={0};
    uint8_t data_dec[16]={0};
 
    cipher_context_t ctx_riot;

    aes_init(&ctx_riot, TEST_1_KEY, sizeof(TEST_1_KEY));
  		
    aes_encrypt(&ctx_riot, data16, data_enc);
    // print encrypted data as hex
    printf("data_enc->"); 
   	for(int i=0;i<16;i++)
    {
     printf("%02x",data_enc[i]);
    }
    printf("\r\n");

    aes_decrypt(&ctx_riot, data_enc, data_dec);
    printf("data_dec->");
    for(int i=0;i<16;i++)
    {
     printf("%c",data_dec[i]);
    }
    printf("\r\n");
    if (memcmp(data16, data_dec, sizeof(data_dec)) != 0) {
        printf("ERROR\n"); }
    else
    {
	printf("SUCCESS \n");  
    }
    return 0;
}

It encrypt the data “This-is-data----” using the 16bytes(128 bit) hex key, The data that is to be encrypted should be 16bytes in length and the output will be also 16bytes, requried by ASE. This code print the encrypted array so you can use other tools to decrypt it and to varify that the encryption is done correctly(just for checking), you can use this online tool that I used to varify the output of this code.

Link To online ASE decrptor

Use the settings as shown in the picture or it will not work. The output of the code I provided. 2

1 Like

I am not an expert in this field(field of cryptography) so don’t take my opinion seriously.

Thank you so much. I will try this one.