Skip to content

Commit 1621908

Browse files
committed
Ada: Remove Text from Finalize_SHA256
1 parent fd378c8 commit 1621908

4 files changed

Lines changed: 39 additions & 98 deletions

File tree

wrapper/Ada/examples/src/sha256_main.adb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ procedure SHA256_Main is
1818
4 => 'f');
1919
SHA256 : WolfSSL.SHA256_Type;
2020
R : Integer;
21-
S : WolfSSL.SHA256_As_String;
2221
begin
2322
WolfSSL.Create_SHA256 (SHA256 => SHA256, Result => R);
2423
if R /= 0 then
@@ -34,10 +33,9 @@ begin
3433
end if;
3534
WolfSSL.Finalize_SHA256 (SHA256 => SHA256,
3635
Hash => Hash,
37-
Text => S,
3836
Result => R);
3937
if R = 0 then
40-
Put (S);
38+
Put ("SHA256 hash computed successfully");
4139
New_Line;
4240
else
4341
Put ("Finalization of SHA256 instance failed");

wrapper/Ada/tests/src/sha256_bindings_tests.adb

Lines changed: 38 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,9 @@ package body SHA256_Bindings_Tests is
1111
-- Helpers
1212
----------------------------------------------------------------------------
1313

14-
procedure Assert_Text_Matches_Hash
15-
(Hash : WolfSSL.SHA256_Hash;
16-
Text : WolfSSL.SHA256_As_String;
17-
Msg : String);
18-
1914
procedure Compute_SHA256
2015
(Input : WolfSSL.Byte_Array;
2116
Hash : out WolfSSL.SHA256_Hash;
22-
Text : out WolfSSL.SHA256_As_String;
23-
Result : out Integer);
24-
25-
procedure Assert_Text_Matches_Hash
26-
(Hash : WolfSSL.SHA256_Hash;
27-
Text : WolfSSL.SHA256_As_String;
28-
Msg : String)
29-
is
30-
use type WolfSSL.Byte_Array;
31-
32-
Expected : constant WolfSSL.Byte_Array := Test_Support.Hex_Bytes (Text);
33-
Actual : WolfSSL.Byte_Array (Expected'Range);
34-
H : WolfSSL.Byte_Index := Hash'First;
35-
begin
36-
-- Copy bytes out of the hash into the expected-range buffer.
37-
-- Do not assume Hash and Expected share the same index range.
38-
for I in Actual'Range loop
39-
Actual (I) := Hash (H);
40-
H := WolfSSL.Byte_Index'Succ (H);
41-
end loop;
42-
AUnit.Assertions.Assert
43-
(Text'Length = 64,
44-
Msg & ": expected 64 hex chars, got" & Integer'Image (Text'Length));
45-
46-
-- `Finalize_SHA256` should generate uppercase hex, validate that expectation.
47-
for J in Text'Range loop
48-
declare
49-
C : constant Character := Text (J);
50-
begin
51-
if C in '0' .. '9' or else C in 'A' .. 'F' then
52-
null;
53-
else
54-
AUnit.Assertions.Assert
55-
(False,
56-
Msg & ": expected uppercase hex at pos" &
57-
Integer'Image (J));
58-
end if;
59-
end;
60-
end loop;
61-
62-
AUnit.Assertions.Assert
63-
(Actual = Expected,
64-
Msg & ": Text/Hash mismatch");
65-
end Assert_Text_Matches_Hash;
66-
67-
procedure Compute_SHA256
68-
(Input : WolfSSL.Byte_Array;
69-
Hash : out WolfSSL.SHA256_Hash;
70-
Text : out WolfSSL.SHA256_As_String;
7117
Result : out Integer)
7218
is
7319
SHA256 : WolfSSL.SHA256_Type;
@@ -90,7 +36,6 @@ package body SHA256_Bindings_Tests is
9036
WolfSSL.Finalize_SHA256
9137
(SHA256 => SHA256,
9238
Hash => Hash,
93-
Text => Text,
9439
Result => R);
9540

9641
Result := R;
@@ -106,53 +51,67 @@ package body SHA256_Bindings_Tests is
10651
pragma Unreferenced (F);
10752

10853
Hash : WolfSSL.SHA256_Hash;
109-
Text : WolfSSL.SHA256_As_String;
11054
R : Integer;
11155

11256
Input : constant WolfSSL.Byte_Array := Test_Support.Bytes ("asdf");
11357

114-
Expected_Text : constant WolfSSL.SHA256_As_String :=
115-
Test_Support.SHA256_Text
116-
("F0E4C2F76C58916EC258F246851BEA091D14D4247A2FC3E18694461B1816E13B");
58+
Expected_Hash : constant WolfSSL.Byte_Array :=
59+
Test_Support.Hex_Bytes
60+
(Test_Support.SHA256_Text
61+
("F0E4C2F76C58916EC258F246851BEA091D14D4247A2FC3E18694461B1816E13B"));
11762
begin
118-
Compute_SHA256 (Input => Input, Hash => Hash, Text => Text, Result => R);
63+
Compute_SHA256 (Input => Input, Hash => Hash, Result => R);
11964

12065
Test_Support.Assert_Success (R, "SHA256(asdf)");
12166

122-
AUnit.Assertions.Assert
123-
(Text = Expected_Text,
124-
"SHA256('asdf') hex mismatch. Got: " & Text);
125-
126-
Assert_Text_Matches_Hash
127-
(Hash => Hash, Text => Text, Msg => "SHA256('asdf')");
67+
-- Compare the hash bytes
68+
declare
69+
use type WolfSSL.Byte_Array;
70+
Hash_Bytes : WolfSSL.Byte_Array (1 .. 32);
71+
J : WolfSSL.Byte_Index := 1;
72+
begin
73+
for I in Hash'Range loop
74+
Hash_Bytes (J) := Hash (I);
75+
J := WolfSSL.Byte_Index'Succ (J);
76+
end loop;
77+
AUnit.Assertions.Assert
78+
(Hash_Bytes = Expected_Hash,
79+
"SHA256('asdf') hash mismatch");
80+
end;
12881
end Test_SHA256_Asdf_Known_Vector;
12982

13083
procedure Test_SHA256_Empty_Message (F : in out Fixture) is
13184
pragma Unreferenced (F);
13285

13386
Hash : WolfSSL.SHA256_Hash;
134-
Text : WolfSSL.SHA256_As_String;
13587
R : Integer;
13688

13789
-- Represent empty input as a null range, matching the existing test style.
13890
Empty : constant WolfSSL.Byte_Array := (1 .. 0 => <>);
13991

140-
Expected_Text : constant WolfSSL.SHA256_As_String :=
141-
Test_Support.SHA256_Text
142-
("E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855");
92+
Expected_Hash : constant WolfSSL.Byte_Array :=
93+
Test_Support.Hex_Bytes
94+
(Test_Support.SHA256_Text
95+
("E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"));
14396
begin
144-
Compute_SHA256 (Input => Empty, Hash => Hash, Text => Text, Result => R);
97+
Compute_SHA256 (Input => Empty, Hash => Hash, Result => R);
14598

14699
Test_Support.Assert_Success (R, "SHA256(empty)");
147100

148-
AUnit.Assertions.Assert
149-
(Text = Expected_Text,
150-
"SHA256('') hex mismatch. Got: " & Text);
151-
152-
Assert_Text_Matches_Hash
153-
(Hash => Hash,
154-
Text => Text,
155-
Msg => "SHA256('')");
101+
-- Compare the hash bytes
102+
declare
103+
use type WolfSSL.Byte_Array;
104+
Hash_Bytes : WolfSSL.Byte_Array (1 .. 32);
105+
J : WolfSSL.Byte_Index := 1;
106+
begin
107+
for I in Hash'Range loop
108+
Hash_Bytes (J) := Hash (I);
109+
J := WolfSSL.Byte_Index'Succ (J);
110+
end loop;
111+
AUnit.Assertions.Assert
112+
(Hash_Bytes = Expected_Hash,
113+
"SHA256('') hash mismatch");
114+
end;
156115
end Test_SHA256_Empty_Message;
157116

158117
----------------------------------------------------------------------------

wrapper/Ada/wolfssl.adb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,26 +1367,11 @@ package body WolfSSL is
13671367

13681368
procedure Finalize_SHA256 (SHA256 : in out SHA256_Type;
13691369
Hash : out SHA256_Hash;
1370-
Text : out SHA256_As_String;
13711370
Result : out Integer) is
1372-
subtype Unsigned_8 is Interfaces.Unsigned_8;
1373-
1374-
use type Unsigned_8;
1375-
13761371
R : int;
1377-
Hex_Chars : constant array (Unsigned_8 range 0 .. 15) of Character :=
1378-
"0123456789ABCDEF";
1379-
I : Integer;
1380-
C : Integer;
13811372
begin
13821373
R := SHA256_Final (SHA256, Hash);
13831374
Result := Integer (R);
1384-
for Index in Positive range 1 .. 32 loop
1385-
I := 2 * (Index - 1) + 1;
1386-
C := Interfaces.C.char'Pos (Hash (size_t (Index)));
1387-
Text (I+0) := Hex_Chars ((Unsigned_8 (C) and 16#F0#) / 16);
1388-
Text (I+1) := Hex_Chars (Unsigned_8 (C) and 16#0F#);
1389-
end loop;
13901375
exception
13911376
when others =>
13921377
Result := Exception_Error;

wrapper/Ada/wolfssl.ads

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,6 @@ package WolfSSL with SPARK_Mode is
675675

676676
procedure Finalize_SHA256 (SHA256 : in out SHA256_Type;
677677
Hash : out SHA256_Hash;
678-
Text : out SHA256_As_String;
679678
Result : out Integer) with
680679
Pre => Is_Valid (SHA256);
681680
-- If successful Result = 0.

0 commit comments

Comments
 (0)