xref: /isa-l_crypto/mh_sha1/mh_sha1_param_test.c (revision 46bddbb784bcf0d2d12d16395f565df9044ef0fc)
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
test_mh_sha1_init_api(void)41 test_mh_sha1_init_api(void)
42 {
43         int ret, retval = 1;
44         struct isal_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 #ifdef FIPS_MODE
54         // Check for invalid algorithm error
55         ret = isal_mh_sha1_init(update_ctx);
56         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_init);
57 #else
58         ret = isal_mh_sha1_init(NULL);
59         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_init);
60 
61         ret = isal_mh_sha1_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
test_mh_sha1_update_api(void)73 test_mh_sha1_update_api(void)
74 {
75         int ret, retval = 1;
76         struct isal_mh_sha1_ctx *update_ctx = NULL;
77         uint8_t *buff = NULL;
78         const char *func_name = "isal_mh_sha1_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_sha1_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_sha1_update(NULL, buff, TEST_LEN);
93         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_update);
94 
95         ret = isal_mh_sha1_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_sha1_update(update_ctx, buff, TEST_LEN);
99         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_update);
100 #endif
101         retval = 0;
102 
103 exit_update:
104         free(update_ctx);
105         free(buff);
106 
107         return retval;
108 }
109 
110 static int
test_mh_sha1_finalize_api(void)111 test_mh_sha1_finalize_api(void)
112 {
113         int ret, retval = 1;
114         struct isal_mh_sha1_ctx *update_ctx = NULL;
115         uint32_t hash_test[ISAL_SHA1_DIGEST_WORDS] = { 0 };
116         const char *func_name = "isal_mh_sha1_finalize";
117 
118         update_ctx = malloc(sizeof(*update_ctx));
119         if (update_ctx == NULL) {
120                 printf("malloc failed test aborted\n");
121                 return retval;
122         }
123 
124 #ifdef FIPS_MODE
125         // Check for invalid algorithm error
126         ret = isal_mh_sha1_finalize(update_ctx, hash_test);
127         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_finalize);
128 #else
129         ret = isal_mh_sha1_finalize(NULL, hash_test);
130         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_finalize);
131 
132         ret = isal_mh_sha1_finalize(update_ctx, NULL);
133         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_AUTH, func_name, exit_finalize);
134 
135         ret = isal_mh_sha1_finalize(update_ctx, hash_test);
136         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_finalize);
137 #endif
138         retval = 0;
139 
140 exit_finalize:
141         free(update_ctx);
142 
143         return retval;
144 }
145 #endif /* SAFE_PARAM */
146 
147 int
main(void)148 main(void)
149 {
150         int fail = 0;
151 #ifdef SAFE_PARAM
152         fail |= test_mh_sha1_init_api();
153         fail |= test_mh_sha1_update_api();
154         fail |= test_mh_sha1_finalize_api();
155         printf(fail ? "Fail\n" : "Pass\n");
156 #else
157         printf("Not Executed\n");
158 #endif
159         return fail;
160 }
161