xref: /isa-l_crypto/sha512_mb/sha512_mb.c (revision 9999e13a5707fddf47ed4073f70fb5ea5ee542b0)
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 "sha512_mb_internal.h"
31 #include "isal_crypto_api.h"
32 #include "multi_buffer.h"
33 
34 int
35 isal_sha512_ctx_mgr_init(ISAL_SHA512_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         _sha512_ctx_mgr_init(mgr);
48 
49         return 0;
50 }
51 
52 int
53 isal_sha512_ctx_mgr_submit(ISAL_SHA512_HASH_CTX_MGR *mgr, ISAL_SHA512_HASH_CTX *ctx_in,
54                            ISAL_SHA512_HASH_CTX **ctx_out, const void *buffer, const uint32_t len,
55                            const ISAL_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         /* OK to have NULL source buffer when flags is HASH_FIRST or HASH_LAST */
63         if (buffer == NULL && (flags == ISAL_HASH_UPDATE || flags == ISAL_HASH_ENTIRE))
64                 return ISAL_CRYPTO_ERR_NULL_SRC;
65 #endif
66 
67 #ifdef FIPS_MODE
68         if (isal_self_tests())
69                 return ISAL_CRYPTO_ERR_SELF_TEST;
70 #endif
71 
72         *ctx_out = _sha512_ctx_mgr_submit(mgr, ctx_in, buffer, len, flags);
73 
74 #ifdef SAFE_PARAM
75         if (*ctx_out != NULL &&
76             (ISAL_SHA512_HASH_CTX *) (*ctx_out)->error != ISAL_HASH_CTX_ERROR_NONE) {
77                 ISAL_SHA512_HASH_CTX *cp = (ISAL_SHA512_HASH_CTX *) (*ctx_out);
78 
79                 if (cp->error == ISAL_HASH_CTX_ERROR_INVALID_FLAGS)
80                         return ISAL_CRYPTO_ERR_INVALID_FLAGS;
81                 if (cp->error == ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING)
82                         return ISAL_CRYPTO_ERR_ALREADY_PROCESSING;
83                 if (cp->error == ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED)
84                         return ISAL_CRYPTO_ERR_ALREADY_COMPLETED;
85         }
86 #endif
87         return 0;
88 }
89 
90 int
91 isal_sha512_ctx_mgr_flush(ISAL_SHA512_HASH_CTX_MGR *mgr, ISAL_SHA512_HASH_CTX **ctx_out)
92 {
93 #ifdef SAFE_PARAM
94         if (mgr == NULL)
95                 return ISAL_CRYPTO_ERR_NULL_MGR;
96         if (ctx_out == NULL)
97                 return ISAL_CRYPTO_ERR_NULL_CTX;
98 #endif
99 
100 #ifdef FIPS_MODE
101         if (isal_self_tests())
102                 return ISAL_CRYPTO_ERR_SELF_TEST;
103 #endif
104 
105         *ctx_out = _sha512_ctx_mgr_flush(mgr);
106 
107         return 0;
108 }
109 
110 /*
111  * =============================================================================
112  * LEGACY / DEPRECATED API
113  * =============================================================================
114  */
115 
116 void
117 sha512_ctx_mgr_init(ISAL_SHA512_HASH_CTX_MGR *mgr)
118 {
119         _sha512_ctx_mgr_init(mgr);
120 }
121 
122 ISAL_SHA512_HASH_CTX *
123 sha512_ctx_mgr_submit(ISAL_SHA512_HASH_CTX_MGR *mgr, ISAL_SHA512_HASH_CTX *ctx, const void *buffer,
124                       uint32_t len, ISAL_HASH_CTX_FLAG flags)
125 {
126         return _sha512_ctx_mgr_submit(mgr, ctx, buffer, len, flags);
127 }
128 
129 ISAL_SHA512_HASH_CTX *
130 sha512_ctx_mgr_flush(ISAL_SHA512_HASH_CTX_MGR *mgr)
131 {
132         return _sha512_ctx_mgr_flush(mgr);
133 }
134