18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/* \file cc_aead.h
58c2ecf20Sopenharmony_ci * ARM CryptoCell AEAD Crypto API
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef __CC_AEAD_H__
98c2ecf20Sopenharmony_ci#define __CC_AEAD_H__
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
138c2ecf20Sopenharmony_ci#include <crypto/ctr.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* mac_cmp - HW writes 8 B but all bytes hold the same value */
168c2ecf20Sopenharmony_ci#define ICV_CMP_SIZE 8
178c2ecf20Sopenharmony_ci#define CCM_CONFIG_BUF_SIZE (AES_BLOCK_SIZE * 3)
188c2ecf20Sopenharmony_ci#define MAX_MAC_SIZE SHA256_DIGEST_SIZE
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* defines for AES GCM configuration buffer */
218c2ecf20Sopenharmony_ci#define GCM_BLOCK_LEN_SIZE 8
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define GCM_BLOCK_RFC4_IV_OFFSET	4
248c2ecf20Sopenharmony_ci#define GCM_BLOCK_RFC4_IV_SIZE		8  /* IV size for rfc's */
258c2ecf20Sopenharmony_ci#define GCM_BLOCK_RFC4_NONCE_OFFSET	0
268c2ecf20Sopenharmony_ci#define GCM_BLOCK_RFC4_NONCE_SIZE	4
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Offsets into AES CCM configuration buffer */
298c2ecf20Sopenharmony_ci#define CCM_B0_OFFSET 0
308c2ecf20Sopenharmony_ci#define CCM_A0_OFFSET 16
318c2ecf20Sopenharmony_ci#define CCM_CTR_COUNT_0_OFFSET 32
328c2ecf20Sopenharmony_ci/* CCM B0 and CTR_COUNT constants. */
338c2ecf20Sopenharmony_ci#define CCM_BLOCK_NONCE_OFFSET 1  /* Nonce offset inside B0 and CTR_COUNT */
348c2ecf20Sopenharmony_ci#define CCM_BLOCK_NONCE_SIZE   3  /* Nonce size inside B0 and CTR_COUNT */
358c2ecf20Sopenharmony_ci#define CCM_BLOCK_IV_OFFSET    4  /* IV offset inside B0 and CTR_COUNT */
368c2ecf20Sopenharmony_ci#define CCM_BLOCK_IV_SIZE      8  /* IV size inside B0 and CTR_COUNT */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cienum aead_ccm_header_size {
398c2ecf20Sopenharmony_ci	ccm_header_size_null = -1,
408c2ecf20Sopenharmony_ci	ccm_header_size_zero = 0,
418c2ecf20Sopenharmony_ci	ccm_header_size_2 = 2,
428c2ecf20Sopenharmony_ci	ccm_header_size_6 = 6,
438c2ecf20Sopenharmony_ci	ccm_header_size_max = S32_MAX
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct aead_req_ctx {
478c2ecf20Sopenharmony_ci	/* Allocate cache line although only 4 bytes are needed to
488c2ecf20Sopenharmony_ci	 *  assure next field falls @ cache line
498c2ecf20Sopenharmony_ci	 *  Used for both: digest HW compare and CCM/GCM MAC value
508c2ecf20Sopenharmony_ci	 */
518c2ecf20Sopenharmony_ci	u8 mac_buf[MAX_MAC_SIZE] ____cacheline_aligned;
528c2ecf20Sopenharmony_ci	u8 ctr_iv[AES_BLOCK_SIZE] ____cacheline_aligned;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	//used in gcm
558c2ecf20Sopenharmony_ci	u8 gcm_iv_inc1[AES_BLOCK_SIZE] ____cacheline_aligned;
568c2ecf20Sopenharmony_ci	u8 gcm_iv_inc2[AES_BLOCK_SIZE] ____cacheline_aligned;
578c2ecf20Sopenharmony_ci	u8 hkey[AES_BLOCK_SIZE] ____cacheline_aligned;
588c2ecf20Sopenharmony_ci	struct {
598c2ecf20Sopenharmony_ci		u8 len_a[GCM_BLOCK_LEN_SIZE] ____cacheline_aligned;
608c2ecf20Sopenharmony_ci		u8 len_c[GCM_BLOCK_LEN_SIZE];
618c2ecf20Sopenharmony_ci	} gcm_len_block;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	u8 ccm_config[CCM_CONFIG_BUF_SIZE] ____cacheline_aligned;
648c2ecf20Sopenharmony_ci	/* HW actual size input */
658c2ecf20Sopenharmony_ci	unsigned int hw_iv_size ____cacheline_aligned;
668c2ecf20Sopenharmony_ci	/* used to prevent cache coherence problem */
678c2ecf20Sopenharmony_ci	u8 backup_mac[MAX_MAC_SIZE];
688c2ecf20Sopenharmony_ci	u8 *backup_iv; /* store orig iv */
698c2ecf20Sopenharmony_ci	u32 assoclen; /* size of AAD buffer to authenticate */
708c2ecf20Sopenharmony_ci	dma_addr_t mac_buf_dma_addr; /* internal ICV DMA buffer */
718c2ecf20Sopenharmony_ci	/* buffer for internal ccm configurations */
728c2ecf20Sopenharmony_ci	dma_addr_t ccm_iv0_dma_addr;
738c2ecf20Sopenharmony_ci	dma_addr_t icv_dma_addr; /* Phys. address of ICV */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	//used in gcm
768c2ecf20Sopenharmony_ci	/* buffer for internal gcm configurations */
778c2ecf20Sopenharmony_ci	dma_addr_t gcm_iv_inc1_dma_addr;
788c2ecf20Sopenharmony_ci	/* buffer for internal gcm configurations */
798c2ecf20Sopenharmony_ci	dma_addr_t gcm_iv_inc2_dma_addr;
808c2ecf20Sopenharmony_ci	dma_addr_t hkey_dma_addr; /* Phys. address of hkey */
818c2ecf20Sopenharmony_ci	dma_addr_t gcm_block_len_dma_addr; /* Phys. address of gcm block len */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	u8 *icv_virt_addr; /* Virt. address of ICV */
848c2ecf20Sopenharmony_ci	struct async_gen_req_ctx gen_ctx;
858c2ecf20Sopenharmony_ci	struct cc_mlli assoc;
868c2ecf20Sopenharmony_ci	struct cc_mlli src;
878c2ecf20Sopenharmony_ci	struct cc_mlli dst;
888c2ecf20Sopenharmony_ci	struct scatterlist *src_sgl;
898c2ecf20Sopenharmony_ci	struct scatterlist *dst_sgl;
908c2ecf20Sopenharmony_ci	unsigned int src_offset;
918c2ecf20Sopenharmony_ci	unsigned int dst_offset;
928c2ecf20Sopenharmony_ci	enum cc_req_dma_buf_type assoc_buff_type;
938c2ecf20Sopenharmony_ci	enum cc_req_dma_buf_type data_buff_type;
948c2ecf20Sopenharmony_ci	struct mlli_params mlli_params;
958c2ecf20Sopenharmony_ci	unsigned int cryptlen;
968c2ecf20Sopenharmony_ci	struct scatterlist ccm_adata_sg;
978c2ecf20Sopenharmony_ci	enum aead_ccm_header_size ccm_hdr_size;
988c2ecf20Sopenharmony_ci	unsigned int req_authsize;
998c2ecf20Sopenharmony_ci	enum drv_cipher_mode cipher_mode;
1008c2ecf20Sopenharmony_ci	bool is_icv_fragmented;
1018c2ecf20Sopenharmony_ci	bool is_single_pass;
1028c2ecf20Sopenharmony_ci	bool plaintext_authenticate_only; //for gcm_rfc4543
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ciint cc_aead_alloc(struct cc_drvdata *drvdata);
1068c2ecf20Sopenharmony_ciint cc_aead_free(struct cc_drvdata *drvdata);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#endif /*__CC_AEAD_H__*/
109