I'm using this library: https://tls.mbed.org/download with an Espressif ESP32. The goal is to encrypt some data using AES-CTR then decrypt the cypher-text back to the original plain-text. I am getting incorrect result after I've decrypted.
Since we're using CTR mode, we don't need to have a separate "decrypt" function; We can just call the encrypt function once to encrypt then call the same function one more time and it should decrypt. At least, that's what most sources say and that was the case with this other implementation: https://github.com/kokke/tiny-AES-c
I tried using the same nonce, same stream-block, different nonces, different stream-blocks, set the key before each of the function calls, etc.
mbedtls_aes_context aes;
unsigned char key[16];
size_t plainText_len = 64;
unsigned int nc_off = 0;
unsigned char nonce_counter[16] = {0};
unsigned char stream_block[16] = {0};
unsigned char plainText[64] = {0x48, 0x45, 0x4c, 0x4c, 0x4f};
unsigned char encryptText[64];
unsigned char decryptText[64];
memcpy(key, key_128, 16); //key_128 comes from a different file
//Print Key
printf("aes Key: \n");
for(int i = 0; i < 16; i++){
printf("%x",key[i]);
}
printf("\n");
//Print plain-text
printf("aes plainText: \n");
for(int i = 0; i < 5; i++){
printf("%x",plainText[i]);
}
printf("\n");
esp_aes_init(&aes); //context is initialized
esp_aes_setkey(&aes, key, 128); //key is associated to context
esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, plainText, encryptText); //encrypt
//Print encrypt-text
printf("aes encryptText: \n");
for(int i = 0; i < 5; i++){
printf("%x",encryptText[i]);
}
printf("\n");
esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, encryptText, decryptText); //decrypt
//Print decrypt-text
printf("aes decrypt: \n");
for(int i = 0; i < 5; i++){
printf("%x",decryptText[i]);
}
printf("\n");
After the encrypt function is called a second time the resulting decryptText should be the same as the original plain-text, however, at the moment plainText =/= decryptText.
This is what my monitor is printing:
aes key: 7d3043fb95355e6ccd850ad8debc279
aes plainText: 48454c4c4f
aes encryptText: 852b97da59
aes decryptText: 814268329f
As you can see, I'm missing something that'll properly decrypt the encryptText!
Since we're using CTR mode, we don't need to have a separate "decrypt" function; We can just call the encrypt function...
I think you are conflating topics. CTR mode does operate the cipher in the forward direction for both encryption and decryption. However, the same sort of symmetry may not exist in the higher level mbedTLS objects.
esp_aes_init(&aes); //context is initialized
esp_aes_setkey(&aes, key, 128); //key is associated to context
esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, plainText, encryptText); //encrypt
// ...
esp_aes_crypt_ctr(&aes, plainText_len, &nc_off, nonce_counter, stream_block, encryptText, decryptText); //decrypt
// ...
esp_aes_crypt_ctr is a define for mbedtls_aes_crypt_ctr. mbedtls_aes_crypt_ctr docs say the function updates both nonce_counter and stream_block. I believe you need to restart both when you want to perform decryption.
Here is one of the mentions of stream_block in the docs. You are not contiuing encryption. You need to restart the parameter.
stream_block - The saved stream block for resuming. This is overwritten by the function. It must be a readable-writeable buffer of
16 Bytes.
But in the bigger picture as was commented by #James... You should probably use mbedTLS the way it was designed to be used rather than trying to take shortcuts.
Related
I have 2 programs communicating with each other through a fifo, one's the writer the other's the reader.
The writer sends a pointer to a struct containing information.
The reader should receive the pointer and be able to see the information inside the struct.
Header file:
typedef struct req{
int _code;
char _client_pipe[PIPENAME];
char _box_name[BOXNAME];
} request;
/*writes to pipe tx a pointer with information*/
void send_request(int tx, request *r1) {
ssize_t ret = write(tx, &r1, sizeof(r1));
if (ret < 0) {
fprintf(stdout, "ERROR: %s\n", ERROR_WRITING_PIPE);
exit(EXIT_FAILURE);
}
}
/*Returns a pointer to a struct containing the request*/
request *serialize(int code, char* client_pipe, char* box_name){
request *r1 = (request*) malloc(sizeof(request));
r1->_code = code;
strcpy(r1->_client_pipe, client_pipe);
strcpy(r1->_box_name, box_name);
return r1;
}
Program writer:
int main(int argc, char **argv){
(void *) argc; // in my program i used argc, but for this problem it's not important hence why the //typecast to void
char register_pipe[PIPENAME];
char personal_pipe[PIPENAME];
char box_name[BOXNAME];
strcpy(register_pipe, argv[1]);
strcpy(personal_pipe, argv[2]);
strcpy(box_name, argv[3]);
int reg_pipe = open(register_pipe, O_WRONLY);
if (reg_pipe == -1) {
fprintf(stdout, "ERROR: %s\n", UNEXISTENT_PIPE);
return -1;
}
send_request(reg_pipe, serialize(1, personal_pipe, box_name));
}
Program reader:
char register_pipe[PIPENAME];
strcpy(register_pipe, argv[1]);
if(mkfifo(register_pipe, 0644) < 0)
exit(EXIT_FAILURE);
if ((reg_pipe = open(register_pipe, O_RDONLY)) < 0){
exit(EXIT_FAILURE);
}
if ((reg_pipe = open(register_pipe, O_RDONLY)) < 0){
exit(EXIT_FAILURE);
}
request* buffer = (request*) malloc(sizeof(request)); //this might be the issue but not sure
ssize_t broker_read= read(reg_pipe, buffer, 256); //is not reading correctly
printf("%d, %s, %s\n", buffer->_code, buffer->_client_pipe, buffer->_box_name);
So if i start program reader and set register pipe as "reg", this will create the register pipe and wait for someone to join it.
Then if i start the program writer like ./writer reg personal box
this will open the reg pipe correctly, create a struct of type request and then sent it to the reader.
The reader should receive a pointer to a struct req set like:
_code = 1;
_client_pipe[PIPENAME] = "personal";
_box_name[BOXNAME] = "box";
The reader is in fact receiving but for some reason it's not receiving correctly.
If i try to print like in the last line, it will output some random numbers and letters.
How can i fix this?
You would need to have that structure exist inside a shared memory region that you have arranged to be mapped into both processes at the same address.
Without some such arrangement, each process has a private address space, so an address known to process A is meaningless to process B.
How to make such an arrangement is very much dependent upon you operating system, and perhaps even variant of said operating system.
You will likely find it easier to just copy the structure, as opposed to its address, via the fifo.
I am trying to implement a AEAD sample code for encryption Using GCM encryption. But I always get invalid argument error while setting the key
static int init_aead(void)
{
printk("Starting encryption\n");
struct crypto_aead *tfm = NULL;
struct aead_request *req;
struct tcrypt_result tresult;
struct scatterlist plaintext[1] ;
struct scatterlist ciphertext[1];
struct scatterlist gmactext[1];
unsigned char *plaindata = NULL;
unsigned char *cipherdata = NULL;
unsigned char *gmacdata = NULL;
const u8 *key = kmalloc(16, GFP_KERNEL);
char *algo = "rfc4106(gcm(aes))";
unsigned char *ivp = NULL;
int ret, i, d;
unsigned int iv_len;
unsigned int keylen = 16;
/* Allocating a cipher handle for AEAD */
tfm = crypto_alloc_aead(algo, 0, 0);
init_completion(&tresult.completion);
if(IS_ERR(tfm)) {
pr_err("alg: aead: Failed to load transform for %s: %ld\n", algo,
PTR_ERR(tfm));
return PTR_ERR(tfm);
}
/* Allocating request data structure to be used with AEAD data structure */
req = aead_request_alloc(tfm, GFP_KERNEL);
if(IS_ERR(req)) {
pr_err("Couldn't allocate request handle for %s:\n", algo);
return PTR_ERR(req);
}
/* Allocting a callback function to be used , when the request completes */
aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, aead_work_done,&tresult);
crypto_aead_clear_flags(tfm, ~0);
/* Set key */
get_random_bytes((void*)key, keylen);
if((ret = crypto_aead_setkey(tfm, key, 16) != 0)) {
pr_err("Return value for setkey is %d\n", ret);
pr_info("key could not be set\n");
ret = -EAGAIN;
return ret;
}
/* Set authentication tag length */
if(crypto_aead_setauthsize(tfm, 16)) {
pr_info("Tag size could not be authenticated\n");
ret = -EAGAIN;
return ret;
}
/* Set IV size */
iv_len = crypto_aead_ivsize(tfm);
if (!(iv_len)){
pr_info("IV size could not be authenticated\n");
ret = -EAGAIN;
return ret;
}
plaindata = kmalloc(16, GFP_KERNEL);
cipherdata = kmalloc(16, GFP_KERNEL);
gmacdata = kmalloc(16, GFP_KERNEL);
ivp = kmalloc(iv_len, GFP_KERNEL);
if(!plaindata || !cipherdata || !gmacdata || !ivp) {
printk("Memory not availaible\n");
ret = -ENOMEM;
return ret;
}
for (i = 0, d = 0; i < 16; i++, d++)
plaindata[i] = d;
memset(cipherdata, 0, 16);
memset(gmacdata, 0, 16);
for (i = 0,d=0xa8; i < 16; i++, d++)
ivp[i] = d;
sg_init_one(&plaintext[0], plaindata, 16);
sg_init_one(&ciphertext[0], cipherdata, 16);
sg_init_one(&gmactext[0], gmacdata, 128);
aead_request_set_crypt(req, plaintext, ciphertext, 16, ivp);
aead_request_set_assoc(req, gmactext, 16);
ret = crypto_aead_encrypt(req);
if (ret)
printk("cipher call returns %d \n", ret);
else
printk("Failure \n");
return 0;
}
module_init(init_aead);
module_exit(exit_aead);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("My code for aead encryption test");
}
On inserting the module I get following output
Starting encryption
Return value for setkey is -22
key could not be set
According to AEAD specification aead uses aes-128 for encryption hence the block size should be 128 bit .
But my system shows only 1 Byte block size support for AEAD
name : rfc4106(gcm(aes))
driver : rfc4106-gcm-aesni
module : aesni_intel
priority : 400
refcnt : 1
selftest : passed
type : nivaead
async : yes
blocksize : 1
ivsize : 8
maxauthsize : 16
geniv : seqiv
Does the invalid argument error is thrown becuase of the block size. If so , what shall I do to make it work ?
The block size of AES is indeed always 128 bit. The block size of GCM is a different matter though. GCM (Galois-Counter Mode) is - as the name suggests - build on top of the CTR (Counter) mode of operation, sometimes also called the SIC (Segmented Integer Counter) mode of operation. This turns AES into a stream cipher. Stream ciphers - by definition - have a block size of one byte (or, more precisely, one bit, but bit level operations are usually not supported by API's).
Block size however has little to do with the key size displayed in the call, and the argument does seem to require bytes instead of bits (in which key lengths are usually defined).
The size of the IV should be 12 bytes (the default). Otherwise additional calculations may be needed by the GCM implementation (if those exist at all).
For Aes GCM RFC 4106 the key must be 20 bytes. I don't known yet why. I've looked into ipsec source code to see how the encryption is made there.
I am implementing a simple caeser cipher on WrapFS to store encrypted data and decrypt while reading. For that purpose I made minor changes to wrapfs_read() and wrapfs_write() functions provided in the source code to encrypt and decypt the data. My decryption is working fine, but due to my encryption code I am getting an error on write as follows bash: echo: write error: bad address. Any help regarding handling it would be really appreciated.
void caeser_encrypt(char __user *encrypted, size_t count)
{
int i;
for(i=0;i<(unsigned int)count;i++)
encrypted[i]=encrypted[i]+3;
printk(KERN_INFO "%s",encrypted);
return;
}
static ssize_t wrapfs_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
int err = 0;
struct file *lower_file;
struct dentry *dentry = file->f_path.dentry;
char *encrypted = NULL;
lower_file = wrapfs_lower_file(file);
/*Added by me*/
encrypted=kmalloc(sizeof(buf),GFP_USER);
memcpy(encrypted,buf,count);
printk(KERN_INFO "%d %d",(int)sizeof(buf), (int)count);
caeser_encrypt(encrypted,count);
err = vfs_write(lower_file, encrypted, count, ppos);
/*Added by me*/
//err = vfs_write(lower_file, buf, count, ppos);
/* update our inode times+sizes upon a successful lower write */
if (err >= 0) {
fsstack_copy_inode_size(dentry->d_inode,
lower_file->f_path.dentry->d_inode);
fsstack_copy_attr_times(dentry->d_inode,
lower_file->f_path.dentry->d_inode);
}
/*Added by me*/
kfree(encrypted);
return err;
}
I would say that "const char __user *buf" is user space address and vfs_write expects user space address and you are passing kernel space address to it. Also you cannot directly copy user space address to kernel space address. You can try using copy_from_user and copy_to_user functions. I hope you are aware that caeser cipher is insecure.
I am trying to get interact with the internal memory of the PIC24F16KA101 MCU. After reading the data-sheet and the discussion on this site (which offer a pretty helpful sample code)used in the project
Now if I put the code below the program work just fine, since I am able to read successfully the same value that I wrote previously. However if after writing I unplug the MCU and perform only a read of the EEPROOM it is not going to return the value written. What could be the problem here?. Why can I write and then read successfully but can not read after a power off?.
Thanks in advance to all for the help
Damian
int __attribute__ ((space(eedata))) ee_addr;
void EepSetup();
void EepErase(void);
int EepWrite(int index, int data);
int EepRead(int index);
int main(int argc, char** argv)
{
unsigned int data = 123;
unsigned int data_read = 0;
Init_UART1();
UART1WriteString("START EEPROM PROGRAM \n");
EepSetup();
UART1WriteString("WRITING DATA TO MEMORY \n");
EepWrite(1,data);
//if the code works, just comment the upper section and read eeprom after
//disconecting the power source
UART1WriteString("READING DATA FROM MEMORY \n");
data_read = EepRead(1);
UART1WriteString("Value Read: ");
UART1WriteInt(data_read,16);
UART1WriteString("\n");
__delay_ms(1000);
return (EXIT_SUCCESS);
}
void EepSetup(){
//Disable Interrupts For 5 instructions
asm volatile("disi #5");
//Issue Unlock Sequence
asm volatile("mov #0x55, W0 \n"
"mov W0, NVMKEY \n"
"mov #0xAA, W1 \n"
"mov W1, NVMKEY \n");
}
void EepErase(void) {
NVMCON = 0x4050; // Set up NVMCON to bulk erase the data EEPROM
asm volatile ("disi #5"); // Disable Interrupts For 5 Instructions
__builtin_write_NVM(); // Issue Unlock Sequence and Start Erase Cycle
while(_WR)
;
}
int EepRead(int index){
unsigned int offset;
TBLPAG = __builtin_tblpage(&ee_addr); // Initialize EE Data page pointer
offset = __builtin_tbloffset(&ee_addr); // Initizlize lower word of address
offset += index * sizeof(int);
return __builtin_tblrdl(offset); // read EEPROM data
}
int EepWrite(int index, int data){
unsigned int offset;
NVMCON = 0x4004; // Set up NVMCON to erase one word of data EEPROM
TBLPAG = __builtin_tblpage(&ee_addr); // Initialize EE Data page pointer
offset = __builtin_tbloffset(&ee_addr); // Initizlize lower word of address
offset += index * sizeof(int);
__builtin_tblwtl(offset, data);
asm volatile ("disi #5"); // Disable Interrupts For 5 Instructions
__builtin_write_NVM(); // Issue Unlock Sequence and Start Erase Cycle
while(_WR);
return (EXIT_SUCCESS);
}
I just figured out what the problem was, it happens that if you use the PICkit 3 with MPLABX you have to check an option in the programmer to preserve the EEPROM memory,so the code was functional, you just need to check the option of "Preserve EEPROM Memory" in the programmer settings. I hope this help others.
Cheers, Damian
I have been struggling to encrypt/decrypt some data using crypto_secretbox_easy() in libsodium. I can't seem to find any good documentation on the usage.
I want to get a password from the user, use that to somehow make a key, then encrypt/decrypt the data using that.
The problem with the toy code that I have posted below is that the crypto_secretbox_open_easy() returns -1 from within verify_16.c. Does anyone have any idea where I could find source showing how to use this interface or what could be going wrong? Thanks!
unsigned char * cipher;
unsigned char * decoded;
unsigned char * message;
unsigned long long message_len = 32;
size_t noncelen = sizeof(char) * crypto_secretbox_noncebytes();
size_t keylen = sizeof(char) * crypto_secretbox_keybytes();
unsigned char * nonce = calloc(noncelen, noncelen);
unsigned char * key = calloc(keylen, keylen);
message = calloc(32*sizeof(char), sizeof(char) * 32);
cipher = calloc(32*sizeof(char), sizeof(char) * 32);
decoded = calloc(32*sizeof(char), sizeof(char) * 32);
crypto_secretbox_easy((unsigned char *)cipher, (const unsigned char *)message,
message_len, nonce, key);
crypto_secretbox_open_easy((unsigned char *)decoded, (const unsigned char *) cipher,
message_len, nonce, key);
The test/secretbox_easy2.c file (in the sodium source code) shows how to use it:
randombytes_buf(nonce, sizeof nonce);
crypto_secretbox_easy(c, m, mlen, nonce, k);
crypto_secretbox_open_easy(decoded, c, mlen + crypto_secretbox_MACBYTES,
nonce, k);
In order to derive a key from a password, sodium provides crypto_pwhash_scryptsalsa208sha256.
the size of cipher should be 16 bytes larger then message for the MAC bytes, so alloc 16 more bytes and on open_easy just add + 16 to the message_len.
Also take a look, your call to calloc actually allocate a lot memory than needed, because calloc do the multiplying inside the method.