xref: /isa-l_crypto/include/sm3_mb.h (revision 3080abda58cde106a7c18c75469cf1480e3d262b)
1 /**********************************************************************
2   Copyright(c) 2011-2020 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 #ifndef _SM3_MB_H_
31 #define _SM3_MB_H_
32 
33 /**
34  *  @file sm3_mb.h
35  *  @brief Multi-buffer CTX API SM3 function prototypes and structures
36  */
37 
38 #include <stdint.h>
39 #include <string.h>
40 #include "multi_buffer.h"
41 #include "types.h"
42 
43 #ifndef _MSC_VER
44 #include <stdbool.h>
45 #endif
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 /*
51  * Define enums from API v2.24, so applications that were using this version
52  * will still be compiled successfully.
53  * This list does not need to be extended for new enums.
54  */
55 #ifndef NO_COMPAT_ISAL_CRYPTO_API_2_24
56 #define SM3_DIGEST_NWORDS ISAL_SM3_DIGEST_NWORDS
57 #define SM3_MAX_LANES     ISAL_SM3_MAX_LANES
58 #define SM3_BLOCK_SIZE    ISAL_SM3_BLOCK_SIZE
59 #define sm3_digest_array  isal_sm3_digest_array
60 #define SM3_JOB           ISAL_SM3_JOB
61 #define SM3_MB_ARGS_X16   ISAL_SM3_MB_ARGS_X16
62 #define SM3_LANE_DATA     ISAL_SM3_LANE_DATA
63 #define SM3_MB_JOB_MGR    ISAL_SM3_MB_JOB_MGR
64 #define SM3_HASH_CTX_MGR  ISAL_SM3_HASH_CTX_MGR
65 #define SM3_HASH_CTX      ISAL_SM3_HASH_CTX
66 #endif /* !NO_COMPAT_ISAL_CRYPTO_API_2_24 */
67 
68 #define ISAL_SM3_DIGEST_NWORDS 8 /* Word in SM3 is 32-bit */
69 #define ISAL_SM3_MAX_LANES     16
70 #define ISAL_SM3_BLOCK_SIZE    64
71 
72 typedef uint32_t isal_sm3_digest_array[ISAL_SM3_DIGEST_NWORDS][ISAL_SM3_MAX_LANES];
73 
74 /** @brief Scheduler layer - Holds info describing a single SM3 job for the multi-buffer manager */
75 
76 typedef struct {
77         uint8_t *buffer; //!< pointer to data buffer for this job
78         uint64_t len;    //!< length of buffer for this job in blocks.
79         DECLARE_ALIGNED(uint32_t result_digest[ISAL_SM3_DIGEST_NWORDS], 64);
80         ISAL_JOB_STS status; //!< output job status
81         void *user_data;     //!< pointer for user's job-related data
82 } ISAL_SM3_JOB;
83 
84 /** @brief Scheduler layer -  Holds arguments for submitted SM3 job */
85 
86 typedef struct {
87         isal_sm3_digest_array digest;
88         uint8_t *data_ptr[ISAL_SM3_MAX_LANES];
89 } ISAL_SM3_MB_ARGS_X16;
90 
91 /** @brief Scheduler layer - Lane data */
92 
93 typedef struct {
94         ISAL_SM3_JOB *job_in_lane;
95 } ISAL_SM3_LANE_DATA;
96 
97 /** @brief Scheduler layer - Holds state for multi-buffer SM3 jobs */
98 
99 typedef struct {
100         ISAL_SM3_MB_ARGS_X16 args;
101         uint32_t lens[ISAL_SM3_MAX_LANES];
102         uint64_t unused_lanes; //!< each nibble is index (0...3 or 0...7) of unused lanes, nibble 4
103                                //!< or 8 is set to F as a flag
104         ISAL_SM3_LANE_DATA ldata[ISAL_SM3_MAX_LANES];
105         uint32_t num_lanes_inuse;
106 } ISAL_SM3_MB_JOB_MGR;
107 
108 /** @brief Context layer - Holds state for multi-buffer SM3 jobs */
109 
110 typedef struct {
111         ISAL_SM3_MB_JOB_MGR mgr;
112 } ISAL_SM3_HASH_CTX_MGR;
113 
114 /** @brief Context layer - Holds info describing a single SM3 job for the multi-buffer CTX manager
115  */
116 
117 typedef struct {
118         ISAL_SM3_JOB job;                // Must be at struct offset 0.
119         ISAL_HASH_CTX_STS status;        //!< Context status flag
120         ISAL_HASH_CTX_ERROR error;       //!< Context error flag
121         uint64_t total_length;           //!< Running counter of length processed for this CTX's job
122         const void *incoming_buffer;     //!< pointer to data input buffer for this CTX's job
123         uint32_t incoming_buffer_length; //!< length of buffer for this job in bytes.
124         uint8_t partial_block_buffer[ISAL_SM3_BLOCK_SIZE * 2]; //!< CTX partial blocks
125         uint32_t partial_block_buffer_length;
126         void *user_data; //!< pointer for user to keep any job-related data
127 } ISAL_SM3_HASH_CTX;
128 
129 /******************** multibinary function prototypes **********************/
130 
131 /**
132  * @brief Initialize the SM3 multi-buffer manager structure.
133  *
134  * @param mgr	Structure holding context level state info
135  * @returns void
136  * @deprecated Please use isal_sm3_ctx_mgr_init() instead.
137  */
138 ISAL_DEPRECATED("Please use isal_sm3_ctx_mgr_init() instead.")
139 void
140 sm3_ctx_mgr_init(ISAL_SM3_HASH_CTX_MGR *mgr);
141 
142 /**
143  * @brief  Submit a new SM3 job to the multi-buffer manager.
144  *
145  * @param  mgr Structure holding context level state info
146  * @param  ctx Structure holding ctx job info
147  * @param  buffer Pointer to buffer to be processed
148  * @param  len Length of buffer (in bytes) to be processed
149  * @param  flags Input flag specifying job type (first, update, last or entire)
150  * @returns NULL if no jobs complete or pointer to jobs structure.
151  * @deprecated Please use isal_sm3_ctx_mgr_submit() instead.
152  */
153 ISAL_DEPRECATED("Please use isal_sm3_ctx_mgr_submit() instead.")
154 ISAL_SM3_HASH_CTX *
155 sm3_ctx_mgr_submit(ISAL_SM3_HASH_CTX_MGR *mgr, ISAL_SM3_HASH_CTX *ctx, const void *buffer,
156                    uint32_t len, ISAL_HASH_CTX_FLAG flags);
157 
158 /**
159  * @brief Finish all submitted SM3 jobs and return when complete.
160  *
161  * @param mgr	Structure holding context level state info
162  * @returns NULL if no jobs to complete or pointer to jobs structure.
163  * @deprecated Please use isal_sm3_ctx_mgr_flush() instead.
164  */
165 ISAL_DEPRECATED("Please use isal_sm3_ctx_mgr_flush() instead.")
166 ISAL_SM3_HASH_CTX *
167 sm3_ctx_mgr_flush(ISAL_SM3_HASH_CTX_MGR *mgr);
168 
169 /**
170  * @brief Initialize the SM3 multi-buffer manager structure.
171  *
172  * @param mgr Structure holding context level state info
173  * @return Operation status
174  * @retval 0 on success
175  * @retval Non-zero \a ISAL_CRYPTO_ERR on failure
176  */
177 int
178 isal_sm3_ctx_mgr_init(ISAL_SM3_HASH_CTX_MGR *mgr);
179 
180 /**
181  * @brief Submit a new SM3 job to the multi-buffer manager.
182  *
183  * @param[in] mgr Structure holding context level state info
184  * @param[in] ctx_in Structure holding ctx job info
185  * @param[out] ctx_out Pointer address to output job ctx info.
186  *                     Modified to point to completed job structure or
187  *                     NULL if no jobs completed.
188  * @param[in] buffer Pointer to buffer to be processed
189  * @param[in] len Length of buffer (in bytes) to be processed
190  * @param[in] flags Input flag specifying job type (first, update, last or entire)
191  * @return Operation status
192  * @retval 0 on success
193  * @retval Non-zero \a ISAL_CRYPTO_ERR on failure
194  */
195 int
196 isal_sm3_ctx_mgr_submit(ISAL_SM3_HASH_CTX_MGR *mgr, ISAL_SM3_HASH_CTX *ctx_in,
197                         ISAL_SM3_HASH_CTX **ctx_out, const void *buffer, const uint32_t len,
198                         const ISAL_HASH_CTX_FLAG flags);
199 
200 /**
201  * @brief Finish all submitted SM3 jobs and return when complete.
202  *
203  * @param[in] mgr Structure holding context level state info
204  * @param[out] ctx_out Pointer address to output job ctx info.
205  *                     Modified to point to completed job structure or
206  *                     NULL if no jobs completed.
207  * @return Operation status
208  * @retval 0 on success
209  * @retval Non-zero \a ISAL_CRYPTO_ERR on failure
210  */
211 int
212 isal_sm3_ctx_mgr_flush(ISAL_SM3_HASH_CTX_MGR *mgr, ISAL_SM3_HASH_CTX **ctx_out);
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 #endif
218