]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Test/UnitTest/Library/BaseCryptLib/DhTests.c
CryptoPkg: Apply uncrustify changes
[mirror_edk2.git] / CryptoPkg / Test / UnitTest / Library / BaseCryptLib / DhTests.c
1 /** @file
2 Application for Diffie-Hellman Primitives Validation.
3
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "TestBaseCryptLib.h"
10
11 VOID *mDh1;
12 VOID *mDh2;
13
14 UNIT_TEST_STATUS
15 EFIAPI
16 TestVerifyDhPreReq (
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
33 VOID
34 EFIAPI
35 TestVerifyDhCleanUp (
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
50 UNIT_TEST_STATUS
51 EFIAPI
52 TestVerifyDhGenerateKey (
53 UNIT_TEST_CONTEXT Context
54 )
55 {
56 UINT8 Prime[64];
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, 64, Prime);
76 UT_ASSERT_TRUE (Status);
77
78 Status = DhSetParameter (mDh2, 2, 64, 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
100 TEST_DESC mDhTest[] = {
101 //
102 // -----Description--------------------------------Class---------------------Function----------------Pre-----------------Post------------Context
103 //
104 { "TestVerifyDhGenerateKey()", "CryptoPkg.BaseCryptLib.Dh", TestVerifyDhGenerateKey, TestVerifyDhPreReq, TestVerifyDhCleanUp, NULL },
105 };
106
107 UINTN mDhTestNum = ARRAY_SIZE (mDhTest);