I'm playing around with DES encryption using CBC mode, and I came across something puzzling. I have a payload (not generated by me, but for which I know the key), accompanied by an IV, as well as a salt, which is meant to be XOR'd with the IV. On my first attempt to decrypt this payload, I found that bytes 3-7 of the plaintext were garbage, but the rest of the plaintext appeared correct. Eventually I discovered that I had failed to mix in the salt. The first 3 bytes of the salt happened to be 0, which explained why the first 3 bytes of the plaintext matched what I expected, but what I can't understand is why the incorrect IV had no impact on bytes 8 through the end of the payload. I found that I could set both the IV and the salt to any value (including all zeros), and still, only the first block (8 bytes) was impacted.
Seeing as this is all highly non-sensitive stuff (i.e. me fiddling around with SNMPv3 on a very out-dated Cisco switch on my local network with a one-word lowercase password, not to mention it doesn't even support AES), I feel no qualms sharing the following with you:
#include <stdio.h>
#include <openssl/des.h>
int main()
{
DES_key_schedule key;
DES_cblock privKey = { 0xc8, 0x13, 0x1b, 0xf0, 0x41, 0xf8, 0xae, 0xef };
DES_cblock iv = { 0xb5, 0xed, 0x65, 0x57, 0x7c, 0x99, 0x54, 0xe4 };
DES_cblock salt = { 0x00, 0x00, 0x00, 0x10, 0x63, 0xc6, 0x01, 0x82 };
const uint8_t cipher[] = {
0xc7, 0xf5, 0x53, 0xe5, 0xb3, 0x8a, 0x19, 0x8b,
0x03, 0xde, 0x49, 0xbb, 0x47, 0x38, 0x73, 0xb7,
0x96, 0x24, 0xa3, 0xba, 0x3a, 0x28, 0x79, 0x16,
0x8b, 0xe4, 0xbf, 0xb6, 0xfc, 0xc2, 0x86, 0xc7,
0x8f, 0x1e, 0x0c, 0x87, 0x53, 0xbe, 0xfc, 0x0a,
0xcd, 0x6a, 0x1a, 0xb3, 0xaa, 0x44, 0xcc, 0xb1,
0xc0, 0x5e, 0xe0, 0x98, 0x33, 0x26, 0x4b, 0xc4,
0x73, 0xa2, 0x93, 0x72, 0x86, 0x5e, 0xd0, 0xdf,
0x4f, 0x7f, 0x53, 0x92, 0xb5, 0xd6, 0x54, 0x16,
0x18, 0x55, 0xe6, 0xc7, 0xb0, 0x6f, 0x6b, 0xa7,
0x53, 0x13, 0x4a, 0x66, 0xc8, 0x65, 0xbf, 0x18,
0x2d, 0x00, 0x1c, 0xe5, 0x2e, 0xbc, 0xb2, 0x7f,
0x76, 0x03, 0x46, 0xaf, 0xac, 0xf7, 0xb3, 0x47,
0xbe, 0x09, 0xcc, 0x78, 0x9b, 0xf7, 0xae, 0x8f,
0x1f, 0xb7, 0xbb, 0xe6, 0x4f, 0x3a, 0xad, 0xdc,
0x5d, 0xde, 0xb4, 0x68, 0x4d, 0x5a, 0x68, 0x59,
0xa3, 0xc5, 0x33, 0x88, 0xad, 0x67, 0xa7, 0x2c,
0x7a, 0xe0, 0x45, 0x37, 0x41, 0x2d, 0x5d, 0xeb,
0x59, 0x20, 0xd1, 0x3e, 0x5f, 0x8b, 0x12, 0xb0,
0xcd, 0x1d, 0xd5, 0xf0, 0x1a, 0xe7, 0x64, 0x41,
0x37, 0xc1, 0xd1, 0x0c, 0x23, 0xc9, 0x90, 0x32,
0xf8, 0x21, 0xb9, 0xd6, 0x0e, 0x0e, 0x78, 0x2d,
0xf0, 0x79, 0x8c, 0x2c, 0x44, 0x04, 0x10, 0x48,
0xd7, 0xdb, 0x4c, 0xe5, 0xeb, 0x40, 0xb4, 0x4a,
0xa9, 0xb5, 0xf7, 0xa6, 0xce, 0x06, 0x28, 0xf4,
0xac, 0x99, 0xf8, 0x01, 0x88, 0xde, 0xb8, 0x75,
0x11, 0xb6, 0x2c, 0x87, 0x22, 0x8e, 0xfe, 0x3e,
0x0b, 0x7b, 0x15, 0xaa, 0xed, 0x1d, 0xaf, 0xfa,
0x88, 0x96, 0xd2, 0x8f, 0x57, 0xf8, 0xcd, 0xf6,
0x14, 0x7c, 0xbf, 0x69, 0x3d, 0x3e, 0x61, 0x2c,
0xb8, 0x01, 0x5a, 0x8a, 0x6a, 0xb1, 0x58, 0x0f,
0xa7, 0xd7, 0xc7, 0x5b, 0xe0, 0x0b, 0x3f, 0x05,
0x88, 0x85, 0xbb, 0xea, 0x82, 0x3e, 0x6f, 0xf4,
0xb7, 0x52, 0x4c, 0xc4, 0xea, 0x51, 0xfd, 0xb6,
0xc4, 0x5b, 0x65, 0x2e, 0xac, 0x29, 0x3c, 0x19,
0x40, 0x08, 0x5d, 0xa7, 0xad, 0xa8, 0x8c, 0x61,
0x78, 0xaa, 0xc4, 0xa2, 0x38, 0x72, 0x45, 0x84,
0x3d, 0x94, 0x8c, 0xa3, 0xba, 0x88, 0xf4, 0x79,
0x0a, 0x87, 0xc6, 0xe9, 0xd3, 0x0e, 0xd0, 0xd0
};
uint8_t plain[sizeof(cipher)];
int i;
for (i = 0; i < sizeof(DES_cblock); i++) {
iv[i] ^= salt[i];
}
DES_set_odd_parity(&privKey);
DES_set_key_unchecked(&privKey, &key);
DES_ncbc_encrypt(cipher, plain, sizeof(cipher), &key, &iv, DES_DECRYPT);
for (i = 0; i < sizeof(plain); i++) {
if (i) {
putchar(i % 8 ? ' ' : '\n');
}
printf("0x%02x", plain[i]);
}
putchar('\n');
}
To compile:
gcc -o main main.c -lcrypto
Running ./main produces the following output:
0x30 0x82 0x01 0x34 0x04 0x0c 0x80 0x00
0x00 0x09 0x03 0x00 0x00 0x24 0x13 0x70
0xb2 0xc1 0x04 0x00 0xa2 0x82 0x01 0x20
0x02 0x04 0x75 0xd7 0x16 0x29 0x02 0x01
0x00 0x02 0x01 0x00 0x30 0x82 0x01 0x10
0x30 0x82 0x01 0x0c 0x06 0x08 0x2b 0x06
0x01 0x02 0x01 0x01 0x01 0x00 0x04 0x81
0xff 0x43 0x69 0x73 0x63 0x6f 0x20 0x49
0x6e 0x74 0x65 0x72 0x6e 0x65 0x74 0x77
0x6f 0x72 0x6b 0x20 0x4f 0x70 0x65 0x72
0x61 0x74 0x69 0x6e 0x67 0x20 0x53 0x79
0x73 0x74 0x65 0x6d 0x20 0x53 0x6f 0x66
0x74 0x77 0x61 0x72 0x65 0x20 0x0d 0x0a
0x49 0x4f 0x53 0x20 0x28 0x74 0x6d 0x29
0x20 0x43 0x32 0x39 0x34 0x30 0x20 0x53
0x6f 0x66 0x74 0x77 0x61 0x72 0x65 0x20
0x28 0x43 0x32 0x39 0x34 0x30 0x2d 0x49
0x36 0x4b 0x32 0x4c 0x32 0x51 0x34 0x2d
0x4d 0x29 0x2c 0x20 0x56 0x65 0x72 0x73
0x69 0x6f 0x6e 0x20 0x31 0x32 0x2e 0x31
0x28 0x32 0x32 0x29 0x45 0x41 0x31 0x33
0x2c 0x20 0x52 0x45 0x4c 0x45 0x41 0x53
0x45 0x20 0x53 0x4f 0x46 0x54 0x57 0x41
0x52 0x45 0x20 0x28 0x66 0x63 0x32 0x29
0x0d 0x0a 0x54 0x65 0x63 0x68 0x6e 0x69
0x63 0x61 0x6c 0x20 0x53 0x75 0x70 0x70
0x6f 0x72 0x74 0x3a 0x20 0x68 0x74 0x74
0x70 0x3a 0x2f 0x2f 0x77 0x77 0x77 0x2e
0x63 0x69 0x73 0x63 0x6f 0x2e 0x63 0x6f
0x6d 0x2f 0x74 0x65 0x63 0x68 0x73 0x75
0x70 0x70 0x6f 0x72 0x74 0x0d 0x0a 0x43
0x6f 0x70 0x79 0x72 0x69 0x67 0x68 0x74
0x20 0x28 0x63 0x29 0x20 0x31 0x39 0x38
0x36 0x2d 0x32 0x30 0x30 0x39 0x20 0x62
0x79 0x20 0x63 0x69 0x73 0x63 0x6f 0x20
0x53 0x79 0x73 0x74 0x65 0x6d 0x73 0x2c
0x20 0x49 0x6e 0x63 0x2e 0x0d 0x0a 0x43
0x6f 0x6d 0x70 0x69 0x6c 0x65 0x64 0x20
0x46 0x72 0x69 0x20 0x32 0x37 0x2d 0x46
If I instead fill iv and salt with all zeros, the first line changes to the following, while the rest remains the same:
0x85 0x6f 0x64 0x73 0x1b 0x53 0xd5 0x66
So the obvious question is, what is going on here?
If anyone can answer that, I would also ask
Has this behavior been observed/documented anywhere?
Why use CBC with DES at all if it provides so little benefit (well, yeah, I know no one really encourages using DES anymore)?
The answer is perfectly simple, and has nothing to do with DES, and everything to do with CBC mode. Thank you to #Topaco for pointing it out to me.
The IV does propagate changes to all blocks when performing encryption, and has the effect of randomizing the contents of each block. Two identical plaintext blocks will produce wildly different ciphertext, so it limits the amount of information that is leaked directly through the ciphertext.
On the other hand, CBC allows you to decrypt any block without first decrypting the blocks before it. This allows decryption to be parallelized. However, it causes exactly the behavior presented in the question. The IV is not required to decrypt any block except the first one.
Related
I need some help with CRC calculation. I spent a week trying to understand how a CRC (or Checksum) is calculated on a communication protocol
I have a Endress+Hauser pH probe (CPS11D-7FA21) that has MemoSense connector. Basically it is a digital pH probe. The communication is RS485 but the protocol is proprietary, so no info on that.
I connected the probe to a meter and sniffed the frames. There is a lot of data going back and fourth in the beginning but the polling repeats.
I was able to write some code for an Arduino and it worked, I can initialize the probe and pool measurements (ph(mV) and temperature), but the problem is that it only work with one probe. If I connect a new one it all stops.
I was digging a bit and I found out that right before the pooling starts the Init frame is different... Ii seem the measuring device somehow calculates the Init data from some other data that is exchanged in the beginning.
Anyway the fist thing is that I want to understand how the data frame is constructed.
Here is an example of pooling one of the probes and the data changes slightly
0x01, 0x01, 0x00, 0x00, 0x00, 0x2e, 0x04, 0x03, 0x09, 0x00, 0x20, 0xb9
0x01, 0x01, 0x00, 0x00, 0x00, 0x2f, 0x04, 0x03, 0x09, 0x00, 0x21, 0x99
0x01, 0x01, 0x00, 0x00, 0x00, 0x30, 0x04, 0x03, 0x09, 0x00, 0x3e, 0x7a
0x01, 0x01, 0x00, 0x00, 0x00, 0x32, 0x04, 0x03, 0x09, 0x00, 0x3c, 0x3a
0x01, 0x01, 0x00, 0x00, 0x00, 0x33, 0x04, 0x03, 0x09, 0x00, 0x3d, 0x1a
| <-------------------> | \ / \ / | | |
Header pH(mV)/10 Temp/100 ? Csum CRC???
0x01, 0x01, 0x00, 0x00, 0x00, 0x8d, 0x06, 0xf4, 0x08, 0x00, 0x77, 0x56
0x01, 0x01, 0x00, 0x00, 0x00, 0x8f, 0x06, 0xf4, 0x08, 0x00, 0x75, 0x16
0x01, 0x01, 0x00, 0x00, 0x00, 0x88, 0x06, 0xf4, 0x08, 0x00, 0x72, 0xf6
0x01, 0x01, 0x00, 0x00, 0x00, 0x94, 0x06, 0xf5, 0x08, 0x00, 0x6f, 0x7d
0x01, 0x09, 0x04, 0x00, 0x0c, 0x00, 0x14, 0x02, 0x03, 0x11, 0x36, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x20, 0x20, 0x20, 0x20, 0x46, 0x57
0x01, 0x09, 0x04, 0x00, 0x0c, 0x00, 0x15, 0x07, 0x17, 0x12, 0x26, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x20, 0x20, 0x20, 0x20, 0x45, 0xb0
0x01, 0x00, 0x04, 0x00, 0x05, 0xa8, 0x4b, 0x53, 0x47, 0x31, 0x20, 0x01, 0x01, 0x01, 0x5a, 0x02, 0x9f, 0x27, 0x40, 0x39, 0x3f, 0x04, 0x52, 0x31, 0x32, 0x38, 0x44, 0x46, 0x30, 0x35, 0x42, 0x30, 0x33, 0x14, 0x01, 0x16, 0x00, 0x69, 0x00
0x01, 0x00, 0x04, 0x00, 0x05, 0xa8, 0x4b, 0x53, 0x47, 0x31, 0x20, 0x01, 0x01, 0x01, 0x01, 0x00, 0x75, 0x27, 0x41, 0xb8, 0x41, 0x04, 0x53, 0x36, 0x33, 0x37, 0x45, 0x44, 0x30, 0x35, 0x42, 0x30, 0x37, 0x15, 0x07, 0x06, 0x00, 0x3c, 0xf3
0x01, 0x08, 0x04, 0x00, 0x0d, 0xb0, 0xe5, 0x69, 0x1b, 0x00, 0x00, 0x69, 0x1b, 0x15, 0x07, 0x17, 0x12, 0x26, 0x02, 0x44, 0x1b, 0xaa, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x20, 0x20, 0x20, 0x20, 0xe9, 0xcd
0x01, 0x08, 0x04, 0x00, 0x0d, 0x40, 0xe7, 0x58, 0x1b, 0x00, 0x00, 0x67, 0x1b, 0x14, 0x02, 0x03, 0x11, 0x36, 0x02, 0x44, 0x1b, 0xaa, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x61, 0x63, 0x74, 0x6f, 0x72, 0x79, 0x20, 0x20, 0x20, 0x20, 0x27, 0xde
Here is what I learned so far, the Csum is a standard 8bit Xor checksum
that works here
https://www.scadacore.com/tools/programming-calculators/online-checksum-calculator/
But the CRC is a mystery for me ... I tried multiple calculator and different data pieces and nothing. To me it seems it is a CRC because the if one bit changes there is a huge difference in the CRC byte
Maybe someone here could shed some light on this
Thank you
The last byte appears to be an exclusive-or and rotate check on the non-header bytes. If the message with check values is the unsigned char array a[0..len-1], then this computes the last two check values, x == a[len - 2] and y == a[len - 1]:
unsigned x = 0, y = 0;
for (size_t i = 0; i < len - 2; i++)
x ^= a[i];
for (size_t i = 5; i < len - 2; i++) {
y = (y ^ a[i]) << 1;
if (y & 0x100)
y ^= 0x101;
}
I see how a QByteArray to convert to and from Ascii character
I see how to convert a QString to utf-8 using QString::toUtf8()
However, I do not see how to initialize a QByteArray with hex literals containing unicode characters. The unicode characters are too large to fit into a char. They will however fit into an unsigned char.
If I run the following code:
QString test = "¢";
QByteArray utf8bytes = test.toUtf8();
My debugger says:
utf8bytes = { (char)-62 '\302', (char)-94 '\242' }
Whatever that means...
What I ultimately want to accomplish is to be able to provide test data in the following unit test code:
void stomp_frame_tests::test_nonAsciiCharactersInHeaderKey()
{
/* Frame we are testing with:
SEND
Bad¢Character:Erroneus
destination:/queue/a
content-type:text/json;charset=utf-8
content-length:22
This is a test message
*/
const char data[] = {0x53, 0x45, 0x4e, 0x44, 0x0a, 0x42, 0x61, 0x64, 0xc2, 0xa2, 0x43, 0x68, 0x61, 0x72, 0x61,
0x63, 0x74, 0x65, 0x72, 0x3a, 0x45, 0x72, 0x72, 0x6f, 0x6e, 0x65, 0x75, 0x73, 0x0a, 0x64,
0x65, 0x73, 0x74, 0x69, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x2f, 0x71, 0x75, 0x65,
0x75, 0x65, 0x2f, 0x61, 0x0a, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79,
0x70, 0x65, 0x3a, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x3b, 0x63, 0x68,
0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x0a, 0x63, 0x6f, 0x6e,
0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x32, 0x32, 0x0a,
0x0a, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74,
0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00};
auto testData = QByteArray::fromRawData(data, sizeof(data));
}
This fails, of course, because 0xc2 and 0xa2 are too large to fit into a char. How would you go about initializing the QByteArray with the appropriate data?
Use QByteArray::fromHex instead:
auto testData = QByteArray::fromHex("C2A2");
Document:
https://doc.qt.io/qt-5/qbytearray.html#fromHex
Perhaps you are looking for QByteArrayLiteral:
auto byteArray = QByteArrayLiteral("\x01\x02\x03\x04\x05");
QByteArrayLiteral Documentation
I installed Crypto++ on BeagleBone black by following native installation method provided at ARM (Command Line). Then when I ran cryptest.sh, it runs smoothly.
Here is my small test-code to test Crypto++ functionality.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "libcryptoWrapperGCM.h"
int main(int argc,char* argv[]){
byte key[] = "123456789012345";
byte vi[] = "123456789012345";
char p[205] = { 0x00, 0x01, 0x00, 0x25, 0x00, 0x00, 0x00, 0x09,
0x00, 0x01, 0x00, 0x05, 0x6f, 0x74, 0x6f, 0x63, 0x6e, 0xf0, 0x01, 0x00, 0x14, 0x00, 0x01, 0x00,
0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x31, 0x00, 0x01, 0x00, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d,
0x32, 0xf0, 0x02, 0x00, 0xa0, 0x00, 0x23, 0x00, 0x40, 0x38, 0x34, 0x32, 0x33, 0x32, 0x30, 0x33,
0x34, 0x62, 0x66, 0x61, 0x36, 0x38, 0x38, 0x32, 0x38, 0x30, 0x64, 0x37, 0x36, 0x38, 0x35, 0x64,
0x39, 0x66, 0x62, 0x62, 0x65, 0x36, 0x34, 0x32, 0x37, 0x36, 0x37, 0x36, 0x33, 0x34, 0x61, 0x64,
0x36, 0x34, 0x37, 0x32, 0x66, 0x66, 0x37, 0x33, 0x63, 0x33, 0x34, 0x32, 0x65, 0x63, 0x34, 0x32,
0x35, 0x38, 0x31, 0x35, 0x37, 0x66, 0x61, 0x33, 0x61, 0x00, 0x24, 0x00, 0x40, 0x36, 0x35, 0x30,
0x38, 0x63, 0x39, 0x31, 0x36, 0x63, 0x37, 0x39, 0x30, 0x63, 0x66, 0x37, 0x33, 0x34, 0x61, 0x31,
0x64, 0x34, 0x35, 0x65, 0x30, 0x32, 0x62, 0x30, 0x64, 0x39, 0x61, 0x39, 0x65, 0x37, 0x35, 0x36,
0x37, 0x34, 0x66, 0x36, 0x64, 0x61, 0x62, 0x31, 0x63, 0x61, 0x30, 0x32, 0x64, 0x31, 0x64, 0x66,
0x64, 0x37, 0x39, 0x63, 0x66, 0x39, 0x35, 0x38, 0x35, 0x64, 0x36, 0x31, 0x33, 0x00, 0x25, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x57, 0x16, 0x69, 0x13, 0x00, 0x26, 0x00, 0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x06, 0x0b, 0x94};
char *p_data_ptr = p;
byte *ebuffer = malloc(205+12);
char *d = GcmEncode(key,vi, p_data_ptr,205,ebuffer,0);
int i ;
for(i=0; i< 205+12; i++){
printf("0x%02x, ",ebuffer[i] & 0xff);
}
char *buffer = malloc(205);
char* decodedMessage = GcmDecode(key, vi,ebuffer, 217,buffer,0);
return 1;
}
When I run the program I get Exception:
terminate called after throwing an instance of 'CryptoPP::InvalidKeyLength'
what(): AES: 4 is not a valid key length
Aborted
The odd thing is that it only happens on BeagleBone Black and not x86 machine, even though both have Ubuntu 14.04.
Any idea how to solve this issue? Thank you.
Edit 1:
Here is the wrapper source:
#include "gcm.h"
using CryptoPP::GCM;
#include "assert.h"
extern "C" {
const char* GcmEncode(byte key_hash[],byte iv_hash[], byte* p_data_ptr,size_t length,uint8_t* destination, int offset) {
string cipher,encoded;
encoded.clear();
GCM< AES >::Encryption e;
e.SetKeyWithIV( key_hash, sizeof(key_hash), iv_hash, sizeof(iv_hash) );
const int TAG_SIZE = 16;
StringSource(p_data_ptr,length, true,
new AuthenticatedEncryptionFilter( e,
new StringSink( cipher ), false, TAG_SIZE)
);
encoded.clear();
StringSource(cipher, true,
new HexEncoder(new StringSink( encoded ))
);
memcpy(&(destination[offset]),cipher.c_str(),length+12);
return cipher.c_str();//NEED TO BE FREED IN FOLLOWING METHOD
}
const char *GcmDecode(byte key_hash[], byte iv_hash[], byte *cipher_ptr,size_t length, uint8_t* destination, int offset){
size_t pLength = length - 16;
string rpdata;
const int TAG_SIZE = 16;
GCM< AES >::Decryption d;
d.SetKeyWithIV(key_hash, sizeof(key_hash), iv_hash, sizeof(iv_hash));
AuthenticatedDecryptionFilter df(d,new StringSink( rpdata ),
AuthenticatedDecryptionFilter::DEFAULT_FLAGS,
TAG_SIZE
);
StringSource(cipher_ptr,length, true,new Redirector(df));
bool b = df.GetLastResult();
if( true == b ){
memcpy(&(destination[offset]),rpdata.c_str(),pLength);
return NULL;//NEED TO BE FREED IN FOLLOWING METHOD
}
else{
return NULL;
}
}
}
And this is the valgrind output:
==26349==
terminate called after throwing an instance of 'CryptoPP::InvalidKeyLength'
what(): AES: 4 is not a valid key length
==26349==
==26349== HEAP SUMMARY:
==26349== in use at exit: 462 bytes in 6 blocks
==26349== total heap usage: 11 allocs, 5 frees, 562 bytes allocated
==26349==
==26349== LEAK SUMMARY:
==26349== definitely lost: 0 bytes in 0 blocks
==26349== indirectly lost: 0 bytes in 0 blocks
==26349== possibly lost: 201 bytes in 3 blocks
==26349== still reachable: 261 bytes in 3 blocks
==26349== suppressed: 0 bytes in 0 blocks
==26349== Rerun with --leak-check=full to see details of leaked memory
==26349==
==26349== For counts of detected and suppressed errors, rerun with: -v
==26349== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Aborted
ubuntu#arm:/home/otocn/Daemon$ valgrind --leak-check=full ./a.out
==26350== Memcheck, a memory error detector
==26350== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==26350== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==26350== Command: ./a.out
==26350==
terminate called after throwing an instance of 'CryptoPP::InvalidKeyLength'
what(): AES: 4 is not a valid key length
==26350==
==26350== HEAP SUMMARY:
==26350== in use at exit: 462 bytes in 6 blocks
==26350== total heap usage: 11 allocs, 5 frees, 562 bytes allocated
==26350==
==26350== 16 bytes in 1 blocks are possibly lost in loss record 2 of 6
==26350== at 0x482E9E0: operator new(unsigned int) (vg_replace_malloc.c:318)
==26350== by 0x4C85191: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x4C86289: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x4C86569: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x49BA727: __static_initialization_and_destruction_0 (cryptlib.cpp:52)
==26350== by 0x49BA727: _GLOBAL__sub_I.00276_cryptlib.cpp (cryptlib.cpp:939)
==26350== by 0x400B19D: call_init (dl-init.c:78)
==26350== by 0x400B22F: _dl_init (dl-init.c:126)
==26350== by 0x4000BF1: ??? (in /lib/arm-linux-gnueabihf/ld-2.19.so)
==26350==
==26350== 45 bytes in 1 blocks are possibly lost in loss record 4 of 6
==26350== at 0x482E9E0: operator new(unsigned int) (vg_replace_malloc.c:318)
==26350== by 0x4C85191: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x4C859E5: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned int) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x4C85A57: std::string::reserve(unsigned int) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x4C85C13: std::string::append(char const*, unsigned int) (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350== by 0x49C4663: append (basic_string.h:1009)
==26350== by 0x49C4663: operator+<char, std::char_traits<char>, std::allocator<char> > (basic_string.h:2406)
==26350== by 0x49C4663: CryptoPP::InvalidKeyLength::InvalidKeyLength(std::string const&, unsigned int) (simple.h:50)
==26350== by 0x49C2E2D: CryptoPP::SimpleKeyingInterface::ThrowIfInvalidKeyLength(unsigned int) (cryptlib.cpp:119)
==26350== by 0x49C2E79: CryptoPP::SimpleKeyingInterface::SetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&) (cryptlib.cpp:102)
==26350== by 0x4A23D8D: CryptoPP::GCM_Base::SetKeyWithoutResync(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&) (gcm.cpp:138)
==26350== by 0x49C8F41: CryptoPP::AuthenticatedSymmetricCipherBase::SetKey(unsigned char const*, unsigned int, CryptoPP::NameValuePairs const&) (authenc.cpp:53)
==26350== by 0x49C2CB3: CryptoPP::SimpleKeyingInterface::SetKeyWithIV(unsigned char const*, unsigned int, unsigned char const*, unsigned int) (cryptlib.cpp:113)
==26350== by 0x49BAAB7: GcmEncode (in /home/otocn/Daemon/libcryptoc.so)
==26350==
==26350== 140 bytes in 1 blocks are possibly lost in loss record 5 of 6
==26350== at 0x482E358: malloc (vg_replace_malloc.c:296)
==26350== by 0x4C46081: __cxa_allocate_exception (in /usr/lib/arm-linux-gnueabihf/libstdc++.so.6.0.19)
==26350==
==26350== LEAK SUMMARY:
==26350== definitely lost: 0 bytes in 0 blocks
==26350== indirectly lost: 0 bytes in 0 blocks
==26350== possibly lost: 201 bytes in 3 blocks
==26350== still reachable: 261 bytes in 3 blocks
==26350== suppressed: 0 bytes in 0 blocks
==26350== Reachable blocks (those to which a pointer was found) are not shown.
==26350== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==26350==
==26350== For counts of detected and suppressed errors, rerun with: -v
==26350== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
Aborted
Is it possible to add an binary data file together with a Arduino sketch that is transferred when sending it over to the Arduino? I manage to add the file in the IDE and it was copied to a "data" directory in my project folder but I can't find a way to access it in my code.
I'm just interested to send one file for testing purposes and don't want to use SD cards or network. I'm using the Arduino-Uno.
You can convert the binary data to a C array, and insert that into the code/source which will allow you to reference it.
A tool like Hexworkshop or other binary file editor should have an option to do this for you.
You could check this answer as well.
Example output:
unsigned char data[84] = {
0x02, 0x00, 0x41, 0x8E, 0x08, 0x8F, 0x09, 0x85, 0x09, 0x82, 0x85, 0x08, 0x83, 0xE0, 0xFE, 0xA3,
0xE0, 0x8E, 0x0A, 0xF5, 0x0B, 0x4E, 0x70, 0xEF, 0x85, 0x09, 0x82, 0x85, 0x08, 0x83, 0xE0, 0xFE,
0xA3, 0xE0, 0xFF, 0x7C, 0x00, 0x7D, 0x00, 0x02, 0x00, 0x00, 0x7B, 0x00, 0x7A, 0x00, 0x79, 0x01,
0x80, 0x06, 0x7B, 0x00, 0x7A, 0x00, 0x79, 0x00, 0x8A, 0x0A, 0x89, 0x0B, 0xE9, 0x4A, 0x70, 0xD8,
0x22, 0x78, 0x7F, 0xE4, 0xF6, 0xD8, 0xFD, 0x75, 0x81, 0x0B, 0x02, 0x00, 0x4D, 0x7F, 0x00, 0x7E,
0x80, 0x02, 0x00, 0x03,
} ;
UPDATE: I can't even get this calculator to reproduce the SMBus PECs illustrated in figures 8 and 9 of this datasheet!
So I'm interfacing an arduino with a Melexis temperature sensor, and it's going okay--aside from the fact that I can't seem to get the CRC check to work.
I've gotten read operations to complete successfully (although my software ignores the packet error code) but I have tried a lot of implementations of CRC8 to check the PEC byte to no avail. The code block I am using now came from OneWire:
uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
while (len--) {
uint8_t inbyte = *addr++;
for (uint8_t i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix) crc ^= 0x8C;
inbyte >>= 1;
}
}
return crc;
}
I rewrote it to consider just the one byte:
int smbCRC(int message) {
uint8_t crc = 0;
uint8_t inbyte = message & 0xFF;
for (uint8_t i = 8; i; i--) {
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix) crc ^= 0x8C;
inbyte >>= 1;
}
return crc;
}
But its CRC does not match that of the MLX datasheet (Figure 8 from here for example). When I print an int with its CRC8 like so:
int message = 0x3aD2;
lcd.print(String(message,HEX) + " " + String(smbCRC(message),HEX));
I get back "3ad2 eb", though the datasheet says the correct PEC is 0x30. Where am I going wrong? It seems like this could be caused by a bad implementation of CRC or bad assumptions on my part about the CRC input, and I'm not sure where to start troubleshooting.
I haven't checked your CRC implementation but there is a mistake in the MLX datasheet or at least it's badly written. You have to include all the I2C frame's data for the PEC's calculation not just the replied data.
For a read word command you have to include [SA_W, Command, SA_R, LSB, MSB] and for a write word command [SA_W, Command, LSB, MSB].
So, for their first example the calculation must be made on [ 0xB4, 0x07, 0xB5, 0xD2, 0x3A ] and not just on [ 0xD2, 0x3A ] and this way you get the expected 0x30.
Here is a simple C implementation of the CRC with a lookup table (non Arduino but it must be quite simple to adapt):
static const uint8_t crc_table[] = {
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31,
0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9,
0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1,
0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe,
0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16,
0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80,
0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8,
0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10,
0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f,
0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7,
0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef,
0xfa, 0xfd, 0xf4, 0xf3
};
uint8_t
crc8(uint8_t *p, uint8_t len)
{
uint16_t i;
uint16_t crc = 0x0;
while (len--) {
i = (crc ^ *p++) & 0xFF;
crc = (crc_table[i] ^ (crc << 8)) & 0xFF;
}
return crc & 0xFF;
}
It is possible to get the compiler to figure out the lookup table for you, as follows:
#include <stdio.h>
#include <stdint.h>
/* * *
* Just change this define to whatever polynomial is in use
*/
#define CRC1B(b) ( (uint8_t)((b)<<1) ^ ((b)&0x80? 0x07 : 0) ) // MS first
/* * *
* 8+1 entry enum lookup table define
*/
#define CRC(b) CRC_##b // or CRC8B(b)
enum {
CRC(0x01) = CRC1B(0x80),
CRC(0x02) = CRC1B(CRC(0x01)),
CRC(0x04) = CRC1B(CRC(0x02)),
CRC(0x08) = CRC1B(CRC(0x04)),
CRC(0x10) = CRC1B(CRC(0x08)),
CRC(0x20) = CRC1B(CRC(0x10)),
CRC(0x40) = CRC1B(CRC(0x20)),
CRC(0x80) = CRC1B(CRC(0x40)),
// Add 0x03 to optimise in CRCTAB1
CRC(0x03) = CRC(0x02)^CRC(0x01)
};
/* * *
* Build a 256 byte CRC constant lookup table, built from from a reduced constant
* lookup table, namely CRC of each bit, 0x00 to 0x80. These will be defined as
* enumerations to take it easy on the compiler. This depends on the relation:
* CRC(a^b) = CRC(a)^CRC(b)
* In other words, we can build up each byte CRC as the xor of the CRC of each bit.
* So CRC(0x05) = CRC(0x04)^CRC(0x01). We include the CRC of 0x03 for a little more
* optimisation, since CRCTAB1 can use it instead of CRC(0x01)^CRC(0x02), again a
* little easier on the compiler.
*/
#define CRCTAB1(ex) CRC(0x01)ex, CRC(0x02)ex, CRC(0x03)ex,
#define CRCTAB2(ex) CRCTAB1(ex) CRC(0x04)ex, CRCTAB1(^CRC(0x04)ex)
#define CRCTAB3(ex) CRCTAB2(ex) CRC(0x08)ex, CRCTAB2(^CRC(0x08)ex)
#define CRCTAB4(ex) CRCTAB3(ex) CRC(0x10)ex, CRCTAB3(^CRC(0x10)ex)
#define CRCTAB5(ex) CRCTAB4(ex) CRC(0x20)ex, CRCTAB4(^CRC(0x20)ex)
#define CRCTAB6(ex) CRCTAB5(ex) CRC(0x40)ex, CRCTAB5(^CRC(0x40)ex)
/* * *
* This is the final lookup table. It is rough on the compiler, but generates the
* required lookup table automagically at compile time.
*/
static const uint8_t crc_table[256] = { 0, CRCTAB6() CRC(0x80), CRCTAB6(^CRC(0x80)) };
uint8_t crc8(uint8_t *p, uint8_t len)
{
uint8_t crc = 0x0;
while (len--) {
crc = crc_table[crc ^ *p++];
}
return crc;
}
void main( void )
{
int i, j;
printf("static const uint8_t crc_table[] = {");
for (i = 0; i < 0x10; i++)
{
printf("\n ");
for (j = 0; j < 0x10; j++)
{
printf( " 0x%02x,", crc_table[i*0x10+j] );
}
}
printf("\n};\n\n");
}
See reference on following links:
http://www.melexis.com/Asset/SMBus-communication-with-MLX90614-DownloadLink-5207.aspx
http://www.sbs-forum.org/marcom/dc2/20_crc-8_firmware_implementations.pdf
Function code for Arduino:
byte c8( byte x ){
for( byte i = 8; i--; ) {
x = ( x << 1 ) ^ ( x & 128 ? 7 : 0 );
}
return x;
}
void setup() {
Serial.begin( 9600 );
int msg = 0x3AD2;
Serial.print( '0x' );
Serial.print( c8( msg ), HEX );
// '0x30' is displayed on Serial Monitor
}
void loop() {}
Since argument x is strong-typed as byte (uint8_t), word-size data (such 0x3AD2) will be truncated to byte-size (which is 0xD2 in case of 0x3AD2).
Stripped code from linux kernel code, i am using it for smbus pec calculation..
#include<stdio.h>
#define POLY (0x1070U << 3)
#define DST_SLAVE_ADDRESS 0x1d //7-bit Slave Address
static unsigned char crc8(short int data)
{
int i;
for (i = 0; i < 8; i++) {
if (data & 0x8000)
data = data ^ POLY;
data = data << 1;
}
return (unsigned char)(data >> 8);
}
unsigned char i2c_smbus_pec(unsigned char crc, unsigned char *p, int count)
{
int i;
for (i = 0; i < count; i++)
crc = crc8((crc ^ p[i]) << 8);
return crc;
}
/* Assume a 7-bit address, which is reasonable for SMBus */
static unsigned char i2c_smbus_msg_pec(unsigned char slave_addr, char *msg, int len)
{
/* The address will be sent first */
unsigned char addr = slave_addr << 1;
int pec;
pec = i2c_smbus_pec(0, &addr, 1);
/* The data buffer follows */
return i2c_smbus_pec(pec, msg, len);
}
main()
{
//SMBUS PEC include everthing include dst slave address
//dst i2c address here is 0x1d (7bit) , 0x3a (8bit)
char msg[] = {0x0F,0x19,0x21,0x01,0x00,0x00,0xEB,0x84,0x08,0x00,0x00,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xAA,0xEF,0x81,0xB4};
//dst i2c address here is 0x10 (7bit) , 0x20 (8bit)
//char msg[] = { 0x0F,0x19,0x3B,0x01,0x00,0x00,0xD3,0x84,0x88,0x00,0x00,0x00,
// 0x00,0x00,0x00,0x30,0xFF,0xEF,0x00,0x00,0x00,0x00,0x00,0x21,0xE5,0xD5,0xA8};
int len = sizeof(msg);
int calc_pec = i2c_smbus_msg_pec(DST_SLAVE_ADDRESS, msg , len);
printf("pec = 0x%x\n", calc_pec);
}