cipher.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2016 Broadcom
  4. */
  5. #ifndef _CIPHER_H
  6. #define _CIPHER_H
  7. #include <linux/atomic.h>
  8. #include <linux/mailbox/brcm-message.h>
  9. #include <linux/mailbox_client.h>
  10. #include <crypto/aes.h>
  11. #include <crypto/internal/hash.h>
  12. #include <crypto/internal/skcipher.h>
  13. #include <crypto/aead.h>
  14. #include <crypto/arc4.h>
  15. #include <crypto/gcm.h>
  16. #include <crypto/sha1.h>
  17. #include <crypto/sha2.h>
  18. #include <crypto/sha3.h>
  19. #include "spu.h"
  20. #include "spum.h"
  21. #include "spu2.h"
  22. /* Driver supports up to MAX_SPUS SPU blocks */
  23. #define MAX_SPUS 16
  24. #define ARC4_STATE_SIZE 4
  25. #define CCM_AES_IV_SIZE 16
  26. #define CCM_ESP_IV_SIZE 8
  27. #define RFC4543_ICV_SIZE 16
  28. #define MAX_KEY_SIZE ARC4_MAX_KEY_SIZE
  29. #define MAX_IV_SIZE AES_BLOCK_SIZE
  30. #define MAX_DIGEST_SIZE SHA3_512_DIGEST_SIZE
  31. #define MAX_ASSOC_SIZE 512
  32. /* size of salt value for AES-GCM-ESP and AES-CCM-ESP */
  33. #define GCM_ESP_SALT_SIZE 4
  34. #define CCM_ESP_SALT_SIZE 3
  35. #define MAX_SALT_SIZE GCM_ESP_SALT_SIZE
  36. #define GCM_ESP_SALT_OFFSET 0
  37. #define CCM_ESP_SALT_OFFSET 1
  38. #define GCM_ESP_DIGESTSIZE 16
  39. #define MAX_HASH_BLOCK_SIZE SHA512_BLOCK_SIZE
  40. /*
  41. * Maximum number of bytes from a non-final hash request that can be deferred
  42. * until more data is available. With new crypto API framework, this
  43. * can be no more than one block of data.
  44. */
  45. #define HASH_CARRY_MAX MAX_HASH_BLOCK_SIZE
  46. /* Force at least 4-byte alignment of all SPU message fields */
  47. #define SPU_MSG_ALIGN 4
  48. /* Number of times to resend mailbox message if mb queue is full */
  49. #define SPU_MB_RETRY_MAX 1000
  50. /* op_counts[] indexes */
  51. enum op_type {
  52. SPU_OP_CIPHER,
  53. SPU_OP_HASH,
  54. SPU_OP_HMAC,
  55. SPU_OP_AEAD,
  56. SPU_OP_NUM
  57. };
  58. enum spu_spu_type {
  59. SPU_TYPE_SPUM,
  60. SPU_TYPE_SPU2,
  61. };
  62. /*
  63. * SPUM_NS2 and SPUM_NSP are the SPU-M block on Northstar 2 and Northstar Plus,
  64. * respectively.
  65. */
  66. enum spu_spu_subtype {
  67. SPU_SUBTYPE_SPUM_NS2,
  68. SPU_SUBTYPE_SPUM_NSP,
  69. SPU_SUBTYPE_SPU2_V1,
  70. SPU_SUBTYPE_SPU2_V2
  71. };
  72. struct spu_type_subtype {
  73. enum spu_spu_type type;
  74. enum spu_spu_subtype subtype;
  75. };
  76. struct cipher_op {
  77. enum spu_cipher_alg alg;
  78. enum spu_cipher_mode mode;
  79. };
  80. struct auth_op {
  81. enum hash_alg alg;
  82. enum hash_mode mode;
  83. };
  84. struct iproc_alg_s {
  85. u32 type;
  86. union {
  87. struct skcipher_alg skcipher;
  88. struct ahash_alg hash;
  89. struct aead_alg aead;
  90. } alg;
  91. struct cipher_op cipher_info;
  92. struct auth_op auth_info;
  93. bool auth_first;
  94. bool registered;
  95. };
  96. /*
  97. * Buffers for a SPU request/reply message pair. All part of one structure to
  98. * allow a single alloc per request.
  99. */
  100. struct spu_msg_buf {
  101. /* Request message fragments */
  102. /*
  103. * SPU request message header. For SPU-M, holds MH, EMH, SCTX, BDESC,
  104. * and BD header. For SPU2, holds FMD, OMD.
  105. */
  106. u8 bcm_spu_req_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];
  107. /* IV or counter. Size to include salt. Also used for XTS tweek. */
  108. u8 iv_ctr[ALIGN(2 * AES_BLOCK_SIZE, SPU_MSG_ALIGN)];
  109. /* Hash digest. request and response. */
  110. u8 digest[ALIGN(MAX_DIGEST_SIZE, SPU_MSG_ALIGN)];
  111. /* SPU request message padding */
  112. u8 spu_req_pad[ALIGN(SPU_PAD_LEN_MAX, SPU_MSG_ALIGN)];
  113. /* SPU-M request message STATUS field */
  114. u8 tx_stat[ALIGN(SPU_TX_STATUS_LEN, SPU_MSG_ALIGN)];
  115. /* Response message fragments */
  116. /* SPU response message header */
  117. u8 spu_resp_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];
  118. /* SPU response message STATUS field padding */
  119. u8 rx_stat_pad[ALIGN(SPU_STAT_PAD_MAX, SPU_MSG_ALIGN)];
  120. /* SPU response message STATUS field */
  121. u8 rx_stat[ALIGN(SPU_RX_STATUS_LEN, SPU_MSG_ALIGN)];
  122. union {
  123. /* Buffers only used for skcipher */
  124. struct {
  125. /*
  126. * Field used for either SUPDT when RC4 is used
  127. * -OR- tweak value when XTS/AES is used
  128. */
  129. u8 supdt_tweak[ALIGN(SPU_SUPDT_LEN, SPU_MSG_ALIGN)];
  130. } c;
  131. /* Buffers only used for aead */
  132. struct {
  133. /* SPU response pad for GCM data */
  134. u8 gcmpad[ALIGN(AES_BLOCK_SIZE, SPU_MSG_ALIGN)];
  135. /* SPU request msg padding for GCM AAD */
  136. u8 req_aad_pad[ALIGN(SPU_PAD_LEN_MAX, SPU_MSG_ALIGN)];
  137. /* SPU response data to be discarded */
  138. u8 resp_aad[ALIGN(MAX_ASSOC_SIZE + MAX_IV_SIZE,
  139. SPU_MSG_ALIGN)];
  140. } a;
  141. };
  142. };
  143. struct iproc_ctx_s {
  144. u8 enckey[MAX_KEY_SIZE + ARC4_STATE_SIZE];
  145. unsigned int enckeylen;
  146. u8 authkey[MAX_KEY_SIZE + ARC4_STATE_SIZE];
  147. unsigned int authkeylen;
  148. u8 salt[MAX_SALT_SIZE];
  149. unsigned int salt_len;
  150. unsigned int salt_offset;
  151. u8 iv[MAX_IV_SIZE];
  152. unsigned int digestsize;
  153. struct iproc_alg_s *alg;
  154. bool is_esp;
  155. struct cipher_op cipher;
  156. enum spu_cipher_type cipher_type;
  157. struct auth_op auth;
  158. bool auth_first;
  159. /*
  160. * The maximum length in bytes of the payload in a SPU message for this
  161. * context. For SPU-M, the payload is the combination of AAD and data.
  162. * For SPU2, the payload is just data. A value of SPU_MAX_PAYLOAD_INF
  163. * indicates that there is no limit to the length of the SPU message
  164. * payload.
  165. */
  166. unsigned int max_payload;
  167. struct crypto_aead *fallback_cipher;
  168. /* auth_type is determined during processing of request */
  169. u8 ipad[MAX_HASH_BLOCK_SIZE];
  170. u8 opad[MAX_HASH_BLOCK_SIZE];
  171. /*
  172. * Buffer to hold SPU message header template. Template is created at
  173. * setkey time for skcipher requests, since most of the fields in the
  174. * header are known at that time. At request time, just fill in a few
  175. * missing pieces related to length of data in the request and IVs, etc.
  176. */
  177. u8 bcm_spu_req_hdr[ALIGN(SPU2_HEADER_ALLOC_LEN, SPU_MSG_ALIGN)];
  178. /* Length of SPU request header */
  179. u16 spu_req_hdr_len;
  180. /* Expected length of SPU response header */
  181. u16 spu_resp_hdr_len;
  182. /*
  183. * shash descriptor - needed to perform incremental hashing in
  184. * software, when hw doesn't support it.
  185. */
  186. struct shash_desc *shash;
  187. bool is_rfc4543; /* RFC 4543 style of GMAC */
  188. };
  189. /* state from iproc_reqctx_s necessary for hash state export/import */
  190. struct spu_hash_export_s {
  191. unsigned int total_todo;
  192. unsigned int total_sent;
  193. u8 hash_carry[HASH_CARRY_MAX];
  194. unsigned int hash_carry_len;
  195. u8 incr_hash[MAX_DIGEST_SIZE];
  196. bool is_sw_hmac;
  197. };
  198. struct iproc_reqctx_s {
  199. /* general context */
  200. struct crypto_async_request *parent;
  201. /* only valid after enqueue() */
  202. struct iproc_ctx_s *ctx;
  203. u8 chan_idx; /* Mailbox channel to be used to submit this request */
  204. /* total todo, rx'd, and sent for this request */
  205. unsigned int total_todo;
  206. unsigned int total_received; /* only valid for skcipher */
  207. unsigned int total_sent;
  208. /*
  209. * num bytes sent to hw from the src sg in this request. This can differ
  210. * from total_sent for incremental hashing. total_sent includes previous
  211. * init() and update() data. src_sent does not.
  212. */
  213. unsigned int src_sent;
  214. /*
  215. * For AEAD requests, start of associated data. This will typically
  216. * point to the beginning of the src scatterlist from the request,
  217. * since assoc data is at the beginning of the src scatterlist rather
  218. * than in its own sg.
  219. */
  220. struct scatterlist *assoc;
  221. /*
  222. * scatterlist entry and offset to start of data for next chunk. Crypto
  223. * API src scatterlist for AEAD starts with AAD, if present. For first
  224. * chunk, src_sg is sg entry at beginning of input data (after AAD).
  225. * src_skip begins at the offset in that sg entry where data begins.
  226. */
  227. struct scatterlist *src_sg;
  228. int src_nents; /* Number of src entries with data */
  229. u32 src_skip; /* bytes of current sg entry already used */
  230. /*
  231. * Same for destination. For AEAD, if there is AAD, output data must
  232. * be written at offset following AAD.
  233. */
  234. struct scatterlist *dst_sg;
  235. int dst_nents; /* Number of dst entries with data */
  236. u32 dst_skip; /* bytes of current sg entry already written */
  237. /* Mailbox message used to send this request to PDC driver */
  238. struct brcm_message mb_mssg;
  239. bool bd_suppress; /* suppress BD field in SPU response? */
  240. /* cipher context */
  241. bool is_encrypt;
  242. /*
  243. * CBC mode: IV. CTR mode: counter. Else empty. Used as a DMA
  244. * buffer for AEAD requests. So allocate as DMAable memory. If IV
  245. * concatenated with salt, includes the salt.
  246. */
  247. u8 *iv_ctr;
  248. /* Length of IV or counter, in bytes */
  249. unsigned int iv_ctr_len;
  250. /*
  251. * Hash requests can be of any size, whether initial, update, or final.
  252. * A non-final request must be submitted to the SPU as an integral
  253. * number of blocks. This may leave data at the end of the request
  254. * that is not a full block. Since the request is non-final, it cannot
  255. * be padded. So, we write the remainder to this hash_carry buffer and
  256. * hold it until the next request arrives. The carry data is then
  257. * submitted at the beginning of the data in the next SPU msg.
  258. * hash_carry_len is the number of bytes currently in hash_carry. These
  259. * fields are only used for ahash requests.
  260. */
  261. u8 hash_carry[HASH_CARRY_MAX];
  262. unsigned int hash_carry_len;
  263. unsigned int is_final; /* is this the final for the hash op? */
  264. /*
  265. * Digest from incremental hash is saved here to include in next hash
  266. * operation. Cannot be stored in req->result for truncated hashes,
  267. * since result may be sized for final digest. Cannot be saved in
  268. * msg_buf because that gets deleted between incremental hash ops
  269. * and is not saved as part of export().
  270. */
  271. u8 incr_hash[MAX_DIGEST_SIZE];
  272. /* hmac context */
  273. bool is_sw_hmac;
  274. /* aead context */
  275. struct crypto_tfm *old_tfm;
  276. crypto_completion_t old_complete;
  277. void *old_data;
  278. gfp_t gfp;
  279. /* Buffers used to build SPU request and response messages */
  280. struct spu_msg_buf msg_buf;
  281. };
  282. /*
  283. * Structure encapsulates a set of function pointers specific to the type of
  284. * SPU hardware running. These functions handling creation and parsing of
  285. * SPU request messages and SPU response messages. Includes hardware-specific
  286. * values read from device tree.
  287. */
  288. struct spu_hw {
  289. void (*spu_dump_msg_hdr)(u8 *buf, unsigned int buf_len);
  290. u32 (*spu_ctx_max_payload)(enum spu_cipher_alg cipher_alg,
  291. enum spu_cipher_mode cipher_mode,
  292. unsigned int blocksize);
  293. u32 (*spu_payload_length)(u8 *spu_hdr);
  294. u16 (*spu_response_hdr_len)(u16 auth_key_len, u16 enc_key_len,
  295. bool is_hash);
  296. u16 (*spu_hash_pad_len)(enum hash_alg hash_alg,
  297. enum hash_mode hash_mode, u32 chunksize,
  298. u16 hash_block_size);
  299. u32 (*spu_gcm_ccm_pad_len)(enum spu_cipher_mode cipher_mode,
  300. unsigned int data_size);
  301. u32 (*spu_assoc_resp_len)(enum spu_cipher_mode cipher_mode,
  302. unsigned int assoc_len,
  303. unsigned int iv_len, bool is_encrypt);
  304. u8 (*spu_aead_ivlen)(enum spu_cipher_mode cipher_mode,
  305. u16 iv_len);
  306. enum hash_type (*spu_hash_type)(u32 src_sent);
  307. u32 (*spu_digest_size)(u32 digest_size, enum hash_alg alg,
  308. enum hash_type);
  309. u32 (*spu_create_request)(u8 *spu_hdr,
  310. struct spu_request_opts *req_opts,
  311. struct spu_cipher_parms *cipher_parms,
  312. struct spu_hash_parms *hash_parms,
  313. struct spu_aead_parms *aead_parms,
  314. unsigned int data_size);
  315. u16 (*spu_cipher_req_init)(u8 *spu_hdr,
  316. struct spu_cipher_parms *cipher_parms);
  317. void (*spu_cipher_req_finish)(u8 *spu_hdr,
  318. u16 spu_req_hdr_len,
  319. unsigned int is_inbound,
  320. struct spu_cipher_parms *cipher_parms,
  321. unsigned int data_size);
  322. void (*spu_request_pad)(u8 *pad_start, u32 gcm_padding,
  323. u32 hash_pad_len, enum hash_alg auth_alg,
  324. enum hash_mode auth_mode,
  325. unsigned int total_sent, u32 status_padding);
  326. u8 (*spu_xts_tweak_in_payload)(void);
  327. u8 (*spu_tx_status_len)(void);
  328. u8 (*spu_rx_status_len)(void);
  329. int (*spu_status_process)(u8 *statp);
  330. void (*spu_ccm_update_iv)(unsigned int digestsize,
  331. struct spu_cipher_parms *cipher_parms,
  332. unsigned int assoclen, unsigned int chunksize,
  333. bool is_encrypt, bool is_esp);
  334. u32 (*spu_wordalign_padlen)(u32 data_size);
  335. /* The base virtual address of the SPU hw registers */
  336. void __iomem *reg_vbase[MAX_SPUS];
  337. /* Version of the SPU hardware */
  338. enum spu_spu_type spu_type;
  339. /* Sub-version of the SPU hardware */
  340. enum spu_spu_subtype spu_subtype;
  341. /* The number of SPUs on this platform */
  342. u32 num_spu;
  343. /* The number of SPU channels on this platform */
  344. u32 num_chan;
  345. };
  346. struct bcm_device_private {
  347. struct platform_device *pdev;
  348. struct spu_hw spu;
  349. atomic_t session_count; /* number of streams active */
  350. atomic_t stream_count; /* monotonic counter for streamID's */
  351. /* Length of BCM header. Set to 0 when hw does not expect BCM HEADER. */
  352. u8 bcm_hdr_len;
  353. /* The index of the channel to use for the next crypto request */
  354. atomic_t next_chan;
  355. struct dentry *debugfs_dir;
  356. struct dentry *debugfs_stats;
  357. /* Number of request bytes processed and result bytes returned */
  358. atomic64_t bytes_in;
  359. atomic64_t bytes_out;
  360. /* Number of operations of each type */
  361. atomic_t op_counts[SPU_OP_NUM];
  362. atomic_t cipher_cnt[CIPHER_ALG_LAST][CIPHER_MODE_LAST];
  363. atomic_t hash_cnt[HASH_ALG_LAST];
  364. atomic_t hmac_cnt[HASH_ALG_LAST];
  365. atomic_t aead_cnt[AEAD_TYPE_LAST];
  366. /* Number of calls to setkey() for each operation type */
  367. atomic_t setkey_cnt[SPU_OP_NUM];
  368. /* Number of times request was resubmitted because mb was full */
  369. atomic_t mb_no_spc;
  370. /* Number of mailbox send failures */
  371. atomic_t mb_send_fail;
  372. /* Number of ICV check failures for AEAD messages */
  373. atomic_t bad_icv;
  374. struct mbox_client mcl;
  375. /* Array of mailbox channel pointers, one for each channel */
  376. struct mbox_chan **mbox;
  377. };
  378. extern struct bcm_device_private iproc_priv;
  379. #endif