1
$\begingroup$

In RFC 1320, It is stated that "A 64-bit representation of b (the length of the message before the padding bits were added) is appended to the result of the previous step. In the unlikely event that b is greater than 2^64, then only the low-order 64 bits of b are used. (These bits are appended as two 32-bit words and appended low-order word first in accordance with the previous conventions.)".

I have three Java implementation of MD4, and each of them first shifts the lenght 3-bit to left. I could not understand why? I am sure it is a Java related issue, but I do not know what causes that?

Implementation 1: https://svn.apache.org/repos/asf/mina/mina/branches/2.0/mina-core/src/main/java/org/apache/mina/proxy/utils/MD4.java

Implementation 2: https://llvm.org/svn/llvm-project/llvm-gcc-4.2/trunk/libjava/classpath/gnu/java/security/hash/MD4.java

You can google for more implementations, I could not enter more than 2 links because of my reputation.

Regards, Bünyamin.

$\endgroup$
1
  • 4
    $\begingroup$ This is a basic Computer Science question, unrelated to cryptography. The left shift by 3 converts the length in octets previously accumulated by the Java code into the length in bits prescribed by the quoted text of RFC 1320, in order to form the padding. $\endgroup$
    – fgrieu
    Jan 22, 2015 at 7:19

1 Answer 1

1
$\begingroup$

This is not a Java-related issue. All of these implementations are doing what the RFC says. Here are the relevant parts of RFC 1320 (emphasis mine):

3.1 Step 1. Append Padding Bits

The message is "padded" (extended) so that its length (in bits) is congruent to 448, modulo 512. (…)

3.2 Step 2. Append Length

A 64-bit representation of b (the length of the message before the padding bits were added) is appended to the result of the previous step. (…)

The length in §3.2 is again the length of the message in bits. All the Java implementations you've seen calculate the length in bits from the length in bytes. One byte is 8 bits in Java (like on almost every platform, but there are platforms that group bits in bytes of different sizes), so the length in bits is the length in bytes multiplied by 8. Shifting to the left by 3 is equivalent to multiplying by 8.

It is common in cryptographic algorithms to consider the data as a stream of bits, and in particular express lengths in bits. The fact that bits may be arranged in groups of 8 called bytes is an implementation detail that does not influence the behavior of the algorithm.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.