just a try for spritz (passing testvectors)
This commit is contained in:
parent
71b0945e0e
commit
176a4e7e44
90
src/main.adb
90
src/main.adb
|
@ -1,64 +1,70 @@
|
||||||
with Ada.Text_IO; use Ada.Text_IO;
|
with Ada.Text_IO; use Ada.Text_IO;
|
||||||
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
|
||||||
with Crypto_Types; use Crypto_Types;
|
|
||||||
with Crypto_Core_Types; use Crypto_Core_Types;
|
with Crypto_Core_Types; use Crypto_Core_Types;
|
||||||
|
with Crypto_Types; use Crypto_Types;
|
||||||
|
with Spritz;
|
||||||
use Crypto_Types.Crypto_Types_u8;
|
use Crypto_Types.Crypto_Types_u8;
|
||||||
|
|
||||||
|
|
||||||
procedure main is
|
procedure main is
|
||||||
|
|
||||||
procedure print_hex(value : in u8) is
|
procedure print_hex(value : in u8) is
|
||||||
hex_table : constant array (0 .. 15) of Character :=
|
hex_table : constant array (0 .. 15) of Character :=
|
||||||
( '0', '1', '2', '3',
|
( '0', '1', '2', '3',
|
||||||
'4', '5', '6', '7',
|
'4', '5', '6', '7',
|
||||||
'8', '9', 'A', 'B',
|
'8', '9', 'A', 'B',
|
||||||
'C', 'D', 'E', 'F');
|
'C', 'D', 'E', 'F');
|
||||||
begin
|
begin
|
||||||
Put(hex_table(Integer(Shift_Right(value, 4))));
|
Put(hex_table(Integer(Shift_Right(value, 4))));
|
||||||
Put(hex_table(Integer(value and 16#F#)));
|
Put(hex_table(Integer(value and 16#F#)));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure print_array_hex(A : in u8_Array) is
|
procedure test_spritz(s : in String) is
|
||||||
|
ctx : Spritz.Context;
|
||||||
|
z : u8;
|
||||||
begin
|
begin
|
||||||
for i in A'Range loop
|
Spritz.InitializeContext(ctx);
|
||||||
print_hex(A(i));
|
Spritz.Absorb(ctx, s);
|
||||||
Put(' ');
|
Put(s);
|
||||||
|
for i in 0 .. 6 - s'Length loop
|
||||||
|
Put(" ");
|
||||||
end loop;
|
end loop;
|
||||||
end;
|
Put(": ");
|
||||||
|
for j in 0 .. 7 loop
|
||||||
|
Spritz.Drip(ctx, z);
|
||||||
|
print_hex(z);
|
||||||
|
Put(" ");
|
||||||
|
end loop;
|
||||||
|
New_Line;
|
||||||
|
end test_spritz;
|
||||||
|
|
||||||
|
procedure test_spritz_hash(s : in String) is
|
||||||
|
ctx : Spritz.Context;
|
||||||
|
z : u8;
|
||||||
|
begin
|
||||||
|
Spritz.InitializeContext(ctx);
|
||||||
|
Spritz.Absorb(ctx, s);
|
||||||
|
Spritz.AbsorbStop(ctx);
|
||||||
|
Spritz.Absorb(ctx, u8(32));
|
||||||
|
Put(s);
|
||||||
|
for i in 0 .. 6 - s'Length loop
|
||||||
|
Put(" ");
|
||||||
|
end loop;
|
||||||
|
Put(": ");
|
||||||
|
for j in 0 .. 7 loop
|
||||||
|
Spritz.Drip(ctx, z);
|
||||||
|
print_hex(z);
|
||||||
|
Put(" ");
|
||||||
|
end loop;
|
||||||
|
New_Line;
|
||||||
|
end test_spritz_hash;
|
||||||
|
|
||||||
a, b : u8_Array(0 .. 255);
|
|
||||||
begin
|
begin
|
||||||
for i in a'Range loop
|
test_spritz("ABC");
|
||||||
a(i) := u8(i);
|
test_spritz("spam");
|
||||||
b(i) := u8(i);
|
test_spritz("arcfour");
|
||||||
end loop;
|
|
||||||
|
|
||||||
print_array_hex(a);
|
|
||||||
New_Line;
|
|
||||||
print_array_hex(b);
|
|
||||||
New_Line;
|
|
||||||
New_Line;
|
New_Line;
|
||||||
|
|
||||||
for i in 1 .. a'Length * u8'Size loop
|
test_spritz_hash("ABC");
|
||||||
a := Rotate_be(A => a, Amount => 1);
|
test_spritz_hash("spam");
|
||||||
Put("a: ");
|
test_spritz_hash("arcfour");
|
||||||
print_array_hex(a);
|
|
||||||
New_Line;
|
|
||||||
b := Rotate_be(A => b, Amount => i);
|
|
||||||
Put("b: ");
|
|
||||||
print_array_hex(b);
|
|
||||||
New_Line;
|
|
||||||
if (a /= b) then
|
|
||||||
Put("Error @ i=");
|
|
||||||
Put(i);
|
|
||||||
New_Line;
|
|
||||||
end if;
|
|
||||||
b := Rotate_be(A => b, Amount => -i);
|
|
||||||
Put("b': ");
|
|
||||||
print_array_hex(b);
|
|
||||||
New_Line;
|
|
||||||
end loop;
|
|
||||||
|
|
||||||
New_Line;
|
New_Line;
|
||||||
end main;
|
end main;
|
||||||
|
|
|
@ -0,0 +1,112 @@
|
||||||
|
package body Spritz is
|
||||||
|
|
||||||
|
procedure InitializeContext (ctx : out Context) is
|
||||||
|
begin
|
||||||
|
ctx.i := 0;
|
||||||
|
ctx.j := 0;
|
||||||
|
ctx.k := 0;
|
||||||
|
ctx.z := 0;
|
||||||
|
ctx.a := 0;
|
||||||
|
ctx.w := 1;
|
||||||
|
for i in ctx.S'Range loop
|
||||||
|
ctx.S(i) := u8(i);
|
||||||
|
end loop;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure Output (ctx : in out Context; z : out u8) is
|
||||||
|
begin
|
||||||
|
ctx.z := ctx.S(ctx.j + ctx.S(ctx.i + ctx.S(ctx.z + ctx.k)));
|
||||||
|
z := ctx.z;
|
||||||
|
end Output;
|
||||||
|
|
||||||
|
procedure Update (ctx : in out Context) is
|
||||||
|
begin
|
||||||
|
ctx.i := ctx.i + ctx.w;
|
||||||
|
ctx.j := ctx.k + ctx.S(ctx.j + ctx.S(ctx.i));
|
||||||
|
ctx.k := ctx.i + ctx.k + ctx.S(ctx.j);
|
||||||
|
Swap(ctx.S(ctx.i), ctx.S(ctx.j));
|
||||||
|
end Update;
|
||||||
|
|
||||||
|
procedure Crush (ctx : in out Context) is
|
||||||
|
begin
|
||||||
|
for v in u8 range 0 .. u8(N / 2 - 1) loop
|
||||||
|
if ctx.S(v) > ctx.S(u8(N - 1) - v) then
|
||||||
|
Swap(ctx.S(v), ctx.S(u8(N - 1) - v));
|
||||||
|
end if;
|
||||||
|
end loop;
|
||||||
|
end Crush;
|
||||||
|
|
||||||
|
procedure Whip (ctx : in out Context) is
|
||||||
|
begin
|
||||||
|
for i in 0 .. (2 * N - 1) loop
|
||||||
|
Update(ctx);
|
||||||
|
end loop;
|
||||||
|
ctx.w := ctx.w + 2;
|
||||||
|
end Whip;
|
||||||
|
|
||||||
|
procedure Shuffle (ctx : in out Context) is
|
||||||
|
begin
|
||||||
|
Whip(ctx);
|
||||||
|
Crush(ctx);
|
||||||
|
Whip(ctx);
|
||||||
|
Crush(ctx);
|
||||||
|
Whip(ctx);
|
||||||
|
ctx.a := 0;
|
||||||
|
end Shuffle;
|
||||||
|
|
||||||
|
procedure Drip (ctx : in out Context; z : out u8) is
|
||||||
|
begin
|
||||||
|
if ctx.a > 0 then
|
||||||
|
Shuffle(ctx);
|
||||||
|
end if;
|
||||||
|
Update(ctx);
|
||||||
|
Output(ctx, z);
|
||||||
|
end Drip;
|
||||||
|
|
||||||
|
procedure Squeeze (ctx : in out Context; P : out u8_Array) is
|
||||||
|
z : u8;
|
||||||
|
begin
|
||||||
|
for i in P'Range loop
|
||||||
|
Drip(ctx, z);
|
||||||
|
P(i) := z;
|
||||||
|
end loop;
|
||||||
|
end Squeeze;
|
||||||
|
|
||||||
|
procedure AbsorbStop (ctx : in out Context) is
|
||||||
|
begin
|
||||||
|
if ctx.a = u8(N / 2) then
|
||||||
|
Shuffle(ctx);
|
||||||
|
end if;
|
||||||
|
ctx.a := ctx.a + 1;
|
||||||
|
end AbsorbStop;
|
||||||
|
|
||||||
|
procedure AbsorbNibble (ctx : in out Context; x : in u8) is
|
||||||
|
begin
|
||||||
|
if ctx.a = u8(N / 2) then
|
||||||
|
Shuffle(ctx);
|
||||||
|
end if;
|
||||||
|
Swap(ctx.S(ctx.a), ctx.S(u8(N / 2) + x));
|
||||||
|
ctx.a := ctx.a + 1;
|
||||||
|
end AbsorbNibble;
|
||||||
|
|
||||||
|
procedure Absorb (ctx : in out Context; x : in u8) is
|
||||||
|
begin
|
||||||
|
AbsorbNibble(ctx, x and 15);
|
||||||
|
AbsorbNibble(ctx, Shift_Right(x, 4));
|
||||||
|
end Absorb;
|
||||||
|
|
||||||
|
procedure Absorb (ctx : in out Context; x : in u8_Array) is
|
||||||
|
begin
|
||||||
|
for i in x'Range loop
|
||||||
|
Absorb(ctx, x(i));
|
||||||
|
end loop;
|
||||||
|
end Absorb;
|
||||||
|
|
||||||
|
procedure Absorb (ctx : in out Context; x : in String) is
|
||||||
|
begin
|
||||||
|
for i in x'Range loop
|
||||||
|
Absorb(ctx, u8(Character'Pos(x(i))));
|
||||||
|
end loop;
|
||||||
|
end Absorb;
|
||||||
|
|
||||||
|
end Spritz;
|
|
@ -0,0 +1,29 @@
|
||||||
|
with Crypto_Core_Types; use Crypto_Core_Types;
|
||||||
|
with Crypto_Types; use Crypto_Types;
|
||||||
|
|
||||||
|
use Crypto_Types.Crypto_Types_u8;
|
||||||
|
|
||||||
|
package Spritz is
|
||||||
|
|
||||||
|
type Context is private;
|
||||||
|
|
||||||
|
procedure InitializeContext (ctx : out Context);
|
||||||
|
procedure AbsorbStop (ctx : in out Context);
|
||||||
|
procedure Absorb (ctx : in out Context; x : in u8);
|
||||||
|
procedure Absorb (ctx : in out Context; x : in u8_Array);
|
||||||
|
procedure Absorb (ctx : in out Context; x : in String);
|
||||||
|
procedure Drip (ctx : in out Context; z : out u8);
|
||||||
|
procedure Squeeze (ctx : in out Context; P : out u8_Array);
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
N : constant Integer:= 256;
|
||||||
|
|
||||||
|
type S_Array is Array (u8 range <>) of u8;
|
||||||
|
|
||||||
|
type Context is record
|
||||||
|
S : S_Array (0 .. u8(N - 1));
|
||||||
|
i, j, k, z, w, a : u8;
|
||||||
|
end record;
|
||||||
|
|
||||||
|
end Spritz;
|
Loading…
Reference in New Issue