18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci * AMCC SoC PPC4xx Crypto Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2008 Applied Micro Circuits Corporation.
68c2ecf20Sopenharmony_ci * All rights reserved. James Hsiao <jhsiao@amcc.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This file defines the security context
98c2ecf20Sopenharmony_ci * associate format.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#ifndef __CRYPTO4XX_SA_H__
138c2ecf20Sopenharmony_ci#define __CRYPTO4XX_SA_H__
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define AES_IV_SIZE				16
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/**
188c2ecf20Sopenharmony_ci * Contents of Dynamic Security Association (SA) with all possible fields
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ciunion dynamic_sa_contents {
218c2ecf20Sopenharmony_ci	struct {
228c2ecf20Sopenharmony_ci		u32 arc4_state_ptr:1;
238c2ecf20Sopenharmony_ci		u32 arc4_ij_ptr:1;
248c2ecf20Sopenharmony_ci		u32 state_ptr:1;
258c2ecf20Sopenharmony_ci		u32 iv3:1;
268c2ecf20Sopenharmony_ci		u32 iv2:1;
278c2ecf20Sopenharmony_ci		u32 iv1:1;
288c2ecf20Sopenharmony_ci		u32 iv0:1;
298c2ecf20Sopenharmony_ci		u32 seq_num_mask3:1;
308c2ecf20Sopenharmony_ci		u32 seq_num_mask2:1;
318c2ecf20Sopenharmony_ci		u32 seq_num_mask1:1;
328c2ecf20Sopenharmony_ci		u32 seq_num_mask0:1;
338c2ecf20Sopenharmony_ci		u32 seq_num1:1;
348c2ecf20Sopenharmony_ci		u32 seq_num0:1;
358c2ecf20Sopenharmony_ci		u32 spi:1;
368c2ecf20Sopenharmony_ci		u32 outer_size:5;
378c2ecf20Sopenharmony_ci		u32 inner_size:5;
388c2ecf20Sopenharmony_ci		u32 key_size:4;
398c2ecf20Sopenharmony_ci		u32 cmd_size:4;
408c2ecf20Sopenharmony_ci	} bf;
418c2ecf20Sopenharmony_ci	u32 w;
428c2ecf20Sopenharmony_ci} __attribute__((packed));
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define DIR_OUTBOUND				0
458c2ecf20Sopenharmony_ci#define DIR_INBOUND				1
468c2ecf20Sopenharmony_ci#define SA_OP_GROUP_BASIC			0
478c2ecf20Sopenharmony_ci#define SA_OPCODE_ENCRYPT			0
488c2ecf20Sopenharmony_ci#define SA_OPCODE_DECRYPT			0
498c2ecf20Sopenharmony_ci#define SA_OPCODE_ENCRYPT_HASH			1
508c2ecf20Sopenharmony_ci#define SA_OPCODE_HASH_DECRYPT			1
518c2ecf20Sopenharmony_ci#define SA_OPCODE_HASH				3
528c2ecf20Sopenharmony_ci#define SA_CIPHER_ALG_DES			0
538c2ecf20Sopenharmony_ci#define SA_CIPHER_ALG_3DES			1
548c2ecf20Sopenharmony_ci#define SA_CIPHER_ALG_ARC4			2
558c2ecf20Sopenharmony_ci#define SA_CIPHER_ALG_AES			3
568c2ecf20Sopenharmony_ci#define SA_CIPHER_ALG_KASUMI			4
578c2ecf20Sopenharmony_ci#define SA_CIPHER_ALG_NULL			15
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define SA_HASH_ALG_MD5				0
608c2ecf20Sopenharmony_ci#define SA_HASH_ALG_SHA1			1
618c2ecf20Sopenharmony_ci#define SA_HASH_ALG_GHASH			12
628c2ecf20Sopenharmony_ci#define SA_HASH_ALG_CBC_MAC			14
638c2ecf20Sopenharmony_ci#define SA_HASH_ALG_NULL			15
648c2ecf20Sopenharmony_ci#define SA_HASH_ALG_SHA1_DIGEST_SIZE		20
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define SA_LOAD_HASH_FROM_SA			0
678c2ecf20Sopenharmony_ci#define SA_LOAD_HASH_FROM_STATE			2
688c2ecf20Sopenharmony_ci#define SA_NOT_LOAD_HASH			3
698c2ecf20Sopenharmony_ci#define SA_LOAD_IV_FROM_SA			0
708c2ecf20Sopenharmony_ci#define SA_LOAD_IV_FROM_INPUT			1
718c2ecf20Sopenharmony_ci#define SA_LOAD_IV_FROM_STATE			2
728c2ecf20Sopenharmony_ci#define SA_LOAD_IV_GEN_IV			3
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define SA_PAD_TYPE_CONSTANT			2
758c2ecf20Sopenharmony_ci#define SA_PAD_TYPE_ZERO			3
768c2ecf20Sopenharmony_ci#define SA_PAD_TYPE_TLS				5
778c2ecf20Sopenharmony_ci#define SA_PAD_TYPE_DTLS			5
788c2ecf20Sopenharmony_ci#define SA_NOT_SAVE_HASH			0
798c2ecf20Sopenharmony_ci#define SA_SAVE_HASH				1
808c2ecf20Sopenharmony_ci#define SA_NOT_SAVE_IV				0
818c2ecf20Sopenharmony_ci#define SA_SAVE_IV				1
828c2ecf20Sopenharmony_ci#define SA_HEADER_PROC				1
838c2ecf20Sopenharmony_ci#define SA_NO_HEADER_PROC			0
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciunion sa_command_0 {
868c2ecf20Sopenharmony_ci	struct {
878c2ecf20Sopenharmony_ci		u32 scatter:1;
888c2ecf20Sopenharmony_ci		u32 gather:1;
898c2ecf20Sopenharmony_ci		u32 save_hash_state:1;
908c2ecf20Sopenharmony_ci		u32 save_iv:1;
918c2ecf20Sopenharmony_ci		u32 load_hash_state:2;
928c2ecf20Sopenharmony_ci		u32 load_iv:2;
938c2ecf20Sopenharmony_ci		u32 digest_len:4;
948c2ecf20Sopenharmony_ci		u32 hdr_proc:1;
958c2ecf20Sopenharmony_ci		u32 extend_pad:1;
968c2ecf20Sopenharmony_ci		u32 stream_cipher_pad:1;
978c2ecf20Sopenharmony_ci		u32 rsv:1;
988c2ecf20Sopenharmony_ci		u32 hash_alg:4;
998c2ecf20Sopenharmony_ci		u32 cipher_alg:4;
1008c2ecf20Sopenharmony_ci		u32 pad_type:2;
1018c2ecf20Sopenharmony_ci		u32 op_group:2;
1028c2ecf20Sopenharmony_ci		u32 dir:1;
1038c2ecf20Sopenharmony_ci		u32 opcode:3;
1048c2ecf20Sopenharmony_ci	} bf;
1058c2ecf20Sopenharmony_ci	u32 w;
1068c2ecf20Sopenharmony_ci} __attribute__((packed));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define CRYPTO_MODE_ECB				0
1098c2ecf20Sopenharmony_ci#define CRYPTO_MODE_CBC				1
1108c2ecf20Sopenharmony_ci#define CRYPTO_MODE_OFB				2
1118c2ecf20Sopenharmony_ci#define CRYPTO_MODE_CFB				3
1128c2ecf20Sopenharmony_ci#define CRYPTO_MODE_CTR				4
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define CRYPTO_FEEDBACK_MODE_NO_FB		0
1158c2ecf20Sopenharmony_ci#define CRYPTO_FEEDBACK_MODE_64BIT_OFB		0
1168c2ecf20Sopenharmony_ci#define CRYPTO_FEEDBACK_MODE_8BIT_CFB		1
1178c2ecf20Sopenharmony_ci#define CRYPTO_FEEDBACK_MODE_1BIT_CFB		2
1188c2ecf20Sopenharmony_ci#define CRYPTO_FEEDBACK_MODE_128BIT_CFB		3
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define SA_AES_KEY_LEN_128			2
1218c2ecf20Sopenharmony_ci#define SA_AES_KEY_LEN_192			3
1228c2ecf20Sopenharmony_ci#define SA_AES_KEY_LEN_256			4
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define SA_REV2					1
1258c2ecf20Sopenharmony_ci/**
1268c2ecf20Sopenharmony_ci * The follow defines bits sa_command_1
1278c2ecf20Sopenharmony_ci * In Basic hash mode  this bit define simple hash or hmac.
1288c2ecf20Sopenharmony_ci * In IPsec mode, this bit define muting control.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ci#define SA_HASH_MODE_HASH			0
1318c2ecf20Sopenharmony_ci#define SA_HASH_MODE_HMAC			1
1328c2ecf20Sopenharmony_ci#define SA_MC_ENABLE				0
1338c2ecf20Sopenharmony_ci#define SA_MC_DISABLE				1
1348c2ecf20Sopenharmony_ci#define SA_NOT_COPY_HDR				0
1358c2ecf20Sopenharmony_ci#define SA_COPY_HDR				1
1368c2ecf20Sopenharmony_ci#define SA_NOT_COPY_PAD				0
1378c2ecf20Sopenharmony_ci#define SA_COPY_PAD				1
1388c2ecf20Sopenharmony_ci#define SA_NOT_COPY_PAYLOAD			0
1398c2ecf20Sopenharmony_ci#define SA_COPY_PAYLOAD				1
1408c2ecf20Sopenharmony_ci#define SA_EXTENDED_SN_OFF			0
1418c2ecf20Sopenharmony_ci#define SA_EXTENDED_SN_ON			1
1428c2ecf20Sopenharmony_ci#define SA_SEQ_MASK_OFF				0
1438c2ecf20Sopenharmony_ci#define SA_SEQ_MASK_ON				1
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ciunion sa_command_1 {
1468c2ecf20Sopenharmony_ci	struct {
1478c2ecf20Sopenharmony_ci		u32 crypto_mode31:1;
1488c2ecf20Sopenharmony_ci		u32 save_arc4_state:1;
1498c2ecf20Sopenharmony_ci		u32 arc4_stateful:1;
1508c2ecf20Sopenharmony_ci		u32 key_len:5;
1518c2ecf20Sopenharmony_ci		u32 hash_crypto_offset:8;
1528c2ecf20Sopenharmony_ci		u32 sa_rev:2;
1538c2ecf20Sopenharmony_ci		u32 byte_offset:1;
1548c2ecf20Sopenharmony_ci		u32 hmac_muting:1;
1558c2ecf20Sopenharmony_ci		u32 feedback_mode:2;
1568c2ecf20Sopenharmony_ci		u32 crypto_mode9_8:2;
1578c2ecf20Sopenharmony_ci		u32 extended_seq_num:1;
1588c2ecf20Sopenharmony_ci		u32 seq_num_mask:1;
1598c2ecf20Sopenharmony_ci		u32 mutable_bit_proc:1;
1608c2ecf20Sopenharmony_ci		u32 ip_version:1;
1618c2ecf20Sopenharmony_ci		u32 copy_pad:1;
1628c2ecf20Sopenharmony_ci		u32 copy_payload:1;
1638c2ecf20Sopenharmony_ci		u32 copy_hdr:1;
1648c2ecf20Sopenharmony_ci		u32 rsv1:1;
1658c2ecf20Sopenharmony_ci	} bf;
1668c2ecf20Sopenharmony_ci	u32 w;
1678c2ecf20Sopenharmony_ci} __attribute__((packed));
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistruct dynamic_sa_ctl {
1708c2ecf20Sopenharmony_ci	union dynamic_sa_contents sa_contents;
1718c2ecf20Sopenharmony_ci	union sa_command_0 sa_command_0;
1728c2ecf20Sopenharmony_ci	union sa_command_1 sa_command_1;
1738c2ecf20Sopenharmony_ci} __attribute__((packed));
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/**
1768c2ecf20Sopenharmony_ci * State Record for Security Association (SA)
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_cistruct  sa_state_record {
1798c2ecf20Sopenharmony_ci	__le32 save_iv[4];
1808c2ecf20Sopenharmony_ci	__le32 save_hash_byte_cnt[2];
1818c2ecf20Sopenharmony_ci	union {
1828c2ecf20Sopenharmony_ci		u32 save_digest[16]; /* for MD5/SHA */
1838c2ecf20Sopenharmony_ci		__le32 save_digest_le32[16]; /* GHASH / CBC */
1848c2ecf20Sopenharmony_ci	};
1858c2ecf20Sopenharmony_ci} __attribute__((packed));
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/**
1888c2ecf20Sopenharmony_ci * Security Association (SA) for AES128
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_cistruct dynamic_sa_aes128 {
1928c2ecf20Sopenharmony_ci	struct dynamic_sa_ctl	ctrl;
1938c2ecf20Sopenharmony_ci	__le32 key[4];
1948c2ecf20Sopenharmony_ci	__le32 iv[4]; /* for CBC, OFC, and CFB mode */
1958c2ecf20Sopenharmony_ci	u32 state_ptr;
1968c2ecf20Sopenharmony_ci	u32 reserved;
1978c2ecf20Sopenharmony_ci} __attribute__((packed));
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define SA_AES128_LEN		(sizeof(struct dynamic_sa_aes128)/4)
2008c2ecf20Sopenharmony_ci#define SA_AES128_CONTENTS	0x3e000042
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci/*
2038c2ecf20Sopenharmony_ci * Security Association (SA) for AES192
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_cistruct dynamic_sa_aes192 {
2068c2ecf20Sopenharmony_ci	struct dynamic_sa_ctl ctrl;
2078c2ecf20Sopenharmony_ci	__le32 key[6];
2088c2ecf20Sopenharmony_ci	__le32 iv[4]; /* for CBC, OFC, and CFB mode */
2098c2ecf20Sopenharmony_ci	u32 state_ptr;
2108c2ecf20Sopenharmony_ci	u32 reserved;
2118c2ecf20Sopenharmony_ci} __attribute__((packed));
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define SA_AES192_LEN		(sizeof(struct dynamic_sa_aes192)/4)
2148c2ecf20Sopenharmony_ci#define SA_AES192_CONTENTS	0x3e000062
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/**
2178c2ecf20Sopenharmony_ci * Security Association (SA) for AES256
2188c2ecf20Sopenharmony_ci */
2198c2ecf20Sopenharmony_cistruct dynamic_sa_aes256 {
2208c2ecf20Sopenharmony_ci	struct dynamic_sa_ctl ctrl;
2218c2ecf20Sopenharmony_ci	__le32 key[8];
2228c2ecf20Sopenharmony_ci	__le32 iv[4]; /* for CBC, OFC, and CFB mode */
2238c2ecf20Sopenharmony_ci	u32 state_ptr;
2248c2ecf20Sopenharmony_ci	u32 reserved;
2258c2ecf20Sopenharmony_ci} __attribute__((packed));
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#define SA_AES256_LEN		(sizeof(struct dynamic_sa_aes256)/4)
2288c2ecf20Sopenharmony_ci#define SA_AES256_CONTENTS	0x3e000082
2298c2ecf20Sopenharmony_ci#define SA_AES_CONTENTS		0x3e000002
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/**
2328c2ecf20Sopenharmony_ci * Security Association (SA) for AES128 CCM
2338c2ecf20Sopenharmony_ci */
2348c2ecf20Sopenharmony_cistruct dynamic_sa_aes128_ccm {
2358c2ecf20Sopenharmony_ci	struct dynamic_sa_ctl ctrl;
2368c2ecf20Sopenharmony_ci	__le32 key[4];
2378c2ecf20Sopenharmony_ci	__le32 iv[4];
2388c2ecf20Sopenharmony_ci	u32 state_ptr;
2398c2ecf20Sopenharmony_ci	u32 reserved;
2408c2ecf20Sopenharmony_ci} __packed;
2418c2ecf20Sopenharmony_ci#define SA_AES128_CCM_LEN	(sizeof(struct dynamic_sa_aes128_ccm)/4)
2428c2ecf20Sopenharmony_ci#define SA_AES128_CCM_CONTENTS	0x3e000042
2438c2ecf20Sopenharmony_ci#define SA_AES_CCM_CONTENTS	0x3e000002
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci/**
2468c2ecf20Sopenharmony_ci * Security Association (SA) for AES128_GCM
2478c2ecf20Sopenharmony_ci */
2488c2ecf20Sopenharmony_cistruct dynamic_sa_aes128_gcm {
2498c2ecf20Sopenharmony_ci	struct dynamic_sa_ctl ctrl;
2508c2ecf20Sopenharmony_ci	__le32 key[4];
2518c2ecf20Sopenharmony_ci	__le32 inner_digest[4];
2528c2ecf20Sopenharmony_ci	__le32 iv[4];
2538c2ecf20Sopenharmony_ci	u32 state_ptr;
2548c2ecf20Sopenharmony_ci	u32 reserved;
2558c2ecf20Sopenharmony_ci} __packed;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci#define SA_AES128_GCM_LEN	(sizeof(struct dynamic_sa_aes128_gcm)/4)
2588c2ecf20Sopenharmony_ci#define SA_AES128_GCM_CONTENTS	0x3e000442
2598c2ecf20Sopenharmony_ci#define SA_AES_GCM_CONTENTS	0x3e000402
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/**
2628c2ecf20Sopenharmony_ci * Security Association (SA) for HASH160: HMAC-SHA1
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_cistruct dynamic_sa_hash160 {
2658c2ecf20Sopenharmony_ci	struct dynamic_sa_ctl ctrl;
2668c2ecf20Sopenharmony_ci	__le32 inner_digest[5];
2678c2ecf20Sopenharmony_ci	__le32 outer_digest[5];
2688c2ecf20Sopenharmony_ci	u32 state_ptr;
2698c2ecf20Sopenharmony_ci	u32 reserved;
2708c2ecf20Sopenharmony_ci} __attribute__((packed));
2718c2ecf20Sopenharmony_ci#define SA_HASH160_LEN		(sizeof(struct dynamic_sa_hash160)/4)
2728c2ecf20Sopenharmony_ci#define SA_HASH160_CONTENTS     0x2000a502
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic inline u32
2758c2ecf20Sopenharmony_ciget_dynamic_sa_offset_state_ptr_field(struct dynamic_sa_ctl *cts)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	u32 offset;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	offset = cts->sa_contents.bf.key_size
2808c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.inner_size
2818c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.outer_size
2828c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.spi
2838c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.seq_num0
2848c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.seq_num1
2858c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.seq_num_mask0
2868c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.seq_num_mask1
2878c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.seq_num_mask2
2888c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.seq_num_mask3
2898c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.iv0
2908c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.iv1
2918c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.iv2
2928c2ecf20Sopenharmony_ci		+ cts->sa_contents.bf.iv3;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return sizeof(struct dynamic_sa_ctl) + offset * 4;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic inline __le32 *get_dynamic_sa_key_field(struct dynamic_sa_ctl *cts)
2988c2ecf20Sopenharmony_ci{
2998c2ecf20Sopenharmony_ci	return (__le32 *) ((unsigned long)cts + sizeof(struct dynamic_sa_ctl));
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic inline __le32 *get_dynamic_sa_inner_digest(struct dynamic_sa_ctl *cts)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	return (__le32 *) ((unsigned long)cts +
3058c2ecf20Sopenharmony_ci		sizeof(struct dynamic_sa_ctl) +
3068c2ecf20Sopenharmony_ci		cts->sa_contents.bf.key_size * 4);
3078c2ecf20Sopenharmony_ci}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci#endif
310