cipher.h 14 KB

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