1 /** @file
2   Application for Diffie-Hellman Primitives Validation.
3 
4 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution.  The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9 
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include "Cryptest.h"
16 
17 /**
18   Validate UEFI-OpenSSL DH Interfaces.
19 
20   @retval  EFI_SUCCESS  Validation succeeded.
21   @retval  EFI_ABORTED  Validation failed.
22 
23 **/
24 EFI_STATUS
ValidateCryptDh(VOID)25 ValidateCryptDh (
26   VOID
27   )
28 {
29   VOID    *Dh1;
30   VOID    *Dh2;
31   UINT8   Prime[64];
32   UINT8   PublicKey1[64];
33   UINTN   PublicKey1Length;
34   UINT8   PublicKey2[64];
35   UINTN   PublicKey2Length;
36   UINT8   Key1[64];
37   UINTN   Key1Length;
38   UINT8   Key2[64];
39   UINTN   Key2Length;
40   BOOLEAN Status;
41 
42   Print (L"\nUEFI-OpenSSL DH Engine Testing:\n");
43 
44   //
45   // Initialize Key Length
46   //
47   PublicKey1Length = sizeof (PublicKey1);
48   PublicKey2Length = sizeof (PublicKey2);
49   Key1Length       = sizeof (Key1);
50   Key2Length       = sizeof (Key2);
51 
52   //
53   // Generate & Initialize DH Context
54   //
55   Print (L"- Context1 ... ");
56   Dh1 = DhNew ();
57   if (Dh1 == NULL) {
58     Print (L"[Fail]");
59     return EFI_ABORTED;
60   }
61 
62   Print (L"Context2 ... ");
63   Dh2 = DhNew ();
64   if (Dh2 == NULL) {
65     Print (L"[Fail]");
66     return EFI_ABORTED;
67   }
68 
69   Print (L"Parameter1 ... ");
70   Status = DhGenerateParameter (Dh1, 2, 64, Prime);
71   if (!Status) {
72     Print (L"[Fail]");
73     return EFI_ABORTED;
74   }
75 
76   Print (L"Parameter2 ... ");
77   Status = DhSetParameter (Dh2, 2, 64, Prime);
78   if (!Status) {
79     Print (L"[Fail]");
80     return EFI_ABORTED;
81   }
82 
83   Print (L"Generate key1 ... ");
84   Status = DhGenerateKey (Dh1, PublicKey1, &PublicKey1Length);
85   if (!Status) {
86     Print (L"[Fail]");
87     return EFI_ABORTED;
88   }
89 
90   Print (L"Generate key2 ... ");
91   Status = DhGenerateKey (Dh2, PublicKey2, &PublicKey2Length);
92   if (!Status) {
93     Print (L"[Fail]");
94     return EFI_ABORTED;
95   }
96 
97   Print (L"Compute key1 ... ");
98   Status = DhComputeKey (Dh1, PublicKey2, PublicKey2Length, Key1, &Key1Length);
99   if (!Status) {
100     Print (L"[Fail]");
101     return EFI_ABORTED;
102   }
103 
104   Print (L"Compute key2 ... ");
105   Status = DhComputeKey (Dh2, PublicKey1, PublicKey1Length, Key2, &Key2Length);
106   if (!Status) {
107     Print (L"[Fail]");
108     return EFI_ABORTED;
109   }
110 
111   Print (L"Compare Keys ... ");
112   if (Key1Length != Key2Length) {
113     Print (L"[Fail]");
114     return EFI_ABORTED;
115   }
116 
117   if (CompareMem (Key1, Key2, Key1Length) != 0) {
118     Print (L"[Fail]");
119     return EFI_ABORTED;
120   }
121 
122   Print (L"[Pass]\n");
123 
124   return EFI_SUCCESS;
125 }