Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright 2016 Broadcom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * This file works with the SPU2 version of the SPU. SPU2 has different message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * formats than the previous version of the SPU. All SPU message format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * differences should be hidden in the spux.c,h files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "spu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "spu2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #define SPU2_TX_STATUS_LEN  0	/* SPU2 has no STATUS in input packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * Controlled by pkt_stat_cnt field in CRYPTO_SS_SPU0_CORE_SPU2_CONTROL0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * register. Defaults to 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define SPU2_RX_STATUS_LEN  2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) enum spu2_proto_sel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 	SPU2_PROTO_RESV = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 	SPU2_MACSEC_SECTAG8_ECB = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 	SPU2_MACSEC_SECTAG8_SCB = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	SPU2_MACSEC_SECTAG16 = 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	SPU2_MACSEC_SECTAG16_8_XPN = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	SPU2_IPSEC = 5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	SPU2_IPSEC_ESN = 6,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	SPU2_TLS_CIPHER = 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	SPU2_TLS_AEAD = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	SPU2_DTLS_CIPHER = 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	SPU2_DTLS_AEAD = 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) static char *spu2_cipher_type_names[] = { "None", "AES128", "AES192", "AES256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 	"DES", "3DES"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static char *spu2_cipher_mode_names[] = { "ECB", "CBC", "CTR", "CFB", "OFB",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	"XTS", "CCM", "GCM"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static char *spu2_hash_type_names[] = { "None", "AES128", "AES192", "AES256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	"Reserved", "Reserved", "MD5", "SHA1", "SHA224", "SHA256", "SHA384",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 	"SHA512", "SHA512/224", "SHA512/256", "SHA3-224", "SHA3-256",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	"SHA3-384", "SHA3-512"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static char *spu2_hash_mode_names[] = { "CMAC", "CBC-MAC", "XCBC-MAC", "HMAC",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	"Rabin", "CCM", "GCM", "Reserved"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) static char *spu2_ciph_type_name(enum spu2_cipher_type cipher_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	if (cipher_type >= SPU2_CIPHER_TYPE_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		return "Reserved";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	return spu2_cipher_type_names[cipher_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static char *spu2_ciph_mode_name(enum spu2_cipher_mode cipher_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	if (cipher_mode >= SPU2_CIPHER_MODE_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		return "Reserved";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	return spu2_cipher_mode_names[cipher_mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) static char *spu2_hash_type_name(enum spu2_hash_type hash_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	if (hash_type >= SPU2_HASH_TYPE_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		return "Reserved";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	return spu2_hash_type_names[hash_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) static char *spu2_hash_mode_name(enum spu2_hash_mode hash_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	if (hash_mode >= SPU2_HASH_MODE_LAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 		return "Reserved";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	return spu2_hash_mode_names[hash_mode];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * Convert from a software cipher mode value to the corresponding value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * for SPU2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static int spu2_cipher_mode_xlate(enum spu_cipher_mode cipher_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 				  enum spu2_cipher_mode *spu2_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	switch (cipher_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	case CIPHER_MODE_ECB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		*spu2_mode = SPU2_CIPHER_MODE_ECB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	case CIPHER_MODE_CBC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		*spu2_mode = SPU2_CIPHER_MODE_CBC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	case CIPHER_MODE_OFB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		*spu2_mode = SPU2_CIPHER_MODE_OFB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	case CIPHER_MODE_CFB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		*spu2_mode = SPU2_CIPHER_MODE_CFB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	case CIPHER_MODE_CTR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		*spu2_mode = SPU2_CIPHER_MODE_CTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	case CIPHER_MODE_CCM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		*spu2_mode = SPU2_CIPHER_MODE_CCM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	case CIPHER_MODE_GCM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		*spu2_mode = SPU2_CIPHER_MODE_GCM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	case CIPHER_MODE_XTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		*spu2_mode = SPU2_CIPHER_MODE_XTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * spu2_cipher_xlate() - Convert a cipher {alg/mode/type} triple to a SPU2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  * cipher type and mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * @cipher_alg:  [in]  cipher algorithm value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * @cipher_mode: [in]  cipher mode value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * @cipher_type: [in]  cipher type value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * @spu2_type:   [out] cipher type value used by spu2 hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  * @spu2_mode:   [out] cipher mode value used by spu2 hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134)  * Return:  0 if successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static int spu2_cipher_xlate(enum spu_cipher_alg cipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 			     enum spu_cipher_mode cipher_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 			     enum spu_cipher_type cipher_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 			     enum spu2_cipher_type *spu2_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 			     enum spu2_cipher_mode *spu2_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	err = spu2_cipher_mode_xlate(cipher_mode, spu2_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		flow_log("Invalid cipher mode %d\n", cipher_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	switch (cipher_alg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	case CIPHER_ALG_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		*spu2_type = SPU2_CIPHER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	case CIPHER_ALG_RC4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		/* SPU2 does not support RC4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		*spu2_type = SPU2_CIPHER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	case CIPHER_ALG_DES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		*spu2_type = SPU2_CIPHER_TYPE_DES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	case CIPHER_ALG_3DES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		*spu2_type = SPU2_CIPHER_TYPE_3DES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	case CIPHER_ALG_AES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		switch (cipher_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		case CIPHER_TYPE_AES128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 			*spu2_type = SPU2_CIPHER_TYPE_AES128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		case CIPHER_TYPE_AES192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 			*spu2_type = SPU2_CIPHER_TYPE_AES192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		case CIPHER_TYPE_AES256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			*spu2_type = SPU2_CIPHER_TYPE_AES256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	case CIPHER_ALG_LAST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		flow_log("Invalid cipher alg %d or type %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			 cipher_alg, cipher_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * Convert from a software hash mode value to the corresponding value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * for SPU2. Note that HASH_MODE_NONE and HASH_MODE_XCBC have the same value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) static int spu2_hash_mode_xlate(enum hash_mode hash_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 				enum spu2_hash_mode *spu2_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	switch (hash_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	case HASH_MODE_XCBC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		*spu2_mode = SPU2_HASH_MODE_XCBC_MAC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	case HASH_MODE_CMAC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		*spu2_mode = SPU2_HASH_MODE_CMAC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	case HASH_MODE_HMAC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		*spu2_mode = SPU2_HASH_MODE_HMAC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	case HASH_MODE_CCM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		*spu2_mode = SPU2_HASH_MODE_CCM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	case HASH_MODE_GCM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		*spu2_mode = SPU2_HASH_MODE_GCM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222)  * spu2_hash_xlate() - Convert a hash {alg/mode/type} triple to a SPU2 hash type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223)  * and mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224)  * @hash_alg:  [in] hash algorithm value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225)  * @hash_mode: [in] hash mode value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226)  * @hash_type: [in] hash type value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227)  * @ciph_type: [in] cipher type value from software enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228)  * @spu2_type: [out] hash type value used by SPU2 hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229)  * @spu2_mode: [out] hash mode value used by SPU2 hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231)  * Return:  0 if successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) spu2_hash_xlate(enum hash_alg hash_alg, enum hash_mode hash_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		enum hash_type hash_type, enum spu_cipher_type ciph_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		enum spu2_hash_type *spu2_type, enum spu2_hash_mode *spu2_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	err = spu2_hash_mode_xlate(hash_mode, spu2_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		flow_log("Invalid hash mode %d\n", hash_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	switch (hash_alg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	case HASH_ALG_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		*spu2_type = SPU2_HASH_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	case HASH_ALG_MD5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		*spu2_type = SPU2_HASH_TYPE_MD5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	case HASH_ALG_SHA1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 		*spu2_type = SPU2_HASH_TYPE_SHA1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	case HASH_ALG_SHA224:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		*spu2_type = SPU2_HASH_TYPE_SHA224;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	case HASH_ALG_SHA256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		*spu2_type = SPU2_HASH_TYPE_SHA256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	case HASH_ALG_SHA384:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		*spu2_type = SPU2_HASH_TYPE_SHA384;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	case HASH_ALG_SHA512:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		*spu2_type = SPU2_HASH_TYPE_SHA512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	case HASH_ALG_AES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		switch (ciph_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		case CIPHER_TYPE_AES128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 			*spu2_type = SPU2_HASH_TYPE_AES128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		case CIPHER_TYPE_AES192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 			*spu2_type = SPU2_HASH_TYPE_AES192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		case CIPHER_TYPE_AES256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			*spu2_type = SPU2_HASH_TYPE_AES256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	case HASH_ALG_SHA3_224:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		*spu2_type = SPU2_HASH_TYPE_SHA3_224;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	case HASH_ALG_SHA3_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		*spu2_type = SPU2_HASH_TYPE_SHA3_256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	case HASH_ALG_SHA3_384:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		*spu2_type = SPU2_HASH_TYPE_SHA3_384;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	case HASH_ALG_SHA3_512:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		*spu2_type = SPU2_HASH_TYPE_SHA3_512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	case HASH_ALG_LAST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		flow_log("Invalid hash alg %d or type %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 			 hash_alg, hash_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) /* Dump FMD ctrl0. The ctrl0 input is in host byte order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) static void spu2_dump_fmd_ctrl0(u64 ctrl0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	enum spu2_cipher_type ciph_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	enum spu2_cipher_mode ciph_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	enum spu2_hash_type hash_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	enum spu2_hash_mode hash_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	char *ciph_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	char *ciph_mode_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	char *hash_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	char *hash_mode_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	u8 cfb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	u8 proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	packet_log(" FMD CTRL0 %#16llx\n", ctrl0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (ctrl0 & SPU2_CIPH_ENCRYPT_EN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		packet_log("  encrypt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		packet_log("  decrypt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	ciph_type = (ctrl0 & SPU2_CIPH_TYPE) >> SPU2_CIPH_TYPE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	ciph_name = spu2_ciph_type_name(ciph_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	packet_log("  Cipher type: %s\n", ciph_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	if (ciph_type != SPU2_CIPHER_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		ciph_mode = (ctrl0 & SPU2_CIPH_MODE) >> SPU2_CIPH_MODE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		ciph_mode_name = spu2_ciph_mode_name(ciph_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		packet_log("  Cipher mode: %s\n", ciph_mode_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	cfb = (ctrl0 & SPU2_CFB_MASK) >> SPU2_CFB_MASK_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	packet_log("  CFB %#x\n", cfb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	proto = (ctrl0 & SPU2_PROTO_SEL) >> SPU2_PROTO_SEL_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	packet_log("  protocol %#x\n", proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (ctrl0 & SPU2_HASH_FIRST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		packet_log("  hash first\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		packet_log("  cipher first\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	if (ctrl0 & SPU2_CHK_TAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		packet_log("  check tag\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	hash_type = (ctrl0 & SPU2_HASH_TYPE) >> SPU2_HASH_TYPE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	hash_name = spu2_hash_type_name(hash_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	packet_log("  Hash type: %s\n", hash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	if (hash_type != SPU2_HASH_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		hash_mode = (ctrl0 & SPU2_HASH_MODE) >> SPU2_HASH_MODE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 		hash_mode_name = spu2_hash_mode_name(hash_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		packet_log("  Hash mode: %s\n", hash_mode_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	if (ctrl0 & SPU2_CIPH_PAD_EN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		packet_log("  Cipher pad: %#2llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 			   (ctrl0 & SPU2_CIPH_PAD) >> SPU2_CIPH_PAD_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) /* Dump FMD ctrl1. The ctrl1 input is in host byte order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) static void spu2_dump_fmd_ctrl1(u64 ctrl1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	u8 hash_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	u8 ciph_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	u8 ret_iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	u8 iv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	u8 iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	u8 hash_tag_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	u8 ret_md;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	packet_log(" FMD CTRL1 %#16llx\n", ctrl1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	if (ctrl1 & SPU2_TAG_LOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		packet_log("  Tag after payload\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	packet_log("  Msg includes ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (ctrl1 & SPU2_HAS_FR_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		packet_log("FD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	if (ctrl1 & SPU2_HAS_AAD1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		packet_log("AAD1 ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	if (ctrl1 & SPU2_HAS_NAAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		packet_log("NAAD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (ctrl1 & SPU2_HAS_AAD2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		packet_log("AAD2 ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (ctrl1 & SPU2_HAS_ESN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		packet_log("ESN ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	packet_log("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	hash_key_len = (ctrl1 & SPU2_HASH_KEY_LEN) >> SPU2_HASH_KEY_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	packet_log("  Hash key len %u\n", hash_key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	ciph_key_len = (ctrl1 & SPU2_CIPH_KEY_LEN) >> SPU2_CIPH_KEY_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	packet_log("  Cipher key len %u\n", ciph_key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	if (ctrl1 & SPU2_GENIV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		packet_log("  Generate IV\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (ctrl1 & SPU2_HASH_IV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		packet_log("  IV included in hash\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	if (ctrl1 & SPU2_RET_IV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		packet_log("  Return IV in output before payload\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	ret_iv_len = (ctrl1 & SPU2_RET_IV_LEN) >> SPU2_RET_IV_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	packet_log("  Length of returned IV %u bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		   ret_iv_len ? ret_iv_len : 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	iv_offset = (ctrl1 & SPU2_IV_OFFSET) >> SPU2_IV_OFFSET_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	packet_log("  IV offset %u\n", iv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	iv_len = (ctrl1 & SPU2_IV_LEN) >> SPU2_IV_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	packet_log("  Input IV len %u bytes\n", iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	hash_tag_len = (ctrl1 & SPU2_HASH_TAG_LEN) >> SPU2_HASH_TAG_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	packet_log("  Hash tag length %u bytes\n", hash_tag_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	packet_log("  Return ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	ret_md = (ctrl1 & SPU2_RETURN_MD) >> SPU2_RETURN_MD_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (ret_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		packet_log("FMD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (ret_md == SPU2_RET_FMD_OMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		packet_log("OMD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	else if (ret_md == SPU2_RET_FMD_OMD_IV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		packet_log("OMD IV ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (ctrl1 & SPU2_RETURN_FD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		packet_log("FD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (ctrl1 & SPU2_RETURN_AAD1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		packet_log("AAD1 ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	if (ctrl1 & SPU2_RETURN_NAAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		packet_log("NAAD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	if (ctrl1 & SPU2_RETURN_AAD2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		packet_log("AAD2 ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	if (ctrl1 & SPU2_RETURN_PAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		packet_log("Payload");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	packet_log("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) /* Dump FMD ctrl2. The ctrl2 input is in host byte order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) static void spu2_dump_fmd_ctrl2(u64 ctrl2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	packet_log(" FMD CTRL2 %#16llx\n", ctrl2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	packet_log("  AAD1 offset %llu length %llu bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		   ctrl2 & SPU2_AAD1_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		   (ctrl2 & SPU2_AAD1_LEN) >> SPU2_AAD1_LEN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	packet_log("  AAD2 offset %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		   (ctrl2 & SPU2_AAD2_OFFSET) >> SPU2_AAD2_OFFSET_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	packet_log("  Payload offset %llu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		   (ctrl2 & SPU2_PL_OFFSET) >> SPU2_PL_OFFSET_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) /* Dump FMD ctrl3. The ctrl3 input is in host byte order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) static void spu2_dump_fmd_ctrl3(u64 ctrl3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	packet_log(" FMD CTRL3 %#16llx\n", ctrl3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	packet_log("  Payload length %llu bytes\n", ctrl3 & SPU2_PL_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	packet_log("  TLS length %llu bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		   (ctrl3 & SPU2_TLS_LEN) >> SPU2_TLS_LEN_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) static void spu2_dump_fmd(struct SPU2_FMD *fmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	spu2_dump_fmd_ctrl0(le64_to_cpu(fmd->ctrl0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	spu2_dump_fmd_ctrl1(le64_to_cpu(fmd->ctrl1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	spu2_dump_fmd_ctrl2(le64_to_cpu(fmd->ctrl2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	spu2_dump_fmd_ctrl3(le64_to_cpu(fmd->ctrl3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) static void spu2_dump_omd(u8 *omd, u16 hash_key_len, u16 ciph_key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			  u16 hash_iv_len, u16 ciph_iv_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	u8 *ptr = omd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	packet_log(" OMD:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (hash_key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		packet_log("  Hash Key Length %u bytes\n", hash_key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		packet_dump("  KEY: ", ptr, hash_key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		ptr += hash_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (ciph_key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		packet_log("  Cipher Key Length %u bytes\n", ciph_key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		packet_dump("  KEY: ", ptr, ciph_key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		ptr += ciph_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	if (hash_iv_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		packet_log("  Hash IV Length %u bytes\n", hash_iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		packet_dump("  hash IV: ", ptr, hash_iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		ptr += ciph_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	if (ciph_iv_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		packet_log("  Cipher IV Length %u bytes\n", ciph_iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		packet_dump("  cipher IV: ", ptr, ciph_iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) /* Dump a SPU2 header for debug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) void spu2_dump_msg_hdr(u8 *buf, unsigned int buf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	struct SPU2_FMD *fmd = (struct SPU2_FMD *)buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	u8 *omd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	u64 ctrl1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	u16 hash_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	u16 ciph_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	u16 hash_iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	u16 ciph_iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	u16 omd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	packet_log("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	packet_log("SPU2 message header %p len: %u\n", buf, buf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	spu2_dump_fmd(fmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	omd = (u8 *)(fmd + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	ctrl1 = le64_to_cpu(fmd->ctrl1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	hash_key_len = (ctrl1 & SPU2_HASH_KEY_LEN) >> SPU2_HASH_KEY_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	ciph_key_len = (ctrl1 & SPU2_CIPH_KEY_LEN) >> SPU2_CIPH_KEY_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	hash_iv_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	ciph_iv_len = (ctrl1 & SPU2_IV_LEN) >> SPU2_IV_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	spu2_dump_omd(omd, hash_key_len, ciph_key_len, hash_iv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		      ciph_iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	/* Double check sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	omd_len = hash_key_len + ciph_key_len + hash_iv_len + ciph_iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	if (FMD_SIZE + omd_len != buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		packet_log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		    (" Packet parsed incorrectly. buf_len %u, sum of MD %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		     buf_len, FMD_SIZE + omd_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	packet_log("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544)  * spu2_fmd_init() - At setkey time, initialize the fixed meta data for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545)  * subsequent skcipher requests for this context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546)  * @spu2_cipher_type:  Cipher algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * @spu2_mode:         Cipher mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  * @cipher_key_len:    Length of cipher key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * @cipher_iv_len:     Length of cipher initialization vector, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * Return:  0 (success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) static int spu2_fmd_init(struct SPU2_FMD *fmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 			 enum spu2_cipher_type spu2_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			 enum spu2_cipher_mode spu2_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			 u32 cipher_key_len, u32 cipher_iv_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	u64 ctrl0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	u64 ctrl1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	u64 ctrl2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	u64 ctrl3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	u32 aad1_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	u32 aad2_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	u16 aad1_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	u64 payload_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	ctrl0 = (spu2_type << SPU2_CIPH_TYPE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	    (spu2_mode << SPU2_CIPH_MODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	ctrl1 = (cipher_key_len << SPU2_CIPH_KEY_LEN_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	    ((u64)cipher_iv_len << SPU2_IV_LEN_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	    ((u64)SPU2_RET_FMD_ONLY << SPU2_RETURN_MD_SHIFT) | SPU2_RETURN_PAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	 * AAD1 offset is from start of FD. FD length is always 0 for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	 * driver. So AAD1_offset is always 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	aad1_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	aad2_offset = aad1_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	payload_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	ctrl2 = aad1_offset |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	    (aad1_len << SPU2_AAD1_LEN_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	    (aad2_offset << SPU2_AAD2_OFFSET_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	    (payload_offset << SPU2_PL_OFFSET_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	ctrl3 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	fmd->ctrl0 = cpu_to_le64(ctrl0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	fmd->ctrl1 = cpu_to_le64(ctrl1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	fmd->ctrl2 = cpu_to_le64(ctrl2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	fmd->ctrl3 = cpu_to_le64(ctrl3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597)  * spu2_fmd_ctrl0_write() - Write ctrl0 field in fixed metadata (FMD) field of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598)  * SPU request packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * @fmd:            Start of FMD field to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * @is_inbound:     true if decrypting. false if encrypting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * @authFirst:      true if alg authenticates before encrypting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * @protocol:       protocol selector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * @cipher_type:    cipher algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  * @cipher_mode:    cipher mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  * @auth_type:      authentication type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * @auth_mode:      authentication mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) static void spu2_fmd_ctrl0_write(struct SPU2_FMD *fmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 				 bool is_inbound, bool auth_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 				 enum spu2_proto_sel protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 				 enum spu2_cipher_type cipher_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 				 enum spu2_cipher_mode cipher_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 				 enum spu2_hash_type auth_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 				 enum spu2_hash_mode auth_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	u64 ctrl0 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if ((cipher_type != SPU2_CIPHER_TYPE_NONE) && !is_inbound)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		ctrl0 |= SPU2_CIPH_ENCRYPT_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	ctrl0 |= ((u64)cipher_type << SPU2_CIPH_TYPE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	    ((u64)cipher_mode << SPU2_CIPH_MODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	if (protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		ctrl0 |= (u64)protocol << SPU2_PROTO_SEL_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	if (auth_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 		ctrl0 |= SPU2_HASH_FIRST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	if (is_inbound && (auth_type != SPU2_HASH_TYPE_NONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		ctrl0 |= SPU2_CHK_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	ctrl0 |= (((u64)auth_type << SPU2_HASH_TYPE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		  ((u64)auth_mode << SPU2_HASH_MODE_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	fmd->ctrl0 = cpu_to_le64(ctrl0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640)  * spu2_fmd_ctrl1_write() - Write ctrl1 field in fixed metadata (FMD) field of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641)  * SPU request packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642)  * @fmd:            Start of FMD field to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643)  * @assoc_size:     Length of additional associated data, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644)  * @auth_key_len:   Length of authentication key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645)  * @cipher_key_len: Length of cipher key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  * @gen_iv:         If true, hw generates IV and returns in response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  * @hash_iv:        IV participates in hash. Used for IPSEC and TLS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * @return_iv:      Return IV in output packet before payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  * @ret_iv_len:     Length of IV returned from SPU, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)  * @ret_iv_offset:  Offset into full IV of start of returned IV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651)  * @cipher_iv_len:  Length of input cipher IV, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652)  * @digest_size:    Length of digest (aka, hash tag or ICV), in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653)  * @return_payload: Return payload in SPU response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654)  * @return_md : return metadata in SPU response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656)  * Packet can have AAD2 w/o AAD1. For algorithms currently supported,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657)  * associated data goes in AAD2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) static void spu2_fmd_ctrl1_write(struct SPU2_FMD *fmd, bool is_inbound,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 				 u64 assoc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 				 u64 auth_key_len, u64 cipher_key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				 bool gen_iv, bool hash_iv, bool return_iv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 				 u64 ret_iv_len, u64 ret_iv_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 				 u64 cipher_iv_len, u64 digest_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 				 bool return_payload, bool return_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	u64 ctrl1 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (is_inbound && digest_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		ctrl1 |= SPU2_TAG_LOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (assoc_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		ctrl1 |= SPU2_HAS_AAD2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		ctrl1 |= SPU2_RETURN_AAD2;  /* need aad2 for gcm aes esp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (auth_key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		ctrl1 |= ((auth_key_len << SPU2_HASH_KEY_LEN_SHIFT) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			  SPU2_HASH_KEY_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (cipher_key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		ctrl1 |= ((cipher_key_len << SPU2_CIPH_KEY_LEN_SHIFT) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			  SPU2_CIPH_KEY_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (gen_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		ctrl1 |= SPU2_GENIV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (hash_iv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		ctrl1 |= SPU2_HASH_IV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	if (return_iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		ctrl1 |= SPU2_RET_IV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		ctrl1 |= ret_iv_len << SPU2_RET_IV_LEN_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		ctrl1 |= ret_iv_offset << SPU2_IV_OFFSET_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	ctrl1 |= ((cipher_iv_len << SPU2_IV_LEN_SHIFT) & SPU2_IV_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (digest_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		ctrl1 |= ((digest_size << SPU2_HASH_TAG_LEN_SHIFT) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			  SPU2_HASH_TAG_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	/* Let's ask for the output pkt to include FMD, but don't need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	 * get keys and IVs back in OMD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	if (return_md)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		ctrl1 |= ((u64)SPU2_RET_FMD_ONLY << SPU2_RETURN_MD_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		ctrl1 |= ((u64)SPU2_RET_NO_MD << SPU2_RETURN_MD_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	/* Crypto API does not get assoc data back. So no need for AAD2. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (return_payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		ctrl1 |= SPU2_RETURN_PAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	fmd->ctrl1 = cpu_to_le64(ctrl1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720)  * spu2_fmd_ctrl2_write() - Set the ctrl2 field in the fixed metadata field of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721)  * SPU2 header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722)  * @fmd:            Start of FMD field to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723)  * @cipher_offset:  Number of bytes from Start of Packet (end of FD field) where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724)  *                  data to be encrypted or decrypted begins
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725)  * @auth_key_len:   Length of authentication key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726)  * @auth_iv_len:    Length of authentication initialization vector, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727)  * @cipher_key_len: Length of cipher key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728)  * @cipher_iv_len:  Length of cipher IV, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static void spu2_fmd_ctrl2_write(struct SPU2_FMD *fmd, u64 cipher_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 				 u64 auth_key_len, u64 auth_iv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 				 u64 cipher_key_len, u64 cipher_iv_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	u64 ctrl2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	u64 aad1_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	u64 aad2_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	u16 aad1_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	u64 payload_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	/* AAD1 offset is from start of FD. FD length always 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	aad1_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	aad2_offset = aad1_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	payload_offset = cipher_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	ctrl2 = aad1_offset |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	    (aad1_len << SPU2_AAD1_LEN_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	    (aad2_offset << SPU2_AAD2_OFFSET_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	    (payload_offset << SPU2_PL_OFFSET_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	fmd->ctrl2 = cpu_to_le64(ctrl2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * spu2_fmd_ctrl3_write() - Set the ctrl3 field in FMD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  * @fmd:          Fixed meta data. First field in SPU2 msg header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * @payload_len:  Length of payload, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) static void spu2_fmd_ctrl3_write(struct SPU2_FMD *fmd, u64 payload_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	u64 ctrl3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	ctrl3 = payload_len & SPU2_PL_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	fmd->ctrl3 = cpu_to_le64(ctrl3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * spu2_ctx_max_payload() - Determine the maximum length of the payload for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  * SPU message for a given cipher and hash alg context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770)  * @cipher_alg:		The cipher algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771)  * @cipher_mode:	The cipher mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772)  * @blocksize:		The size of a block of data for this algo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774)  * For SPU2, the hardware generally ignores the PayloadLen field in ctrl3 of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  * FMD and just keeps computing until it receives a DMA descriptor with the EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  * flag set. So we consider the max payload to be infinite. AES CCM is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  * exception.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779)  * Return: Max payload length in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) u32 spu2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 			 enum spu_cipher_mode cipher_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			 unsigned int blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	if ((cipher_alg == CIPHER_ALG_AES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	    (cipher_mode == CIPHER_MODE_CCM)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		u32 excess = SPU2_MAX_PAYLOAD % blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		return SPU2_MAX_PAYLOAD - excess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return SPU_MAX_PAYLOAD_INF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796)  * spu_payload_length() -  Given a SPU2 message header, extract the payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797)  * length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798)  * @spu_hdr:  Start of SPU message header (FMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800)  * Return: payload length, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) u32 spu2_payload_length(u8 *spu_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	struct SPU2_FMD *fmd = (struct SPU2_FMD *)spu_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	u32 pl_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	u64 ctrl3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	ctrl3 = le64_to_cpu(fmd->ctrl3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	pl_len = ctrl3 & SPU2_PL_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	return pl_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815)  * spu_response_hdr_len() - Determine the expected length of a SPU response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  * header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * @auth_key_len:  Length of authentication key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  * @enc_key_len:   Length of encryption key, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * For SPU2, includes just FMD. OMD is never requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822)  * Return: Length of FMD, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) u16 spu2_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	return FMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830)  * spu_hash_pad_len() - Calculate the length of hash padding required to extend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831)  * data to a full block size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832)  * @hash_alg:        hash algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833)  * @hash_mode:       hash mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  * @chunksize:       length of data, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  * @hash_block_size: size of a hash block, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837)  * SPU2 hardware does all hash padding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839)  * Return:  length of hash pad in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) u16 spu2_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		      u32 chunksize, u16 hash_block_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * spu2_gcm_ccm_padlen() -  Determine the length of GCM/CCM padding for either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * the AAD field or the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851)  * Return:  0. Unlike SPU-M, SPU2 hardware does any GCM/CCM padding required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) u32 spu2_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			 unsigned int data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860)  * spu_assoc_resp_len() - Determine the size of the AAD2 buffer needed to catch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861)  * associated data in a SPU2 output packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862)  * @cipher_mode:   cipher mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863)  * @assoc_len:     length of additional associated data, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864)  * @iv_len:        length of initialization vector, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865)  * @is_encrypt:    true if encrypting. false if decrypt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867)  * Return: Length of buffer to catch associated data in response
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) u32 spu2_assoc_resp_len(enum spu_cipher_mode cipher_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			unsigned int assoc_len, unsigned int iv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			bool is_encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	u32 resp_len = assoc_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	if (is_encrypt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		/* gcm aes esp has to write 8-byte IV in response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		resp_len += iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	return resp_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  * spu_aead_ivlen() - Calculate the length of the AEAD IV to be included
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883)  * in a SPU request after the AAD and before the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884)  * @cipher_mode:  cipher mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885)  * @iv_ctr_len:   initialization vector length in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887)  * For SPU2, AEAD IV is included in OMD and does not need to be repeated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888)  * prior to the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890)  * Return: Length of AEAD IV in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) u8 spu2_aead_ivlen(enum spu_cipher_mode cipher_mode, u16 iv_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898)  * spu2_hash_type() - Determine the type of hash operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899)  * @src_sent:  The number of bytes in the current request that have already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900)  *             been sent to the SPU to be hashed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902)  * SPU2 always does a FULL hash operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) enum hash_type spu2_hash_type(u32 src_sent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	return HASH_TYPE_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910)  * spu2_digest_size() - Determine the size of a hash digest to expect the SPU to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911)  * return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912)  * alg_digest_size: Number of bytes in the final digest for the given algo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913)  * alg:             The hash algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914)  * htype:           Type of hash operation (init, update, full, etc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) u32 spu2_digest_size(u32 alg_digest_size, enum hash_alg alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		     enum hash_type htype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	return alg_digest_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924)  * spu_create_request() - Build a SPU2 request message header, includint FMD and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925)  * OMD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926)  * @spu_hdr: Start of buffer where SPU request header is to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927)  * @req_opts: SPU request message options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928)  * @cipher_parms: Parameters related to cipher algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * @hash_parms:   Parameters related to hash algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  * @aead_parms:   Parameters related to AEAD operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * @data_size:    Length of data to be encrypted or authenticated. If AEAD, does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  *		  not include length of AAD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * Construct the message starting at spu_hdr. Caller should allocate this buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  * in DMA-able memory at least SPU_HEADER_ALLOC_LEN bytes long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  * Return: the length of the SPU header in bytes. 0 if an error occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) u32 spu2_create_request(u8 *spu_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			struct spu_request_opts *req_opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 			struct spu_cipher_parms *cipher_parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			struct spu_hash_parms *hash_parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			struct spu_aead_parms *aead_parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			unsigned int data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	struct SPU2_FMD *fmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	u8 *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	unsigned int buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	enum spu2_cipher_type spu2_ciph_type = SPU2_CIPHER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	enum spu2_cipher_mode spu2_ciph_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	enum spu2_hash_type spu2_auth_type = SPU2_HASH_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	enum spu2_hash_mode spu2_auth_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	bool return_md = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	enum spu2_proto_sel proto = SPU2_PROTO_RESV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	/* size of the payload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	unsigned int payload_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	    hash_parms->prebuf_len + data_size + hash_parms->pad_len -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	    ((req_opts->is_aead && req_opts->is_inbound) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	     hash_parms->digestsize : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	/* offset of prebuf or data from start of AAD2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	unsigned int cipher_offset = aead_parms->assoc_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			aead_parms->aad_pad_len + aead_parms->iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	/* total size of the data following OMD (without STAT word padding) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	unsigned int real_db_size = spu_real_db_size(aead_parms->assoc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 						 aead_parms->iv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 						 hash_parms->prebuf_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 						 data_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 						 aead_parms->aad_pad_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 						 aead_parms->data_pad_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 						 hash_parms->pad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	unsigned int assoc_size = aead_parms->assoc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (req_opts->is_aead &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	    (cipher_parms->alg == CIPHER_ALG_AES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	    (cipher_parms->mode == CIPHER_MODE_GCM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		 * On SPU 2, aes gcm cipher first on encrypt, auth first on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		 * decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		req_opts->auth_first = req_opts->is_inbound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	/* and do opposite for ccm (auth 1st on encrypt) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	if (req_opts->is_aead &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	    (cipher_parms->alg == CIPHER_ALG_AES) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	    (cipher_parms->mode == CIPHER_MODE_CCM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		req_opts->auth_first = !req_opts->is_inbound;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	flow_log("%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	flow_log("  in:%u authFirst:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		 req_opts->is_inbound, req_opts->auth_first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	flow_log("  cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		 cipher_parms->mode, cipher_parms->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	flow_log("  is_esp: %s\n", req_opts->is_esp ? "yes" : "no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	flow_log("    key: %d\n", cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	flow_dump("    key: ", cipher_parms->key_buf, cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	flow_log("    iv: %d\n", cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	flow_dump("    iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	flow_log("  auth alg:%u mode:%u type %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		 hash_parms->alg, hash_parms->mode, hash_parms->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	flow_log("  digestsize: %u\n", hash_parms->digestsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	flow_log("  authkey: %d\n", hash_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	flow_dump("  authkey: ", hash_parms->key_buf, hash_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	flow_log("  assoc_size:%u\n", assoc_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	flow_log("  prebuf_len:%u\n", hash_parms->prebuf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	flow_log("  data_size:%u\n", data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	flow_log("  hash_pad_len:%u\n", hash_parms->pad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	flow_log("  real_db_size:%u\n", real_db_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	flow_log("  cipher_offset:%u payload_len:%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		 cipher_offset, payload_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	flow_log("  aead_iv: %u\n", aead_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	/* Convert to spu2 values for cipher alg, hash alg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	err = spu2_cipher_xlate(cipher_parms->alg, cipher_parms->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 				cipher_parms->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 				&spu2_ciph_type, &spu2_ciph_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	/* If we are doing GCM hashing only - either via rfc4543 transform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	 * or because we happen to do GCM with AAD only and no payload - we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	 * need to configure hardware to use hash key rather than cipher key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	 * and put data into payload.  This is because unlike SPU-M, running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	 * GCM cipher with 0 size payload is not permitted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	if ((req_opts->is_rfc4543) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	    ((spu2_ciph_mode == SPU2_CIPHER_MODE_GCM) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	    (payload_len == 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		/* Use hashing (only) and set up hash key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		spu2_ciph_type = SPU2_CIPHER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		hash_parms->key_len = cipher_parms->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		memcpy(hash_parms->key_buf, cipher_parms->key_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		       cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		cipher_parms->key_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		if (req_opts->is_rfc4543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			payload_len += assoc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			payload_len = assoc_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		cipher_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		assoc_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	flow_log("spu2 cipher type %s, cipher mode %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		 spu2_ciph_type_name(spu2_ciph_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		 spu2_ciph_mode_name(spu2_ciph_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	err = spu2_hash_xlate(hash_parms->alg, hash_parms->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			      hash_parms->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			      cipher_parms->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			      &spu2_auth_type, &spu2_auth_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	flow_log("spu2 hash type %s, hash mode %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		 spu2_hash_type_name(spu2_auth_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		 spu2_hash_mode_name(spu2_auth_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	fmd = (struct SPU2_FMD *)spu_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	spu2_fmd_ctrl0_write(fmd, req_opts->is_inbound, req_opts->auth_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 			     proto, spu2_ciph_type, spu2_ciph_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			     spu2_auth_type, spu2_auth_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	spu2_fmd_ctrl1_write(fmd, req_opts->is_inbound, assoc_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			     hash_parms->key_len, cipher_parms->key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			     false, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			     aead_parms->return_iv, aead_parms->ret_iv_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			     aead_parms->ret_iv_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			     cipher_parms->iv_len, hash_parms->digestsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 			     !req_opts->bd_suppress, return_md);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	spu2_fmd_ctrl2_write(fmd, cipher_offset, hash_parms->key_len, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			     cipher_parms->key_len, cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	spu2_fmd_ctrl3_write(fmd, payload_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	ptr = (u8 *)(fmd + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	buf_len = sizeof(struct SPU2_FMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	/* Write OMD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (hash_parms->key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		memcpy(ptr, hash_parms->key_buf, hash_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 		ptr += hash_parms->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		buf_len += hash_parms->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	if (cipher_parms->key_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		memcpy(ptr, cipher_parms->key_buf, cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		ptr += cipher_parms->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		buf_len += cipher_parms->key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	if (cipher_parms->iv_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		memcpy(ptr, cipher_parms->iv_buf, cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		ptr += cipher_parms->iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		buf_len += cipher_parms->iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	packet_dump("  SPU request header: ", spu_hdr, buf_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	return buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)  * spu_cipher_req_init() - Build an skcipher SPU2 request message header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111)  * including FMD and OMD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  * @spu_hdr:       Location of start of SPU request (FMD field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)  * @cipher_parms:  Parameters describing cipher request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)  * Called at setkey time to initialize a msg header that can be reused for all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)  * subsequent skcipher requests. Construct the message starting at spu_hdr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)  * Caller should allocate this buffer in DMA-able memory at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)  * SPU_HEADER_ALLOC_LEN bytes long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  * Return: the total length of the SPU header (FMD and OMD) in bytes. 0 if an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)  * error occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) u16 spu2_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	struct SPU2_FMD *fmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	u8 *omd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	enum spu2_cipher_type spu2_type = SPU2_CIPHER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	enum spu2_cipher_mode spu2_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	flow_log("%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	flow_log("  cipher alg:%u mode:%u type %u\n", cipher_parms->alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		 cipher_parms->mode, cipher_parms->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	flow_log("  cipher_iv_len: %u\n", cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	flow_log("    key: %d\n", cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	flow_dump("    key: ", cipher_parms->key_buf, cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	/* Convert to spu2 values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	err = spu2_cipher_xlate(cipher_parms->alg, cipher_parms->mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 				cipher_parms->type, &spu2_type, &spu2_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	flow_log("spu2 cipher type %s, cipher mode %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 		 spu2_ciph_type_name(spu2_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		 spu2_ciph_mode_name(spu2_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	/* Construct the FMD header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	fmd = (struct SPU2_FMD *)spu_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	err = spu2_fmd_init(fmd, spu2_type, spu2_mode, cipher_parms->key_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 			    cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	/* Write cipher key to OMD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	omd = (u8 *)(fmd + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	if (cipher_parms->key_buf && cipher_parms->key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 		memcpy(omd, cipher_parms->key_buf, cipher_parms->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	packet_dump("  SPU request header: ", spu_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		    FMD_SIZE + cipher_parms->key_len + cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	return FMD_SIZE + cipher_parms->key_len + cipher_parms->iv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)  * spu_cipher_req_finish() - Finish building a SPU request message header for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)  * block cipher request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)  * @spu_hdr:         Start of the request message header (MH field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)  * @spu_req_hdr_len: Length in bytes of the SPU request header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)  * @isInbound:       0 encrypt, 1 decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)  * @cipher_parms:    Parameters describing cipher operation to be performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)  * @data_size:       Length of the data in the BD field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)  * Assumes much of the header was already filled in at setkey() time in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)  * spu_cipher_req_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)  * spu_cipher_req_init() fills in the encryption key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) void spu2_cipher_req_finish(u8 *spu_hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			    u16 spu_req_hdr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 			    unsigned int is_inbound,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 			    struct spu_cipher_parms *cipher_parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 			    unsigned int data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	struct SPU2_FMD *fmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	u8 *omd;		/* start of optional metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	u64 ctrl0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	u64 ctrl3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	flow_log("%s()\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	flow_log(" in: %u\n", is_inbound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	flow_log(" cipher alg: %u, cipher_type: %u\n", cipher_parms->alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		 cipher_parms->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	flow_log(" iv len: %d\n", cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	flow_dump("    iv: ", cipher_parms->iv_buf, cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	flow_log(" data_size: %u\n", data_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	fmd = (struct SPU2_FMD *)spu_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	omd = (u8 *)(fmd + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	 * FMD ctrl0 was initialized at setkey time. update it to indicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	 * whether we are encrypting or decrypting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	ctrl0 = le64_to_cpu(fmd->ctrl0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	if (is_inbound)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		ctrl0 &= ~SPU2_CIPH_ENCRYPT_EN;	/* decrypt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		ctrl0 |= SPU2_CIPH_ENCRYPT_EN;	/* encrypt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	fmd->ctrl0 = cpu_to_le64(ctrl0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	if (cipher_parms->alg && cipher_parms->iv_buf && cipher_parms->iv_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		/* cipher iv provided so put it in here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		memcpy(omd + cipher_parms->key_len, cipher_parms->iv_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		       cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	ctrl3 = le64_to_cpu(fmd->ctrl3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	data_size &= SPU2_PL_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	ctrl3 |= data_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	fmd->ctrl3 = cpu_to_le64(ctrl3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	packet_dump("  SPU request header: ", spu_hdr, spu_req_hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)  * spu_request_pad() - Create pad bytes at the end of the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)  * @pad_start:      Start of buffer where pad bytes are to be written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)  * @gcm_padding:    Length of GCM padding, in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)  * @hash_pad_len:   Number of bytes of padding extend data to full block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)  * @auth_alg:       Authentication algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)  * @auth_mode:      Authentication mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  * @total_sent:     Length inserted at end of hash pad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  * @status_padding: Number of bytes of padding to align STATUS word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  * There may be three forms of pad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  *  1. GCM pad - for GCM mode ciphers, pad to 16-byte alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  *  2. hash pad - pad to a block length, with 0x80 data terminator and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  *                size at the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)  *  3. STAT pad - to ensure the STAT field is 4-byte aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) void spu2_request_pad(u8 *pad_start, u32 gcm_padding, u32 hash_pad_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 		      enum hash_alg auth_alg, enum hash_mode auth_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		      unsigned int total_sent, u32 status_padding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	u8 *ptr = pad_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	/* fix data alignent for GCM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	if (gcm_padding > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		flow_log("  GCM: padding to 16 byte alignment: %u bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			 gcm_padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		memset(ptr, 0, gcm_padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		ptr += gcm_padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	if (hash_pad_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		/* clear the padding section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		memset(ptr, 0, hash_pad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		/* terminate the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		*ptr = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		ptr += (hash_pad_len - sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		/* add the size at the end as required per alg */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		if (auth_alg == HASH_ALG_MD5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			*(u64 *)ptr = cpu_to_le64((u64)total_sent * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		else		/* SHA1, SHA2-224, SHA2-256 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			*(u64 *)ptr = cpu_to_be64((u64)total_sent * 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		ptr += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	/* pad to a 4byte alignment for STAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (status_padding > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		flow_log("  STAT: padding to 4 byte alignment: %u bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 			 status_padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		memset(ptr, 0, status_padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		ptr += status_padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)  * spu2_xts_tweak_in_payload() - Indicate that SPU2 does NOT place the XTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)  * tweak field in the packet payload (it uses IV instead)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  * Return: 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) u8 spu2_xts_tweak_in_payload(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)  * spu2_tx_status_len() - Return the length of the STATUS field in a SPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)  * response message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)  * Return: Length of STATUS field in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) u8 spu2_tx_status_len(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	return SPU2_TX_STATUS_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)  * spu2_rx_status_len() - Return the length of the STATUS field in a SPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)  * response message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)  * Return: Length of STATUS field in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) u8 spu2_rx_status_len(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	return SPU2_RX_STATUS_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)  * spu_status_process() - Process the status from a SPU response message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)  * @statp:  start of STATUS word
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)  * Return:  0 - if status is good and response should be processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)  *         !0 - status indicates an error and response is invalid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) int spu2_status_process(u8 *statp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	/* SPU2 status is 2 bytes by default - SPU_RX_STATUS_LEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	u16 status = le16_to_cpu(*(__le16 *)statp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	if (status == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	flow_log("rx status is %#x\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	if (status == SPU2_INVALID_ICV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		return SPU_INVALID_ICV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	return -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338)  * spu2_ccm_update_iv() - Update the IV as per the requirements for CCM mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)  * @digestsize:		Digest size of this request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)  * @cipher_parms:	(pointer to) cipher parmaeters, includes IV buf & IV len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)  * @assoclen:		Length of AAD data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)  * @chunksize:		length of input data to be sent in this req
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)  * @is_encrypt:		true if this is an output/encrypt operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)  * @is_esp:		true if this is an ESP / RFC4309 operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) void spu2_ccm_update_iv(unsigned int digestsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			struct spu_cipher_parms *cipher_parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			unsigned int assoclen, unsigned int chunksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			bool is_encrypt, bool is_esp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	int L;  /* size of length field, in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	 * In RFC4309 mode, L is fixed at 4 bytes; otherwise, IV from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	 * testmgr contains (L-1) in bottom 3 bits of first byte,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	 * per RFC 3610.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	if (is_esp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 		L = CCM_ESP_L_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		L = ((cipher_parms->iv_buf[0] & CCM_B0_L_PRIME) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 		      CCM_B0_L_PRIME_SHIFT) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	/* SPU2 doesn't want these length bytes nor the first byte... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	cipher_parms->iv_len -= (1 + L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	memmove(cipher_parms->iv_buf, &cipher_parms->iv_buf[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		cipher_parms->iv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)  * spu2_wordalign_padlen() - SPU2 does not require padding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)  * @data_size: length of data field in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)  * Return: length of status field padding, in bytes (always 0 on SPU2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) u32 spu2_wordalign_padlen(u32 data_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }