Hello,
I want to build station which will upload data to Amazon DanamoDB, so I should encrypt some key and messages using sha256.
So, I should perform a step by step encryption, where output of one encryption is the secret key to the next.
My problem is this, only the first encryption works well with correct output. Next encryptions are faulty.
I compare the output which same outputs from python program, which works well.
Is there anyone who faced the same problem and could help me.
I upload the code, which make the encryption, without network part
#include "sha256.h"
const char signed_head[] PROGMEM = "content-type;host;x-amz-date;x-amz-target";
const char bodya[] PROGMEM = "{\"TableName\": \"gaiokthmonas_ektash1\", \"Item\": {\"meastime\": {\"S\": \"202105270852\"}, \"humidity\": {\"N\": \"0.5\"}, \"temperature\": {\"N\": \"22\"}}\0";
const char bodyb[] PROGMEM = ", \"ConditionExpression\": \"attribute_not_exists(meastime)\"}\0";
const char can_head0[] PROGMEM = "content-type:application/x-amz-json-1.0\nhost:dynamodb.eu-west-2.amazonaws.com\nx-amz-date:";
const char can_head1[] PROGMEM = "\nx-amz-target:DynamoDB_20120810.PutItem\n";
const char string_5[] PROGMEM = "String 5";
const char *const string_table[] PROGMEM = {signed_head, bodya, bodyb, can_head0, can_head1, string_5};
aws_secret_access_key[45]="xxxxxxxxxxxxxxx\0";
char utc_time[16] = "20210527T055121Z";
char utc_date[8] = "20210527";
char can_method[4] = "POST";
char can_uri[1] = "/";
char can_query_string[1] = "";
char region[9] = "eu-west-2";
char can_head[290];
char body[221];
void CryptoMessage(uint8_t *result_digest, uint8_t *secret_key,int key_len, char *message, int message_len){
message[message_len-1]='\0';
secret_key[key_len-1]='\0';
message_len=message_len-1;
key_len=key_len-1;
// Serial.println("function");
// Serial.print("message: ");
// Serial.print(message);
// Serial.print(" message len: ");
// Serial.println(message_len);
// Serial.print("secret_key: ");
// Serial.print(secret_key);
// Serial.print(" key_len: ");
// Serial.println(key_len);
Sha256.initHmac(secret_key, key_len);
Sha256.print(message);
uint8_t * result = Sha256.resultHmac();
char hexencoded[64];
strncpy(result_digest,result,64);
result_digest[64] = '\0';
for(int i = 0; i < 64; i+=2){
hexencoded[i] = "0123456789abcdef"[result[i / 2] >> 4];
hexencoded[i + 1] = "0123456789abcdef"[result[i / 2] & 0xf];
}
// encoded[64]='\0';
Serial.print("hexencoded: ");
Serial.println(hexencoded);
Serial.println("-------");
}
char buffer[159];
void setup(void)
{
Serial.begin(9600);
Serial.println("======");
strcpy_P(buffer, (char *)pgm_read_word(&(string_table[1])));
strcpy(body, buffer);
strcpy_P(buffer, (char *)pgm_read_word(&(string_table[2])));
strcat(body, buffer);
Serial.println(body);
Serial.println("======");
can_head[0]=0;//empty char array
strcpy_P(buffer, (char *)pgm_read_word(&(string_table[3])));
strcpy(can_head,buffer );
strcat(can_head, utc_time);
strcpy_P(buffer, (char *)pgm_read_word(&(string_table[4])));
strcat(can_head, buffer);
Serial.println(can_head);
Serial.println("======");
Serial.println("\"\"");
char encodedMessage[65];
char message[9]="20210527";
message[8]='\0';
char messageDate[9] = "20210527";
messageDate[8]='\0';
char messageRegion[10] = "eu-west-2";
messageRegion[9] ='\0';
char messageServive[9] = "dynamodb";
messageServive[8] ='\0';
char messageSigning[13] = "aws4_request";
messageSigning[12] ='\0';
Serial.println(message);
Serial.println(messageDate);
Serial.println(messageRegion);
Serial.println(messageServive);
Serial.println(messageSigning);
uint8_t kDate[65];
uint8_t kRegion[65];
uint8_t kService[65];
uint8_t kSigning[65];
Serial.println("kDate: ");
CryptoMessage(kDate,aws_secret_access_key,sizeof(aws_secret_access_key),messageDate,sizeof(messageDate)); //ypologismos kDate
//Serial.println(encodedMessage);
Serial.println("KRegion: ");
CryptoMessage(kRegion,kDate,sizeof(kDate),messageRegion,sizeof(messageRegion)); //ypologismos kRegion
//Serial.println(encodedMessage);
Serial.println("Kservice: ");
CryptoMessage(kService,kRegion,sizeof(kRegion),messageServive,sizeof(messageServive)); //ypologismos kService
//Serial.println(encodedMessage);
Serial.println("kSigning: ");
CryptoMessage(kSigning,kService,sizeof(kService),messageSigning,sizeof(messageSigning)); //ypologismos kSigning
//Serial.println(encodedMessage);
//Serial.println("kSigning -> ...");
/*CryptoMessage(encodedMessage,aws_secret_access_key,sizeof(aws_secret_access_key),encodedMessage,sizeof(encodedMessage));
//CryptoMessage(encodedMessage,aws_secret_access_key,sizeof(aws_secret_access_key),encodedMessage,sizeof(encodedMessage)); // de xreiazetai auto
Serial.println(encodedMessage);
*/
}
void loop(void)
{}