I had a quick look at some functions and have some remarks.
function ROR32(Data: LongWord; N: LongWord): LongWord;
begin
Result := ((Data shr N) or (Data shl (32 - N)));
end;
Why using N:Longword and not N: Byte ?
function AddWrap32(X: LongWord; Y: LongWord): LongWord;
begin
Result := (X + Y) mod 4294967295; // Upper-bound of 32-bit unsigned int is the modulus
end;
This makes no sense to me. X+Y will never exceed High(LongWord) anyway. what you wrote here is something like:
begin
Result := X + Y;
if Result = High(LongWord) then Result := 0;
end;
Reading para 3.2, they talk about 2^w and that is $100000000 and not $0FFFFFFFF
So the mod is not needed.
function Ch32(X, Y, Z: LongWord): LongWord;
begin
Result := (X and Y) xor (not X and Z);
end;
It's not clear from the document is the not should operate on X or on (X and Z), but I guess you're right.
K: array[0..63] of LongWord = (
($428a2f98), ($71374491), ($b5c0fbcf), ($e9b5dba5), ($3956c25b), ($59f111f1), ($923f82a4), ($ab1c5ed5),
....
you don't need to put () around the constants, but it doesn't hurt.
My advice, avoid using lowercase L as variable, it is very easy to misinterpret as 1 (one)
var
W: array [0..63] of LongWord;
begin
FillByte(W, length(W), 0);
....
Lenght(W) returns the length of the array, 64 in this case. What you want here is either
FillByte(W, SizeOf(W), 0);
or
FillLongWord(W, Length(W), 0);
for i := 0 to length(Plaintext) - 1 do begin
BPlain[ i ] := NtoBE(Ord(Plaintext[i+1]));
end;
BPlain[ i ] is a Byte, Plaintext[i+1] is a Char, Ord(Char) is a Byte. There is no need to convert a Byte to little of bigendean. There doesn't exist a NtoBE(Byte) version, so I fear the compiler converts it to a NtoBE(Word) call, resulting in the value 0 in your case.
BPlain[high(BPlain[ i ])] := $80;
No clue what you are trying to do here. BPlain[ i ] is a Byte, High(Byte) equals to 255, so you effectively wrote
Besides that you refer to the for loop variable i, which is undefined outside the for loop.
Congruent := false;
while (not Congruent) do begin
if (((length(BPlain) * 8 ) mod 512) = 448 ) then begin
Congruent := true;
break;
end
else begin
SetLength(BPlain, length(BPlain) + 1);
BPlain[high(BPlain)] := 0;
end;
end;
this can way easier...
The combo congruent := true and break is double. By breaking you are already outside the loop, so no need to set it to true.
I'm to lazy to verify, but I guess that the extra length needed is
(64 - ((length(BPlain)+8 ) mod 64) )mod 64
Move(l, BPlain[high(BPlain) - sizeof(l)], sizeof(l)); // Copy the message length into the message buffer
Here you copy the lenght in bigendean order to the end of the buffer ?
(here I stopped)