cipher.h 14 KB

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