sodnpoo.com

salsa20

Here's an implementation of Salsa20 by DJ Bernstein in pascal. There's a unit salsa20.pas and a test unit handily named testsalsa20.pas. Built and tested using freepascal .

salsa20.pas:

unit salsa20;

interface

uses
  sysutils;

type TSalsa20Word = array[0..15] of longword;

type TSalsa20Key = array[0..31] of byte;

type TArrayOfByte = array of byte;

procedure Salsa20KeySetup(var KeyState : TSalsa20Word; key : TSalsa20Key);

procedure Salsa20IvSetup(var KeyState : TSalsa20Word; Iv1, Iv2 : longword);

function Salsa20Encrypt(var KeyState : TSalsa20Word; msg : TArrayOfByte): TArrayOfByte;

function FourBytesToLongword( b0, b1, b2, b3 : byte ): longword;

procedure LongwordToFourBytes( lw : longword;  var b0, b1, b2, b3 : byte); 

function Salsa20Crypt(Key : TSalsa20Key; Iv1, Iv2 : longword; msg : TArrayOfByte) : TArrayOfByte;

implementation

function FourBytesToLongword( b0, b1, b2, b3 : byte ): longword;
var
  lw : longword;
  lwa : array[0..3] of byte;
begin
  lwa[0] := b0;
  lwa[1] := b1;
  lwa[2] := b2;
  lwa[3] := b3;
  move( lwa[0], lw, SizeOf(longword) );
  FourBytesToLongword := lw;
end;

procedure LongwordToFourBytes( lw : longword; var b0,b1,b2,b3 : byte );
var
  lwa : array[0..3] of byte;
begin
  move( lw, lwa[0], SizeOf(longword) );
  b0 := lwa[0];
  b1 := lwa[1];
  b2 := lwa[2];
  b3 := lwa[3];
end;

function R(a, b : longword):longword;
begin
  R := ((a shl b) or (a shr (32 - b)));
end;

function Salsa20WordSpec(InWord : TSalsa20Word): TSalsa20Word;
var
  x : TSalsa20Word;
  i : integer;
begin
  x := InWord;

  i := 20;
  repeat
  begin
//    writeln('i:'+IntToStr(i));
    x[ 4] := x[ 4] xor R(x[ 0] + x[12],  7);
    x[ 8] := x[ 8] xor R(x[ 4] + x[ 0],  9);

    x[12] := x[12] xor R(x[ 8] + x[ 4], 13);
    x[ 0] := x[ 0] xor R(x[12] + x[ 8], 18);  

    x[ 9] := x[ 9] xor R(x[ 5] + x[ 1],  7);
    x[13] := x[13] xor R(x[ 9] + x[ 5],  9);

    x[ 1] := x[ 1] xor R(x[13] + x[ 9], 13);
    x[ 5] := x[ 5] xor R(x[ 1] + x[13], 18);

    x[14] := x[14] xor R(x[10] + x[ 6],  7);
    x[ 2] := x[ 2] xor R(x[14] + x[10],  9);

    x[ 6] := x[ 6] xor R(x[ 2] + x[14], 13);
    x[10] := x[10] xor R(x[ 6] + x[ 2], 18);

    x[ 3] := x[ 3] xor R(x[15] + x[11],  7);
    x[ 7] := x[ 7] xor R(x[ 3] + x[15],  9);

    x[11] := x[11] xor R(x[ 7] + x[ 3], 13);
    x[15] := x[15] xor R(x[11] + x[ 7], 18);

    x[ 1] := x[ 1] xor R(x[ 0] + x[ 3],  7);
    x[ 2] := x[ 2] xor R(x[ 1] + x[ 0],  9);

    x[ 3] := x[ 3] xor R(x[ 2] + x[ 1], 13);
    x[ 0] := x[ 0] xor R(x[ 3] + x[ 2], 18);

    x[ 6] := x[ 6] xor R(x[ 5] + x[ 4],  7);
    x[ 7] := x[ 7] xor R(x[ 6] + x[ 5],  9);

    x[ 4] := x[ 4] xor R(x[ 7] + x[ 6], 13);
    x[ 5] := x[ 5] xor R(x[ 4] + x[ 7], 18);

    x[11] := x[11] xor R(x[10] + x[ 9],  7);
    x[ 8] := x[ 8] xor R(x[11] + x[10],  9);

    x[ 9] := x[ 9] xor R(x[ 8] + x[11], 13);
    x[10] := x[10] xor R(x[ 9] + x[ 8], 18);

    x[12] := x[12] xor R(x[15] + x[14],  7);
    x[13] := x[13] xor R(x[12] + x[15],  9);

    x[14] := x[14] xor R(x[13] + x[12], 13);
    x[15] := x[15] xor R(x[14] + x[13], 18);

    i := i - 2;
  end
  until i < 1;

  for i := 0 to 15 do
  begin
    Salsa20WordSpec[i] := x[i] + InWord[i];
  end;

end;

procedure Salsa20IvSetup(var KeyState : TSalsa20Word; Iv1, Iv2 : longword);
begin
  KeyState[6] := Iv1;
  KeyState[7] := Iv2;
end;

procedure Salsa20KeySetup(var KeyState : TSalsa20Word; key : TSalsa20Key );
const
  sigma = 'expand 32-byte k';
begin
  KeyState[0] := 
FourbytesToLongword(byte(sigma[1]),byte(sigma[2]),byte(sigma[3]),byte(sigma[4]));

  KeyState[5] := 
FourBytesToLongword(byte(sigma[5]),byte(sigma[6]),byte(sigma[7]),byte(sigma[8]));

  KeyState[10] := 
FourBytesToLongword(byte(sigma[9]),byte(sigma[10]),byte(sigma[11]),byte(sigma[12]));

  KeyState[15] := 
FourBytesToLongword(byte(sigma[13]),byte(sigma[14]),byte(sigma[15]),byte(sigma[16]));

  KeyState[1] := FourBytesToLongword(key[0],key[1],key[2],key[3]);
  KeyState[2] := FourBytesToLongword(key[4],key[5],key[6],key[7]);
  KeyState[3] := FourBytesToLongword(key[8],key[9],key[10],key[11]);
  KeyState[4] := FourBytesToLongword(key[12],key[13],key[14],key[15]);
  KeyState[11] := FourBytesToLongword(key[16],key[17],key[18],key[19]);
  KeyState[12] := FourBytesToLongword(key[20],key[21],key[22],key[23]);
  KeyState[13] := FourBytesToLongword(key[24],key[25],key[26],key[27]);
  KeyState[14] := FourBytesToLongword(key[28],key[29],key[30],key[31]);  
end;

function Salsa20Encrypt(var KeyState : TSalsa20Word; msg : TArrayOfByte):TArrayOfByte;
var
  output : TSalsa20Word;
  cursor, i, Len, AlignedLen : longword;
  b1, b2, b3, b4 : byte;
begin
  if Length(msg) > 0 then
  begin
    KeyState[8] := 0;
    KeyState[9] := 0;

    Len := Length(msg);
    AlignedLen := (Len+(64-1)) and (not(64-1));

    SetLength(msg, AlignedLen);
    SetLength(Salsa20Encrypt, AlignedLen);

    cursor:=0;
    repeat
    begin
      output := Salsa20WordSpec( KeyState );

      i:=0;
      repeat
      begin
        LongwordToFourBytes( output[i], b1, b2, b3, b4);

        Salsa20Encrypt[(i*4)+0+cursor] := msg[(i*4)+0+cursor] xor b1;
        Salsa20Encrypt[(i*4)+1+cursor] := msg[(i*4)+1+cursor] xor b2;
        Salsa20Encrypt[(i*4)+2+cursor] := msg[(i*4)+2+cursor] xor b3;
        Salsa20Encrypt[(i*4)+3+cursor] := msg[(i*4)+3+cursor] xor b4;

        Inc(i);
      end
      until i = 16; 
      cursor:=cursor+64;

      if KeyState[8] = $ffffffff then
      begin
        Inc( KeyState[9] );
      end;
      Inc( KeyState[8] );
    end
    until (cursor > Len-2);
    
    SetLength(Salsa20Encrypt, Len);
  end;
end;

function Salsa20Crypt(Key : TSalsa20Key; Iv1, Iv2 : longword; msg : TArrayOfByte) : TArrayOfByte;
var
  KeyState : TSalsa20Word;
begin
  Salsa20KeySetup(KeyState, Key);
  Salsa20IvSetup(KeyState, Iv1, Iv2);
  Salsa20Crypt := Salsa20Encrypt(KeyState, msg);
end;

end.

testsalsa20.pas:

program testsalsa20;

uses
  salsa20, sysutils;

function Salsa20WordToHex( Salsa20 : TSalsa20Word ): ansistring;
var
  i : integer;
  b0, b1, b2, b3 : byte;
begin
  Salsa20WordToHex := '';
  for i := 0 to 15 do
  begin
    Salsa20WordToHex := Salsa20WordToHex + ' $' + IntToHex(Salsa20[i],8);
    LongwordToFourBytes( Salsa20[i], b0, b1, b2, b3 );
    Salsa20WordToHex := Salsa20WordToHex + ' ' + IntToStr(b0) + 
    ' ' +IntToStr(b1) + ' ' + IntToStr(b2) + ' ' + IntToStr(b3);
  end;
end;

function TArrayOfByteToHex( ByteArray : TArrayOfByte ): ansistring;
var
  i : longword;
begin
  for i := 0 to Length(ByteArray)-1 do
  begin
    TArrayOfByteToHex:=TArrayOfByteToHex + ' $' + IntToHex(ByteArray[i],2);
  end;
end;

const
  INLEN = 1000;
var
// KeyState, KeyState2 : TSalsa20Word;
 Key : TSalsa20Key;
 InBuf, OutBuf, Golden : TArrayOfByte;
 i : longword;
begin
  SetLength( InBuf, INLEN );  
  for i := 0 to INLEN-1 do
  begin
    if Odd(i) = true then
    begin
      InBuf[i] := $00;
    end
    else
    begin
      InBuf[i] := $ff;
    end;
  end;

  for i := 0 to 15 do
  begin
    Key[i] := i+1;
    Key[i+16] := 201 + i; 
  end;

  writeln('InBuf:');
  writeln( TArrayOfByteToHex( InBuf) );

  OutBuf := Salsa20Crypt(Key, $01234567, $76543210, InBuf);

  writeln('OutBuf:');
  writeln( TArrayOfByteToHex( OutBuf) );

  Golden := Salsa20Crypt(Key, $01234567, $76543210, OutBuf);

  writeln('golden:');
  writeln( TArrayOfByteToHex( Golden ) );

end.

Enjoy! ;-)