spu2.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /*
  17. * This file contains SPU message definitions specific to SPU2.
  18. */
  19. #ifndef _SPU2_H
  20. #define _SPU2_H
  21. enum spu2_cipher_type {
  22. SPU2_CIPHER_TYPE_NONE = 0x0,
  23. SPU2_CIPHER_TYPE_AES128 = 0x1,
  24. SPU2_CIPHER_TYPE_AES192 = 0x2,
  25. SPU2_CIPHER_TYPE_AES256 = 0x3,
  26. SPU2_CIPHER_TYPE_DES = 0x4,
  27. SPU2_CIPHER_TYPE_3DES = 0x5,
  28. SPU2_CIPHER_TYPE_LAST
  29. };
  30. enum spu2_cipher_mode {
  31. SPU2_CIPHER_MODE_ECB = 0x0,
  32. SPU2_CIPHER_MODE_CBC = 0x1,
  33. SPU2_CIPHER_MODE_CTR = 0x2,
  34. SPU2_CIPHER_MODE_CFB = 0x3,
  35. SPU2_CIPHER_MODE_OFB = 0x4,
  36. SPU2_CIPHER_MODE_XTS = 0x5,
  37. SPU2_CIPHER_MODE_CCM = 0x6,
  38. SPU2_CIPHER_MODE_GCM = 0x7,
  39. SPU2_CIPHER_MODE_LAST
  40. };
  41. enum spu2_hash_type {
  42. SPU2_HASH_TYPE_NONE = 0x0,
  43. SPU2_HASH_TYPE_AES128 = 0x1,
  44. SPU2_HASH_TYPE_AES192 = 0x2,
  45. SPU2_HASH_TYPE_AES256 = 0x3,
  46. SPU2_HASH_TYPE_MD5 = 0x6,
  47. SPU2_HASH_TYPE_SHA1 = 0x7,
  48. SPU2_HASH_TYPE_SHA224 = 0x8,
  49. SPU2_HASH_TYPE_SHA256 = 0x9,
  50. SPU2_HASH_TYPE_SHA384 = 0xa,
  51. SPU2_HASH_TYPE_SHA512 = 0xb,
  52. SPU2_HASH_TYPE_SHA512_224 = 0xc,
  53. SPU2_HASH_TYPE_SHA512_256 = 0xd,
  54. SPU2_HASH_TYPE_SHA3_224 = 0xe,
  55. SPU2_HASH_TYPE_SHA3_256 = 0xf,
  56. SPU2_HASH_TYPE_SHA3_384 = 0x10,
  57. SPU2_HASH_TYPE_SHA3_512 = 0x11,
  58. SPU2_HASH_TYPE_LAST
  59. };
  60. enum spu2_hash_mode {
  61. SPU2_HASH_MODE_CMAC = 0x0,
  62. SPU2_HASH_MODE_CBC_MAC = 0x1,
  63. SPU2_HASH_MODE_XCBC_MAC = 0x2,
  64. SPU2_HASH_MODE_HMAC = 0x3,
  65. SPU2_HASH_MODE_RABIN = 0x4,
  66. SPU2_HASH_MODE_CCM = 0x5,
  67. SPU2_HASH_MODE_GCM = 0x6,
  68. SPU2_HASH_MODE_RESERVED = 0x7,
  69. SPU2_HASH_MODE_LAST
  70. };
  71. enum spu2_ret_md_opts {
  72. SPU2_RET_NO_MD = 0, /* return no metadata */
  73. SPU2_RET_FMD_OMD = 1, /* return both FMD and OMD */
  74. SPU2_RET_FMD_ONLY = 2, /* return only FMD */
  75. SPU2_RET_FMD_OMD_IV = 3, /* return FMD and OMD with just IVs */
  76. };
  77. /* Fixed Metadata format */
  78. struct SPU2_FMD {
  79. u64 ctrl0;
  80. u64 ctrl1;
  81. u64 ctrl2;
  82. u64 ctrl3;
  83. };
  84. #define FMD_SIZE sizeof(struct SPU2_FMD)
  85. /* Fixed part of request message header length in bytes. Just FMD. */
  86. #define SPU2_REQ_FIXED_LEN FMD_SIZE
  87. #define SPU2_HEADER_ALLOC_LEN (SPU_REQ_FIXED_LEN + \
  88. 2 * MAX_KEY_SIZE + 2 * MAX_IV_SIZE)
  89. /* FMD ctrl0 field masks */
  90. #define SPU2_CIPH_ENCRYPT_EN 0x1 /* 0: decrypt, 1: encrypt */
  91. #define SPU2_CIPH_TYPE 0xF0 /* one of spu2_cipher_type */
  92. #define SPU2_CIPH_TYPE_SHIFT 4
  93. #define SPU2_CIPH_MODE 0xF00 /* one of spu2_cipher_mode */
  94. #define SPU2_CIPH_MODE_SHIFT 8
  95. #define SPU2_CFB_MASK 0x7000 /* cipher feedback mask */
  96. #define SPU2_CFB_MASK_SHIFT 12
  97. #define SPU2_PROTO_SEL 0xF00000 /* MACsec, IPsec, TLS... */
  98. #define SPU2_PROTO_SEL_SHIFT 20
  99. #define SPU2_HASH_FIRST 0x1000000 /* 1: hash input is input pkt
  100. * data
  101. */
  102. #define SPU2_CHK_TAG 0x2000000 /* 1: check digest provided */
  103. #define SPU2_HASH_TYPE 0x1F0000000 /* one of spu2_hash_type */
  104. #define SPU2_HASH_TYPE_SHIFT 28
  105. #define SPU2_HASH_MODE 0xF000000000 /* one of spu2_hash_mode */
  106. #define SPU2_HASH_MODE_SHIFT 36
  107. #define SPU2_CIPH_PAD_EN 0x100000000000 /* 1: Add pad to end of payload for
  108. * enc
  109. */
  110. #define SPU2_CIPH_PAD 0xFF000000000000 /* cipher pad value */
  111. #define SPU2_CIPH_PAD_SHIFT 48
  112. /* FMD ctrl1 field masks */
  113. #define SPU2_TAG_LOC 0x1 /* 1: end of payload, 0: undef */
  114. #define SPU2_HAS_FR_DATA 0x2 /* 1: msg has frame data */
  115. #define SPU2_HAS_AAD1 0x4 /* 1: msg has AAD1 field */
  116. #define SPU2_HAS_NAAD 0x8 /* 1: msg has NAAD field */
  117. #define SPU2_HAS_AAD2 0x10 /* 1: msg has AAD2 field */
  118. #define SPU2_HAS_ESN 0x20 /* 1: msg has ESN field */
  119. #define SPU2_HASH_KEY_LEN 0xFF00 /* len of hash key in bytes.
  120. * HMAC only.
  121. */
  122. #define SPU2_HASH_KEY_LEN_SHIFT 8
  123. #define SPU2_CIPH_KEY_LEN 0xFF00000 /* len of cipher key in bytes */
  124. #define SPU2_CIPH_KEY_LEN_SHIFT 20
  125. #define SPU2_GENIV 0x10000000 /* 1: hw generates IV */
  126. #define SPU2_HASH_IV 0x20000000 /* 1: IV incl in hash */
  127. #define SPU2_RET_IV 0x40000000 /* 1: return IV in output msg
  128. * b4 payload
  129. */
  130. #define SPU2_RET_IV_LEN 0xF00000000 /* length in bytes of IV returned.
  131. * 0 = 16 bytes
  132. */
  133. #define SPU2_RET_IV_LEN_SHIFT 32
  134. #define SPU2_IV_OFFSET 0xF000000000 /* gen IV offset */
  135. #define SPU2_IV_OFFSET_SHIFT 36
  136. #define SPU2_IV_LEN 0x1F0000000000 /* length of input IV in bytes */
  137. #define SPU2_IV_LEN_SHIFT 40
  138. #define SPU2_HASH_TAG_LEN 0x7F000000000000 /* hash tag length in bytes */
  139. #define SPU2_HASH_TAG_LEN_SHIFT 48
  140. #define SPU2_RETURN_MD 0x300000000000000 /* return metadata */
  141. #define SPU2_RETURN_MD_SHIFT 56
  142. #define SPU2_RETURN_FD 0x400000000000000
  143. #define SPU2_RETURN_AAD1 0x800000000000000
  144. #define SPU2_RETURN_NAAD 0x1000000000000000
  145. #define SPU2_RETURN_AAD2 0x2000000000000000
  146. #define SPU2_RETURN_PAY 0x4000000000000000 /* return payload */
  147. /* FMD ctrl2 field masks */
  148. #define SPU2_AAD1_OFFSET 0xFFF /* byte offset of AAD1 field */
  149. #define SPU2_AAD1_LEN 0xFF000 /* length of AAD1 in bytes */
  150. #define SPU2_AAD1_LEN_SHIFT 12
  151. #define SPU2_AAD2_OFFSET 0xFFF00000 /* byte offset of AAD2 field */
  152. #define SPU2_AAD2_OFFSET_SHIFT 20
  153. #define SPU2_PL_OFFSET 0xFFFFFFFF00000000 /* payload offset from AAD2 */
  154. #define SPU2_PL_OFFSET_SHIFT 32
  155. /* FMD ctrl3 field masks */
  156. #define SPU2_PL_LEN 0xFFFFFFFF /* payload length in bytes */
  157. #define SPU2_TLS_LEN 0xFFFF00000000 /* TLS encrypt: cipher len
  158. * TLS decrypt: compressed len
  159. */
  160. #define SPU2_TLS_LEN_SHIFT 32
  161. /*
  162. * Max value that can be represented in the Payload Length field of the
  163. * ctrl3 word of FMD.
  164. */
  165. #define SPU2_MAX_PAYLOAD SPU2_PL_LEN
  166. /* Error values returned in STATUS field of response messages */
  167. #define SPU2_INVALID_ICV 1
  168. void spu2_dump_msg_hdr(u8 *buf, unsigned int buf_len);
  169. u32 spu2_ctx_max_payload(enum spu_cipher_alg cipher_alg,
  170. enum spu_cipher_mode cipher_mode,
  171. unsigned int blocksize);
  172. u32 spu2_payload_length(u8 *spu_hdr);
  173. u16 spu2_response_hdr_len(u16 auth_key_len, u16 enc_key_len, bool is_hash);
  174. u16 spu2_hash_pad_len(enum hash_alg hash_alg, enum hash_mode hash_mode,
  175. u32 chunksize, u16 hash_block_size);
  176. u32 spu2_gcm_ccm_pad_len(enum spu_cipher_mode cipher_mode,
  177. unsigned int data_size);
  178. u32 spu2_assoc_resp_len(enum spu_cipher_mode cipher_mode,
  179. unsigned int assoc_len, unsigned int iv_len,
  180. bool is_encrypt);
  181. u8 spu2_aead_ivlen(enum spu_cipher_mode cipher_mode,
  182. u16 iv_len);
  183. enum hash_type spu2_hash_type(u32 src_sent);
  184. u32 spu2_digest_size(u32 alg_digest_size, enum hash_alg alg,
  185. enum hash_type htype);
  186. u32 spu2_create_request(u8 *spu_hdr,
  187. struct spu_request_opts *req_opts,
  188. struct spu_cipher_parms *cipher_parms,
  189. struct spu_hash_parms *hash_parms,
  190. struct spu_aead_parms *aead_parms,
  191. unsigned int data_size);
  192. u16 spu2_cipher_req_init(u8 *spu_hdr, struct spu_cipher_parms *cipher_parms);
  193. void spu2_cipher_req_finish(u8 *spu_hdr,
  194. u16 spu_req_hdr_len,
  195. unsigned int is_inbound,
  196. struct spu_cipher_parms *cipher_parms,
  197. bool update_key,
  198. unsigned int data_size);
  199. void spu2_request_pad(u8 *pad_start, u32 gcm_padding, u32 hash_pad_len,
  200. enum hash_alg auth_alg, enum hash_mode auth_mode,
  201. unsigned int total_sent, u32 status_padding);
  202. u8 spu2_xts_tweak_in_payload(void);
  203. u8 spu2_tx_status_len(void);
  204. u8 spu2_rx_status_len(void);
  205. int spu2_status_process(u8 *statp);
  206. void spu2_ccm_update_iv(unsigned int digestsize,
  207. struct spu_cipher_parms *cipher_parms,
  208. unsigned int assoclen, unsigned int chunksize,
  209. bool is_encrypt, bool is_esp);
  210. u32 spu2_wordalign_padlen(u32 data_size);
  211. #endif