xref: /linux/drivers/crypto/amcc/crypto4xx_sa.h (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1c942fddfSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2*73f04d3dSAditya Srivastava /*
3049359d6SJames Hsiao  * AMCC SoC PPC4xx Crypto Driver
4049359d6SJames Hsiao  *
5049359d6SJames Hsiao  * Copyright (c) 2008 Applied Micro Circuits Corporation.
6049359d6SJames Hsiao  * All rights reserved. James Hsiao <jhsiao@amcc.com>
7049359d6SJames Hsiao  *
8049359d6SJames Hsiao  * This file defines the security context
925985edcSLucas De Marchi  * associate format.
10049359d6SJames Hsiao  */
11049359d6SJames Hsiao 
12049359d6SJames Hsiao #ifndef __CRYPTO4XX_SA_H__
13049359d6SJames Hsiao #define __CRYPTO4XX_SA_H__
14049359d6SJames Hsiao 
15049359d6SJames Hsiao #define AES_IV_SIZE				16
16049359d6SJames Hsiao 
17*73f04d3dSAditya Srivastava /*
18049359d6SJames Hsiao  * Contents of Dynamic Security Association (SA) with all possible fields
19049359d6SJames Hsiao  */
20049359d6SJames Hsiao union dynamic_sa_contents {
21049359d6SJames Hsiao 	struct {
22049359d6SJames Hsiao 		u32 arc4_state_ptr:1;
23049359d6SJames Hsiao 		u32 arc4_ij_ptr:1;
24049359d6SJames Hsiao 		u32 state_ptr:1;
25049359d6SJames Hsiao 		u32 iv3:1;
26049359d6SJames Hsiao 		u32 iv2:1;
27049359d6SJames Hsiao 		u32 iv1:1;
28049359d6SJames Hsiao 		u32 iv0:1;
29049359d6SJames Hsiao 		u32 seq_num_mask3:1;
30049359d6SJames Hsiao 		u32 seq_num_mask2:1;
31049359d6SJames Hsiao 		u32 seq_num_mask1:1;
32049359d6SJames Hsiao 		u32 seq_num_mask0:1;
33049359d6SJames Hsiao 		u32 seq_num1:1;
34049359d6SJames Hsiao 		u32 seq_num0:1;
35049359d6SJames Hsiao 		u32 spi:1;
36049359d6SJames Hsiao 		u32 outer_size:5;
37049359d6SJames Hsiao 		u32 inner_size:5;
38049359d6SJames Hsiao 		u32 key_size:4;
39049359d6SJames Hsiao 		u32 cmd_size:4;
40049359d6SJames Hsiao 	} bf;
41049359d6SJames Hsiao 	u32 w;
42049359d6SJames Hsiao } __attribute__((packed));
43049359d6SJames Hsiao 
44049359d6SJames Hsiao #define DIR_OUTBOUND				0
45049359d6SJames Hsiao #define DIR_INBOUND				1
46049359d6SJames Hsiao #define SA_OP_GROUP_BASIC			0
47049359d6SJames Hsiao #define SA_OPCODE_ENCRYPT			0
48049359d6SJames Hsiao #define SA_OPCODE_DECRYPT			0
49a0aae821SChristian Lamparter #define SA_OPCODE_ENCRYPT_HASH			1
50a0aae821SChristian Lamparter #define SA_OPCODE_HASH_DECRYPT			1
51049359d6SJames Hsiao #define SA_OPCODE_HASH				3
52049359d6SJames Hsiao #define SA_CIPHER_ALG_DES			0
53049359d6SJames Hsiao #define SA_CIPHER_ALG_3DES			1
54049359d6SJames Hsiao #define SA_CIPHER_ALG_ARC4			2
55049359d6SJames Hsiao #define SA_CIPHER_ALG_AES			3
56049359d6SJames Hsiao #define SA_CIPHER_ALG_KASUMI			4
57049359d6SJames Hsiao #define SA_CIPHER_ALG_NULL			15
58049359d6SJames Hsiao 
59049359d6SJames Hsiao #define SA_HASH_ALG_MD5				0
60049359d6SJames Hsiao #define SA_HASH_ALG_SHA1			1
61a0aae821SChristian Lamparter #define SA_HASH_ALG_GHASH			12
62a0aae821SChristian Lamparter #define SA_HASH_ALG_CBC_MAC			14
63049359d6SJames Hsiao #define SA_HASH_ALG_NULL			15
64049359d6SJames Hsiao #define SA_HASH_ALG_SHA1_DIGEST_SIZE		20
65049359d6SJames Hsiao 
66049359d6SJames Hsiao #define SA_LOAD_HASH_FROM_SA			0
67049359d6SJames Hsiao #define SA_LOAD_HASH_FROM_STATE			2
68049359d6SJames Hsiao #define SA_NOT_LOAD_HASH			3
69049359d6SJames Hsiao #define SA_LOAD_IV_FROM_SA			0
70049359d6SJames Hsiao #define SA_LOAD_IV_FROM_INPUT			1
71049359d6SJames Hsiao #define SA_LOAD_IV_FROM_STATE			2
72049359d6SJames Hsiao #define SA_LOAD_IV_GEN_IV			3
73049359d6SJames Hsiao 
74049359d6SJames Hsiao #define SA_PAD_TYPE_CONSTANT			2
75049359d6SJames Hsiao #define SA_PAD_TYPE_ZERO			3
76049359d6SJames Hsiao #define SA_PAD_TYPE_TLS				5
77049359d6SJames Hsiao #define SA_PAD_TYPE_DTLS			5
78049359d6SJames Hsiao #define SA_NOT_SAVE_HASH			0
79049359d6SJames Hsiao #define SA_SAVE_HASH				1
80049359d6SJames Hsiao #define SA_NOT_SAVE_IV				0
81049359d6SJames Hsiao #define SA_SAVE_IV				1
82049359d6SJames Hsiao #define SA_HEADER_PROC				1
83049359d6SJames Hsiao #define SA_NO_HEADER_PROC			0
84049359d6SJames Hsiao 
85049359d6SJames Hsiao union sa_command_0 {
86049359d6SJames Hsiao 	struct {
87049359d6SJames Hsiao 		u32 scatter:1;
88049359d6SJames Hsiao 		u32 gather:1;
89049359d6SJames Hsiao 		u32 save_hash_state:1;
90049359d6SJames Hsiao 		u32 save_iv:1;
91049359d6SJames Hsiao 		u32 load_hash_state:2;
92049359d6SJames Hsiao 		u32 load_iv:2;
93049359d6SJames Hsiao 		u32 digest_len:4;
94049359d6SJames Hsiao 		u32 hdr_proc:1;
95049359d6SJames Hsiao 		u32 extend_pad:1;
96049359d6SJames Hsiao 		u32 stream_cipher_pad:1;
97049359d6SJames Hsiao 		u32 rsv:1;
98049359d6SJames Hsiao 		u32 hash_alg:4;
99049359d6SJames Hsiao 		u32 cipher_alg:4;
100049359d6SJames Hsiao 		u32 pad_type:2;
101049359d6SJames Hsiao 		u32 op_group:2;
102049359d6SJames Hsiao 		u32 dir:1;
103049359d6SJames Hsiao 		u32 opcode:3;
104049359d6SJames Hsiao 	} bf;
105049359d6SJames Hsiao 	u32 w;
106049359d6SJames Hsiao } __attribute__((packed));
107049359d6SJames Hsiao 
108049359d6SJames Hsiao #define CRYPTO_MODE_ECB				0
109049359d6SJames Hsiao #define CRYPTO_MODE_CBC				1
110f2a13e7cSChristian Lamparter #define CRYPTO_MODE_OFB				2
111f2a13e7cSChristian Lamparter #define CRYPTO_MODE_CFB				3
112f2a13e7cSChristian Lamparter #define CRYPTO_MODE_CTR				4
113049359d6SJames Hsiao 
114049359d6SJames Hsiao #define CRYPTO_FEEDBACK_MODE_NO_FB		0
115049359d6SJames Hsiao #define CRYPTO_FEEDBACK_MODE_64BIT_OFB		0
116049359d6SJames Hsiao #define CRYPTO_FEEDBACK_MODE_8BIT_CFB		1
117049359d6SJames Hsiao #define CRYPTO_FEEDBACK_MODE_1BIT_CFB		2
118049359d6SJames Hsiao #define CRYPTO_FEEDBACK_MODE_128BIT_CFB		3
119049359d6SJames Hsiao 
120049359d6SJames Hsiao #define SA_AES_KEY_LEN_128			2
121049359d6SJames Hsiao #define SA_AES_KEY_LEN_192			3
122049359d6SJames Hsiao #define SA_AES_KEY_LEN_256			4
123049359d6SJames Hsiao 
124049359d6SJames Hsiao #define SA_REV2					1
125*73f04d3dSAditya Srivastava /*
126049359d6SJames Hsiao  * The follow defines bits sa_command_1
127049359d6SJames Hsiao  * In Basic hash mode  this bit define simple hash or hmac.
128049359d6SJames Hsiao  * In IPsec mode, this bit define muting control.
129049359d6SJames Hsiao  */
130049359d6SJames Hsiao #define SA_HASH_MODE_HASH			0
131049359d6SJames Hsiao #define SA_HASH_MODE_HMAC			1
132049359d6SJames Hsiao #define SA_MC_ENABLE				0
133049359d6SJames Hsiao #define SA_MC_DISABLE				1
134049359d6SJames Hsiao #define SA_NOT_COPY_HDR				0
135049359d6SJames Hsiao #define SA_COPY_HDR				1
136049359d6SJames Hsiao #define SA_NOT_COPY_PAD				0
137049359d6SJames Hsiao #define SA_COPY_PAD				1
138049359d6SJames Hsiao #define SA_NOT_COPY_PAYLOAD			0
139049359d6SJames Hsiao #define SA_COPY_PAYLOAD				1
140049359d6SJames Hsiao #define SA_EXTENDED_SN_OFF			0
141049359d6SJames Hsiao #define SA_EXTENDED_SN_ON			1
142049359d6SJames Hsiao #define SA_SEQ_MASK_OFF				0
143049359d6SJames Hsiao #define SA_SEQ_MASK_ON				1
144049359d6SJames Hsiao 
145049359d6SJames Hsiao union sa_command_1 {
146049359d6SJames Hsiao 	struct {
147049359d6SJames Hsiao 		u32 crypto_mode31:1;
148049359d6SJames Hsiao 		u32 save_arc4_state:1;
149049359d6SJames Hsiao 		u32 arc4_stateful:1;
150049359d6SJames Hsiao 		u32 key_len:5;
151049359d6SJames Hsiao 		u32 hash_crypto_offset:8;
152049359d6SJames Hsiao 		u32 sa_rev:2;
153049359d6SJames Hsiao 		u32 byte_offset:1;
154049359d6SJames Hsiao 		u32 hmac_muting:1;
155049359d6SJames Hsiao 		u32 feedback_mode:2;
156049359d6SJames Hsiao 		u32 crypto_mode9_8:2;
157049359d6SJames Hsiao 		u32 extended_seq_num:1;
158049359d6SJames Hsiao 		u32 seq_num_mask:1;
159049359d6SJames Hsiao 		u32 mutable_bit_proc:1;
160049359d6SJames Hsiao 		u32 ip_version:1;
161049359d6SJames Hsiao 		u32 copy_pad:1;
162049359d6SJames Hsiao 		u32 copy_payload:1;
163049359d6SJames Hsiao 		u32 copy_hdr:1;
164049359d6SJames Hsiao 		u32 rsv1:1;
165049359d6SJames Hsiao 	} bf;
166049359d6SJames Hsiao 	u32 w;
167049359d6SJames Hsiao } __attribute__((packed));
168049359d6SJames Hsiao 
169049359d6SJames Hsiao struct dynamic_sa_ctl {
170453e3090SChristian Lamparter 	union dynamic_sa_contents sa_contents;
171049359d6SJames Hsiao 	union sa_command_0 sa_command_0;
172049359d6SJames Hsiao 	union sa_command_1 sa_command_1;
173049359d6SJames Hsiao } __attribute__((packed));
174049359d6SJames Hsiao 
175*73f04d3dSAditya Srivastava /*
176049359d6SJames Hsiao  * State Record for Security Association (SA)
177049359d6SJames Hsiao  */
178049359d6SJames Hsiao struct  sa_state_record {
1794865b122SChristian Lamparter 	__le32 save_iv[4];
1804865b122SChristian Lamparter 	__le32 save_hash_byte_cnt[2];
1814865b122SChristian Lamparter 	union {
1824865b122SChristian Lamparter 		u32 save_digest[16]; /* for MD5/SHA */
1834865b122SChristian Lamparter 		__le32 save_digest_le32[16]; /* GHASH / CBC */
1844865b122SChristian Lamparter 	};
185049359d6SJames Hsiao } __attribute__((packed));
186049359d6SJames Hsiao 
187*73f04d3dSAditya Srivastava /*
188049359d6SJames Hsiao  * Security Association (SA) for AES128
189049359d6SJames Hsiao  *
190049359d6SJames Hsiao  */
191049359d6SJames Hsiao struct dynamic_sa_aes128 {
192049359d6SJames Hsiao 	struct dynamic_sa_ctl	ctrl;
1934865b122SChristian Lamparter 	__le32 key[4];
1944865b122SChristian Lamparter 	__le32 iv[4]; /* for CBC, OFC, and CFB mode */
195049359d6SJames Hsiao 	u32 state_ptr;
196049359d6SJames Hsiao 	u32 reserved;
197049359d6SJames Hsiao } __attribute__((packed));
198049359d6SJames Hsiao 
199049359d6SJames Hsiao #define SA_AES128_LEN		(sizeof(struct dynamic_sa_aes128)/4)
200049359d6SJames Hsiao #define SA_AES128_CONTENTS	0x3e000042
201049359d6SJames Hsiao 
202049359d6SJames Hsiao /*
203049359d6SJames Hsiao  * Security Association (SA) for AES192
204049359d6SJames Hsiao  */
205049359d6SJames Hsiao struct dynamic_sa_aes192 {
206049359d6SJames Hsiao 	struct dynamic_sa_ctl ctrl;
2074865b122SChristian Lamparter 	__le32 key[6];
2084865b122SChristian Lamparter 	__le32 iv[4]; /* for CBC, OFC, and CFB mode */
209049359d6SJames Hsiao 	u32 state_ptr;
210049359d6SJames Hsiao 	u32 reserved;
211049359d6SJames Hsiao } __attribute__((packed));
212049359d6SJames Hsiao 
213049359d6SJames Hsiao #define SA_AES192_LEN		(sizeof(struct dynamic_sa_aes192)/4)
214049359d6SJames Hsiao #define SA_AES192_CONTENTS	0x3e000062
215049359d6SJames Hsiao 
216*73f04d3dSAditya Srivastava /*
217049359d6SJames Hsiao  * Security Association (SA) for AES256
218049359d6SJames Hsiao  */
219049359d6SJames Hsiao struct dynamic_sa_aes256 {
220049359d6SJames Hsiao 	struct dynamic_sa_ctl ctrl;
2214865b122SChristian Lamparter 	__le32 key[8];
2224865b122SChristian Lamparter 	__le32 iv[4]; /* for CBC, OFC, and CFB mode */
223049359d6SJames Hsiao 	u32 state_ptr;
224049359d6SJames Hsiao 	u32 reserved;
225049359d6SJames Hsiao } __attribute__((packed));
226049359d6SJames Hsiao 
227049359d6SJames Hsiao #define SA_AES256_LEN		(sizeof(struct dynamic_sa_aes256)/4)
228049359d6SJames Hsiao #define SA_AES256_CONTENTS	0x3e000082
229049359d6SJames Hsiao #define SA_AES_CONTENTS		0x3e000002
230049359d6SJames Hsiao 
231*73f04d3dSAditya Srivastava /*
232a0aae821SChristian Lamparter  * Security Association (SA) for AES128 CCM
233a0aae821SChristian Lamparter  */
234a0aae821SChristian Lamparter struct dynamic_sa_aes128_ccm {
235a0aae821SChristian Lamparter 	struct dynamic_sa_ctl ctrl;
236a0aae821SChristian Lamparter 	__le32 key[4];
237a0aae821SChristian Lamparter 	__le32 iv[4];
238a0aae821SChristian Lamparter 	u32 state_ptr;
239a0aae821SChristian Lamparter 	u32 reserved;
240a0aae821SChristian Lamparter } __packed;
241a0aae821SChristian Lamparter #define SA_AES128_CCM_LEN	(sizeof(struct dynamic_sa_aes128_ccm)/4)
242a0aae821SChristian Lamparter #define SA_AES128_CCM_CONTENTS	0x3e000042
243a0aae821SChristian Lamparter #define SA_AES_CCM_CONTENTS	0x3e000002
244a0aae821SChristian Lamparter 
245*73f04d3dSAditya Srivastava /*
246a0aae821SChristian Lamparter  * Security Association (SA) for AES128_GCM
247a0aae821SChristian Lamparter  */
248a0aae821SChristian Lamparter struct dynamic_sa_aes128_gcm {
249a0aae821SChristian Lamparter 	struct dynamic_sa_ctl ctrl;
250a0aae821SChristian Lamparter 	__le32 key[4];
251a0aae821SChristian Lamparter 	__le32 inner_digest[4];
252a0aae821SChristian Lamparter 	__le32 iv[4];
253a0aae821SChristian Lamparter 	u32 state_ptr;
254a0aae821SChristian Lamparter 	u32 reserved;
255a0aae821SChristian Lamparter } __packed;
256a0aae821SChristian Lamparter 
257a0aae821SChristian Lamparter #define SA_AES128_GCM_LEN	(sizeof(struct dynamic_sa_aes128_gcm)/4)
258a0aae821SChristian Lamparter #define SA_AES128_GCM_CONTENTS	0x3e000442
259a0aae821SChristian Lamparter #define SA_AES_GCM_CONTENTS	0x3e000402
260a0aae821SChristian Lamparter 
261*73f04d3dSAditya Srivastava /*
262049359d6SJames Hsiao  * Security Association (SA) for HASH160: HMAC-SHA1
263049359d6SJames Hsiao  */
264049359d6SJames Hsiao struct dynamic_sa_hash160 {
265049359d6SJames Hsiao 	struct dynamic_sa_ctl ctrl;
2664865b122SChristian Lamparter 	__le32 inner_digest[5];
2674865b122SChristian Lamparter 	__le32 outer_digest[5];
268049359d6SJames Hsiao 	u32 state_ptr;
269049359d6SJames Hsiao 	u32 reserved;
270049359d6SJames Hsiao } __attribute__((packed));
271049359d6SJames Hsiao #define SA_HASH160_LEN		(sizeof(struct dynamic_sa_hash160)/4)
272049359d6SJames Hsiao #define SA_HASH160_CONTENTS     0x2000a502
273049359d6SJames Hsiao 
274249c8d98SChristian Lamparter static inline u32
get_dynamic_sa_offset_state_ptr_field(struct dynamic_sa_ctl * cts)275249c8d98SChristian Lamparter get_dynamic_sa_offset_state_ptr_field(struct dynamic_sa_ctl *cts)
276249c8d98SChristian Lamparter {
277249c8d98SChristian Lamparter 	u32 offset;
278249c8d98SChristian Lamparter 
279249c8d98SChristian Lamparter 	offset = cts->sa_contents.bf.key_size
280249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.inner_size
281249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.outer_size
282249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.spi
283249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.seq_num0
284249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.seq_num1
285249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.seq_num_mask0
286249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.seq_num_mask1
287249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.seq_num_mask2
288249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.seq_num_mask3
289249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.iv0
290249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.iv1
291249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.iv2
292249c8d98SChristian Lamparter 		+ cts->sa_contents.bf.iv3;
293249c8d98SChristian Lamparter 
294249c8d98SChristian Lamparter 	return sizeof(struct dynamic_sa_ctl) + offset * 4;
295249c8d98SChristian Lamparter }
296249c8d98SChristian Lamparter 
get_dynamic_sa_key_field(struct dynamic_sa_ctl * cts)2974865b122SChristian Lamparter static inline __le32 *get_dynamic_sa_key_field(struct dynamic_sa_ctl *cts)
298249c8d98SChristian Lamparter {
2994865b122SChristian Lamparter 	return (__le32 *) ((unsigned long)cts + sizeof(struct dynamic_sa_ctl));
300249c8d98SChristian Lamparter }
301249c8d98SChristian Lamparter 
get_dynamic_sa_inner_digest(struct dynamic_sa_ctl * cts)302a0aae821SChristian Lamparter static inline __le32 *get_dynamic_sa_inner_digest(struct dynamic_sa_ctl *cts)
303a0aae821SChristian Lamparter {
304a0aae821SChristian Lamparter 	return (__le32 *) ((unsigned long)cts +
305a0aae821SChristian Lamparter 		sizeof(struct dynamic_sa_ctl) +
306a0aae821SChristian Lamparter 		cts->sa_contents.bf.key_size * 4);
307a0aae821SChristian Lamparter }
308a0aae821SChristian Lamparter 
309049359d6SJames Hsiao #endif
310