xref: /linux/drivers/crypto/bcm/spu.h (revision a3a4a816b4b194c45d0217e8b9e08b2639802cda)
1 /*
2  * Copyright 2016 Broadcom
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2, as
6  * published by the Free Software Foundation (the "GPL").
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License version 2 (GPLv2) for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * version 2 (GPLv2) along with this source code.
15  */
16 
17 /*
18  * This file contains the definition of SPU messages. There are currently two
19  * SPU message formats: SPU-M and SPU2. The hardware uses different values to
20  * identify the same things in SPU-M vs SPU2. So this file defines values that
21  * are hardware independent. Software can use these values for any version of
22  * SPU hardware. These values are used in APIs in spu.c. Functions internal to
23  * spu.c and spu2.c convert these to hardware-specific values.
24  */
25 
26 #ifndef _SPU_H
27 #define _SPU_H
28 
29 #include <linux/types.h>
30 #include <linux/scatterlist.h>
31 #include <crypto/sha.h>
32 
33 enum spu_cipher_alg {
34 	CIPHER_ALG_NONE = 0x0,
35 	CIPHER_ALG_RC4 = 0x1,
36 	CIPHER_ALG_DES = 0x2,
37 	CIPHER_ALG_3DES = 0x3,
38 	CIPHER_ALG_AES = 0x4,
39 	CIPHER_ALG_LAST = 0x5
40 };
41 
42 enum spu_cipher_mode {
43 	CIPHER_MODE_NONE = 0x0,
44 	CIPHER_MODE_ECB = 0x0,
45 	CIPHER_MODE_CBC = 0x1,
46 	CIPHER_MODE_OFB = 0x2,
47 	CIPHER_MODE_CFB = 0x3,
48 	CIPHER_MODE_CTR = 0x4,
49 	CIPHER_MODE_CCM = 0x5,
50 	CIPHER_MODE_GCM = 0x6,
51 	CIPHER_MODE_XTS = 0x7,
52 	CIPHER_MODE_LAST = 0x8
53 };
54 
55 enum spu_cipher_type {
56 	CIPHER_TYPE_NONE = 0x0,
57 	CIPHER_TYPE_DES = 0x0,
58 	CIPHER_TYPE_3DES = 0x0,
59 	CIPHER_TYPE_INIT = 0x0,	/* used for ARC4 */
60 	CIPHER_TYPE_AES128 = 0x0,
61 	CIPHER_TYPE_AES192 = 0x1,
62 	CIPHER_TYPE_UPDT = 0x1,	/* used for ARC4 */
63 	CIPHER_TYPE_AES256 = 0x2,
64 };
65 
66 enum hash_alg {
67 	HASH_ALG_NONE = 0x0,
68 	HASH_ALG_MD5 = 0x1,
69 	HASH_ALG_SHA1 = 0x2,
70 	HASH_ALG_SHA224 = 0x3,
71 	HASH_ALG_SHA256 = 0x4,
72 	HASH_ALG_AES = 0x5,
73 	HASH_ALG_SHA384 = 0x6,
74 	HASH_ALG_SHA512 = 0x7,
75 	/* Keep SHA3 algorithms at the end always */
76 	HASH_ALG_SHA3_224 = 0x8,
77 	HASH_ALG_SHA3_256 = 0x9,
78 	HASH_ALG_SHA3_384 = 0xa,
79 	HASH_ALG_SHA3_512 = 0xb,
80 	HASH_ALG_LAST
81 };
82 
83 enum hash_mode {
84 	HASH_MODE_NONE = 0x0,
85 	HASH_MODE_HASH = 0x0,
86 	HASH_MODE_XCBC = 0x0,
87 	HASH_MODE_CMAC = 0x1,
88 	HASH_MODE_CTXT = 0x1,
89 	HASH_MODE_HMAC = 0x2,
90 	HASH_MODE_RABIN = 0x4,
91 	HASH_MODE_FHMAC = 0x6,
92 	HASH_MODE_CCM = 0x5,
93 	HASH_MODE_GCM = 0x6,
94 };
95 
96 enum hash_type {
97 	HASH_TYPE_NONE = 0x0,
98 	HASH_TYPE_FULL = 0x0,
99 	HASH_TYPE_INIT = 0x1,
100 	HASH_TYPE_UPDT = 0x2,
101 	HASH_TYPE_FIN = 0x3,
102 	HASH_TYPE_AES128 = 0x0,
103 	HASH_TYPE_AES192 = 0x1,
104 	HASH_TYPE_AES256 = 0x2
105 };
106 
107 enum aead_type {
108 	AES_CCM,
109 	AES_GCM,
110 	AUTHENC,
111 	AEAD_TYPE_LAST
112 };
113 
114 extern char *hash_alg_name[HASH_ALG_LAST];
115 extern char *aead_alg_name[AEAD_TYPE_LAST];
116 
117 struct spu_request_opts {
118 	bool is_inbound;
119 	bool auth_first;
120 	bool is_aead;
121 	bool is_esp;
122 	bool bd_suppress;
123 	bool is_rfc4543;
124 };
125 
126 struct spu_cipher_parms {
127 	enum spu_cipher_alg  alg;
128 	enum spu_cipher_mode mode;
129 	enum spu_cipher_type type;
130 	u8                  *key_buf;
131 	u16                  key_len;
132 	/* iv_buf and iv_len include salt, if applicable */
133 	u8                  *iv_buf;
134 	u16                  iv_len;
135 };
136 
137 struct spu_hash_parms {
138 	enum hash_alg  alg;
139 	enum hash_mode mode;
140 	enum hash_type type;
141 	u8             digestsize;
142 	u8            *key_buf;
143 	u16            key_len;
144 	u16            prebuf_len;
145 	/* length of hash pad. signed, needs to handle roll-overs */
146 	int            pad_len;
147 };
148 
149 struct spu_aead_parms {
150 	u32 assoc_size;
151 	u16 iv_len;      /* length of IV field between assoc data and data */
152 	u8  aad_pad_len; /* For AES GCM/CCM, length of padding after AAD */
153 	u8  data_pad_len;/* For AES GCM/CCM, length of padding after data */
154 	bool return_iv;  /* True if SPU should return an IV */
155 	u32 ret_iv_len;  /* Length in bytes of returned IV */
156 	u32 ret_iv_off;  /* Offset into full IV if partial IV returned */
157 };
158 
159 /************** SPU sizes ***************/
160 
161 #define SPU_RX_STATUS_LEN  4
162 
163 /* Max length of padding for 4-byte alignment of STATUS field */
164 #define SPU_STAT_PAD_MAX  4
165 
166 /* Max length of pad fragment. 4 is for 4-byte alignment of STATUS field */
167 #define SPU_PAD_LEN_MAX (SPU_GCM_CCM_ALIGN + MAX_HASH_BLOCK_SIZE + \
168 			 SPU_STAT_PAD_MAX)
169 
170 /* GCM and CCM require 16-byte alignment */
171 #define SPU_GCM_CCM_ALIGN 16
172 
173 /* Length up SUPDT field in SPU response message for RC4 */
174 #define SPU_SUPDT_LEN 260
175 
176 /* SPU status error codes. These used as common error codes across all
177  * SPU variants.
178  */
179 #define SPU_INVALID_ICV  1
180 
181 /* Indicates no limit to the length of the payload in a SPU message */
182 #define SPU_MAX_PAYLOAD_INF  0xFFFFFFFF
183 
184 /* Size of XTS tweak ("i" parameter), in bytes */
185 #define SPU_XTS_TWEAK_SIZE 16
186 
187 /* CCM B_0 field definitions, common for SPU-M and SPU2 */
188 #define CCM_B0_ADATA		0x40
189 #define CCM_B0_ADATA_SHIFT	   6
190 #define CCM_B0_M_PRIME		0x38
191 #define CCM_B0_M_PRIME_SHIFT	   3
192 #define CCM_B0_L_PRIME		0x07
193 #define CCM_B0_L_PRIME_SHIFT	   0
194 #define CCM_ESP_L_VALUE		   4
195 
196 /**
197  * spu_req_incl_icv() - Return true if SPU request message should include the
198  * ICV as a separate buffer.
199  * @cipher_mode:  the cipher mode being requested
200  * @is_encrypt:   true if encrypting. false if decrypting.
201  *
202  * Return:  true if ICV to be included as separate buffer
203  */
204 static __always_inline  bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode,
205 					      bool is_encrypt)
206 {
207 	if ((cipher_mode == CIPHER_MODE_GCM) && !is_encrypt)
208 		return true;
209 	if ((cipher_mode == CIPHER_MODE_CCM) && !is_encrypt)
210 		return true;
211 
212 	return false;
213 }
214 
215 static __always_inline u32 spu_real_db_size(u32 assoc_size,
216 					    u32 aead_iv_buf_len,
217 					    u32 prebuf_len,
218 					    u32 data_size,
219 					    u32 aad_pad_len,
220 					    u32 gcm_pad_len,
221 					    u32 hash_pad_len)
222 {
223 	return assoc_size + aead_iv_buf_len + prebuf_len + data_size +
224 	    aad_pad_len + gcm_pad_len + hash_pad_len;
225 }
226 
227 /************** SPU Functions Prototypes **************/
228 
229 void spum_dump_msg_hdr(u8 *buf, unsigned int buf_len);
230 
231 u32 spum_ns2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
232 			     enum spu_cipher_mode cipher_mode,
233 			     unsigned int blocksize);
234 u32 spum_nsp_ctx_max_payload(enum spu_cipher_alg cipher_alg,
235 			     enum spu_cipher_mode cipher_mode,
236 			     unsigned int blocksize);
237 u32 spum_payload_length(u8 *spu_hdr);
238 u16 spum_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash);
239 u16 spum_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
240 		      u32 chunksize, u16 hash_block_size);
241 u32 spum_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
242 			 unsigned int data_size);
243 u32 spum_assoc_resp_len(enum spu_cipher_mode cipher_mode,
244 			unsigned int assoc_len, unsigned int iv_len,
245 			bool is_encrypt);
246 u8 spum_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len);
247 bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode, bool is_encrypt);
248 enum hash_type spum_hash_type(u32 src_sent);
249 u32 spum_digest_size(u32 alg_digest_size, enum hash_alg alg,
250 		     enum hash_type htype);
251 
252 u32 spum_create_request(u8 *spu_hdr,
253 			struct spu_request_opts *req_opts,
254 			struct spu_cipher_parms *cipher_parms,
255 			struct spu_hash_parms *hash_parms,
256 			struct spu_aead_parms *aead_parms,
257 			unsigned int data_size);
258 
259 u16 spum_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms);
260 
261 void spum_cipher_req_finish(u8 *spu_hdr,
262 			    u16 spu_req_hdr_len,
263 			    unsigned int is_inbound,
264 			    struct spu_cipher_parms *cipher_parms,
265 			    bool update_key,
266 			    unsigned int data_size);
267 
268 void spum_request_pad(u8 *pad_start,
269 		      u32 gcm_padding,
270 		      u32 hash_pad_len,
271 		      enum hash_alg auth_alg,
272 		      enum hash_mode auth_mode,
273 		      unsigned int total_sent, u32 status_padding);
274 
275 u8 spum_xts_tweak_in_payload(void);
276 u8 spum_tx_status_len(void);
277 u8 spum_rx_status_len(void);
278 int spum_status_process(u8 *statp);
279 
280 void spum_ccm_update_iv(unsigned int digestsize,
281 			struct spu_cipher_parms *cipher_parms,
282 			unsigned int assoclen,
283 			unsigned int chunksize,
284 			bool is_encrypt,
285 			bool is_esp);
286 u32 spum_wordalign_padlen(u32 data_size);
287 #endif
288