summaryrefslogtreecommitdiffstats
path: root/CryptoPkg/Test/UnitTest/Library/BaseCryptLib/DhTests.c
blob: 422b990f2c2d9b03d3ea108e7a98601c27a5ebfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/** @file
  Application for Diffie-Hellman Primitives Validation.

Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "TestBaseCryptLib.h"

VOID    *mDh1;
VOID    *mDh2;

UNIT_TEST_STATUS
EFIAPI
TestVerifyDhPreReq (
  UNIT_TEST_CONTEXT           Context
  )
{
  mDh1 = DhNew ();
  if (mDh1 == NULL) {
    return UNIT_TEST_ERROR_TEST_FAILED;
  }

  mDh2 = DhNew ();
  if (mDh2 == NULL) {
    return UNIT_TEST_ERROR_TEST_FAILED;
  }

  return UNIT_TEST_PASSED;
}

VOID
EFIAPI
TestVerifyDhCleanUp (
  UNIT_TEST_CONTEXT           Context
  )
{
  if (mDh1 != NULL) {
    DhFree (mDh1);
    mDh1 = NULL;
  }
  if (mDh2 != NULL) {
    DhFree (mDh2);
    mDh2 = NULL;
  }
}

UNIT_TEST_STATUS
EFIAPI
TestVerifyDhGenerateKey (
  UNIT_TEST_CONTEXT           Context
  )
{
  UINT8   Prime[64];
  UINT8   PublicKey1[64];
  UINTN   PublicKey1Length;
  UINT8   PublicKey2[64];
  UINTN   PublicKey2Length;
  UINT8   Key1[64];
  UINTN   Key1Length;
  UINT8   Key2[64];
  UINTN   Key2Length;
  BOOLEAN Status;

  //
  // Initialize Key Length
  //
  PublicKey1Length = sizeof (PublicKey1);
  PublicKey2Length = sizeof (PublicKey2);
  Key1Length       = sizeof (Key1);
  Key2Length       = sizeof (Key2);

  Status = DhGenerateParameter (mDh1, 2, 64, Prime);
  UT_ASSERT_TRUE (Status);

  Status = DhSetParameter (mDh2, 2, 64, Prime);
  UT_ASSERT_TRUE (Status);

  Status = DhGenerateKey (mDh1, PublicKey1, &PublicKey1Length);
  UT_ASSERT_TRUE (Status);

  Status = DhGenerateKey (mDh2, PublicKey2, &PublicKey2Length);
  UT_ASSERT_TRUE (Status);

  Status = DhComputeKey (mDh1, PublicKey2, PublicKey2Length, Key1, &Key1Length);
  UT_ASSERT_TRUE (Status);

  Status = DhComputeKey (mDh2, PublicKey1, PublicKey1Length, Key2, &Key2Length);
  UT_ASSERT_TRUE (Status);

  UT_ASSERT_EQUAL (Key1Length, Key2Length);

  UT_ASSERT_MEM_EQUAL (Key1, Key2, Key1Length);

  return UNIT_TEST_PASSED;
}

TEST_DESC mDhTest[] = {
    //
    // -----Description--------------------------------Class---------------------Function----------------Pre-----------------Post------------Context
    //
    {"TestVerifyDhGenerateKey()",        "CryptoPkg.BaseCryptLib.Dh",   TestVerifyDhGenerateKey,  TestVerifyDhPreReq, TestVerifyDhCleanUp, NULL},
};

UINTN mDhTestNum = ARRAY_SIZE(mDhTest);