Passing the data
Closed this issue · 26 comments
Hi,
Sorry If Iam annoying you,Iam confused with passing a string to the program.
From the program I understand string is to be converted to hexadecimal 128 bit .Am I right or anything is missing.
This is not an issue with the program but I don't know any other means to communicate with you.I went through the documents you asked me to go through.It told me to represent the string as bit string.
Please help me if you can let me know how to insert the following string as plain text in the program "Hello" .Like what kind of encoder to use etc.
Thank you for your understanding
Hi @22karthik
I am not annoyed with you and I did not close your other two issues to shut you up.
I closed them because I thought they were not bug reports but questions about usage.
You should be able to continue commenting on the issues even though they are closed.
From the program I understand string is to be converted to hexadecimal 128 bit .Am I right or anything is missing.
You're missing something here.
It is correct that ECB and CBC mode works with data in 128 bit / 16-byte chunks.
That means you can only encrypt 16, 32, 48, 64 ... bytes etc.
CTR will work with any input size.
Do you understand the concept of the different modes of operations?
Be prepared that you cannot expect ASCII input to be valid ASCII after being encrypted.
This is the reason for printing out all data as hexadecimals in test.c
.
In other words, after encrypting the data you cannot use printf("%s", ...)
to display it as an ASCII-string.
You don't need any encoding to encrypt the string "hello".
You can encrypt it as-is in CTR-mode or after applying padding in ECB and CBC-mode.
Thank you very much .
Iam working on iot devices.My purpose is to encrypt a data packet ,send it to receiver and the receiver has to decrypt the received packets and show it to the user.
I can see your passing the strings as "encrypt" and "decrypt"which is getting encrypted and decrypted in ctr function.
can it solve my purpose of decrypting the obtained encrypted result and return back the original string.
And the purpose of the variables like in,out,iv inside the function is not that clear to me.
My question might be premature as I am new to this domain.
Thank you
I can see your passing the strings as "encrypt" and "decrypt"which is getting encrypted and decrypted in ctr function.
CTR-mode uses the same function for encryption and decryption.
The strings "encrypt" and "descrypt" are not being encrypted or decrypted.
Those strings are only written to STDOUT.
can it solve my purpose of decrypting the obtained encrypted result and return back the original string.
Of course :) That is basically what the test code is doing.
And the purpose of the variables like in,out,iv inside the function is not that clear to me.
My question might be premature as I am new to this domain.
In function test_xcrypt_ctr()
in test.c
, this is what the variables are used for
uint8_t key[] <-- contains the secret key to encrypt the data with
uint8_t in[] <-- contains the input to be encrypted or decrypted
uint8_t out[] <-- contains the result of input being encrypted / decrypted
uint8_t iv[] <-- contains the Initialization Vector used in this session
Hi,
So to provide input to the ctr function as "hello" I need to convert the string to 16 byte chunk and pass it.
For example "encrypt" gave me the following data chunk --->65 6E 63 72 79 70 74 which is 7 bytes what to do for the remaining bytes if I want to encrypt it.
From your previous reply I understand that ctr doesn't require any padding.
Also you are initializing the uint8_t out[] beforehand and how do you know the result of encryption beforehand.Then you are doing memcpy to compare the result and print success or failure.
So to sum up,How do I pass input to this encryption/decryption function and from the decryption how do I get back the original text before encryption at the receiver?
Thank you :)
Hi @22karthik
So to provide input to the ctr function as "hello" I need to convert the string to 16 byte chunk and pass it.
For example "encrypt" gave me the following data chunk --->65 6E 63 72 79 70 74 which is 7 bytes what to do for the remaining bytes if I want to encrypt it.
Only CBC and ECB mode needs 16 byte chunks.
CTR-mode works with arbitrary lengths and requires no padding.
Also you are initializing the uint8_t out[] beforehand and how do you know the result of encryption beforehand.
The document NIST SP 800-38A contains test vectors which are verified input/output pairs.
I know the expected output because it has been given in that document.
hi @kokke ,
I am really thankful to you ,I am happy the code is working to my requirement.
Thank you :)
Hi @22karthik ,
You're welcome - Happy you got the code working :)
hi,
Iam taking inputs as string converting them to character array then I need to convert this character array to uint8_t it to aes function
But no casting is working like converting character array to uin8_t
I'm not sure I understand your problem from your description. uint8_t is just another name for unsigned char, so you can convert a char to uint8_t by casting.
Hi @kokke
No problem I reframe my question
Iam accepting the data and key from user.I need to pass this inputs to ctr decrypt function to the decrypt function I need to pass this as uint8_t (array of hexadecimal numbers)
Also converting char to uint8_t by casting is giving me a error
I think I need to see some code to be able to help you.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include<stdlib.h>
// Enable ECB, CTR and CBC mode. Note this can be done before including aes.h or at compile-time.
// E.g. with GCC by using the -D flag: gcc -c aes.c -DCBC=0 -DCTR=1 -DECB=1
#define CBC 1
#define CTR 1
#define ECB 1
#include "aes.h"
static uint8_t* decrypt(uint8_t* key,uint8_t* in);
static uint8_t* returnbytesfromstring(void *bytes, int nbytes);
uint8_t returnbytes[20];
int main(void)
{
uint8_t* data;
uint8_t* data1;
char in[20];
void *bytes;
int isizeofin;
int i;
uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
#ifdef AES128
printf("\nTesting AES128\n\n");
#elif defined(AES192)
printf("\nTesting AES192\n\n");
#elif defined(AES256)
printf("\nTesting AES256\n\n");
#else
printf("You need to specify a symbol between AES128, AES192 or AES256. Exiting");
return 0;
#endif
//data=decrypt(key,in);
//i need unint
printf("enter the string to sent in air\n");
fgets(in,20,stdin);
//printf("obtained string is %s\n",in);
//make void* point to the char array
//bytes=in;
isizeofin=sizeof(in);
printf("size of n %d\n",isizeofin);
//returned uint8_t
data1=returnbytesfromstring(in,isizeofin);
//data1=decrypt(key,data1);
//print the obtained data1
for(i=0;i<=5;i++)
printf("0x%.2X ", data1[i]);
printf("\n");
printf("success");
return 0;
}
//key is pwd
static uint8_t* decrypt(uint8_t* key,uint8_t* in)
{
int n=0;
uint8_t iv[16] = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CTR_xcrypt_buffer(&ctx, in, n);
return in;
}
static uint8_t* returnbytesfromstring(void *bytes, int nbytes)
{
//uint8_t returnbytes[nbytes];
int i=0;
//for(i=0;i<nbytes;i++){
printf("the received string is %s",bytes);
sscanf(bytes,"0x%.2X",returnbytes);
//returnbytes=(uint8_t)bytes[i];
//}
return returnbytes;
}
If you see the decrypt function it returns hex array but whenI run the code it prints just zeros
In the decrypt function you set n = 0, setting the input length to zero doesn't seem right.
Hi
Thanks for the reply
But I corrected my mistake but there is some problem in this code,please have a look at it
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include<stdlib.h>
// Enable ECB, CTR and CBC mode. Note this can be done before including aes.h or at compile-time.
// E.g. with GCC by using the -D flag: gcc -c aes.c -DCBC=0 -DCTR=1 -DECB=1
#define CBC 1
#define CTR 1
#define ECB 1
#include "aes.h"
static uint8_t* decrypt(uint8_t* key,uint8_t* in);
static uint8_t* returnbytesfromstring(void *bytes, int nbytes);
uint8_t returnbytes[20];
int main(void)
{
uint8_t* data;
uint8_t* data1;
uint8_t* data2;
char in[20];
void *bytes;
int isizeofin;
int i;
uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c };
#ifdef AES128
printf("\nTesting AES128\n\n");
#elif defined(AES192)
printf("\nTesting AES192\n\n");
#elif defined(AES256)
printf("\nTesting AES256\n\n");
#else
printf("You need to specify a symbol between AES128, AES192 or AES256. Exiting");
return 0;
#endif
//data=decrypt(key,in);
//i need unint
printf("enter the string to sent in air\n");
fgets(in,20,stdin);
//printf("obtained string is %s\n",in);
//make void* point to the char array
//bytes=in;
isizeofin=sizeof(in);
//returned uint8_t
data1=returnbytesfromstring(in,isizeofin);
data2=decrypt(key,data1);
printf("the data from encrypt function is:\n");
//print the obtained data1
for(i=0;i<=5;i++)
printf("0x%.2X ", data2[i]);
printf("\n");
printf("success");
return 0;
}
//key is pwd
static uint8_t* decrypt(uint8_t* key,uint8_t* in)
{
int n=0;
uint8_t iv[16] = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
struct AES_ctx ctx;
n=sizeof(in);
AES_init_ctx_iv(&ctx, key, iv);
AES_CTR_xcrypt_buffer(&ctx, in, n);
return in;
}
static uint8_t* returnbytesfromstring(void *bytes, int nbytes)
{
char *hex;
//uint8_t returnbytes[nbytes];
int i=0;
//for(i=0;i<nbytes;i++){
hex=(char*)bytes;
sscanf(hex,"0x%.2X",returnbytes);
//}
return returnbytes;
}
The sscanf in returnbytefromstring is not working ,there is a problem in converting string to hexadecimal array which has to be passed to decrypt function.
Hi @22karthik - I have edited your answer and formatted the text as code like this:
```C
<code goes here>
```
This formats your code as C and makes it easier to read.
See here for more info on the markdown syntax.
I haven't read all the code through thoroughly, but as far as I can tell there is still an error in the decrypt function and how you are assigning to n: n=sizeof(in)
.
You can't use sizeof()
to determine the size of the input data-buffer - you need to pass the length along as a parameter.
sizeof()
is a compile-time operator and will in this case return the size of the POINTER - not the size of the array-pointed-to.
That means n is always 4 or 8 - because the pointer is always 32 or 64 bits long (4 or 8 bytes).
Hi @kokke
Got your point so Iam passing array size from main function to the decrypt function.But there is error in function returnbytefromstring.
I have attached the modified code
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include<stdlib.h>
// Enable ECB, CTR and CBC mode. Note this can be done before including aes.h or at compile-time.
// E.g. with GCC by using the -D flag: gcc -c aes.c -DCBC=0 -DCTR=1 -DECB=1
#define CBC 1
#define CTR 1
#define ECB 1
#include "aes.h"
static uint8_t* decrypt(uint8_t* key,uint8_t* in,int n);
static uint8_t* returnbytesfromstring(void *bytes, int nbytes);
uint8_t returnbytes[20];
int main(void)
{
uint8_t* data;
uint8_t* data1;
uint8_t* data2;
char in[20];
void *bytes;
int isizeofin;
int i;
uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3d };
#ifdef AES128
printf("\nTesting AES128\n\n");
#elif defined(AES192)
printf("\nTesting AES192\n\n");
#elif defined(AES256)
printf("\nTesting AES256\n\n");
#else
printf("You need to specify a symbol between AES128, AES192 or AES256. Exiting");
return 0;
#endif
//data=decrypt(key,in);
//i need unint
printf("enter the string to sent in air\n");
fgets(in,20,stdin);
//printf("obtained string is %s\n",in);
//make void* point to the char array
//bytes=in;
isizeofin=sizeof(in);
//returned uint8_t
data1=returnbytesfromstring(in,isizeofin);
data2=decrypt(key,data1,isizeofin);
printf("the data from encrypt function is:\n");
//print the obtained data1
for(i=0;i<=isizeofin;i++)
printf("0x%.2X ", data2[i]);
printf("\n");
printf("success");
return 0;
}
//key is pwd
static uint8_t* decrypt(uint8_t* key,uint8_t* in,int m)
{
uint8_t iv[16] = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CTR_xcrypt_buffer(&ctx, in, m);
return in;
}
static uint8_t* returnbytesfromstring(void *bytes, int nbytes)
{
char *hex;
//uint8_t returnbytes[nbytes];
int i=0;
//for(i=0;i<nbytes;i++){
hex=(char*)bytes;
sscanf(hex,"0x%.2X",returnbytes);
for(i=0;i<=nbytes;i++)
printf("0x%.2X ",returnbytes[i]);
printf("\n");
//}
return returnbytes;
}
Another comment:
In main()
the variable isizeofin
is always 20 because of this statement isizeofin=sizeof(in);
- is that your intention?
The size of in
is statically determinable because the array has a static size.
So you are always working on 20 bytes when doing data1=returnbytesfromstring(in,isizeofin);
and data2=decrypt(key,data1,isizeofin);
- no matter how many bytes fgets()
reads.
Maybe you meant to use strlen()
instead of sizeof()
?
Hi @kokke
I found out the mistake ,I was calling the function directly from main,but on calling the function indirectly I get the required output
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include<stdlib.h>
// Enable ECB, CTR and CBC mode. Note this can be done before including aes.h or at compile-time.
// E.g. with GCC by using the -D flag: gcc -c aes.c -DCBC=0 -DCTR=1 -DECB=1
#define CBC 1
#define CTR 1
#define ECB 1
#include "aes.h"
static void test_decrypt_ctr(void);
int main(void)
{
#ifdef AES128
printf("\nTesting AES128\n\n");
#elif defined(AES192)
printf("\nTesting AES192\n\n");
#elif defined(AES256)
printf("\nTesting AES256\n\n");
#else
printf("You need to specify a symbol between AES128, AES192 or AES256. Exiting");
return 0;
#endif
//decryption/encryption() copy paste the hex code in key and plaintext to see the encrytion/decryption working
test_decrypt_ctr();
return 0;
}
//Please see this section of code
static void test_xcrypt_ctr(const char* xcrypt);
static void test_decrypt_ctr(void)
{
test_xcrypt_ctr("decr");
}
static void test_xcrypt_ctr(const char* xcrypt)
{
int n=0,i=0;
//hex for encrypt 0x65,0x6E,0x63,0x72 ,0x79 ,0x70 ,0x74
// 0x19, 0x58, 0x29, 0xC8, 0xA2, 0xBB, 0xF2,encrpted string
//secret key,0x6B,0x61 ,0x72
uint8_t key[3] = {0x6B,0x61 ,0x72};//kar,type your key here in hex format
uint8_t in[7] = {0x65,0x6E,0x63,0x72 ,0x79 ,0x70 ,0x74};//encrypt,type your plaintext here in hex format**
uint8_t iv[16] = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CTR_xcrypt_buffer(&ctx, in, 7);
//pass the in[] which contains the encrypted/decrypted string
n=sizeof(in);
printf("printing the encrypted string in hex format\n");
for(i=0;i<7;i++)
printf("0x%.2X, ", in[i]);
printf("\n");
char * data;
data=malloc(n);
data=(char *)in;
printf("The encrypted string is'%s'",data);
}
Hi @kokke
I accept the input from user and pass them to the encryption which accepts the parameters as const,how do I convert the parameters to constant
You can just cast the 'uint8_t'-pointer to 'const'