jrcarter / PragmARC

The PragmAda Reusable Components
27 stars 6 forks source link

(WhishList) Skein 512 hash #17

Open danieagle opened 4 weeks ago

danieagle commented 4 weeks ago

Hi(!) Carter! :-)

Can you do a version of Skein 512 hash? or create the version 256bits and I do hands on approach to create a 512bit and 1024bit versions ?

Very Very Very Thanks!

GrateFull, God and Mary Bless you and yours, Dani.

jrcarter commented 4 weeks ago

The first thing would be to implement UBI. The main effort would be providing addition for 96-bit integers. Once you have that, the rest looks fairly simple. I wonder if a single implementation could be used for all block sizes.

I won't be able to look at this in detail for a while, but I suspect you could do it before then if you have time.

danieagle commented 3 weeks ago

Hi(!) Carter!

The recent gnat have a 128bits integer support. my main difficult is interpreting the schemas, please(!) can you show me the idea of ubi in english or Ada ? Very Very Very Thanks!

GrateFull, God and Mary Bless you and yours, Dani.

jrcarter commented 3 weeks ago

Recent versions of GNAT for 64-bit machines have 128-bit integers, but the PragmARCs are intended to be portable to any Ada-12 compiler with 64-bit integers, and ObjectAda does not have 128-bit integers, so I won't use them. If you want a 64-bit-GNAT-only version of UBI/Skein, then that's something not really relevant here.

Using names from package Threefish and one of the Threefish.Block_n packages, UBI takes 3 inputs:

  1. An initial key, called G in the Skein V1.3 document; this a Block
  2. A message, called M; this is a Byte_List; M'Length < 2 ** 96
  3. An initial Tweak, called Ts; this is a Couple; this is treated as a 128-bit integer with the least-significant bits in Ts (Ts'First); typically it is all zeroes except for the Type field [bits 120-125, or Ts (Ts'Last) bits 56-61]

The result is a Block, called H.

M is allowed to have unused bits in its last byte; if this is the case, then the last byte is padded to create M', but we'll ignore this initially and use M' = M. If M'Length is not a multiple of Block_As_Bytes'Length, then M' is padded with zero bytes to such a multiple to create the message that will be processed. M', including any padding bytes, is converted to a Block_List, called M''. Calling M'' Padded, processing then goes

H := G;
Tweak := Ts;
for K in Padded'range loop
   Key := H;
   -- Add (if K < Padded'Last or M'Length rem Block_As_Bytes'Length = 0 then
   --         Block_As_Bytes'Length
   --      else
   --         M'Length rem Block_As_Bytes'Length)
   -- to the Position part of Tweak (bits 0-95)
   -- Set bit 126 of Tweak to (if K = Padded'First then 1 else 0)
   -- Set bit 127 of Tweak to (if K = Padded'Last  then 1 else 0)
   Create_Key_Schedule (Key => Key, Tweak => Tweak, Key_Schedule => KS);
   H := Padded (K);
   Encrypt (Key_Schedule => KS, Text => H);
end loop;
return H;
jrcarter commented 3 weeks ago

Looking at this realistically, a Byte_List is defined with an index of Positive range <>, or up to 2 GB. It's unlikely that anyone will be able to store the 16 EB possible with a 64-bit index any time soon, so we don't need more than 64 bits for the Position value even with such a change to Byte_List. It therefore seems reasonable to use the first word of the tweak as the Position value and have the second word only store the Type and the bit flags. Then the loop becomes

for K in Padded'range loop
   Key := H;
   Tweak (Tweak'First) := (if K = Padded'Last then M'Length
                           else Tweak (Tweak'First) + Block_As_Bytes'Length);
   Tweak (Tweak'Last) := Ts (Ts'Last) + (if K = Padded'First then 2 ** 62   -- First block
                                         elsif K = Padded'Last then 2 ** 63 -- Last block
                                         else 0);
   Create_Key_Schedule (Key => Key, Tweak => Tweak, Key_Schedule => KS);
   H := Padded (K);
   Encrypt (Key_Schedule => KS, Text => H);
end loop;
danieagle commented 3 weeks ago

Uáú (!) ÔBA ^_^!

I asked to the right person.

I'll try It!

Very Very Very Thanks(!) GrateFull, God and Mary Bless you and yours, Dani. :-)