Skip to content

Commit

Permalink
move X and T0 to function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jklmnn committed Dec 12, 2018
1 parent fec5945 commit 6faae4d
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 32 deletions.
3 changes: 1 addition & 2 deletions examples/generate_2fa_token.adb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ is
Import,
Convention => C,
External_Name => "time";
package TOTP is new OTP.T;
begin
pragma Assert (LSC.Byte_Arrays.Natural_Index'Last / 5 > 32);
pragma Warnings (Off, "no Global contract");
Expand All @@ -26,7 +25,7 @@ begin
(for all C of Ada.Command_Line.Argument (1) =>
Base32.Valid_Base32_Character (C)))
then
Ada.Text_IO.Put_Line (OTP.Image (TOTP.TOTP (
Ada.Text_IO.Put_Line (OTP.Image (OTP.T.TOTP (
Base32.Decode (Ada.Command_Line.Argument (1)),
Time (System.Null_Address)), 6));
else
Expand Down
8 changes: 4 additions & 4 deletions src/base32.adb
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ is
is
N : Byte;
begin
case Character'Pos (C) is
when 97 .. 122 =>
case C is
when 'a' .. 'z' =>
N := Byte (Character'Pos (C) - 97);
when 65 .. 90 =>
when 'A' .. 'Z' =>
N := Byte (Character'Pos (C) - 65);
when 50 .. 55 =>
when '2' .. '7' =>
N := Byte (Character'Pos (C) - 24);
when others =>
raise Constraint_Error;
Expand Down
2 changes: 2 additions & 0 deletions src/otp-h.ads
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ is
Depends => (HOTP'Result => (Key, Counter)),
Pre => Key'Length <= 64;

private

-- @private Only used to generate the token
function Extract
(Mac : HMAC.HMAC_Type)
Expand Down
9 changes: 5 additions & 4 deletions src/otp-t.adb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
with Interfaces;
with OTP.H;
use all type Interfaces.Unsigned_64;

package body OTP.T
with SPARK_Mode
is

----------
-- TOTP --
----------

function TOTP
(Key : LSC.Byte_Arrays.Byte_Array_Type;
Time : LSC.Types.Word64)
(Key : LSC.Byte_Arrays.Byte_Array_Type;
Time : LSC.Types.Word64;
X : LSC.Types.Word64 := 30;
T0 : LSC.Types.Word64 := 0)
return OTP_Token
is
begin
Expand Down
19 changes: 10 additions & 9 deletions src/otp-t.ads
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
with LSC.Types;
with LSC.Byte_Arrays;
use all type LSC.Types.Word64;

-- @summary
-- Time based One-Time-Pad
--
-- @description
-- Calculates the Time based One-Time-Pad based on RFC 6238
--
-- @param X Time step size
-- @param T0 Reference time
generic
X : LSC.Types.Word64 := 30;
T0 : LSC.Types.Word64 := 0;

package OTP.T
with SPARK_Mode
is

-- Calculates the TOTP Token from a given key and time offset
-- @param HMAC key
-- @param Current time
-- @param X Time step size
-- @param T0 Reference time
-- @return TOTP Token as 31bit word
function TOTP
(Key : LSC.Byte_Arrays.Byte_Array_Type;
Time : LSC.Types.Word64)
Time : LSC.Types.Word64;
X : LSC.Types.Word64 := 30;
T0 : LSC.Types.Word64 := 0)
return OTP_Token
with
Depends => (TOTP'Result => (Key, Time)),
Pre => Key'Length <= 64;
Depends => (TOTP'Result => (Key, Time, X, T0)),
Pre => Key'Length <= 64 and X > 0;

end OTP.T;
4 changes: 1 addition & 3 deletions src/otp.adb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ is
Value : OTP_Value (1 .. D) := (others => '0');
begin
for I in reverse Value'Range loop
pragma Loop_Invariant (for all C of Value =>
Character'Pos (C) > 47 and
Character'Pos (C) < 58);
pragma Loop_Invariant (for all C of Value => C in '0' .. '9');
Value (I) := Character'Val ((Token mod 10) + 48);
Token := Token / 10;
end loop;
Expand Down
3 changes: 1 addition & 2 deletions src/otp.ads
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ is
subtype OTP_Value is String
with
Dynamic_Predicate =>
(for all C of OTP_Value =>
Character'Pos (C) > 47 and Character'Pos (C) < 58);
(for all C of OTP_Value => C in '0' .. '9');

-- 31 bit OTP token
subtype OTP_Token is Integer range 0 .. 2 ** 31 - 1;
Expand Down
7 changes: 3 additions & 4 deletions tests/totp_test.adb → tests/otp-t-test.adb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ with OTP.T;
with LSC.Types;
with LSC.Byte_Arrays;

package body TOTP_Test
package body OTP.T.Test
with SPARK_Mode
is

Expand All @@ -20,12 +20,11 @@ is
16#37#, 16#38#, 16#39#, 16#30#);
Time : constant LSC.Types.Word64 := 59;
Totp_Value : constant OTP.OTP_Value := "94287082";
package TOTP is new OTP.T;
begin
if OTP.Image (TOTP.TOTP (Key, Time), 8) /= Totp_Value then
if OTP.Image (OTP.T.TOTP (Key, Time), 8) /= Totp_Value then
return "Calculating TOTP value failed";
end if;
return "PASSED";
end Run;

end TOTP_Test;
end OTP.T.Test;
4 changes: 2 additions & 2 deletions tests/totp_test.ads → tests/otp-t-test.ads
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TOTP_Test
package OTP.T.Test
with SPARK_Mode
is

Expand All @@ -8,4 +8,4 @@ is
with
Post => Run'Result'Length <= 128;

end TOTP_Test;
end OTP.T.Test;
4 changes: 2 additions & 2 deletions tests/test.adb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ with Base32.Test;
with HMAC.Test;
with OTP.Test;
with OTP.H.Test;
with TOTP_Test;
with OTP.T.Test;

procedure Test
with SPARK_Mode
Expand Down Expand Up @@ -39,7 +39,7 @@ begin
Eval (HMAC.Test.Name, HMAC.Test.Run, Passed);
Eval (OTP.Test.Name, OTP.Test.Run, Passed);
Eval (OTP.H.Test.Name, OTP.H.Test.Run, Passed);
Eval (TOTP_Test.Name, TOTP_Test.Run, Passed);
Eval (OTP.T.Test.Name, OTP.T.Test.Run, Passed);
if not Passed then
Ada.Command_Line.Set_Exit_Status (1);
end if;
Expand Down

0 comments on commit 6faae4d

Please sign in to comment.