0

I would like to create block for MD5 algorithm. This is my code:

uint8_t buffer[64];
for (int i = size + 1; i < 56; i++)
    buffer[i] = 0;

But I don't know how can I add message length on the last 64bits. Any idea?

Bakus123
  • 1,399
  • 6
  • 21
  • 40

1 Answers1

1

You declared buffer of length 8, how come you expect this to work:

for (int i = size + 1; i < 56; i++)
    buffer[i] = 0;

Out of bounds access?

Ok you said from 56th element you want to store length. Why not do something like this:

memcpy(&buffer[56], &length, sizeof(int)); // you may want to check sizeof(int) on your machine to know how many bytes were written

but you should be careful about endianness. This will write the length according to the endianness your machine has. If you want to store it in some particular endianness maybe you can do it manually using bit wise operators. Check some more info here, and here

If you want length to have fixed size, you can declare it as uint32_t and use 4 as last parameter in memcpy directly.

Community
  • 1
  • 1
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • @Bakus123: what is value of `size`? – Giorgi Moniava Apr 08 '15 at 17:41
  • Size = message length (from 1 to 10). It's integer. – Bakus123 Apr 08 '15 at 17:42
  • @Bakus123: I am sorry the question as it stands now I don't exactly understand what you want to achieve. btw md5 output length is 16 bytes; if you can make it more clear .. – Giorgi Moniava Apr 08 '15 at 17:45
  • I want create 512bit block where first bits = message, next is 0x80 and zeros until 448 bit and next(last bits) = message length. – Bakus123 Apr 08 '15 at 17:49
  • How can I do the same using bitwise operators (I use little endian)? Because i write program on GPU and I'm not sure if memory references it's good idea for me... – Bakus123 Apr 08 '15 at 18:57
  • In your code I get error - "subscript out of range" – Bakus123 Apr 08 '15 at 18:58
  • 1
    @Bakus123: please check that link for endianness I gave. about out of range that should not happen your buffer is 64 bytes so there should be space for 8 more bytes. please look at how memcpy is used also – Giorgi Moniava Apr 08 '15 at 19:01
  • @Bakus123:I added another link about endianness in answer – Giorgi Moniava Apr 08 '15 at 19:13
  • Thx for link. I received error: "subscript out of range" because I had buffer[8]. But I have one problem yet. When I use uint8_t *bytes = (uint8_t*)malloc(64); it works but when I use uint8_t bytes[64]; it doesn't works. What is different? I prefer second way because og GPU I shouldn't use malloc. – Bakus123 Apr 08 '15 at 19:25
  • I get wrong MD5 hash.I'm checking it accurately now. – Bakus123 Apr 08 '15 at 19:28
  • @Bakus123: It really should not have to do with whether you allocate it as pointer (first example) or on the stack(uint8_t bytes[64]); usage is mostly similar – Giorgi Moniava Apr 08 '15 at 19:29
  • I made pritf in loop and in case without malloc on 61 and 62 bits are values other tan 0 but I don't know why... the rest is the same – Bakus123 Apr 08 '15 at 19:36
  • @Bakus123: really can't tell maybe you can ask other question about that; but like I said their usage is mostly similar you should not get that difference – Giorgi Moniava Apr 08 '15 at 19:37