1/*
2 * Header file for multi buffer SHA256 algorithm data structure
3 *
4 * This file is provided under a dual BSD/GPLv2 license.  When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * Copyright(c) 2016 Intel Corporation.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * Contact Information:
21 *     Megha Dey <megha.dey@linux.intel.com>
22 *
23 * BSD LICENSE
24 *
25 * Copyright(c) 2016 Intel Corporation.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 *
31 *   * Redistributions of source code must retain the above copyright
32 *     notice, this list of conditions and the following disclaimer.
33 *   * Redistributions in binary form must reproduce the above copyright
34 *     notice, this list of conditions and the following disclaimer in
35 *     the documentation and/or other materials provided with the
36 *     distribution.
37 *   * Neither the name of Intel Corporation nor the names of its
38 *     contributors may be used to endorse or promote products derived
39 *     from this software without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
45 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54# Macros for defining data structures
55
56# Usage example
57
58#START_FIELDS	# JOB_AES
59###	name		size	align
60#FIELD	_plaintext,	8,	8	# pointer to plaintext
61#FIELD	_ciphertext,	8,	8	# pointer to ciphertext
62#FIELD	_IV,		16,	8	# IV
63#FIELD	_keys,		8,	8	# pointer to keys
64#FIELD	_len,		4,	4	# length in bytes
65#FIELD	_status,	4,	4	# status enumeration
66#FIELD	_user_data,	8,	8	# pointer to user data
67#UNION  _union,         size1,  align1, \
68#	                size2,  align2, \
69#	                size3,  align3, \
70#	                ...
71#END_FIELDS
72#%assign _JOB_AES_size	_FIELD_OFFSET
73#%assign _JOB_AES_align	_STRUCT_ALIGN
74
75#########################################################################
76
77# Alternate "struc-like" syntax:
78#	STRUCT job_aes2
79#	RES_Q	.plaintext,	1
80#	RES_Q	.ciphertext, 	1
81#	RES_DQ	.IV,		1
82#	RES_B	.nested,	_JOB_AES_SIZE, _JOB_AES_ALIGN
83#	RES_U	.union,		size1, align1, \
84#				size2, align2, \
85#				...
86#	ENDSTRUCT
87#	# Following only needed if nesting
88#	%assign job_aes2_size	_FIELD_OFFSET
89#	%assign job_aes2_align	_STRUCT_ALIGN
90#
91# RES_* macros take a name, a count and an optional alignment.
92# The count in in terms of the base size of the macro, and the
93# default alignment is the base size.
94# The macros are:
95# Macro    Base size
96# RES_B	    1
97# RES_W	    2
98# RES_D     4
99# RES_Q     8
100# RES_DQ   16
101# RES_Y    32
102# RES_Z    64
103#
104# RES_U defines a union. It's arguments are a name and two or more
105# pairs of "size, alignment"
106#
107# The two assigns are only needed if this structure is being nested
108# within another. Even if the assigns are not done, one can still use
109# STRUCT_NAME_size as the size of the structure.
110#
111# Note that for nesting, you still need to assign to STRUCT_NAME_size.
112#
113# The differences between this and using "struc" directly are that each
114# type is implicitly aligned to its natural length (although this can be
115# over-ridden with an explicit third parameter), and that the structure
116# is padded at the end to its overall alignment.
117#
118
119#########################################################################
120
121#ifndef _DATASTRUCT_ASM_
122#define _DATASTRUCT_ASM_
123
124#define SZ8			8*SHA256_DIGEST_WORD_SIZE
125#define ROUNDS			64*SZ8
126#define PTR_SZ                  8
127#define SHA256_DIGEST_WORD_SIZE 4
128#define MAX_SHA256_LANES        8
129#define SHA256_DIGEST_WORDS 8
130#define SHA256_DIGEST_ROW_SIZE  (MAX_SHA256_LANES * SHA256_DIGEST_WORD_SIZE)
131#define SHA256_DIGEST_SIZE      (SHA256_DIGEST_ROW_SIZE * SHA256_DIGEST_WORDS)
132#define SHA256_BLK_SZ           64
133
134# START_FIELDS
135.macro START_FIELDS
136 _FIELD_OFFSET = 0
137 _STRUCT_ALIGN = 0
138.endm
139
140# FIELD name size align
141.macro FIELD name size align
142 _FIELD_OFFSET = (_FIELD_OFFSET + (\align) - 1) & (~ ((\align)-1))
143 \name	= _FIELD_OFFSET
144 _FIELD_OFFSET = _FIELD_OFFSET + (\size)
145.if (\align > _STRUCT_ALIGN)
146 _STRUCT_ALIGN = \align
147.endif
148.endm
149
150# END_FIELDS
151.macro END_FIELDS
152 _FIELD_OFFSET = (_FIELD_OFFSET + _STRUCT_ALIGN-1) & (~ (_STRUCT_ALIGN-1))
153.endm
154
155########################################################################
156
157.macro STRUCT p1
158START_FIELDS
159.struc \p1
160.endm
161
162.macro ENDSTRUCT
163 tmp = _FIELD_OFFSET
164 END_FIELDS
165 tmp = (_FIELD_OFFSET - %%tmp)
166.if (tmp > 0)
167	.lcomm	tmp
168.endif
169.endstruc
170.endm
171
172## RES_int name size align
173.macro RES_int p1 p2 p3
174 name = \p1
175 size = \p2
176 align = .\p3
177
178 _FIELD_OFFSET = (_FIELD_OFFSET + (align) - 1) & (~ ((align)-1))
179.align align
180.lcomm name size
181 _FIELD_OFFSET = _FIELD_OFFSET + (size)
182.if (align > _STRUCT_ALIGN)
183 _STRUCT_ALIGN = align
184.endif
185.endm
186
187# macro RES_B name, size [, align]
188.macro RES_B _name, _size, _align=1
189RES_int _name _size _align
190.endm
191
192# macro RES_W name, size [, align]
193.macro RES_W _name, _size, _align=2
194RES_int _name 2*(_size) _align
195.endm
196
197# macro RES_D name, size [, align]
198.macro RES_D _name, _size, _align=4
199RES_int _name 4*(_size) _align
200.endm
201
202# macro RES_Q name, size [, align]
203.macro RES_Q _name, _size, _align=8
204RES_int _name 8*(_size) _align
205.endm
206
207# macro RES_DQ name, size [, align]
208.macro RES_DQ _name, _size, _align=16
209RES_int _name 16*(_size) _align
210.endm
211
212# macro RES_Y name, size [, align]
213.macro RES_Y _name, _size, _align=32
214RES_int _name 32*(_size) _align
215.endm
216
217# macro RES_Z name, size [, align]
218.macro RES_Z _name, _size, _align=64
219RES_int _name 64*(_size) _align
220.endm
221
222#endif
223
224
225########################################################################
226#### Define SHA256 Out Of Order Data Structures
227########################################################################
228
229START_FIELDS    # LANE_DATA
230###     name            size    align
231FIELD   _job_in_lane,   8,      8       # pointer to job object
232END_FIELDS
233
234 _LANE_DATA_size = _FIELD_OFFSET
235 _LANE_DATA_align = _STRUCT_ALIGN
236
237########################################################################
238
239START_FIELDS    # SHA256_ARGS_X4
240###     name            size    align
241FIELD   _digest,        4*8*8,  4       # transposed digest
242FIELD   _data_ptr,      8*8,    8       # array of pointers to data
243END_FIELDS
244
245 _SHA256_ARGS_X4_size  =  _FIELD_OFFSET
246 _SHA256_ARGS_X4_align = _STRUCT_ALIGN
247 _SHA256_ARGS_X8_size  =	_FIELD_OFFSET
248 _SHA256_ARGS_X8_align =	_STRUCT_ALIGN
249
250#######################################################################
251
252START_FIELDS    # MB_MGR
253###     name            size    align
254FIELD   _args,          _SHA256_ARGS_X4_size, _SHA256_ARGS_X4_align
255FIELD   _lens,          4*8,    8
256FIELD   _unused_lanes,  8,      8
257FIELD   _ldata,         _LANE_DATA_size*8, _LANE_DATA_align
258END_FIELDS
259
260 _MB_MGR_size  =  _FIELD_OFFSET
261 _MB_MGR_align =  _STRUCT_ALIGN
262
263_args_digest   =     _args + _digest
264_args_data_ptr =     _args + _data_ptr
265
266#######################################################################
267
268START_FIELDS    #STACK_FRAME
269###     name            size    align
270FIELD   _data,		16*SZ8,   1       # transposed digest
271FIELD   _digest,         8*SZ8,   1       # array of pointers to data
272FIELD   _ytmp,           4*SZ8,   1
273FIELD   _rsp,            8,       1
274END_FIELDS
275
276 _STACK_FRAME_size  =  _FIELD_OFFSET
277 _STACK_FRAME_align =  _STRUCT_ALIGN
278
279#######################################################################
280
281########################################################################
282#### Define constants
283########################################################################
284
285#define STS_UNKNOWN             0
286#define STS_BEING_PROCESSED     1
287#define STS_COMPLETED           2
288
289########################################################################
290#### Define JOB_SHA256 structure
291########################################################################
292
293START_FIELDS    # JOB_SHA256
294
295###     name                            size    align
296FIELD   _buffer,                        8,      8       # pointer to buffer
297FIELD   _len,                           8,      8       # length in bytes
298FIELD   _result_digest,                 8*4,    32      # Digest (output)
299FIELD   _status,                        4,      4
300FIELD   _user_data,                     8,      8
301END_FIELDS
302
303 _JOB_SHA256_size = _FIELD_OFFSET
304 _JOB_SHA256_align = _STRUCT_ALIGN
305