xref: /isa-l_crypto/sm3_mb/sm3_mb.c (revision 6801b27bd9295090e36d2864110576f428efdf61)
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 "sm3_mb.h"
31 #include "sm3_mb_internal.h"
32 #include "isal_crypto_api.h"
33 
34 int
isal_sm3_ctx_mgr_init(ISAL_SM3_HASH_CTX_MGR * mgr)35 isal_sm3_ctx_mgr_init(ISAL_SM3_HASH_CTX_MGR *mgr)
36 {
37 #ifdef FIPS_MODE
38         return ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO;
39 #else
40 #ifdef SAFE_PARAM
41         if (mgr == NULL)
42                 return ISAL_CRYPTO_ERR_NULL_MGR;
43 #endif
44         _sm3_ctx_mgr_init(mgr);
45 
46         return 0;
47 #endif
48 }
49 
50 int
isal_sm3_ctx_mgr_submit(ISAL_SM3_HASH_CTX_MGR * mgr,ISAL_SM3_HASH_CTX * ctx_in,ISAL_SM3_HASH_CTX ** ctx_out,const void * buffer,const uint32_t len,const ISAL_HASH_CTX_FLAG flags)51 isal_sm3_ctx_mgr_submit(ISAL_SM3_HASH_CTX_MGR *mgr, ISAL_SM3_HASH_CTX *ctx_in,
52                         ISAL_SM3_HASH_CTX **ctx_out, const void *buffer, const uint32_t len,
53                         const ISAL_HASH_CTX_FLAG flags)
54 {
55 #ifdef FIPS_MODE
56         return ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO;
57 #else
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         if (buffer == NULL && len != 0)
64                 return ISAL_CRYPTO_ERR_NULL_SRC;
65 #endif
66         ISAL_SM3_HASH_CTX *cp = _sm3_ctx_mgr_submit(mgr, ctx_in, buffer, len, flags);
67 
68         *ctx_out = cp;
69 
70 #ifdef SAFE_PARAM
71         if (cp != NULL && cp->error != ISAL_HASH_CTX_ERROR_NONE) {
72                 if (cp->error == ISAL_HASH_CTX_ERROR_INVALID_FLAGS)
73                         return ISAL_CRYPTO_ERR_INVALID_FLAGS;
74                 if (cp->error == ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING)
75                         return ISAL_CRYPTO_ERR_ALREADY_PROCESSING;
76                 if (cp->error == ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED)
77                         return ISAL_CRYPTO_ERR_ALREADY_COMPLETED;
78         }
79 #endif
80         return 0;
81 #endif
82 }
83 
84 int
isal_sm3_ctx_mgr_flush(ISAL_SM3_HASH_CTX_MGR * mgr,ISAL_SM3_HASH_CTX ** ctx_out)85 isal_sm3_ctx_mgr_flush(ISAL_SM3_HASH_CTX_MGR *mgr, ISAL_SM3_HASH_CTX **ctx_out)
86 {
87 #ifdef FIPS_MODE
88         return ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO;
89 #else
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         *ctx_out = _sm3_ctx_mgr_flush(mgr);
97 
98         return 0;
99 #endif
100 }
101 
102 /*
103  * =============================================================================
104  * LEGACY / DEPRECATED API
105  * =============================================================================
106  */
107 
108 void
sm3_ctx_mgr_init(ISAL_SM3_HASH_CTX_MGR * mgr)109 sm3_ctx_mgr_init(ISAL_SM3_HASH_CTX_MGR *mgr)
110 {
111         _sm3_ctx_mgr_init(mgr);
112 }
113 
114 ISAL_SM3_HASH_CTX *
sm3_ctx_mgr_submit(ISAL_SM3_HASH_CTX_MGR * mgr,ISAL_SM3_HASH_CTX * ctx,const void * buffer,uint32_t len,ISAL_HASH_CTX_FLAG flags)115 sm3_ctx_mgr_submit(ISAL_SM3_HASH_CTX_MGR *mgr, ISAL_SM3_HASH_CTX *ctx, const void *buffer,
116                    uint32_t len, ISAL_HASH_CTX_FLAG flags)
117 {
118         return _sm3_ctx_mgr_submit(mgr, ctx, buffer, len, flags);
119 }
120 
121 ISAL_SM3_HASH_CTX *
sm3_ctx_mgr_flush(ISAL_SM3_HASH_CTX_MGR * mgr)122 sm3_ctx_mgr_flush(ISAL_SM3_HASH_CTX_MGR *mgr)
123 {
124         return _sm3_ctx_mgr_flush(mgr);
125 }
126