2 * Copyright 2016 Broadcom
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").
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.
13 * You should have received a copy of the GNU General Public License
14 * version 2 (GPLv2) along with this source code.
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.
29 #include <linux/types.h>
30 #include <linux/scatterlist.h>
31 #include <crypto/sha.h>
34 CIPHER_ALG_NONE
= 0x0,
37 CIPHER_ALG_3DES
= 0x3,
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
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,
70 HASH_ALG_SHA224
= 0x3,
71 HASH_ALG_SHA256
= 0x4,
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,
90 HASH_MODE_RABIN
= 0x4,
91 HASH_MODE_FHMAC
= 0x6,
100 HASH_TYPE_UPDT
= 0x2,
102 HASH_TYPE_AES128
= 0x0,
103 HASH_TYPE_AES192
= 0x1,
104 HASH_TYPE_AES256
= 0x2
114 extern char *hash_alg_name
[HASH_ALG_LAST
];
115 extern char *aead_alg_name
[AEAD_TYPE_LAST
];
117 struct spu_request_opts
{
126 struct spu_cipher_parms
{
127 enum spu_cipher_alg alg
;
128 enum spu_cipher_mode mode
;
129 enum spu_cipher_type type
;
132 /* iv_buf and iv_len include salt, if applicable */
137 struct spu_hash_parms
{
145 /* length of hash pad. signed, needs to handle roll-overs */
149 struct spu_aead_parms
{
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 */
159 /************** SPU sizes ***************/
161 #define SPU_RX_STATUS_LEN 4
163 /* Max length of padding for 4-byte alignment of STATUS field */
164 #define SPU_STAT_PAD_MAX 4
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 + \
170 /* GCM and CCM require 16-byte alignment */
171 #define SPU_GCM_CCM_ALIGN 16
173 /* Length up SUPDT field in SPU response message for RC4 */
174 #define SPU_SUPDT_LEN 260
176 /* SPU status error codes. These used as common error codes across all
179 #define SPU_INVALID_ICV 1
181 /* Indicates no limit to the length of the payload in a SPU message */
182 #define SPU_MAX_PAYLOAD_INF 0xFFFFFFFF
184 /* Size of XTS tweak ("i" parameter), in bytes */
185 #define SPU_XTS_TWEAK_SIZE 16
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
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.
202 * Return: true if ICV to be included as separate buffer
204 static __always_inline
bool spu_req_incl_icv(enum spu_cipher_mode cipher_mode
,
207 if ((cipher_mode
== CIPHER_MODE_GCM
) && !is_encrypt
)
209 if ((cipher_mode
== CIPHER_MODE_CCM
) && !is_encrypt
)
215 static __always_inline u32
spu_real_db_size(u32 assoc_size
,
223 return assoc_size
+ aead_iv_buf_len
+ prebuf_len
+ data_size
+
224 aad_pad_len
+ gcm_pad_len
+ hash_pad_len
;
227 /************** SPU Functions Prototypes **************/
229 void spum_dump_msg_hdr(u8
*buf
, unsigned int buf_len
);
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
,
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
);
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
);
259 u16
spum_cipher_req_init(u8
*spu_hdr
, struct spu_cipher_parms
*cipher_parms
);
261 void spum_cipher_req_finish(u8
*spu_hdr
,
263 unsigned int is_inbound
,
264 struct spu_cipher_parms
*cipher_parms
,
266 unsigned int data_size
);
268 void spum_request_pad(u8
*pad_start
,
271 enum hash_alg auth_alg
,
272 enum hash_mode auth_mode
,
273 unsigned int total_sent
, u32 status_padding
);
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
);
280 void spum_ccm_update_iv(unsigned int digestsize
,
281 struct spu_cipher_parms
*cipher_parms
,
282 unsigned int assoclen
,
283 unsigned int chunksize
,
286 u32
spum_wordalign_padlen(u32 data_size
);