xref: /isa-l_crypto/mh_sha256/mh_sha256_param_test.c (revision 122c17795e4bee7e9242fceb7a09851e8f4e3a31)
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 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         ret = isal_mh_sha256_init(NULL);
54         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_init);
55 
56         ret = isal_mh_sha256_init(update_ctx);
57         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_init);
58 
59         retval = 0;
60 
61 exit_init:
62         free(update_ctx);
63 
64         return retval;
65 }
66 
67 static int
68 test_mh_sha256_update_api(void)
69 {
70         int ret, retval = 1;
71         struct mh_sha256_ctx *update_ctx = NULL;
72         uint8_t *buff = NULL;
73         const char *func_name = "isal_mh_sha256_update";
74 
75         update_ctx = malloc(sizeof(*update_ctx));
76         buff = malloc(TEST_LEN);
77         if (update_ctx == NULL || buff == NULL) {
78                 printf("malloc failed test aborted\n");
79                 goto exit_update;
80         }
81 
82         ret = isal_mh_sha256_update(NULL, buff, TEST_LEN);
83         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_update);
84 
85         ret = isal_mh_sha256_update(update_ctx, NULL, TEST_LEN);
86         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_SRC, func_name, exit_update);
87 
88         ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN);
89         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_update);
90 
91         retval = 0;
92 
93 exit_update:
94         free(update_ctx);
95         free(buff);
96 
97         return retval;
98 }
99 
100 static int
101 test_mh_sha256_finalize_api(void)
102 {
103         int ret, retval = 1;
104         struct mh_sha256_ctx *update_ctx = NULL;
105         uint32_t hash_test[SHA256_DIGEST_WORDS] = { 0 };
106         const char *func_name = "isal_mh_sha256_finalize";
107 
108         update_ctx = malloc(sizeof(*update_ctx));
109         if (update_ctx == NULL) {
110                 printf("malloc failed test aborted\n");
111                 return retval;
112         }
113 
114         ret = isal_mh_sha256_finalize(NULL, hash_test);
115         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_finalize);
116 
117         ret = isal_mh_sha256_finalize(update_ctx, NULL);
118         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_AUTH, func_name, exit_finalize);
119 
120         ret = isal_mh_sha256_finalize(update_ctx, hash_test);
121         CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_finalize);
122         retval = 0;
123 
124 exit_finalize:
125         free(update_ctx);
126 
127         return retval;
128 }
129 #endif /* SAFE_PARAM */
130 
131 int
132 main(void)
133 {
134         int fail = 0;
135 #ifdef SAFE_PARAM
136         fail |= test_mh_sha256_init_api();
137         fail |= test_mh_sha256_update_api();
138         fail |= test_mh_sha256_finalize_api();
139         printf(fail ? "Fail\n" : "Pass\n");
140 #else
141         printf("Not Executed\n");
142 #endif
143         return fail;
144 }
145