From 262194ad9d30891295aa59d236d568421002d6fe Mon Sep 17 00:00:00 2001 From: bg Date: Sat, 15 Aug 2015 04:47:20 +0200 Subject: [PATCH] some fixes --- src/crypto_core_types.adb | 32 ++++++++++++++++- src/crypto_core_types.ads | 17 ++++++++- src/tests/sha_test_io.adb | 9 +++-- src/tests/sha_test_io.ads | 2 -- src/tests/test_keccak.adb | 60 +++++++++++++++++++++++++++++++ src/tests/test_sha2.adb | 1 - src/tests/test_sha224.adb | 4 +-- src/tests/test_sha256.adb | 58 +++++++++++++----------------- src/tests/test_sha3.adb | 75 +++++++++++++++++++++++++++++++++++++++ src/tests/test_sha384.adb | 4 +-- src/tests/test_sha512.adb | 4 +-- 11 files changed, 220 insertions(+), 46 deletions(-) create mode 100644 src/tests/test_keccak.adb create mode 100644 src/tests/test_sha3.adb diff --git a/src/crypto_core_types.adb b/src/crypto_core_types.adb index a2b4074..ecf6983 100644 --- a/src/crypto_core_types.adb +++ b/src/crypto_core_types.adb @@ -73,7 +73,7 @@ package body Crypto_Core_Types is function From_Hex(S : String) return u8_Array is - A : u8_Array(1 .. (S'Length + 1) / 2); + A : u8_Array(1 .. (S'Length + 1) / 2) := (others => 0); C : Character; V : Integer range -1 .. 15; Index : Positive := 1; @@ -111,5 +111,35 @@ package body Crypto_Core_Types is return A; end From_Ascii; + procedure Bit_Clear(Buffer : in out u8_Array; Index : in Positive) is + begin + Buffer(Buffer'First + Integer(Index / 8)) := Buffer(Buffer'First + Integer(Index / 8)) and (not Shift_Left(1, 7 - (Index - 1) mod 8)); + end Bit_Clear; + + procedure Bit_Set(Buffer : in out u8_Array; Index : in Positive) is + begin + Buffer(Integer(Buffer'First + Index / 8)) := Buffer(Buffer'First + Integer(Index / 8)) or Shift_Left(1, 7 - (Index - 1) mod 8); + end Bit_Set; + + procedure Bit_Toggle(Buffer : in out u8_Array; Index : in Positive) is + begin + Buffer(Integer(Buffer'First + Index / 8)) := Buffer(Buffer'First + Integer(Index / 8)) xor Shift_Left(1, 7 - (Index - 1) mod 8); + end Bit_Toggle; + + + procedure Bit_Set(Buffer : in out u8_Array; Index : in Positive; Value : in Bit) is + begin + if Value = 1 then + Bit_Set(Buffer, Index); + else + Bit_Clear(Buffer, Index); + end if; + end Bit_Set; + + function Bit_Get(Buffer : in u8_Array; Index : in Positive) return Bit is + begin + return Bit(Shift_Right(Buffer(Buffer'First + Index / 8), 7 - (Index - 1) mod 8) and 1); + end Bit_Get; + end Crypto_Core_types; diff --git a/src/crypto_core_types.ads b/src/crypto_core_types.ads index 3a61c3e..0c13450 100644 --- a/src/crypto_core_types.ads +++ b/src/crypto_core_types.ads @@ -17,6 +17,9 @@ with Interfaces; use Interfaces; package Crypto_Core_Types is + type Bit is mod 2; + for Bit'Size use 1; + type u8 is new Unsigned_8; for u8'Size use 8; @@ -39,6 +42,9 @@ package Crypto_Core_Types is type u32_Array_Access is access all u32_Array; type u64_Array_Access is access all u64_Array; + subtype Block_32_Bit is u8_Array(1 .. 32 / 8); + subtype Block_48_Bit is u8_Array(1 .. 48 / 8); + subtype Block_56_Bit is u8_Array(1 .. 56 / 8); subtype Block_64_Bit is u8_Array(1 .. 64 / 8); subtype Block_96_Bit is u8_Array(1 .. 96 / 8); subtype Block_128_Bit is u8_Array(1 .. 128 / 8); @@ -52,13 +58,22 @@ package Crypto_Core_Types is subtype Block_768_Bit is u8_Array(1 .. 768 / 8); subtype Block_1024_Bit is u8_Array(1 .. 1024 / 8); subtype Block_1536_Bit is u8_Array(1 .. 1536 / 8); - subtype Block_2084_Bit is u8_Array(1 .. 2048 / 8); + subtype Block_2048_Bit is u8_Array(1 .. 2048 / 8); + subtype Block_4096_Bit is u8_Array(1 .. 4096 / 8); + subtype Block_8192_Bit is u8_Array(1 .. 8192 / 8); Wrong_Opertaion_Order : exception; Format_Violation : exception; + function To_Hex(A : u8) return String; function To_Hex(A : u8_Array) return String; function From_Hex(S : String) return u8_Array; function From_Ascii(S : String) return u8_Array; + procedure Bit_Clear(Buffer : in out u8_Array; Index : in Positive); + procedure Bit_Set(Buffer : in out u8_Array; Index : in Positive); + procedure Bit_Set(Buffer : in out u8_Array; Index : in Positive; Value : in Bit); + procedure Bit_Toggle(Buffer : in out u8_Array; Index : in Positive); + function Bit_Get(Buffer : in u8_Array; Index : in Positive) return Bit; + end Crypto_Core_Types; diff --git a/src/tests/sha_test_io.adb b/src/tests/sha_test_io.adb index e8dd4d2..3caf7bb 100644 --- a/src/tests/sha_test_io.adb +++ b/src/tests/sha_test_io.adb @@ -89,7 +89,9 @@ package body Sha_Test_IO is when others => null; end case; end loop; - Goto_Data(Context.File); + if not End_Of_File(Context.File) then + Goto_Data(Context.File); + end if; if End_Of_File(Context.File) then Close(Context.File); Next := Finish; @@ -137,7 +139,7 @@ package body Sha_Test_IO is f : Context_T; nt : Next_Type; count_val : Integer; - dlen : Integer; + dlen : Integer := DigestSize_Bits / 8; len, lenb : Integer; DigestSize_Bytes : constant Natural := (DigestSize_Bits + 7 ) / 8; digest, ref_Digest : u8_Array(1 .. DigestSize_Bytes) := (others => 0); @@ -196,6 +198,8 @@ package body Sha_Test_IO is else fail_test := fail_test + 1; Put('!'); + Put_Line(" DBG: is: " & To_Hex(digest)); + Put_Line(" DBG: should: " & To_Hex(ref_digest)); end if; when Message_Block => declare @@ -209,6 +213,7 @@ package body Sha_Test_IO is end if; num := num + 1; Get_Data(f, buf); +-- Put_Line(" DBG: dlen = " & Integer'Image(dlen) & "; len = " & Integer'Image(len)); Hash(buf, digest(1 .. dlen), len); end; end case; diff --git a/src/tests/sha_test_io.ads b/src/tests/sha_test_io.ads index 5d1931a..0bd1b56 100644 --- a/src/tests/sha_test_io.ads +++ b/src/tests/sha_test_io.ads @@ -53,8 +53,6 @@ package Sha_Test_IO is generic --- type Context_T is limited private; --- BlockSize_Bits : Natural; DigestSize_Bits : Natural; with procedure Hash(Data : in u8_Array; Digest : out u8_Array; Bits : in Integer := -1); procedure Test_With_File(FileName : in String); diff --git a/src/tests/test_keccak.adb b/src/tests/test_keccak.adb new file mode 100644 index 0000000..8c71e90 --- /dev/null +++ b/src/tests/test_keccak.adb @@ -0,0 +1,60 @@ +-- Copyright (C) 2015 Daniel Otte +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +with Ada.Text_IO; use Ada.Text_IO; +-- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; +with Crypto_Core_Types; use Crypto_Core_Types; +with Crypto_Types; use Crypto_Types; + +with Sha3_Generic; + +use Crypto_Types.Crypto_Types_u8; +use Crypto_Types.Crypto_Types_u64; + + procedure Test_Keccak is +-- procedure Print_State(A : State_T) is +-- c : Natural := 1; +-- l : u8_Array(1 .. 8); +-- begin +-- for i in y_T'Range loop +-- for j in x_T'Range loop +-- Store_le(A => l, +-- value => A(j, i)); +-- for z in l'Range loop +-- Put(To_Hex(l(z))); +-- Put(' '); +-- if c mod 16 = 0 then +-- New_Line; +-- end if; +-- c := c + 1; +-- end loop; +-- end loop; +-- end loop; +-- New_Line; +-- end; + + +-- A : State_T := ( 0 => ( 0 => 6, others => 0 ), 1 => ( 3 => 16#8000000000000000#, others => 0 ), others => ( others => 0 )); + + package Sha3_224 is new Sha3_Generic(Capacity_Bits => 448); + Digest : Sha3_224.Digest_T; +begin +-- Print_State(A); +-- Permute(A); +-- New_Line; +-- Print_State(A); + Sha3_224.Hash(u8_Array'( 1 => 16#13#), Digest, 5); + Put_Line(To_Hex(Digest)); +end Test_Keccak; diff --git a/src/tests/test_sha2.adb b/src/tests/test_sha2.adb index 53093d0..a497fe8 100644 --- a/src/tests/test_sha2.adb +++ b/src/tests/test_sha2.adb @@ -14,7 +14,6 @@ -- along with this program. If not, see . with Ada.Text_IO; use Ada.Text_IO; -with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Crypto_Core_Types; use Crypto_Core_Types; with Crypto_Types; use Crypto_Types; diff --git a/src/tests/test_sha224.adb b/src/tests/test_sha224.adb index 58967c6..6a780f3 100644 --- a/src/tests/test_sha224.adb +++ b/src/tests/test_sha224.adb @@ -22,7 +22,7 @@ with SHA2_224; use Crypto_Types.Crypto_Types_u8; -procedure main is +procedure Test_SHA224 is -- package u8_IO is new Crypto_Types.u8_Sequential_IO; procedure Print_Hex(value : in u8) is @@ -79,4 +79,4 @@ begin test_sha224("abc"); test_sha224("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); New_Line; -end main; +end Test_SHA224; diff --git a/src/tests/test_sha256.adb b/src/tests/test_sha256.adb index ddb516e..d3fcb92 100644 --- a/src/tests/test_sha256.adb +++ b/src/tests/test_sha256.adb @@ -14,7 +14,6 @@ -- along with this program. If not, see . with Ada.Text_IO; use Ada.Text_IO; -with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Crypto_Core_Types; use Crypto_Core_Types; with Crypto_Types; use Crypto_Types; @@ -23,40 +22,33 @@ with SHA2_256; use Crypto_Types.Crypto_Types_u8; -procedure main is - -- package u8_IO is new Crypto_Types.u8_Sequential_IO; - - procedure Print_Hex(value : in u8_Array) is - begin - Put(To_Hex(value)); - Put(" "); - end; +procedure Test_SHA256 is - procedure test_sha256(Data : in u8_Array; Bits : in Integer := -1) is - Digest : Block_256_Bit; - q : Integer := Bits; - begin - if q < 0 then - q := Data'Length * 8; - end if; - Print_Hex(Data); - Put(" (" & Integer'Image(q) & "): "); - Sha2_256.Hash(Data, Digest, Bits); - Print_Hex(Digest); - New_Line; - end test_sha256; +-- procedure test_sha256(Data : in u8_Array; Bits : in Integer := -1) is +-- Digest : Block_256_Bit; +-- q : Integer := Bits; +-- begin +-- if q < 0 then +-- q := Data'Length * 8; +-- end if; +-- Print_Hex(Data); +-- Put(" (" & Integer'Image(q) & "): "); +-- Sha2_256.Hash(Data, Digest, Bits); +-- Print_Hex(Digest); +-- New_Line; +-- end test_sha256; - procedure test_sha256(Msg : in String) is - Data : u8_Array(1 .. Msg'Length); - begin - Put("""" & Msg & """: "); - for i in data'Range loop - Data(i) := u8(Character'Pos(Msg(Msg'First + i - Data'First))); - end loop; - test_sha256(Data); - New_Line; - end test_sha256; +-- procedure test_sha256(Msg : in String) is +-- Data : u8_Array(1 .. Msg'Length); +-- begin +-- Put("""" & Msg & """: "); +-- for i in data'Range loop +-- Data(i) := u8(Character'Pos(Msg(Msg'First + i - Data'First))); +-- end loop; +-- test_sha256(Data); +-- New_Line; +-- end test_sha256; procedure test_sha256_with_File is new Sha_Test_IO.Test_With_File(DigestSize_Bits => SHA2_256.DigestSize_Bits, Hash => SHA2_256.Hash); @@ -74,4 +66,4 @@ begin test_sha256_with_File("testvectors/sha2/byte/SHA256ShortMsg.rsp"); test_sha256_with_File("testvectors/sha2/byte/SHA256LongMsg.rsp"); test_sha256_with_File("testvectors/sha2/byte/SHA256Monte.rsp"); -end main; +end Test_SHA256; diff --git a/src/tests/test_sha3.adb b/src/tests/test_sha3.adb new file mode 100644 index 0000000..64768a6 --- /dev/null +++ b/src/tests/test_sha3.adb @@ -0,0 +1,75 @@ +-- Copyright (C) 2015 Daniel Otte +-- +-- This program is free software: you can redistribute it and/or modify +-- it under the terms of the GNU General Public License as published by +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This program is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . + +with Ada.Text_IO; use Ada.Text_IO; +with Crypto_Core_Types; use Crypto_Core_Types; +with Crypto_Types; use Crypto_Types; + +with Sha_Test_IO; + +with SHA3; use SHA3; + +use Crypto_Types.Crypto_Types_u8; + +procedure Test_SHA3 is + + procedure test_sha3_224_with_File is new Sha_Test_IO.Test_With_File(DigestSize_Bits => SHA3_224.DigestSize_Bits, Hash => SHA3_224.Hash); + procedure test_sha3_256_with_File is new Sha_Test_IO.Test_With_File(DigestSize_Bits => SHA3_256.DigestSize_Bits, Hash => SHA3_256.Hash); + procedure test_sha3_384_with_File is new Sha_Test_IO.Test_With_File(DigestSize_Bits => SHA3_384.DigestSize_Bits, Hash => SHA3_384.Hash); + procedure test_sha3_512_with_File is new Sha_Test_IO.Test_With_File(DigestSize_Bits => SHA3_512.DigestSize_Bits, Hash => SHA3_512.Hash); + +begin + New_Line; + + test_sha3_224_with_File("testvectors/sha3/pre/ShortMsgKAT_SHA3-224.txt"); + test_sha3_256_with_File("testvectors/sha3/pre/ShortMsgKAT_SHA3-256.txt"); + test_sha3_384_with_File("testvectors/sha3/pre/ShortMsgKAT_SHA3-384.txt"); + test_sha3_512_with_File("testvectors/sha3/pre/ShortMsgKAT_SHA3-512.txt"); + +-- New_Line; +-- test_sha3_224_with_File("testvectors/sha3/bit/sha3_224ShortMsg.rsp"); +-- test_sha3_224_with_File("testvectors/sha3/bit/sha3_224LongMsg.rsp"); +-- test_sha3_224_with_File("testvectors/sha3/bit/sha3_224Monte.rsp"); +-- test_sha3_224_with_File("testvectors/sha3/byte/sha3_224ShortMsg.rsp"); +-- test_sha3_224_with_File("testvectors/sha3/byte/sha3_224LongMsg.rsp"); +-- test_sha3_224_with_File("testvectors/sha3/byte/sha3_224Monte.rsp"); +-- +-- New_Line; +-- test_sha3_256_with_File("testvectors/sha3/bit/sha3_256ShortMsg.rsp"); +-- test_sha3_256_with_File("testvectors/sha3/bit/sha3_256LongMsg.rsp"); +-- test_sha3_256_with_File("testvectors/sha3/bit/sha3_256Monte.rsp"); +-- test_sha3_256_with_File("testvectors/sha3/byte/sha3_256ShortMsg.rsp"); +-- test_sha3_256_with_File("testvectors/sha3/byte/sha3_256LongMsg.rsp"); +-- test_sha3_256_with_File("testvectors/sha3/byte/sha3_256Monte.rsp"); +-- +-- New_Line; +-- test_sha3_384_with_File("testvectors/sha3/bit/sha3_284ShortMsg.rsp"); +-- test_sha3_384_with_File("testvectors/sha3/bit/sha3_284LongMsg.rsp"); +-- test_sha3_384_with_File("testvectors/sha3/bit/sha3_284Monte.rsp"); +-- test_sha3_384_with_File("testvectors/sha3/byte/sha3_284ShortMsg.rsp"); +-- test_sha3_384_with_File("testvectors/sha3/byte/sha3_284LongMsg.rsp"); +-- test_sha3_384_with_File("testvectors/sha3/byte/sha3_284Monte.rsp"); +-- +-- New_Line; +-- test_sha3_512_with_File("testvectors/sha3/bit/SHA512ShortMsg.rsp"); +-- test_sha3_512_with_File("testvectors/sha3/bit/SHA512LongMsg.rsp"); +-- test_sha3_512_with_File("testvectors/sha3/bit/SHA512Monte.rsp"); +-- test_sha3_512_with_File("testvectors/sha3/byte/SHA512ShortMsg.rsp"); +-- test_sha3_512_with_File("testvectors/sha3/byte/SHA512LongMsg.rsp"); +-- test_sha3_512_with_File("testvectors/sha3/byte/SHA512Monte.rsp"); + + New_Line; + +end Test_SHA3; diff --git a/src/tests/test_sha384.adb b/src/tests/test_sha384.adb index 2583b9c..8d71ff6 100644 --- a/src/tests/test_sha384.adb +++ b/src/tests/test_sha384.adb @@ -22,7 +22,7 @@ with SHA2_384; use Crypto_Types.Crypto_Types_u8; -procedure main is +procedure Test_SHA384 is -- package u8_IO is new Crypto_Types.u8_Sequential_IO; procedure Print_Hex(value : in u8) is @@ -79,4 +79,4 @@ begin test_sha384("abc"); test_sha384("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); New_Line; -end main; +end Test_SHA384; diff --git a/src/tests/test_sha512.adb b/src/tests/test_sha512.adb index 4c88fcb..8e9169f 100644 --- a/src/tests/test_sha512.adb +++ b/src/tests/test_sha512.adb @@ -22,7 +22,7 @@ with SHA2_512; use Crypto_Types.Crypto_Types_u8; -procedure main is +procedure Test_SHA512 is -- package u8_IO is new Crypto_Types.u8_Sequential_IO; procedure Print_Hex(value : in u8) is @@ -79,4 +79,4 @@ begin test_sha512("abc"); test_sha512("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); New_Line; -end main; +end Test_SHA512;