xref: /isa-l_crypto/mh_sha1/mh_sha1_param_test.c (revision 6a8a917c326c2ff122456e880cbb7bb196c8924f)
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_sha1.h"
35 #include "test.h"
36 
37 #ifdef SAFE_PARAM
38 #define TEST_LEN 16 * 1024
39 
40 static int
41 test_mh_sha1_init_api(void)
42 {
43         int ret, retval = 1;
44         struct mh_sha1_ctx *update_ctx = NULL;
45         const char *func_name = "isal_mh_sha1_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         ret = isal_mh_sha1_init(NULL);
54         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_init);
55 
56         ret = isal_mh_sha1_init(update_ctx);
57 #ifdef FIPS_MODE
58         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_init);
59 #else
60         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_init);
61 #endif
62         retval = 0;
63 
64 exit_init:
65         free(update_ctx);
66 
67         return retval;
68 }
69 
70 static int
71 test_mh_sha1_update_api(void)
72 {
73         int ret, retval = 1;
74         struct mh_sha1_ctx *update_ctx = NULL;
75         uint8_t *buff = NULL;
76         const char *func_name = "isal_mh_sha1_update";
77 
78         update_ctx = malloc(sizeof(*update_ctx));
79         buff = malloc(TEST_LEN);
80         if (update_ctx == NULL || buff == NULL) {
81                 printf("malloc failed test aborted\n");
82                 goto exit_update;
83         }
84 
85         ret = isal_mh_sha1_update(NULL, buff, TEST_LEN);
86         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_update);
87 
88         ret = isal_mh_sha1_update(update_ctx, NULL, TEST_LEN);
89         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_SRC, func_name, exit_update);
90 
91         ret = isal_mh_sha1_update(update_ctx, buff, TEST_LEN);
92 #ifdef FIPS_MODE
93         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_update);
94 #else
95         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_update);
96 #endif
97         retval = 0;
98 
99 exit_update:
100         free(update_ctx);
101         free(buff);
102 
103         return retval;
104 }
105 
106 static int
107 test_mh_sha1_finalize_api(void)
108 {
109         int ret, retval = 1;
110         struct mh_sha1_ctx *update_ctx = NULL;
111         uint32_t hash_test[SHA1_DIGEST_WORDS] = { 0 };
112         const char *func_name = "isal_mh_sha1_finalize";
113 
114         update_ctx = malloc(sizeof(*update_ctx));
115         if (update_ctx == NULL) {
116                 printf("malloc failed test aborted\n");
117                 return retval;
118         }
119 
120         ret = isal_mh_sha1_finalize(NULL, hash_test);
121         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_finalize);
122 
123         ret = isal_mh_sha1_finalize(update_ctx, NULL);
124         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_AUTH, func_name, exit_finalize);
125 
126         ret = isal_mh_sha1_finalize(update_ctx, hash_test);
127 #ifdef FIPS_MODE
128         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_finalize);
129 #else
130         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_finalize);
131 #endif
132         retval = 0;
133 
134 exit_finalize:
135         free(update_ctx);
136 
137         return retval;
138 }
139 #endif /* SAFE_PARAM */
140 
141 int
142 main(void)
143 {
144         int fail = 0;
145 #ifdef SAFE_PARAM
146         fail |= test_mh_sha1_init_api();
147         fail |= test_mh_sha1_update_api();
148         fail |= test_mh_sha1_finalize_api();
149         printf(fail ? "Fail\n" : "Pass\n");
150 #else
151         printf("Not Executed\n");
152 #endif
153         return fail;
154 }
155