I have on radio transmitter and one radio receiver like on the picture. What I am trying to do.
I send data through USART2 PA2 (pin2) on data transmitter.
I connected usb-uart to PA2 (pin2), and looked in terminal what data is there and I saw something like this (11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44 ).It's okay i transmit what i want.
Then i connected usb-uart to PA10 (pin10) radio receiver, and looked in terminal, there was a lot of data but mostly of them was (44 FD 11 22 33 44 7F 10 22 33 44 FE 11 22 33 44 00 00 00 A4 26 F4 F8 11 22 33 44 FE 11 22 33 44 FA 10 22 33 44 FF 11 22 33 44 FE 11 22 33 44 D6 11 22 33 44 ).It's okay i receive from receiver module data that was transmitted and something else....
Then I connected usb-uart to PA9 (pin9), and looked in terminal, there was only (11 33 44 FC 11 33 44 FF 10 33 44 FF 11 33 44 FE ).
When USART1 receiving data from transmitter(Pin10)(44 FD 11 22 33 44 7F 10 ) it receiving full sequence(11 22 33 44), but when it transmit to Pin9 i see only (11 33 44) in usb-uart.
Where is one byte (22)? Where is error and why it's happening thanks!
The main question what I am doing wrong, why USART1 TX deformates data? Or what? How to fix this?
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#define SYNC 0xAA
#define RADDR 0x44
#define LEDON 0x11//switch led on command
#define LEDOFF 0x22//switch led off command
void Delay(uint32_t value)
{
volatile uint32_t i;
for (i = 0; i != value; i++)
;
}
void send_to_uart1(uint8_t data)
{
while(!(USART1->SR & USART_SR_TC))
{
}
USART1->DR=data;
}
void send_to_uart2(uint8_t data)
{
while (!(USART2->SR & USART_SR_TXE));
USART2->DR = data;
}
uint8_t GetChar (void) {
while (!(USART1->SR & USART_SR_RXNE));
return ((USART1->DR));
}
uint8_t uart_data;
int k=0;
char message[4]={0x11,0x22,0x33,0x44};
int main(void)
{
GPIO_InitTypeDef PORTA_init_struct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
PORTA_init_struct.GPIO_Pin = GPIO_Pin_9;
PORTA_init_struct.GPIO_Speed = GPIO_Speed_2MHz;
PORTA_init_struct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &PORTA_init_struct);
PORTA_init_struct.GPIO_Pin = GPIO_Pin_10;
PORTA_init_struct.GPIO_Speed = GPIO_Speed_2MHz;
PORTA_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &PORTA_init_struct);
PORTA_init_struct.GPIO_Pin = GPIO_Pin_2;
PORTA_init_struct.GPIO_Speed = GPIO_Speed_2MHz;
PORTA_init_struct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &PORTA_init_struct);
PORTA_init_struct.GPIO_Pin = GPIO_Pin_3;
PORTA_init_struct.GPIO_Speed = GPIO_Speed_2MHz;
PORTA_init_struct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &PORTA_init_struct);
USART_InitTypeDef uart_struct1;
uart_struct1.USART_BaudRate = 1200;
uart_struct1.USART_WordLength = USART_WordLength_8b;
uart_struct1.USART_StopBits = USART_StopBits_1;
uart_struct1.USART_Parity = USART_Parity_No;
uart_struct1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart_struct1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &uart_struct1);
USART_Cmd(USART1, ENABLE);
USART_InitTypeDef uart_struct2;
uart_struct2.USART_BaudRate = 1200;
uart_struct2.USART_WordLength = USART_WordLength_8b;
uart_struct2.USART_StopBits = USART_StopBits_1;
uart_struct2.USART_Parity = USART_Parity_No;
uart_struct2.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
uart_struct2.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &uart_struct2);
USART_Cmd(USART2, ENABLE);
while (1) {
while(k<4)
{
for (int j=0;j<4;j++)
{
send_to_uart2(message[k]);
k=k+1;
}
for (int i=0;i<4;i++)
{
send_to_uart1(GetChar());
}
}
k=0;
}
}
Related
I have token (base64url) that i need to decrypt with aes-gcm. The token contains:
16 bytes for IV, 17 bytes for TAG and the rest is the binary that needs to be decrypted.
I can't seem to be able to figure out, this is my code:
{
unsigned char * source = "BASE64-ENCODED-BINARY";
unsigned char key_raw[] = "KEY";
unsigned char key[2048];
int key_len = 0;
unsigned char output[2048];
int output_len = 0;
unsigned char * plaintext;
int c, r;
size_t out;
int dest_len = 4*(sizeof(source)/3);
// int key_len = 4*(sizeof(key_raw)/3);
unsigned char iv[16];
unsigned char tag[16];
unsigned char content[2048];
int content_len = 0;
b64ud_t s;
EVP_CIPHER_CTX *ctx;
int outlen, tmplen, rv;
unsigned char outbuf[2048];
/* token decode */
base64url_decode_reset(&s);
//memset( output,0, dest_len );
base64url_decode( output, 2048, source, strlen(source), NULL );
/* Just look through the output to get the decode result len */
for(;;)
{
if( output[output_len] )
{
output_len++;
} else {
break;
}
}
printf("decoded-token: [length: %d]\n", output_len );
BIO_dump_fp(stdout, output, output_len);
/* ket decode */
base64url_decode_reset(&s);
memset( key, 0, key_len );
base64url_decode( key, key_len-1, key_raw, strlen(key_raw), NULL );
/* Just look through the key to get the decode result len */
for(;;)
{
if( key[key_len] )
{
key_len++;
} else {
break;
}
}
printf("decoded-key: [Length: %d]\n", key_len );
BIO_dump_fp(stdout, key, key_len);
/*
The token is composed like so:
[16bytes IV] + [16 bytes TAG] + [Encrypted Message]
*/
printf("getting 16 bytes out of the decode output and storing them in IV\n");
for(int v=0;v<16;v++)
{
iv[v] = output[v];
}
printf("getting the NEXT 16 bytes out of the decode output and storing them in TAG\n");
for(int v=16;v<32;v++)
{
tag[v-16] = output[v];
}
printf("Just count haw many non-00 bytes remain and store it in content_len\n");
for(int i=32;i<output_len;i++)
{
if(output[i])
{
content_len++;
} else {
break;
}
}
printf("%d\n", content_len);
printf("We now use content_len and get the remaining bytes and store them in content\n");
for(int v=0; v<content_len;v++)
{
content[v] = output[v+32];
}
printf( "iv:\n" );
BIO_dump_fp(stdout, iv, sizeof(iv));
printf("tag:\n" );
BIO_dump_fp(stdout, tag, sizeof(tag));
printf("content :\n" );
BIO_dump_fp(stdout, content, content_len);
printf("AES GCM Decrypt:\n");
unsigned char * key_final;
key_final = key;
unsigned char * ciphertext;
ciphertext = content;
printf("Ciphertext:\n");
BIO_dump_fp(stdout, content, content_len);
ctx = EVP_CIPHER_CTX_new();
/* Select cipher */
EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
/* Set IV length, omit for 96 bits */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(iv), NULL);
/* Specify key and IV */
EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv);
/* Zero or more calls to specify any AAD */
//EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
/* Decrypt plaintext */
EVP_DecryptUpdate(ctx, outbuf, &outlen, ciphertext, content_len);
/* Output decrypted block */
printf("Plaintext:\n");
BIO_dump_fp(stdout, outbuf, outlen);
/* Set expected tag value. */
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), (void *)tag);
/* Finalise: note get no output for GCM */
rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
/*
* Print out return value. If this is not successful authentication
* failed and plaintext is not trustworthy.
*/
printf("outbuf: %s", outbuf);
printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!");
EVP_CIPHER_CTX_free(ctx);
return 0;
}
My output is:
decoded-token: [length: 57]
0000 - ae 3f d9 92 46 54 39 93-31 64 e7 ce 98 ba 44 50 .?..FT9.1d....DP
0010 - 1d ec 89 4e ee e9 18 d9-15 e3 3d b3 e8 1b ff 10 ...N......=.....
0020 - 91 e7 a5 85 28 50 09 88-cc 85 d9 3e 82 05 19 a5 ....(P.....>....
0030 - 87 f4 b2 d2 2f e5 7f 24-fd ..../..$.
decoded-key: [Length: 33]
0000 - 0e 0b e4 0a b9 32 04 d4-b2 f7 21 cf d5 8c e7 c9 .....2....!.....
0010 - cd 83 90 74 c8 51 76 8e-e8 d9 44 c3 80 92 ab 40 ...t.Qv...D....#
0020 - e3 .
doing iv
doing tag
getting content length: 25
copying content length to content var
iv:
0000 - ae 3f d9 92 46 54 39 93-31 64 e7 ce 98 ba 44 50 .?..FT9.1d....DP
tag:
0000 - 1d ec 89 4e ee e9 18 d9-15 e3 3d b3 e8 1b ff 10 ...N......=.....
content :
0000 - 91 e7 a5 85 28 50 09 88-cc 85 d9 3e 82 05 19 a5 ....(P.....>....
0010 - 87 f4 b2 d2 2f e5 7f 24-fd ..../..$.
AES GCM Decrypt:
Ciphertext:
0000 - 91 e7 a5 85 28 50 09 88-cc 85 d9 3e 82 05 19 a5 ....(P.....>....
0010 - 87 f4 b2 d2 2f e5 7f 24-fd ..../..$.
Plaintext:
0000 - f3 6d 72 13 d9 dd 5b a3-b6 af 73 8d a2 93 8b f7 .mr...[...s.....
0010 - 0e 9e 2a 87 6c 82 84 bd-46 ..*.l...F
outbuf: �mr��[���s������*�l���FTag Verify Failed!
I can't be 100% I'm doing it right, but anyway i'm getting Verify Failed!
Help is GREATLY appreciated!
I'm not very familiar with AES, but this code looks suspicious:
int dest_len = 4*(sizeof(source)/3);
because source is defined as:
unsigned char * source = "BASE64-ENCODED-BINARY";
and in consequence sizeof(source) gives you the size of a pointer.
Try:
int dest_len = 4*(strlen(source)/3);
or define source as:
const unsigned char source[] = "BASE64-ENCODED-BINARY";
So my knowledge of pointers is a bit rusty and I think thats where I'm getting messed up, I am trying to write a function that will grab hex values (an amount n) at a specified offset in the file. And write those values to an array.
File I'm reading from, Example
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 F6 EA 9D DE D8 40 1C 44 19 24 59 D2 6A 2C 48 1D
1 FC 96 DE 94 AF 95 FC 42 9B 6D DA 15 D4 CE 88 BB
2 B8 24 99 8F 65 B5 D3 7E D9 5D 51 44 89 97 61 85
3 2D 40 1A DC D5 16 1F 70 84 F9 85 58 C8 0E 13 80
4 32 AC 10 97 61 B3 16 3B 40 67 7A CA FE E1 4F 2B
5 21 A9 07 F6 80 26 66 04 20 EC 5C E8 FA 70 68 2C
6 1C 78 C4 7E 5C DA B9 9C 41 38 66 3F 19 B6 6A 3A
Here's the function I've written thus far.
aDest point's to an array of size nBytes + 1
bAddr point's to firstbyte of the memory region
OffsetAmt is a location which is relative bAddr
nBytes is just the number of bytes that I want to copy
Heres the function
void getHexBytesAt(uint8_t* const aDest, const uint8_t* const bAddr,
uint16_t OffsetAmt, uint8_t nBytes)
{
const uint8_t *point1 = bAddr; //set he address of point1 to base address value
//front point1 shift until we get to the specified offset value
for (int i = 0; i < Offset; i++)
{
point1 = (point1 + 1);
}
//set the values of *aDest to the value of point1;
//increment point1
for (int k = 0; k < nBytes; k++)
{
*aDest = point1;
point1 = (point1 + 1);
}
The problem I'm having is im not even getting the first byte copied into the array correctly,
My output looks like this Getting 9 bytes,
starting at the offset 2C
MY OUTPUT: 84 CA CA CA CA CA CA CA CA
FILE: 89 97 61 85 2D 40 1A DC D5
If you want to read the data from the Memory bAddr then you must
dereference the pointer for reading
increment the Destination pointer
This would be implemented like this:
void getHexBytesAt(uint8_t* const aDest, const uint8_t* const bAddr,
uint16_t OffsetAmt, uint8_t nBytes)
{
const uint8_t *point1 = bAddr; //set he address of point1 to base address value
//front point1 shift until we get to the specified offset value
for (int i = 0; i < OffsetAmt; i++) // fixed typo
{
point1 = (point1 + 1);
}
//set the values of *aDest to the value of point1;
//increment point1
for (int k = 0; k < nBytes; k++)
{
*aDest = *point1; // copy data from address the point1 points to
aDest = aDest + 1; // increment destination pointer
point1 = (point1 + 1);
}
}
But this can be done much simpler:
void getHexBytesAt(uint8_t* const aDest, const uint8_t* const bAddr,
uint16_t OffsetAmt, uint8_t nBytes)
{
memcpy(aDest, bAddr + OffsetAmt, nBytes);
}
You should consider replacing the function with the one-liner that implements it in your code.
BTW: There is no file used in the code. You should review your question.
I have a problem with my PIC24F16KA102. I am using the PicKit2 UART Tool to communicate with the PC.
So, these are my routines:
UART1Init:
void UART1Init(int BAUDRATE)
{
U1BRG = BAUDRATE;
U1MODEbits.UARTEN = 1; // UART2 is Enabled
U1MODEbits.USIDL = 0; // Continue operation at Idlestate
U1MODEbits.IREN = 0; // IrDA En/Decoder is disabled
U1MODEbits.RTSMD = 0; // flow control mode
U1MODEbits.UEN = 0b10; // UTX, RTX, U2CTS, U2RTS are enable and on use.
U1MODEbits.WAKE = 1; // Wake-up on start bit is enabled
U1MODEbits.LPBACK = 0; // Loop-back is disabled
U1MODEbits.ABAUD = 0; // auto baud is disabled
U1MODEbits.RXINV = 0; // No RX inversion
U1MODEbits.BRGH = 0; // low boud rate
U1MODEbits.PDSEL = 0b00; // 8bit no parity
U1MODEbits.STSEL = 1; // one stop bit
U1STAbits.UTXISEL1 = 0b00;
U1STAbits.UTXBRK = 0; // sync break tx is disabled
U1STAbits.UTXEN = 1; //transmit is enabled
U1MODEbits.RXINV = 1; //Invert RX and TX Idle state
U1STAbits.UTXINV = 1;
}
void UART1putChar(unsigned char ch)
{
while(U1STAbits.UTXBF == 1); //wait till transmit buffer is not full anymore
U1TXREG = (ch);
}
char UART1GetChar()
{
char Temp;
//wait for buffer to fill up, wait for interrupt
while(IFS0bits.U1RXIF == 0);
Temp = U1RXREG;
//reset interrupt
IFS0bits.U1RXIF = 0;
//return my received byte
return Temp;
}
and the main function:
int main(void) {
init();
TRISB=0x0004;
UART1Init(25);
while(1)
{
UART1putChar(UART1GetChar());
}
return 0;
}
When I use the PicKit2 UART Tool to send and receive characters, this is what I get:
hekllo
TX: 68 65 6B 6C 6C 6F
RX: 85
TX: 68 65 6B 6C 6C 6F
RX: 85
TX: 68 65 6B 6C 6C 6F
RX: 88
TX: 68 65 6B 6C 6C 6F
RX: 85
TX: 68 65 6B 6C 6C 6F
RX: 63 0C
TX: 68 65 6B 6C 6C 6F
RX: 88
TX: 68 65 6B 6C 6C 6F
RX: 88
TX: 68 65 6B 6C 6C 6F
RX: 61 0C
When I transmit the "1" character, I never receive an "1", but sometimes I receive "//carriage return ? ".
So, my question is : Where should I start debugging? What can possibly go wrong?
I am developing a Client/Server based on UDP I want to send different messages to the client from the server. There are different C structures defined for each message.
I would like to understand what is wrong in the way I am serializing the data.
struct Task
{
int mType;
int tType;
int cCnt;
int* cId;
char data[128];
};
Serialization/Deserialization functions
unsigned char * serialize_int(unsigned char *buffer, int value)
{
buffer[0] = value >> 24;
buffer[1] = value >> 16;
buffer[2] = value >> 8;
buffer[3] = value;
return buffer + 4;
}
unsigned char * serialize_char(unsigned char *buffer, char value)
{
buffer[0] = value;
return buffer + 1;
}
int deserialize_int(unsigned char *buffer)
{
int value = 0;
value |= buffer[0] << 24;
value |= buffer[1] << 16;
value |= buffer[2] << 8;
value |= buffer[3];
return value;
}
char deserialize_char(unsigned char *buffer)
{
return buffer[0];
}
Sender side code to serialize the structure
unsigned char* serializeTask(unsigned char* msg, const Task* t)
{
msg = serialize_int(msg,t->mType);
msg = serialize_int(msg,t->tkType);
msg = serialize_int(msg,t->cCnt);
for(int i=0; i<t->cCnt; i++)
msg = serialize_int(msg,t->cId[i*4]);
for(int i=0; i<strlen(data); i++)
msg = serialize_char(msg,t->data[i]);
return msg;
}
Receiver side code to de-serialize data
printf("Msg type:%d\n", deserialize_int(message) );
printf("Task Type:%d\n", deserialize_int(message+4) );
printf("Task Count:%d\n", deserialize_int(message+8));
Output
Msg type:50364598 //Expected value is 3
Task Type:-2013036362 //Expected value is 1
Task Count:1745191094 //Expected value is 3
Question 1:
Why is the de-serialized value not same as expected?
Question 2:
How is serialization/de-serialization method different from memcpy?
Task t;
memcpy(&t, msg, sizeof(t)); //msg is unsigned char* holding the struct data
EDIT
Code which invokes serializeTask
void addToDatabase(unsigned char* message, int msgSize, Task* task)
{
message = new unsigned char[2*msgSize+1];
unsigned char* msg = message; //To preserve start address of message
message = serializeTask(message, task); //Now message points to end of the array
//Insert serialized data to DB
//msg is inserted to DB
}
Serialized data stored in DB
Message:
00
03 70 B6 88 03 70 B6 68 05 70 B6 68 05 70 B6 00
00 00 00 00 00 00 00 A8 05 70 B6 AC 05 70 B6 B4
05 70 B6 C9 05 70 B6 DE 05 70 B6 E6 05 70 B6 EE
05 70 B6 FB 05 70 B6 64 65 66 00 63 6F 68 6F 72
74 73 00 70 65 6E 64 69 6E 67 5F 61 73 73 69 67
6E 5F 74 61 73 6B 73 00 70 65 6E 64 69 6E 67 5F
61 73 73 69 67 6E 5F 74 61 73 6B 73 00 6D 65 73
73 61 67 65 00 6D 65 73 73 61 67 65 00 3F 00 FF
FF 00 00 FC 90 00 00 00 00 00 00 00 C9 2D B7 00
00 00 00 10 06 70 B6 00 00 00 00 00 00 00 00 30
06 70 B6 34 06 70 B6 3C 06 70 B6
OP has 2 problems in serializeTask()
for(int i=0; i<t->cCnt; i++)
msg = serialize_int(msg,t->cId[i*4]); [i*4]
...
for(int i=0; i<strlen(data); i++)
msg = serialize_char(msg,t->data[i]); strlen(data)
Should be (assuming i<strlen(data) should have been i<strlen(t->data)
for(int i=0; i<t->cCnt; i++)
msg = serialize_int(msg,t->cId[i]); // [i]
...
for(int i=0; i<strlen(t->data); i++) // strlen(data) + 1
msg = serialize_char(msg,t->data[i]);
The first for loop serialize every 4th cId[]. OP certainly wanted to serialize consecutive cId[].
Only the length of the data string was serialized. OP certainly wanted to serialize all that and a NUL terminating byte.
The data in the posted buffer is more likely the below, which does not match the serialization code. This implies the higher level code populating Task* t is wrong. I am confident that the values seen in fields mType and tkType are either pointers or float, again Task* t is likely amiss before the serialization.
0xb6700300 or -3.576453e-06
0xb6700388 or -3.576484e-06
0xb6700568 or -3.576593e-06
0xb6700568 or -3.576593e-06
0x000000 or 0.000000e+00
0x000000 or 0.000000e+00
0xb67005a8 or -3.576608e-06
0xb67005ac or -3.576609e-06
0xb67005b4 or -3.576611e-06
0xb67005c9 or -3.576615e-06
0xb67005de or -3.576620e-06
0xb67005e6 or -3.576622e-06
0xb67005ee or -3.576624e-06
0xb67005fb or -3.576627e-06
def\0cohorts\0pending_assign_tasks\0pending_assign_tasks\0message\0message\0?\0
...
I want to do padding with PKCS7 :
char *test1 = "azertyuiopqsdfgh";
char *test2 = malloc(32*sizeof(char));
memcpy(test2, test1, strlen(test1));
char pad = (char)(32-strlen(test1));
printf("pad = %d\n", pad);
for(int i = strlen(test1) ; i < 32 ; i++) {
test2[i] = pad;
}
for (int i = 0 ; i < 32 ; i++)
printf("%x ", test2[i]);
printf("\n");
I obtain :
pad = 16
61 7a 65 72 74 79 75 69 6f 70 71 73 64 66 67 68 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
But i want :
pad = 16
61 7a 65 72 74 79 75 69 6f 70 71 73 64 66 67 68 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
How can i modify my code ?
Thanks in advance.
With
printf("%x ", test2[i]);
You are printing in hexadecimal (%x) whereas with
printf("pad = %d\n", pad);` you are printing in decimal (%d).
And (decimal) 16 => (hexa) 10, So you are displaying the right thing.
You could probably play a bit with your printing to display 16 instead of 10 but I don't think this is what you are searching for.