xref: /isa-l_crypto/sha1_mb/sha1_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 "sha1_mb.h"
31 #include "isal_crypto_api.h"
32 #include "multi_buffer.h"
33 
34 int
35 isal_sha1_ctx_mgr_init(SHA1_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         sha1_ctx_mgr_init(mgr);
48 
49         return 0;
50 }
51 
52 int
53 isal_sha1_ctx_mgr_submit(SHA1_HASH_CTX_MGR *mgr, SHA1_HASH_CTX *ctx_in, SHA1_HASH_CTX **ctx_out,
54                          const void *buffer, const uint32_t len, const HASH_CTX_FLAG flags)
55 {
56 #ifdef SAFE_PARAM
57         if (mgr == NULL)
58                 return ISAL_CRYPTO_ERR_NULL_MGR;
59         if (ctx_in == NULL || ctx_out == NULL)
60                 return ISAL_CRYPTO_ERR_NULL_CTX;
61         if (buffer == NULL)
62                 return ISAL_CRYPTO_ERR_NULL_SRC;
63 #endif
64 
65 #ifdef FIPS_MODE
66         if (isal_self_tests())
67                 return ISAL_CRYPTO_ERR_SELF_TEST;
68 #endif
69 
70         *ctx_out = sha1_ctx_mgr_submit(mgr, ctx_in, buffer, len, flags);
71 
72 #ifdef SAFE_PARAM
73         if (*ctx_out != NULL && (SHA1_HASH_CTX *) (*ctx_out)->error != HASH_CTX_ERROR_NONE) {
74                 SHA1_HASH_CTX *cp = (SHA1_HASH_CTX *) (*ctx_out);
75 
76                 if (cp->error == HASH_CTX_ERROR_INVALID_FLAGS)
77                         return ISAL_CRYPTO_ERR_INVALID_FLAGS;
78                 if (cp->error == HASH_CTX_ERROR_ALREADY_PROCESSING)
79                         return ISAL_CRYPTO_ERR_ALREADY_PROCESSING;
80                 if (cp->error == HASH_CTX_ERROR_ALREADY_COMPLETED)
81                         return ISAL_CRYPTO_ERR_ALREADY_COMPLETED;
82         }
83 #endif
84         return 0;
85 }
86 
87 int
88 isal_sha1_ctx_mgr_flush(SHA1_HASH_CTX_MGR *mgr, SHA1_HASH_CTX **ctx_out)
89 {
90 #ifdef SAFE_PARAM
91         if (mgr == NULL)
92                 return ISAL_CRYPTO_ERR_NULL_MGR;
93         if (ctx_out == NULL)
94                 return ISAL_CRYPTO_ERR_NULL_CTX;
95 #endif
96 
97 #ifdef FIPS_MODE
98         if (isal_self_tests())
99                 return ISAL_CRYPTO_ERR_SELF_TEST;
100 #endif
101 
102         *ctx_out = sha1_ctx_mgr_flush(mgr);
103 
104         return 0;
105 }
106