1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Crypto Component                                                 */
16 /**                                                                       */
17 /**   Crypto Self Test                                                    */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_CRYPTO_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 #include "nx_crypto_method_self_test.h"
27 
28 #ifndef NX_CRYPTO_DES_KEY_LEN_IN_BITS
29 #define NX_CRYPTO_DES_KEY_LEN_IN_BITS 64
30 #endif
31 
32 
33 #ifdef NX_CRYPTO_SELF_TEST
34 
35 #define INPUT_OUTPUT_LENGTH 64
36 
37 /* 7a52e4d342aa0725 */
38 static UCHAR key_1[] = {
39 0x7a, 0x52, 0xe4, 0xd3, 0x42, 0xaa, 0x07, 0x25,
40 };
41 
42 /* 5a7e7c34266cf730 */
43 static UCHAR iv_1[] = {
44 0x5a, 0x7e, 0x7c, 0x34, 0x26, 0x6c, 0xf7, 0x30,
45 };
46 
47 /* 7e771c6ee4b26db89050e982ba7e9803c8da34606434dd85d2910e538076d00168c9885ba2be03181f65f1e04e83d6ba6880467550bcf099be26dc9d9c0af15a */
48 static UCHAR plain_1[] = {
49 0x7e, 0x77, 0x1c, 0x6e, 0xe4, 0xb2, 0x6d, 0xb8, 0x90, 0x50, 0xe9, 0x82, 0xba, 0x7e, 0x98, 0x03,
50 0xc8, 0xda, 0x34, 0x60, 0x64, 0x34, 0xdd, 0x85, 0xd2, 0x91, 0x0e, 0x53, 0x80, 0x76, 0xd0, 0x01,
51 0x68, 0xc9, 0x88, 0x5b, 0xa2, 0xbe, 0x03, 0x18, 0x1f, 0x65, 0xf1, 0xe0, 0x4e, 0x83, 0xd6, 0xba,
52 0x68, 0x80, 0x46, 0x75, 0x50, 0xbc, 0xf0, 0x99, 0xbe, 0x26, 0xdc, 0x9d, 0x9c, 0x0a, 0xf1, 0x5a,
53 };
54 
55 /* 13570ef15c1e8a71ae9de5552d9c7f7ca12eb830d85e6eb6d0402f627e23b967ba6e15cd97e9c9b64b16fe910903d9cb3757b3078a90c2ba4645fd638fa0725c */
56 static UCHAR secret_1[] = {
57 0x13, 0x57, 0x0e, 0xf1, 0x5c, 0x1e, 0x8a, 0x71, 0xae, 0x9d, 0xe5, 0x55, 0x2d, 0x9c, 0x7f, 0x7c,
58 0xa1, 0x2e, 0xb8, 0x30, 0xd8, 0x5e, 0x6e, 0xb6, 0xd0, 0x40, 0x2f, 0x62, 0x7e, 0x23, 0xb9, 0x67,
59 0xba, 0x6e, 0x15, 0xcd, 0x97, 0xe9, 0xc9, 0xb6, 0x4b, 0x16, 0xfe, 0x91, 0x09, 0x03, 0xd9, 0xcb,
60 0x37, 0x57, 0xb3, 0x07, 0x8a, 0x90, 0xc2, 0xba, 0x46, 0x45, 0xfd, 0x63, 0x8f, 0xa0, 0x72, 0x5c,
61 };
62 
63 static UCHAR output[INPUT_OUTPUT_LENGTH];
64 
65 /**************************************************************************/
66 /*                                                                        */
67 /*  FUNCTION                                               RELEASE        */
68 /*                                                                        */
69 /*    nx_crypto_method_self_test_des                      PORTABLE C      */
70 /*                                                           6.1.7        */
71 /*  AUTHOR                                                                */
72 /*                                                                        */
73 /*    Timothy Stapko, Microsoft Corporation                               */
74 /*                                                                        */
75 /*  DESCRIPTION                                                           */
76 /*                                                                        */
77 /*    This function performs the Known Answer Test for DES crypto method. */
78 /*                                                                        */
79 /*  INPUT                                                                 */
80 /*                                                                        */
81 /*    method_ptr                            Pointer to the crypto method  */
82 /*                                            to be tested.               */
83 /*                                                                        */
84 /*  OUTPUT                                                                */
85 /*                                                                        */
86 /*    status                                Completion status             */
87 /*                                                                        */
88 /*  CALLS                                                                 */
89 /*                                                                        */
90 /*    None                                                                */
91 /*                                                                        */
92 /*  CALLED BY                                                             */
93 /*                                                                        */
94 /*    Application Code                                                    */
95 /*                                                                        */
96 /*  RELEASE HISTORY                                                       */
97 /*                                                                        */
98 /*    DATE              NAME                      DESCRIPTION             */
99 /*                                                                        */
100 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
101 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
102 /*                                            resulting in version 6.1    */
103 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
104 /*                                            renamed FIPS symbol to      */
105 /*                                            self-test,                  */
106 /*                                            resulting in version 6.1.7  */
107 /*                                                                        */
108 /**************************************************************************/
_nx_crypto_method_self_test_des(NX_CRYPTO_METHOD * crypto_method_des,VOID * metadata,UINT metadata_size)109 NX_CRYPTO_KEEP UINT _nx_crypto_method_self_test_des(NX_CRYPTO_METHOD *crypto_method_des,
110                                                     VOID *metadata, UINT metadata_size)
111 {
112 UINT status;
113 VOID *handler = NX_CRYPTO_NULL;
114 
115 
116     /* Validate the crypto method */
117     if(crypto_method_des == NX_CRYPTO_NULL)
118         return(NX_CRYPTO_PTR_ERROR);
119 
120     if (crypto_method_des -> nx_crypto_init)
121     {
122         status = crypto_method_des -> nx_crypto_init(crypto_method_des,
123                                                      key_1,
124                                                      NX_CRYPTO_DES_KEY_LEN_IN_BITS,
125                                                      &handler,
126                                                      metadata,
127                                                      metadata_size);
128 
129         if (status != NX_CRYPTO_SUCCESS)
130         {
131             return(status);
132         }
133     }
134 
135     if (crypto_method_des -> nx_crypto_operation == NX_CRYPTO_NULL)
136     {
137         return(NX_CRYPTO_PTR_ERROR);
138     }
139 
140     /* Encryption. */
141     status = crypto_method_des -> nx_crypto_operation(NX_CRYPTO_ENCRYPT,
142                                                       handler,
143                                                       crypto_method_des,
144                                                       key_1,
145                                                       NX_CRYPTO_DES_KEY_LEN_IN_BITS,
146                                                       plain_1,
147                                                       INPUT_OUTPUT_LENGTH,
148                                                       iv_1,
149                                                       (UCHAR *)output,
150                                                       INPUT_OUTPUT_LENGTH,
151                                                       metadata,
152                                                       metadata_size,
153                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
154 
155     if (status != NX_CRYPTO_SUCCESS)
156     {
157         return(status);
158     }
159 
160     if (NX_CRYPTO_MEMCMP(output, secret_1, INPUT_OUTPUT_LENGTH) != 0)
161     {
162         return(NX_CRYPTO_NOT_SUCCESSFUL);
163     }
164 
165     /* Decryption. */
166     status = crypto_method_des -> nx_crypto_operation(NX_CRYPTO_DECRYPT,
167                                                       handler,
168                                                       crypto_method_des,
169                                                       key_1,
170                                                       NX_CRYPTO_DES_KEY_LEN_IN_BITS,
171                                                       secret_1,
172                                                       INPUT_OUTPUT_LENGTH,
173                                                       iv_1,
174                                                       output,
175                                                       INPUT_OUTPUT_LENGTH,
176                                                       metadata,
177                                                       metadata_size,
178                                                       NX_CRYPTO_NULL, NX_CRYPTO_NULL);
179 
180     if (status != NX_CRYPTO_SUCCESS)
181     {
182         return(status);
183     }
184 
185     if (NX_CRYPTO_MEMCMP(output, plain_1, INPUT_OUTPUT_LENGTH) != 0)
186     {
187         return(NX_CRYPTO_NOT_SUCCESSFUL);
188     }
189 
190     if (crypto_method_des -> nx_crypto_cleanup)
191     {
192         status = crypto_method_des -> nx_crypto_cleanup(metadata);
193     }
194 
195     return(status);
196 }
197 #endif
198