C# HMACSHA1 Hash is not Right - cryptography

I'm trying to decrypt a file. First 2 steps:
Copy the first 16 bytes of the file to a buffer. This is the HMAC-SHA1 hash of the file which is made using one of the keys above.
Use HMAC-SHA1 on that buffer with a key from above to create the RC4 key, which is 0x10 bytes.
My code is:
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
BinaryReader binread = new BinaryReader(File.Open(ofd.FileName, FileMode.Open));
byte[] RetailKey = { 0xE1, 0xBC, 0x15, 0x9C, 0x73, 0xB1, 0xEA, 0xE9, 0xAB, 0x31, 0x70, 0xF3, 0xAD, 0x47, 0xEB, 0xF3 };
HMACSHA1 SHA = new HMACSHA1(RetailKey); //Initalize HMAC w/ retail or development key
byte[] buffer = binread.ReadBytes(16);
buffer = SHA.ComputeHash(buffer);
MessageBox.Show(buffer.Length.ToString());
As you can see, it says buffer has to be 10 bytes but messagebox says it is 20 bytes.Where is my mistake?

SHA-1 and thus HMAC-SHA-1 outputs 20 bytes.
You only need 16 (0x10) of them so you need to truncate. For example with byte[] key = hmacSha1.ComputeHash(input).Take(16).ToArray().
The 0x in 0x10 is a prefix denoting hexadecimal numbers in c (and derived languages). So 0x10 means 16 and not 10.

Related

Increase Report Size From 64 bytes to 256 bytes CUSTOM USB HID?

I want to transfer 256 bytes from host to stm32f103 device over USB Custom HID interface
Currently, 64 bytes Read/Write is Working
Following is the Report Descriptor in my stm32:
/** Usb HID report descriptor. */
static uint8_t CUSTOM_HID_ReportDesc_FS[USBD_CUSTOM_HID_REPORT_DESC_SIZE] __ALIGN_END ={
0x06,0x00,0xFF, //Usage Page 0xff00
0x09, 0x01, //USAGE (Pointer)
0xA1,0x01, //Collection (application)
//Input Report
0x19,0x01, //Usage Minimum
0x29,0x40, //Usage Minimum
0x15,0x00, //Logical Minimum
0x26,0xFF,0x00, //Logical Minimum
0x75,0x08, //report size : 8-bit field size
0x95, CUSTOM_HID_EPIN_SIZE,//Report count
0x81,0x02, //Input (data, array, Abs)
//Output Report
0x19,0x01, //usage Minimum
0x29,0x40, //usage Minimum
0x75,0x08, //report size : 8-bit field size
0x95,CUSTOM_HID_EPOUT_SIZE,//Report Count
0x91,0x02, //Output (data, array, Abs)
0xC0 //END_COLLECTION
};
If, I am changing the CUSTOM_HID_EPIN_SIZE,CUSTOM_HID_EPOUT_SIZE to 256, my USB device is not recognized.
I need some help to modify the report descriptor to support 256 byte transfer
Thanks

trouble with {base64 encoding of sha256 result} password generator

I'd like to encode a SHA-256 output (in hex) to give me a 16 character base64 string for password using purposes. base64 appears to not be what I would think it should be. Here's what I want.
(0) "00000000000000000000000000000000" -> "AAAAAAAAAAAAAAAA"
(64) "00000000000000000000000000000040" -> "AAAAAAAAAAAAAABA"
(255) "000000000000000000000000000000ff" -> "AAAAAAAAAAAAAAC/"
(2^16+16) "0000000000000000000000000000100f" -> "AAAAAAAAAAAAAPAP"
heres what I get
base64encode("00000000000000000000000000000000")
"MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAA"
This result is consistent with online converters, using R, etc.,
so obviously base64 is not what I think it is and I'm trying to do something else. If I'm not trying to "encode in base 64", what am I trying to do?
My favorite is:
> base64encode(64)
[1] "AAAAAAAAUEA="
> base64encode("64")
[1] "NjQA"
which just baffles me
Base64 works on byte arrays, so in your example, base64encode("00000000000000000000000000000000") is encoding the string value "000...", as a byte array. Since the byte value for the character "0" is 0x30, you're essentially encoding a byte array consisting of 32 0x30 bytes (and by the looks of it, a null terminator (0x00) at the end).
If you're trying to get a 16 character output, you need to encode a 12 byte input (since Base64 produces 4 characters of output for 3 bytes of input), e.g. (You don't give the language, so I'm guessing at the syntax for byte arrays as { 0x.., 0x.., ... }):
base64encode({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
= "AAAAAAAAAAAAAAAA"
base64encode({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01})
= "AAAAAAAAAAAAAAAB"
base64encode({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40})
= "AAAAAAAAAAAAAABA"
etc...
The SHA-xxx algorithms should naturally produce a byte-array output, so you should be able to take the appropriate number of bytes from it, and pass them to base64encode. If your SHA method produces a hex string output, then you'll need to convert the hex string back to a byte array before passing Base64 encoding.

Authentication with Mifare Ultralight C

I am wanting to authenticate with the Mifare Ultralight C tag, I have written the key into the tag already, the key is 0x"000102030405060708090A0B0C0D0E0F", so I have to write backward like this into pages of 0x2C to 0x2F
uint8_t buffer1[] = {0x07, 0x06, 0x05, 0x04};
uint8_t buffer2[] = {0x03, 0x02, 0x01, 0x00};
uint8_t buffer3[] = {0x0F, 0x0E, 0x0D, 0x0C};
uint8_t buffer4[] = {0x0B, 0x0A, 0x09, 0x08};
// write the data
success = nfc->mifareultralight_WritePage(0x2C, buffer1);
success &= nfc->mifareultralight_WritePage(0x2D, buffer2);
success &= nfc->mifareultralight_WritePage(0x2E, buffer3);
success &= nfc->mifareultralight_WritePage(0x2F, buffer4);`
But I don't know the command for authentication, can anyone tell me the authentication steps and commands to authenticate with this key?

Strange results for hard coded bytes in objective c

I'm debugging some CCCrypto code in Cocoa and I noticed the IV I'm hard setting (yes, I know it should be randomized) is giving me weird results when I debug.
This is my IV:
unsigned char iv[17] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x00};
And this is the contents of my memory after I step past the above line when debugging:
(lldb) p iv
(unsigned char [17]) $1 = "\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10"
Where do \a\b\t\n\v\f\r come from? I fully expected to see
\x07\x08\x09\x0a\x0b\x0c\x0d
Those are the normal representations of those character values in ASCII and UTF-8. Remember, these may be eight-bit integers, but they're being interpreted as characters in a string. The character with the value 7 is '\a', also known as the "bell character" (it is supposed to make your computer beep if you print it). The character with the value 8 is backspace, or '\b'. 9 is tab, or '\t'. Then come line feed, vertical tab, form feed and carriage return.

Byte array in objective-c

How can I create a byte array in Objective-C?
I have some data like this:0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, that I want to prepare as a byte array.
You can simply use plain C syntax, which is available in Objective-C:
const char myByteArray[] = {
0x12,0x23,0x34,0x45,
0x56,0x67,0x78,0x89,
0x12,0x23,0x34,0x45,
0x56,0x67,0x78,0x89 };
Either use the plain C solution given by mouviciel or read the class description of NSData and NSMutableData.