xref: /isa-l_crypto/sha256_mb/sha256_mb.c (revision 37c1320fef459b6ac01a1fae6f17aa16f54f55fb)
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 "sha256_mb.h"
31 #include "isal_crypto_api.h"
32 #include "multi_buffer.h"
33 
34 int
35 isal_sha256_ctx_mgr_init(SHA256_HASH_CTX_MGR *mgr)
36 {
37 #ifdef SAFE_PARAM
38         if (mgr == NULL)
39                 return ISAL_CRYPTO_ERR_NULL_MGR;
40 #endif
41 
42 #ifdef FIPS_MODE
43         if (isal_self_tests())
44                 return ISAL_CRYPTO_ERR_SELF_TEST;
45 #endif
46 
47         sha256_ctx_mgr_init(mgr);
48 
49         return 0;
50 }
51 
52 int
53 isal_sha256_ctx_mgr_submit(SHA256_HASH_CTX_MGR *mgr, SHA256_HASH_CTX *ctx_in,
54                            SHA256_HASH_CTX **ctx_out, const void *buffer, const uint32_t len,
55                            const HASH_CTX_FLAG flags)
56 {
57 #ifdef SAFE_PARAM
58         if (mgr == NULL)
59                 return ISAL_CRYPTO_ERR_NULL_MGR;
60         if (ctx_in == NULL || ctx_out == NULL)
61                 return ISAL_CRYPTO_ERR_NULL_CTX;
62         if (buffer == NULL)
63                 return ISAL_CRYPTO_ERR_NULL_SRC;
64 #endif
65 
66 #ifdef FIPS_MODE
67         if (isal_self_tests())
68                 return ISAL_CRYPTO_ERR_SELF_TEST;
69 #endif
70 
71         *ctx_out = sha256_ctx_mgr_submit(mgr, ctx_in, buffer, len, flags);
72 
73 #ifdef SAFE_PARAM
74         if (*ctx_out != NULL && (SHA256_HASH_CTX *) (*ctx_out)->error != HASH_CTX_ERROR_NONE) {
75                 SHA256_HASH_CTX *cp = (SHA256_HASH_CTX *) (*ctx_out);
76 
77                 if (cp->error == HASH_CTX_ERROR_INVALID_FLAGS)
78                         return ISAL_CRYPTO_ERR_INVALID_FLAGS;
79                 if (cp->error == HASH_CTX_ERROR_ALREADY_PROCESSING)
80                         return ISAL_CRYPTO_ERR_ALREADY_PROCESSING;
81                 if (cp->error == HASH_CTX_ERROR_ALREADY_COMPLETED)
82                         return ISAL_CRYPTO_ERR_ALREADY_COMPLETED;
83         }
84 #endif
85         return 0;
86 }
87 
88 int
89 isal_sha256_ctx_mgr_flush(SHA256_HASH_CTX_MGR *mgr, SHA256_HASH_CTX **ctx_out)
90 {
91 #ifdef SAFE_PARAM
92         if (mgr == NULL)
93                 return ISAL_CRYPTO_ERR_NULL_MGR;
94         if (ctx_out == NULL)
95                 return ISAL_CRYPTO_ERR_NULL_CTX;
96 #endif
97 
98 #ifdef FIPS_MODE
99         if (isal_self_tests())
100                 return ISAL_CRYPTO_ERR_SELF_TEST;
101 #endif
102 
103         *ctx_out = sha256_ctx_mgr_flush(mgr);
104 
105         return 0;
106 }
107