xref: /isa-l_crypto/sha1_mb/sha1_mb.c (revision 8cb7fe780eac8ee5f1e0aa3ca37466e89c673ccd)
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 "sha1_mb.h"
31 #include "sha1_mb_internal.h"
32 #include "isal_crypto_api.h"
33 #include "multi_buffer.h"
34 
35 int
isal_sha1_ctx_mgr_init(ISAL_SHA1_HASH_CTX_MGR * mgr)36 isal_sha1_ctx_mgr_init(ISAL_SHA1_HASH_CTX_MGR *mgr)
37 {
38 #ifdef SAFE_PARAM
39         if (mgr == NULL)
40                 return ISAL_CRYPTO_ERR_NULL_MGR;
41 #endif
42 
43 #ifdef FIPS_MODE
44         if (isal_self_tests())
45                 return ISAL_CRYPTO_ERR_SELF_TEST;
46 #endif
47 
48         _sha1_ctx_mgr_init(mgr);
49 
50         return 0;
51 }
52 
53 int
isal_sha1_ctx_mgr_submit(ISAL_SHA1_HASH_CTX_MGR * mgr,ISAL_SHA1_HASH_CTX * ctx_in,ISAL_SHA1_HASH_CTX ** ctx_out,const void * buffer,const uint32_t len,const ISAL_HASH_CTX_FLAG flags)54 isal_sha1_ctx_mgr_submit(ISAL_SHA1_HASH_CTX_MGR *mgr, ISAL_SHA1_HASH_CTX *ctx_in,
55                          ISAL_SHA1_HASH_CTX **ctx_out, const void *buffer, const uint32_t len,
56                          const ISAL_HASH_CTX_FLAG flags)
57 {
58 #ifdef SAFE_PARAM
59         if (mgr == NULL)
60                 return ISAL_CRYPTO_ERR_NULL_MGR;
61         if (ctx_in == NULL || ctx_out == NULL)
62                 return ISAL_CRYPTO_ERR_NULL_CTX;
63         /* OK to have NULL source buffer when flags is ISAL_HASH_FIRST or ISAL_HASH_LAST */
64         if (buffer == NULL && (flags == ISAL_HASH_UPDATE || flags == ISAL_HASH_ENTIRE))
65                 return ISAL_CRYPTO_ERR_NULL_SRC;
66 #endif
67 
68 #ifdef FIPS_MODE
69         if (isal_self_tests())
70                 return ISAL_CRYPTO_ERR_SELF_TEST;
71 #endif
72 
73         *ctx_out = _sha1_ctx_mgr_submit(mgr, ctx_in, buffer, len, flags);
74 
75 #ifdef SAFE_PARAM
76         if (*ctx_out != NULL &&
77             (ISAL_SHA1_HASH_CTX *) (*ctx_out)->error != ISAL_HASH_CTX_ERROR_NONE) {
78                 ISAL_SHA1_HASH_CTX *cp = (ISAL_SHA1_HASH_CTX *) (*ctx_out);
79 
80                 if (cp->error == ISAL_HASH_CTX_ERROR_INVALID_FLAGS)
81                         return ISAL_CRYPTO_ERR_INVALID_FLAGS;
82                 if (cp->error == ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING)
83                         return ISAL_CRYPTO_ERR_ALREADY_PROCESSING;
84                 if (cp->error == ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED)
85                         return ISAL_CRYPTO_ERR_ALREADY_COMPLETED;
86         }
87 #endif
88         return 0;
89 }
90 
91 int
isal_sha1_ctx_mgr_flush(ISAL_SHA1_HASH_CTX_MGR * mgr,ISAL_SHA1_HASH_CTX ** ctx_out)92 isal_sha1_ctx_mgr_flush(ISAL_SHA1_HASH_CTX_MGR *mgr, ISAL_SHA1_HASH_CTX **ctx_out)
93 {
94 #ifdef SAFE_PARAM
95         if (mgr == NULL)
96                 return ISAL_CRYPTO_ERR_NULL_MGR;
97         if (ctx_out == NULL)
98                 return ISAL_CRYPTO_ERR_NULL_CTX;
99 #endif
100 
101 #ifdef FIPS_MODE
102         if (isal_self_tests())
103                 return ISAL_CRYPTO_ERR_SELF_TEST;
104 #endif
105 
106         *ctx_out = _sha1_ctx_mgr_flush(mgr);
107 
108         return 0;
109 }
110 
111 /*
112  * =============================================================================
113  * LEGACY / DEPRECATED API
114  * =============================================================================
115  */
116 
117 void
sha1_ctx_mgr_init(ISAL_SHA1_HASH_CTX_MGR * mgr)118 sha1_ctx_mgr_init(ISAL_SHA1_HASH_CTX_MGR *mgr)
119 {
120         _sha1_ctx_mgr_init(mgr);
121 }
122 
123 ISAL_SHA1_HASH_CTX *
sha1_ctx_mgr_submit(ISAL_SHA1_HASH_CTX_MGR * mgr,ISAL_SHA1_HASH_CTX * ctx,const void * buffer,uint32_t len,ISAL_HASH_CTX_FLAG flags)124 sha1_ctx_mgr_submit(ISAL_SHA1_HASH_CTX_MGR *mgr, ISAL_SHA1_HASH_CTX *ctx, const void *buffer,
125                     uint32_t len, ISAL_HASH_CTX_FLAG flags)
126 {
127         return _sha1_ctx_mgr_submit(mgr, ctx, buffer, len, flags);
128 }
129 
130 ISAL_SHA1_HASH_CTX *
sha1_ctx_mgr_flush(ISAL_SHA1_HASH_CTX_MGR * mgr)131 sha1_ctx_mgr_flush(ISAL_SHA1_HASH_CTX_MGR *mgr)
132 {
133         return _sha1_ctx_mgr_flush(mgr);
134 }
135