Browse Source

adding support for not using a SMN

master
bg nerilex 5 years ago
parent
commit
1f0716f387
9 changed files with 159 additions and 99 deletions
  1. +35
    -21
      src/algorithms/pi-cipher/pi16cipher_spec.adb
  2. +4
    -4
      src/algorithms/pi-cipher/pi16cipher_spec.ads
  3. +35
    -21
      src/algorithms/pi-cipher/pi32cipher_spec.adb
  4. +4
    -4
      src/algorithms/pi-cipher/pi32cipher_spec.ads
  5. +35
    -21
      src/algorithms/pi-cipher/pi64cipher_spec.adb
  6. +4
    -4
      src/algorithms/pi-cipher/pi64cipher_spec.ads
  7. +22
    -16
      src/tests/test_pi16cipher.adb
  8. +10
    -4
      src/tests/test_pi32cipher.adb
  9. +10
    -4
      src/tests/test_pi64cipher.adb

+ 35
- 21
src/algorithms/pi-cipher/pi16cipher_spec.adb View File

@@ -251,23 +251,37 @@ package body Pi16Cipher_Spec is
Context.Counter := Context.Counter + u64(Num);
end Process_Header_Last_Block;

procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T) is
State : constant State_T := Pi(Context + Block_Number_T'(1)) xor Block;
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array) is
State : State_T;
begin
Block := Extract(State);
Context.State := Pi(State);
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
if Block'Length = Block_T'Length then
State := Pi(Context + Block_Number_T'(1)) xor Block;
Block := Extract(State);
Context.State := Pi(State);
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
elsif Block'Length = 0 then
null;
else
raise Format_Violation;
end if;
end Encrypt_Secret_Message_Number;

procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T) is
State : constant State_T := Pi(Context + Block_Number_T'(1)) xor Block;
Block_In : constant Block_T := Block;
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array) is
State : State_T;
Block_In : constant u8_Array(Block'Range) := Block;
begin
Block := Extract(State);
Context.State := Pi(set(State, Block_In));
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
if Block'Length = Block_T'Length then
State := Pi(Context + Block_Number_T'(1)) xor Block;
Block := Extract(State);
Context.State := Pi(set(State, Block_In));
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
elsif Block'Length = 0 then
null;
else
raise Format_Violation;
end if;
end Decrypt_Secret_Message_Number;

procedure Encrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T) is
@@ -340,27 +354,27 @@ package body Pi16Cipher_Spec is
return Get_Tag(Context) = Should_Tag;
end Is_Valid;

function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : Block_T; Key : u8_Array) return u8_Array is
function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : u8_Array; Key : u8_Array) return u8_Array is
Crypt : u8_Array(1 .. Secret_Nonce'Length + Msg'Length + Tag_Bytes);
Ctx : Context_T;
begin
Initialize(Context => Ctx, Key => Key, Public_Nonce => Public_Nonce);
Process_Header_Last_Block(Context => Ctx, Block => AD, Block_Number => 1);
Crypt(Crypt'First .. Crypt'First + Secret_Message_Number_Bytes - 1) := Secret_Nonce;
Crypt(Crypt'First + Secret_Message_Number_Bytes .. Crypt'Last - Tag_Bytes) := Msg;
Encrypt_Secret_Message_Number(Context => Ctx, Block => Crypt(Crypt'First .. Crypt'First + Secret_Message_Number_Bytes - 1));
Encrypt_Last_Block(Context => Ctx, Block => Crypt(Crypt'First + Secret_Message_Number_Bytes .. Crypt'Last - Tag_Bytes), Block_Number => 1);
Crypt(Crypt'First .. Crypt'First + Secret_Nonce'Length - 1) := Secret_Nonce;
Crypt(Crypt'First + Secret_Nonce'Length .. Crypt'Last - Tag_Bytes) := Msg;
Encrypt_Secret_Message_Number(Context => Ctx, Block => Crypt(Crypt'First .. Crypt'First + Secret_Nonce'Length - 1));
Encrypt_Last_Block(Context => Ctx, Block => Crypt(Crypt'First + Secret_Nonce'Length .. Crypt'Last - Tag_Bytes), Block_Number => 1);
Crypt(Crypt'Last - Tag_Bytes + 1 .. Crypt'Last) := Get_Tag(Ctx);
return Crypt;
end Encrypt;

procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out Block_T; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out u8_Array; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
Ctx : Context_T;
begin
Initialize(Context => Ctx, Key => Key, Public_Nonce => Public_Nonce);
Process_Header_Last_Block(Context => Ctx, Block => AD, Block_Number => 1);
Secret_Nonce := Cipher(Cipher'First .. Cipher'First + Secret_Message_Number_Bytes - 1);
Msg := Cipher(Cipher'First + Secret_Message_Number_Bytes .. Cipher'Last - Tag_Bytes);
Secret_Nonce := Cipher(Cipher'First .. Cipher'First + Secret_Nonce'Length - 1);
Msg := Cipher(Cipher'First + Secret_Nonce'Length .. Cipher'Last - Tag_Bytes);
Decrypt_Secret_Message_Number(Context => Ctx, Block => Secret_Nonce);
Decrypt_Last_Block(Context => Ctx, Block => Msg, Block_Number => 1);
Is_Valid := Pi16Cipher_Spec.Is_Valid(Ctx, Cipher(Cipher'Last - Tag_Bytes + 1 .. Cipher'Last));


+ 4
- 4
src/algorithms/pi-cipher/pi16cipher_spec.ads View File

@@ -45,8 +45,8 @@ package Pi16Cipher_Spec is
procedure Initialize(Context : out Context_T; Key : in u8_Array; Public_Nonce : in u8_Array);
procedure Process_Header_Block (Context : in out Context_T; Block : Block_T; Block_Number : Block_Number_T);
procedure Process_Header_Last_Block (Context : in out Context_T; Block : u8_Array; Block_Number : Block_Number_T);
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T);
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T);
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array);
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array);
procedure Encrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T);
procedure Decrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T);
procedure Encrypt_Last_Block(Context : in out Context_T; Block : in out u8_Array; Block_Number : Block_Number_T);
@@ -55,8 +55,8 @@ package Pi16Cipher_Spec is
function Is_Valid(Is_Tag : in Tag_T; Should_Tag : in Tag_T) return Boolean;
function Is_Valid(Context : in Context_T; Should_Tag : in Tag_T) return Boolean;

function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : Block_T; Key : u8_Array) return u8_Array;
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out Block_T; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array);
function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : u8_Array; Key : u8_Array) return u8_Array;
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out u8_Array; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array);


private


+ 35
- 21
src/algorithms/pi-cipher/pi32cipher_spec.adb View File

@@ -251,23 +251,37 @@ package body Pi32Cipher_Spec is
Context.Counter := Context.Counter + u64(Num);
end Process_Header_Last_Block;

procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T) is
State : constant State_T := Pi(Context + Block_Number_T'(1)) xor Block;
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array) is
State : State_T;
begin
Block := Extract(State);
Context.State := Pi(State);
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
if Block'Length = Block_T'Length then
State := Pi(Context + Block_Number_T'(1)) xor Block;
Block := Extract(State);
Context.State := Pi(State);
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
elsif Block'Length = 0 then
null;
else
raise Format_Violation;
end if;
end Encrypt_Secret_Message_Number;

procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T) is
State : constant State_T := Pi(Context + Block_Number_T'(1)) xor Block;
Block_In : constant Block_T := Block;
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array) is
State : State_T;
Block_In : constant u8_Array(Block'Range) := Block;
begin
Block := Extract(State);
Context.State := Pi(set(State, Block_In));
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
if Block'Length = Block_T'Length then
State := Pi(Context + Block_Number_T'(1)) xor Block;
Block := Extract(State);
Context.State := Pi(set(State, Block_In));
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
elsif Block'Length = 0 then
null;
else
raise Format_Violation;
end if;
end Decrypt_Secret_Message_Number;

procedure Encrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T) is
@@ -340,27 +354,27 @@ package body Pi32Cipher_Spec is
return Get_Tag(Context) = Should_Tag;
end Is_Valid;

function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : Block_T; Key : u8_Array) return u8_Array is
function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : u8_Array; Key : u8_Array) return u8_Array is
Crypt : u8_Array(1 .. Secret_Nonce'Length + Msg'Length + Tag_Bytes);
Ctx : Context_T;
begin
Initialize(Context => Ctx, Key => Key, Public_Nonce => Public_Nonce);
Process_Header_Last_Block(Context => Ctx, Block => AD, Block_Number => 1);
Crypt(Crypt'First .. Crypt'First + Secret_Message_Number_Bytes - 1) := Secret_Nonce;
Crypt(Crypt'First + Secret_Message_Number_Bytes .. Crypt'Last - Tag_Bytes) := Msg;
Encrypt_Secret_Message_Number(Context => Ctx, Block => Crypt(Crypt'First .. Crypt'First + Secret_Message_Number_Bytes - 1));
Encrypt_Last_Block(Context => Ctx, Block => Crypt(Crypt'First + Secret_Message_Number_Bytes .. Crypt'Last - Tag_Bytes), Block_Number => 1);
Crypt(Crypt'First .. Crypt'First + Secret_Nonce'Length - 1) := Secret_Nonce;
Crypt(Crypt'First + Secret_Nonce'Length .. Crypt'Last - Tag_Bytes) := Msg;
Encrypt_Secret_Message_Number(Context => Ctx, Block => Crypt(Crypt'First .. Crypt'First + Secret_Nonce'Length - 1));
Encrypt_Last_Block(Context => Ctx, Block => Crypt(Crypt'First + Secret_Nonce'Length .. Crypt'Last - Tag_Bytes), Block_Number => 1);
Crypt(Crypt'Last - Tag_Bytes + 1 .. Crypt'Last) := Get_Tag(Ctx);
return Crypt;
end Encrypt;

procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out Block_T; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out u8_Array; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
Ctx : Context_T;
begin
Initialize(Context => Ctx, Key => Key, Public_Nonce => Public_Nonce);
Process_Header_Last_Block(Context => Ctx, Block => AD, Block_Number => 1);
Secret_Nonce := Cipher(Cipher'First .. Cipher'First + Secret_Message_Number_Bytes - 1);
Msg := Cipher(Cipher'First + Secret_Message_Number_Bytes .. Cipher'Last - Tag_Bytes);
Secret_Nonce := Cipher(Cipher'First .. Cipher'First + Secret_Nonce'Length - 1);
Msg := Cipher(Cipher'First + Secret_Nonce'Length .. Cipher'Last - Tag_Bytes);
Decrypt_Secret_Message_Number(Context => Ctx, Block => Secret_Nonce);
Decrypt_Last_Block(Context => Ctx, Block => Msg, Block_Number => 1);
Is_Valid := Pi32Cipher_Spec.Is_Valid(Ctx, Cipher(Cipher'Last - Tag_Bytes + 1 .. Cipher'Last));


+ 4
- 4
src/algorithms/pi-cipher/pi32cipher_spec.ads View File

@@ -45,8 +45,8 @@ package Pi32Cipher_Spec is
procedure Initialize(Context : out Context_T; Key : in u8_Array; Public_Nonce : in u8_Array);
procedure Process_Header_Block (Context : in out Context_T; Block : Block_T; Block_Number : Block_Number_T);
procedure Process_Header_Last_Block (Context : in out Context_T; Block : u8_Array; Block_Number : Block_Number_T);
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T);
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T);
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array);
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array);
procedure Encrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T);
procedure Decrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T);
procedure Encrypt_Last_Block(Context : in out Context_T; Block : in out u8_Array; Block_Number : Block_Number_T);
@@ -55,8 +55,8 @@ package Pi32Cipher_Spec is
function Is_Valid(Is_Tag : in Tag_T; Should_Tag : in Tag_T) return Boolean;
function Is_Valid(Context : in Context_T; Should_Tag : in Tag_T) return Boolean;

function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : Block_T; Key : u8_Array) return u8_Array;
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out Block_T; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array);
function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : u8_Array; Key : u8_Array) return u8_Array;
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out u8_Array; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array);


private


+ 35
- 21
src/algorithms/pi-cipher/pi64cipher_spec.adb View File

@@ -259,23 +259,37 @@ package body Pi64Cipher_Spec is
Context.Counter := Context.Counter + u64(Num);
end Process_Header_Last_Block;

procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T) is
State : constant State_T := Pi(Context + Block_Number_T'(1)) xor Block;
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array) is
State : State_T;
begin
Block := Extract(State);
Context.State := Pi(State);
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
if Block'Length = Block_T'Length then
State := Pi(Context + Block_Number_T'(1)) xor Block;
Block := Extract(State);
Context.State := Pi(State);
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
elsif Block'Length = 0 then
null;
else
raise Format_Violation;
end if;
end Encrypt_Secret_Message_Number;

procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T) is
State : constant State_T := Pi(Context + Block_Number_T'(1)) xor Block;
Block_In : constant Block_T := Block;
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array) is
State : State_T;
Block_In : constant u8_Array(Block'Range) := Block;
begin
Block := Extract(State);
Context.State := Pi(set(State, Block_In));
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
if Block'Length = Block_T'Length then
State := Pi(Context + Block_Number_T'(1)) xor Block;
Block := Extract(State);
Context.State := Pi(set(State, Block_In));
Context.Tag := Context.Tag + Extract(Context.State);
Context.Counter := Context.Counter + 1;
elsif Block'Length = 0 then
null;
else
raise Format_Violation;
end if;
end Decrypt_Secret_Message_Number;

procedure Encrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T) is
@@ -348,27 +362,27 @@ package body Pi64Cipher_Spec is
return Get_Tag(Context) = Should_Tag;
end Is_Valid;

function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : Block_T; Key : u8_Array) return u8_Array is
function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : u8_Array; Key : u8_Array) return u8_Array is
Crypt : u8_Array(1 .. Secret_Nonce'Length + Msg'Length + Tag_Bytes);
Ctx : Context_T;
begin
Initialize(Context => Ctx, Key => Key, Public_Nonce => Public_Nonce);
Process_Header_Last_Block(Context => Ctx, Block => AD, Block_Number => 1);
Crypt(Crypt'First .. Crypt'First + Secret_Message_Number_Bytes - 1) := Secret_Nonce;
Crypt(Crypt'First + Secret_Message_Number_Bytes .. Crypt'Last - Tag_Bytes) := Msg;
Encrypt_Secret_Message_Number(Context => Ctx, Block => Crypt(Crypt'First .. Crypt'First + Secret_Message_Number_Bytes - 1));
Encrypt_Last_Block(Context => Ctx, Block => Crypt(Crypt'First + Secret_Message_Number_Bytes .. Crypt'Last - Tag_Bytes), Block_Number => 1);
Crypt(Crypt'First .. Crypt'First + Secret_Nonce'Length - 1) := Secret_Nonce;
Crypt(Crypt'First + Secret_Nonce'Length .. Crypt'Last - Tag_Bytes) := Msg;
Encrypt_Secret_Message_Number(Context => Ctx, Block => Crypt(Crypt'First .. Crypt'First + Secret_Nonce'Length - 1));
Encrypt_Last_Block(Context => Ctx, Block => Crypt(Crypt'First + Secret_Nonce'Length .. Crypt'Last - Tag_Bytes), Block_Number => 1);
Crypt(Crypt'Last - Tag_Bytes + 1 .. Crypt'Last) := Get_Tag(Ctx);
return Crypt;
end Encrypt;

procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out Block_T; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out u8_Array; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
Ctx : Context_T;
begin
Initialize(Context => Ctx, Key => Key, Public_Nonce => Public_Nonce);
Process_Header_Last_Block(Context => Ctx, Block => AD, Block_Number => 1);
Secret_Nonce := Cipher(Cipher'First .. Cipher'First + Secret_Message_Number_Bytes - 1);
Msg := Cipher(Cipher'First + Secret_Message_Number_Bytes .. Cipher'Last - Tag_Bytes);
Secret_Nonce := Cipher(Cipher'First .. Cipher'First + Secret_Nonce'Length - 1);
Msg := Cipher(Cipher'First + Secret_Nonce'Length .. Cipher'Last - Tag_Bytes);
Decrypt_Secret_Message_Number(Context => Ctx, Block => Secret_Nonce);
Decrypt_Last_Block(Context => Ctx, Block => Msg, Block_Number => 1);
Is_Valid := Pi64Cipher_Spec.Is_Valid(Ctx, Cipher(Cipher'Last - Tag_Bytes + 1 .. Cipher'Last));


+ 4
- 4
src/algorithms/pi-cipher/pi64cipher_spec.ads View File

@@ -45,8 +45,8 @@ package Pi64Cipher_Spec is
procedure Initialize(Context : out Context_T; Key : in u8_Array; Public_Nonce : in u8_Array);
procedure Process_Header_Block (Context : in out Context_T; Block : Block_T; Block_Number : Block_Number_T);
procedure Process_Header_Last_Block (Context : in out Context_T; Block : u8_Array; Block_Number : Block_Number_T);
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T);
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out Block_T);
procedure Encrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array);
procedure Decrypt_Secret_Message_Number(Context : in out Context_T; Block : in out u8_Array);
procedure Encrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T);
procedure Decrypt_Block(Context : in out Context_T; Block : in out Block_T; Block_Number : Block_Number_T);
procedure Encrypt_Last_Block(Context : in out Context_T; Block : in out u8_Array; Block_Number : Block_Number_T);
@@ -55,8 +55,8 @@ package Pi64Cipher_Spec is
function Is_Valid(Is_Tag : in Tag_T; Should_Tag : in Tag_T) return Boolean;
function Is_Valid(Context : in Context_T; Should_Tag : in Tag_T) return Boolean;

function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : Block_T; Key : u8_Array) return u8_Array;
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out Block_T; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array);
function Encrypt(Msg : u8_Array; AD : u8_Array; Public_Nonce : u8_Array; Secret_Nonce : u8_Array; Key : u8_Array) return u8_Array;
procedure Decrypt(Is_Valid : out Boolean; Msg : out u8_Array; Secret_Nonce : out u8_Array; Cipher : in u8_Array; AD : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array);


private


+ 22
- 16
src/tests/test_pi16cipher.adb View File

@@ -77,12 +77,12 @@ procedure Test_Pi16Cipher is
Is_Valid : Boolean;
-- Tag : Block_128_Bit;
begin
Put_Line("Key: " & To_Hex(Key));
Put_Line("public IV: " & To_Hex(IvPub));
Put_Line("secret IV: " & To_Hex(IvSec));
Put_Line("Header: " & To_Hex(Header));
Put_Line("Plaintext: " & To_Hex(Msg));
Put_Line("Ciphertext: " & To_Hex(Crypt, True));
Put_Line("Key: " & To_Hex(Key, Spacing => 1));
Put_Line("public IV: " & To_Hex(IvPub, Spacing => 1));
Put_Line("secret IV: " & To_Hex(IvSec, Spacing => 1));
Put_Line("Header: " & To_Hex(Header, Spacing => 1));
Put_Line("Plaintext: " & To_Hex(Msg, Spacing => 1));
Put_Line("Ciphertext: " & To_Hex(Crypt, True, Spacing => 1));
New_Line;
PiCipher.Decrypt(Is_Valid, Msg, IvSec, Crypt, Header, IvPub, Key);
if Is_Valid then
@@ -90,12 +90,12 @@ procedure Test_Pi16Cipher is
else
Put_Line(">>! verfication failed<<");
end if;
Put_Line("Key: " & To_Hex(Key));
Put_Line("public IV: " & To_Hex(IvPub));
Put_Line("secret IV: " & To_Hex(IvSec));
Put_Line("Header: " & To_Hex(Header));
Put_Line("Plaintext: " & To_Hex(Msg));
Put_Line("Ciphertext: " & To_Hex(Crypt, True));
Put_Line("Key: " & To_Hex(Key, Spacing => 1));
Put_Line("public IV: " & To_Hex(IvPub, Spacing => 1));
Put_Line("secret IV: " & To_Hex(IvSec, Spacing => 1));
Put_Line("Header: " & To_Hex(Header, Spacing => 1));
Put_Line("Plaintext: " & To_Hex(Msg, Spacing => 1));
Put_Line("Ciphertext: " & To_Hex(Crypt, True, Spacing => 1));
New_Line;
end;

@@ -130,7 +130,7 @@ procedure Test_Pi16Cipher is

procedure Single_Testvector(Msg : in u8_Array; AD : in u8_Array; Secret_Nonce : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
Msg_Check : u8_Array(Msg'Range);
Smn_Check : PiCipher.Block_T;
Smn_Check : u8_Array(Secret_Nonce'Range);
Is_Valid : Boolean;
Crypt : constant u8_Array := PiCipher.Encrypt(Msg => Msg, AD => AD, Public_Nonce => Public_Nonce, Secret_Nonce => Secret_Nonce, Key => Key);
begin
@@ -167,15 +167,21 @@ procedure Test_Pi16Cipher is
Put_Line("[msg_len = " & Trim(Integer'Image(Msg_Len), Both) & "]");
Put_Line("[ad_len = " & Trim(Integer'Image(AD_Len), Both) & "]");
New_Line;
for i in 1 .. 8 loop
for i in 1 .. 9 loop
Put_Line("[vector #" & Trim(Integer'Image(Counter), Both) & " (" & Trim(Integer'Image(i), Both) & ")]");
Counter := Counter + 1;
Random_Fill(Key);
Random_Fill(Public_Nonce);
Random_Fill(Secret_Nonce);
if i < 9 then
Random_Fill(Secret_Nonce);
end if;
Random_Fill(AD(1 .. AD_Len));
Random_Fill(Msg(1 .. Msg_Len));
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_Nonce, Public_Nonce => Public_Nonce, Key => Key);
if i < 9 then
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_Nonce, Public_Nonce => Public_Nonce, Key => Key);
else
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_nonce(1 .. 0), Public_Nonce => Public_Nonce, Key => Key);
end if;
end loop;
end loop;
end loop;


+ 10
- 4
src/tests/test_pi32cipher.adb View File

@@ -73,7 +73,7 @@ procedure Test_Pi32Cipher is

procedure Single_Testvector(Msg : in u8_Array; AD : in u8_Array; Secret_Nonce : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
Msg_Check : u8_Array(Msg'Range);
Smn_Check : PiCipher.Block_T;
Smn_Check : u8_Array(Secret_Nonce'Range);
Is_Valid : Boolean;
Crypt : constant u8_Array := PiCipher.Encrypt(Msg => Msg, AD => AD, Public_Nonce => Public_Nonce, Secret_Nonce => Secret_Nonce, Key => Key);
begin
@@ -110,15 +110,21 @@ procedure Test_Pi32Cipher is
Put_Line("[msg_len = " & Trim(Integer'Image(Msg_Len), Both) & "]");
Put_Line("[ad_len = " & Trim(Integer'Image(AD_Len), Both) & "]");
New_Line;
for i in 1 .. 8 loop
for i in 1 .. 9 loop
Put_Line("[vector #" & Trim(Integer'Image(Counter), Both) & " (" & Trim(Integer'Image(i), Both) & ")]");
Counter := Counter + 1;
Random_Fill(Key);
Random_Fill(Public_Nonce);
Random_Fill(Secret_Nonce);
if i < 9 then
Random_Fill(Secret_Nonce);
end if;
Random_Fill(AD(1 .. AD_Len));
Random_Fill(Msg(1 .. Msg_Len));
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_Nonce, Public_Nonce => Public_Nonce, Key => Key);
if i < 9 then
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_Nonce, Public_Nonce => Public_Nonce, Key => Key);
else
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_nonce(1 .. 0), Public_Nonce => Public_Nonce, Key => Key);
end if;
end loop;
end loop;
end loop;


+ 10
- 4
src/tests/test_pi64cipher.adb View File

@@ -130,7 +130,7 @@ procedure Test_Pi64Cipher is

procedure Single_Testvector(Msg : in u8_Array; AD : in u8_Array; Secret_Nonce : in u8_Array; Public_Nonce : in u8_Array; Key : in u8_Array) is
Msg_Check : u8_Array(Msg'Range);
Smn_Check : PiCipher.Block_T;
Smn_Check : u8_Array(Secret_Nonce'Range);
Is_Valid : Boolean;
Crypt : constant u8_Array := PiCipher.Encrypt(Msg => Msg, AD => AD, Public_Nonce => Public_Nonce, Secret_Nonce => Secret_Nonce, Key => Key);
begin
@@ -167,15 +167,21 @@ procedure Test_Pi64Cipher is
Put_Line("[msg_len = " & Trim(Integer'Image(Msg_Len), Both) & "]");
Put_Line("[ad_len = " & Trim(Integer'Image(AD_Len), Both) & "]");
New_Line;
for i in 1 .. 8 loop
for i in 1 .. 9 loop
Put_Line("[vector #" & Trim(Integer'Image(Counter), Both) & " (" & Trim(Integer'Image(i), Both) & ")]");
Counter := Counter + 1;
Random_Fill(Key);
Random_Fill(Public_Nonce);
Random_Fill(Secret_Nonce);
if i < 9 then
Random_Fill(Secret_Nonce);
end if;
Random_Fill(AD(1 .. AD_Len));
Random_Fill(Msg(1 .. Msg_Len));
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_Nonce, Public_Nonce => Public_Nonce, Key => Key);
if i < 9 then
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_Nonce, Public_Nonce => Public_Nonce, Key => Key);
else
Single_Testvector(Msg => Msg(1 .. Msg_Len), AD => AD(1 .. AD_Len), Secret_Nonce => Secret_nonce(1 .. 0), Public_Nonce => Public_Nonce, Key => Key);
end if;
end loop;
end loop;
end loop;


Loading…
Cancel
Save