xref: /isa-l_crypto/mh_sha256/mh_sha256_param_test.c (revision d28f1034f736e3eb791c3cf6bff3e2fa81fb5331)
1 /**********************************************************************
2   Copyright(c) 2024 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "isal_crypto_api.h"
34 #include "mh_sha256.h"
35 #include "test.h"
36 
37 #ifdef SAFE_PARAM
38 #define TEST_LEN 16 * 1024
39 
40 static int
41 test_mh_sha256_init_api(void)
42 {
43         int ret, retval = 1;
44         struct isal_mh_sha256_ctx *update_ctx = NULL;
45         const char *func_name = "isal_mh_sha256_init";
46 
47         update_ctx = malloc(sizeof(*update_ctx));
48         if (update_ctx == NULL) {
49                 printf("malloc failed test aborted\n");
50                 return retval;
51         }
52 
53 #ifdef FIPS_MODE
54         // Check for invalid algorithm error
55         ret = isal_mh_sha256_init(update_ctx);
56         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_init);
57 #else
58         ret = isal_mh_sha256_init(NULL);
59         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_init);
60 
61         ret = isal_mh_sha256_init(update_ctx);
62         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_init);
63 #endif
64         retval = 0;
65 
66 exit_init:
67         free(update_ctx);
68 
69         return retval;
70 }
71 
72 static int
73 test_mh_sha256_update_api(void)
74 {
75         int ret, retval = 1;
76         struct isal_mh_sha256_ctx *update_ctx = NULL;
77         uint8_t *buff = NULL;
78         const char *func_name = "isal_mh_sha256_update";
79 
80         update_ctx = malloc(sizeof(*update_ctx));
81         buff = malloc(TEST_LEN);
82         if (update_ctx == NULL || buff == NULL) {
83                 printf("malloc failed test aborted\n");
84                 goto exit_update;
85         }
86 
87 #ifdef FIPS_MODE
88         // Check for invalid algorithm error
89         ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN);
90         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_update);
91 #else
92         ret = isal_mh_sha256_update(NULL, buff, TEST_LEN);
93         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_update);
94 
95         ret = isal_mh_sha256_update(update_ctx, NULL, TEST_LEN);
96         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_SRC, func_name, exit_update);
97 
98         ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN);
99         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_update);
100 #endif
101 
102         retval = 0;
103 
104 exit_update:
105         free(update_ctx);
106         free(buff);
107 
108         return retval;
109 }
110 
111 static int
112 test_mh_sha256_finalize_api(void)
113 {
114         int ret, retval = 1;
115         struct isal_mh_sha256_ctx *update_ctx = NULL;
116         uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS] = { 0 };
117         const char *func_name = "isal_mh_sha256_finalize";
118 
119         update_ctx = malloc(sizeof(*update_ctx));
120         if (update_ctx == NULL) {
121                 printf("malloc failed test aborted\n");
122                 return retval;
123         }
124 
125 #ifdef FIPS_MODE
126         // Check for invalid algorithm error
127         ret = isal_mh_sha256_finalize(update_ctx, hash_test);
128         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_finalize);
129 #else
130         ret = isal_mh_sha256_finalize(NULL, hash_test);
131         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_finalize);
132 
133         ret = isal_mh_sha256_finalize(update_ctx, NULL);
134         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_AUTH, func_name, exit_finalize);
135 
136         ret = isal_mh_sha256_finalize(update_ctx, hash_test);
137         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_finalize);
138 #endif
139         retval = 0;
140 
141 exit_finalize:
142         free(update_ctx);
143 
144         return retval;
145 }
146 #endif /* SAFE_PARAM */
147 
148 int
149 main(void)
150 {
151         int fail = 0;
152 #ifdef SAFE_PARAM
153         fail |= test_mh_sha256_init_api();
154         fail |= test_mh_sha256_update_api();
155         fail |= test_mh_sha256_finalize_api();
156         printf(fail ? "Fail\n" : "Pass\n");
157 #else
158         printf("Not Executed\n");
159 #endif
160         return fail;
161 }
162