TianoCore EDK2 master
Loading...
Searching...
No Matches
DhTests.c
Go to the documentation of this file.
1
9#include "TestBaseCryptLib.h"
10
11VOID *mDh1;
12VOID *mDh2;
13
15EFIAPI
16TestVerifyDhPreReq (
17 UNIT_TEST_CONTEXT Context
18 )
19{
20 mDh1 = DhNew ();
21 if (mDh1 == NULL) {
22 return UNIT_TEST_ERROR_TEST_FAILED;
23 }
24
25 mDh2 = DhNew ();
26 if (mDh2 == NULL) {
27 return UNIT_TEST_ERROR_TEST_FAILED;
28 }
29
30 return UNIT_TEST_PASSED;
31}
32
33VOID
34EFIAPI
35TestVerifyDhCleanUp (
36 UNIT_TEST_CONTEXT Context
37 )
38{
39 if (mDh1 != NULL) {
40 DhFree (mDh1);
41 mDh1 = NULL;
42 }
43
44 if (mDh2 != NULL) {
45 DhFree (mDh2);
46 mDh2 = NULL;
47 }
48}
49
51EFIAPI
52TestVerifyDhGenerateKey (
53 UNIT_TEST_CONTEXT Context
54 )
55{
56 UINT8 Prime[512];
57 UINT8 PublicKey1[64];
58 UINTN PublicKey1Length;
59 UINT8 PublicKey2[64];
60 UINTN PublicKey2Length;
61 UINT8 Key1[64];
62 UINTN Key1Length;
63 UINT8 Key2[64];
64 UINTN Key2Length;
65 BOOLEAN Status;
66
67 //
68 // Initialize Key Length
69 //
70 PublicKey1Length = sizeof (PublicKey1);
71 PublicKey2Length = sizeof (PublicKey2);
72 Key1Length = sizeof (Key1);
73 Key2Length = sizeof (Key2);
74
75 Status = DhGenerateParameter (mDh1, 2, sizeof (Prime), Prime);
76 UT_ASSERT_TRUE (Status);
77
78 Status = DhSetParameter (mDh2, 2, sizeof (Prime), Prime);
79 UT_ASSERT_TRUE (Status);
80
81 Status = DhGenerateKey (mDh1, PublicKey1, &PublicKey1Length);
82 UT_ASSERT_TRUE (Status);
83
84 Status = DhGenerateKey (mDh2, PublicKey2, &PublicKey2Length);
85 UT_ASSERT_TRUE (Status);
86
87 Status = DhComputeKey (mDh1, PublicKey2, PublicKey2Length, Key1, &Key1Length);
88 UT_ASSERT_TRUE (Status);
89
90 Status = DhComputeKey (mDh2, PublicKey1, PublicKey1Length, Key2, &Key2Length);
91 UT_ASSERT_TRUE (Status);
92
93 UT_ASSERT_EQUAL (Key1Length, Key2Length);
94
95 UT_ASSERT_MEM_EQUAL (Key1, Key2, Key1Length);
96
97 return UNIT_TEST_PASSED;
98}
99
100TEST_DESC mDhTest[] = {
101 //
102 // -----Description--------------------------------Class---------------------Function----------------Pre-----------------Post------------Context
103 //
104 { "TestVerifyDhGenerateKey()", "CryptoPkg.BaseCryptLib.Dh", TestVerifyDhGenerateKey, TestVerifyDhPreReq, TestVerifyDhCleanUp, NULL },
105};
106
107UINTN mDhTestNum = ARRAY_SIZE (mDhTest);
UINT64 UINTN
BOOLEAN EFIAPI DhGenerateParameter(IN OUT VOID *DhContext, IN UINTN Generator, IN UINTN PrimeLength, OUT UINT8 *Prime)
Definition: CryptDh.c:76
VOID *EFIAPI DhNew(VOID)
Definition: CryptDh.c:22
BOOLEAN EFIAPI DhGenerateKey(IN OUT VOID *DhContext, OUT UINT8 *PublicKey, IN OUT UINTN *PublicKeySize)
Definition: CryptDh.c:196
VOID EFIAPI DhFree(IN VOID *DhContext)
Definition: CryptDh.c:42
BOOLEAN EFIAPI DhSetParameter(IN OUT VOID *DhContext, IN UINTN Generator, IN UINTN PrimeLength, IN CONST UINT8 *Prime)
Definition: CryptDh.c:131
BOOLEAN EFIAPI DhComputeKey(IN OUT VOID *DhContext, IN CONST UINT8 *PeerPublicKey, IN UINTN PeerPublicKeySize, OUT UINT8 *Key, IN OUT UINTN *KeySize)
Definition: CryptDh.c:265
#define NULL
Definition: Base.h:319
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length)
Definition: UnitTestLib.h:389
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16