Path: blob/master/drivers/crypto/allwinner/sun4i-ss/sun4i-ss.h
26292 views
/* SPDX-License-Identifier: GPL-2.0-only */1/*2* sun4i-ss.h - hardware cryptographic accelerator for Allwinner A20 SoC3*4* Copyright (C) 2013-2015 Corentin LABBE <[email protected]>5*6* Support AES cipher with 128,192,256 bits keysize.7* Support MD5 and SHA1 hash algorithms.8* Support DES and 3DES9*10* You could find the datasheet in Documentation/arch/arm/sunxi.rst11*/1213#include <linux/clk.h>14#include <linux/crypto.h>15#include <linux/io.h>16#include <linux/module.h>17#include <linux/of.h>18#include <linux/platform_device.h>19#include <linux/reset.h>20#include <crypto/scatterwalk.h>21#include <linux/scatterlist.h>22#include <linux/interrupt.h>23#include <linux/delay.h>24#include <linux/pm_runtime.h>25#include <crypto/md5.h>26#include <crypto/skcipher.h>27#include <crypto/sha1.h>28#include <crypto/hash.h>29#include <crypto/internal/hash.h>30#include <crypto/internal/skcipher.h>31#include <crypto/aes.h>32#include <crypto/internal/des.h>33#include <crypto/internal/rng.h>34#include <crypto/rng.h>3536#define SS_CTL 0x0037#define SS_KEY0 0x0438#define SS_KEY1 0x0839#define SS_KEY2 0x0C40#define SS_KEY3 0x1041#define SS_KEY4 0x1442#define SS_KEY5 0x1843#define SS_KEY6 0x1C44#define SS_KEY7 0x204546#define SS_IV0 0x2447#define SS_IV1 0x2848#define SS_IV2 0x2C49#define SS_IV3 0x305051#define SS_FCSR 0x445253#define SS_MD0 0x4C54#define SS_MD1 0x5055#define SS_MD2 0x5456#define SS_MD3 0x5857#define SS_MD4 0x5C5859#define SS_RXFIFO 0x20060#define SS_TXFIFO 0x2046162/* SS_CTL configuration values */6364/* PRNG generator mode - bit 15 */65#define SS_PRNG_ONESHOT (0 << 15)66#define SS_PRNG_CONTINUE (1 << 15)6768/* IV mode for hash */69#define SS_IV_ARBITRARY (1 << 14)7071/* SS operation mode - bits 12-13 */72#define SS_ECB (0 << 12)73#define SS_CBC (1 << 12)74#define SS_CTS (3 << 12)7576/* Counter width for CNT mode - bits 10-11 */77#define SS_CNT_16BITS (0 << 10)78#define SS_CNT_32BITS (1 << 10)79#define SS_CNT_64BITS (2 << 10)8081/* Key size for AES - bits 8-9 */82#define SS_AES_128BITS (0 << 8)83#define SS_AES_192BITS (1 << 8)84#define SS_AES_256BITS (2 << 8)8586/* Operation direction - bit 7 */87#define SS_ENCRYPTION (0 << 7)88#define SS_DECRYPTION (1 << 7)8990/* SS Method - bits 4-6 */91#define SS_OP_AES (0 << 4)92#define SS_OP_DES (1 << 4)93#define SS_OP_3DES (2 << 4)94#define SS_OP_SHA1 (3 << 4)95#define SS_OP_MD5 (4 << 4)96#define SS_OP_PRNG (5 << 4)9798/* Data end bit - bit 2 */99#define SS_DATA_END (1 << 2)100101/* PRNG start bit - bit 1 */102#define SS_PRNG_START (1 << 1)103104/* SS Enable bit - bit 0 */105#define SS_DISABLED (0 << 0)106#define SS_ENABLED (1 << 0)107108/* SS_FCSR configuration values */109/* RX FIFO status - bit 30 */110#define SS_RXFIFO_FREE (1 << 30)111112/* RX FIFO empty spaces - bits 24-29 */113#define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f)114115/* TX FIFO status - bit 22 */116#define SS_TXFIFO_AVAILABLE (1 << 22)117118/* TX FIFO available spaces - bits 16-21 */119#define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f)120121#define SS_RX_MAX 32122#define SS_RX_DEFAULT SS_RX_MAX123#define SS_TX_MAX 33124125#define SS_RXFIFO_EMP_INT_PENDING (1 << 10)126#define SS_TXFIFO_AVA_INT_PENDING (1 << 8)127#define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)128#define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)129130#define SS_SEED_LEN 192131#define SS_DATA_LEN 160132133/*134* struct ss_variant - Describe SS hardware variant135* @sha1_in_be: The SHA1 digest is given by SS in BE, and so need to be inverted.136*/137struct ss_variant {138bool sha1_in_be;139};140141struct sun4i_ss_ctx {142const struct ss_variant *variant;143void __iomem *base;144int irq;145struct clk *busclk;146struct clk *ssclk;147struct reset_control *reset;148struct device *dev;149struct resource *res;150char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */151char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */152spinlock_t slock; /* control the use of the device */153#ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG154u32 seed[SS_SEED_LEN / BITS_PER_LONG];155#endif156struct dentry *dbgfs_dir;157struct dentry *dbgfs_stats;158};159160struct sun4i_ss_alg_template {161u32 type;162u32 mode;163union {164struct skcipher_alg crypto;165struct ahash_alg hash;166struct rng_alg rng;167} alg;168struct sun4i_ss_ctx *ss;169unsigned long stat_req;170unsigned long stat_fb;171unsigned long stat_bytes;172unsigned long stat_opti;173};174175struct sun4i_tfm_ctx {176u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */177u32 keylen;178u32 keymode;179struct sun4i_ss_ctx *ss;180struct crypto_skcipher *fallback_tfm;181};182183struct sun4i_cipher_req_ctx {184u32 mode;185u8 backup_iv[AES_BLOCK_SIZE];186struct skcipher_request fallback_req; // keep at the end187};188189struct sun4i_req_ctx {190u32 mode;191u64 byte_count; /* number of bytes "uploaded" to the device */192u32 hash[5]; /* for storing SS_IVx register */193char buf[64];194unsigned int len;195int flags;196};197198int sun4i_hash_crainit(struct crypto_tfm *tfm);199void sun4i_hash_craexit(struct crypto_tfm *tfm);200int sun4i_hash_init(struct ahash_request *areq);201int sun4i_hash_update(struct ahash_request *areq);202int sun4i_hash_final(struct ahash_request *areq);203int sun4i_hash_finup(struct ahash_request *areq);204int sun4i_hash_digest(struct ahash_request *areq);205int sun4i_hash_export_md5(struct ahash_request *areq, void *out);206int sun4i_hash_import_md5(struct ahash_request *areq, const void *in);207int sun4i_hash_export_sha1(struct ahash_request *areq, void *out);208int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in);209210int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq);211int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq);212int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq);213int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq);214215int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq);216int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq);217int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq);218int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq);219220int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq);221int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq);222int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq);223int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq);224225int sun4i_ss_cipher_init(struct crypto_tfm *tfm);226void sun4i_ss_cipher_exit(struct crypto_tfm *tfm);227int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,228unsigned int keylen);229int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,230unsigned int keylen);231int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,232unsigned int keylen);233int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,234unsigned int slen, u8 *dst, unsigned int dlen);235int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);236237238