162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * caam - Freescale FSL CAAM support for Public Key Cryptography descriptors
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2016 Freescale Semiconductor, Inc.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * There is no Shared Descriptor for PKC so that the Job Descriptor must carry
862306a36Sopenharmony_ci * all the desired key parameters, input and output pointers.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef _PKC_DESC_H_
1262306a36Sopenharmony_ci#define _PKC_DESC_H_
1362306a36Sopenharmony_ci#include "compat.h"
1462306a36Sopenharmony_ci#include "pdb.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/**
1762306a36Sopenharmony_ci * caam_priv_key_form - CAAM RSA private key representation
1862306a36Sopenharmony_ci * CAAM RSA private key may have either of three forms.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * 1. The first representation consists of the pair (n, d), where the
2162306a36Sopenharmony_ci *    components have the following meanings:
2262306a36Sopenharmony_ci *        n      the RSA modulus
2362306a36Sopenharmony_ci *        d      the RSA private exponent
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * 2. The second representation consists of the triplet (p, q, d), where the
2662306a36Sopenharmony_ci *    components have the following meanings:
2762306a36Sopenharmony_ci *        p      the first prime factor of the RSA modulus n
2862306a36Sopenharmony_ci *        q      the second prime factor of the RSA modulus n
2962306a36Sopenharmony_ci *        d      the RSA private exponent
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * 3. The third representation consists of the quintuple (p, q, dP, dQ, qInv),
3262306a36Sopenharmony_ci *    where the components have the following meanings:
3362306a36Sopenharmony_ci *        p      the first prime factor of the RSA modulus n
3462306a36Sopenharmony_ci *        q      the second prime factor of the RSA modulus n
3562306a36Sopenharmony_ci *        dP     the first factors's CRT exponent
3662306a36Sopenharmony_ci *        dQ     the second factors's CRT exponent
3762306a36Sopenharmony_ci *        qInv   the (first) CRT coefficient
3862306a36Sopenharmony_ci *
3962306a36Sopenharmony_ci * The benefit of using the third or the second key form is lower computational
4062306a36Sopenharmony_ci * cost for the decryption and signature operations.
4162306a36Sopenharmony_ci */
4262306a36Sopenharmony_cienum caam_priv_key_form {
4362306a36Sopenharmony_ci	FORM1,
4462306a36Sopenharmony_ci	FORM2,
4562306a36Sopenharmony_ci	FORM3
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/**
4962306a36Sopenharmony_ci * caam_rsa_key - CAAM RSA key structure. Keys are allocated in DMA zone.
5062306a36Sopenharmony_ci * @n           : RSA modulus raw byte stream
5162306a36Sopenharmony_ci * @e           : RSA public exponent raw byte stream
5262306a36Sopenharmony_ci * @d           : RSA private exponent raw byte stream
5362306a36Sopenharmony_ci * @p           : RSA prime factor p of RSA modulus n
5462306a36Sopenharmony_ci * @q           : RSA prime factor q of RSA modulus n
5562306a36Sopenharmony_ci * @dp          : RSA CRT exponent of p
5662306a36Sopenharmony_ci * @dp          : RSA CRT exponent of q
5762306a36Sopenharmony_ci * @qinv        : RSA CRT coefficient
5862306a36Sopenharmony_ci * @tmp1        : CAAM uses this temporary buffer as internal state buffer.
5962306a36Sopenharmony_ci *                It is assumed to be as long as p.
6062306a36Sopenharmony_ci * @tmp2        : CAAM uses this temporary buffer as internal state buffer.
6162306a36Sopenharmony_ci *                It is assumed to be as long as q.
6262306a36Sopenharmony_ci * @n_sz        : length in bytes of RSA modulus n
6362306a36Sopenharmony_ci * @e_sz        : length in bytes of RSA public exponent
6462306a36Sopenharmony_ci * @d_sz        : length in bytes of RSA private exponent
6562306a36Sopenharmony_ci * @p_sz        : length in bytes of RSA prime factor p of RSA modulus n
6662306a36Sopenharmony_ci * @q_sz        : length in bytes of RSA prime factor q of RSA modulus n
6762306a36Sopenharmony_ci * @priv_form   : CAAM RSA private key representation
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_cistruct caam_rsa_key {
7062306a36Sopenharmony_ci	u8 *n;
7162306a36Sopenharmony_ci	u8 *e;
7262306a36Sopenharmony_ci	u8 *d;
7362306a36Sopenharmony_ci	u8 *p;
7462306a36Sopenharmony_ci	u8 *q;
7562306a36Sopenharmony_ci	u8 *dp;
7662306a36Sopenharmony_ci	u8 *dq;
7762306a36Sopenharmony_ci	u8 *qinv;
7862306a36Sopenharmony_ci	u8 *tmp1;
7962306a36Sopenharmony_ci	u8 *tmp2;
8062306a36Sopenharmony_ci	size_t n_sz;
8162306a36Sopenharmony_ci	size_t e_sz;
8262306a36Sopenharmony_ci	size_t d_sz;
8362306a36Sopenharmony_ci	size_t p_sz;
8462306a36Sopenharmony_ci	size_t q_sz;
8562306a36Sopenharmony_ci	enum caam_priv_key_form priv_form;
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/**
8962306a36Sopenharmony_ci * caam_rsa_ctx - per session context.
9062306a36Sopenharmony_ci * @key         : RSA key in DMA zone
9162306a36Sopenharmony_ci * @dev         : device structure
9262306a36Sopenharmony_ci * @padding_dma : dma address of padding, for adding it to the input
9362306a36Sopenharmony_ci */
9462306a36Sopenharmony_cistruct caam_rsa_ctx {
9562306a36Sopenharmony_ci	struct caam_rsa_key key;
9662306a36Sopenharmony_ci	struct device *dev;
9762306a36Sopenharmony_ci	dma_addr_t padding_dma;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/**
10262306a36Sopenharmony_ci * caam_rsa_req_ctx - per request context.
10362306a36Sopenharmony_ci * @src           : input scatterlist (stripped of leading zeros)
10462306a36Sopenharmony_ci * @fixup_src     : input scatterlist (that might be stripped of leading zeros)
10562306a36Sopenharmony_ci * @fixup_src_len : length of the fixup_src input scatterlist
10662306a36Sopenharmony_ci * @edesc         : s/w-extended rsa descriptor
10762306a36Sopenharmony_ci * @akcipher_op_done : callback used when operation is done
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_cistruct caam_rsa_req_ctx {
11062306a36Sopenharmony_ci	struct scatterlist src[2];
11162306a36Sopenharmony_ci	struct scatterlist *fixup_src;
11262306a36Sopenharmony_ci	unsigned int fixup_src_len;
11362306a36Sopenharmony_ci	struct rsa_edesc *edesc;
11462306a36Sopenharmony_ci	void (*akcipher_op_done)(struct device *jrdev, u32 *desc, u32 err,
11562306a36Sopenharmony_ci				 void *context);
11662306a36Sopenharmony_ci};
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/**
11962306a36Sopenharmony_ci * rsa_edesc - s/w-extended rsa descriptor
12062306a36Sopenharmony_ci * @src_nents     : number of segments in input s/w scatterlist
12162306a36Sopenharmony_ci * @dst_nents     : number of segments in output s/w scatterlist
12262306a36Sopenharmony_ci * @mapped_src_nents: number of segments in input h/w link table
12362306a36Sopenharmony_ci * @mapped_dst_nents: number of segments in output h/w link table
12462306a36Sopenharmony_ci * @sec4_sg_bytes : length of h/w link table
12562306a36Sopenharmony_ci * @bklog         : stored to determine if the request needs backlog
12662306a36Sopenharmony_ci * @sec4_sg_dma   : dma address of h/w link table
12762306a36Sopenharmony_ci * @sec4_sg       : pointer to h/w link table
12862306a36Sopenharmony_ci * @pdb           : specific RSA Protocol Data Block (PDB)
12962306a36Sopenharmony_ci * @hw_desc       : descriptor followed by link tables if any
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_cistruct rsa_edesc {
13262306a36Sopenharmony_ci	int src_nents;
13362306a36Sopenharmony_ci	int dst_nents;
13462306a36Sopenharmony_ci	int mapped_src_nents;
13562306a36Sopenharmony_ci	int mapped_dst_nents;
13662306a36Sopenharmony_ci	int sec4_sg_bytes;
13762306a36Sopenharmony_ci	bool bklog;
13862306a36Sopenharmony_ci	dma_addr_t sec4_sg_dma;
13962306a36Sopenharmony_ci	struct sec4_sg_entry *sec4_sg;
14062306a36Sopenharmony_ci	union {
14162306a36Sopenharmony_ci		struct rsa_pub_pdb pub;
14262306a36Sopenharmony_ci		struct rsa_priv_f1_pdb priv_f1;
14362306a36Sopenharmony_ci		struct rsa_priv_f2_pdb priv_f2;
14462306a36Sopenharmony_ci		struct rsa_priv_f3_pdb priv_f3;
14562306a36Sopenharmony_ci	} pdb;
14662306a36Sopenharmony_ci	u32 hw_desc[];
14762306a36Sopenharmony_ci};
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci/* Descriptor construction primitives. */
15062306a36Sopenharmony_civoid init_rsa_pub_desc(u32 *desc, struct rsa_pub_pdb *pdb);
15162306a36Sopenharmony_civoid init_rsa_priv_f1_desc(u32 *desc, struct rsa_priv_f1_pdb *pdb);
15262306a36Sopenharmony_civoid init_rsa_priv_f2_desc(u32 *desc, struct rsa_priv_f2_pdb *pdb);
15362306a36Sopenharmony_civoid init_rsa_priv_f3_desc(u32 *desc, struct rsa_priv_f3_pdb *pdb);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci#endif
156