crypto4xx_sa.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * AMCC SoC PPC4xx Crypto Driver
  3. *
  4. * Copyright (c) 2008 Applied Micro Circuits Corporation.
  5. * All rights reserved. James Hsiao <jhsiao@amcc.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * This file defines the security context
  18. * associate format.
  19. */
  20. #ifndef __CRYPTO4XX_SA_H__
  21. #define __CRYPTO4XX_SA_H__
  22. #define AES_IV_SIZE 16
  23. /**
  24. * Contents of Dynamic Security Association (SA) with all possible fields
  25. */
  26. union dynamic_sa_contents {
  27. struct {
  28. u32 arc4_state_ptr:1;
  29. u32 arc4_ij_ptr:1;
  30. u32 state_ptr:1;
  31. u32 iv3:1;
  32. u32 iv2:1;
  33. u32 iv1:1;
  34. u32 iv0:1;
  35. u32 seq_num_mask3:1;
  36. u32 seq_num_mask2:1;
  37. u32 seq_num_mask1:1;
  38. u32 seq_num_mask0:1;
  39. u32 seq_num1:1;
  40. u32 seq_num0:1;
  41. u32 spi:1;
  42. u32 outer_size:5;
  43. u32 inner_size:5;
  44. u32 key_size:4;
  45. u32 cmd_size:4;
  46. } bf;
  47. u32 w;
  48. } __attribute__((packed));
  49. #define DIR_OUTBOUND 0
  50. #define DIR_INBOUND 1
  51. #define SA_OP_GROUP_BASIC 0
  52. #define SA_OPCODE_ENCRYPT 0
  53. #define SA_OPCODE_DECRYPT 0
  54. #define SA_OPCODE_ENCRYPT_HASH 1
  55. #define SA_OPCODE_HASH_DECRYPT 1
  56. #define SA_OPCODE_HASH 3
  57. #define SA_CIPHER_ALG_DES 0
  58. #define SA_CIPHER_ALG_3DES 1
  59. #define SA_CIPHER_ALG_ARC4 2
  60. #define SA_CIPHER_ALG_AES 3
  61. #define SA_CIPHER_ALG_KASUMI 4
  62. #define SA_CIPHER_ALG_NULL 15
  63. #define SA_HASH_ALG_MD5 0
  64. #define SA_HASH_ALG_SHA1 1
  65. #define SA_HASH_ALG_GHASH 12
  66. #define SA_HASH_ALG_CBC_MAC 14
  67. #define SA_HASH_ALG_NULL 15
  68. #define SA_HASH_ALG_SHA1_DIGEST_SIZE 20
  69. #define SA_LOAD_HASH_FROM_SA 0
  70. #define SA_LOAD_HASH_FROM_STATE 2
  71. #define SA_NOT_LOAD_HASH 3
  72. #define SA_LOAD_IV_FROM_SA 0
  73. #define SA_LOAD_IV_FROM_INPUT 1
  74. #define SA_LOAD_IV_FROM_STATE 2
  75. #define SA_LOAD_IV_GEN_IV 3
  76. #define SA_PAD_TYPE_CONSTANT 2
  77. #define SA_PAD_TYPE_ZERO 3
  78. #define SA_PAD_TYPE_TLS 5
  79. #define SA_PAD_TYPE_DTLS 5
  80. #define SA_NOT_SAVE_HASH 0
  81. #define SA_SAVE_HASH 1
  82. #define SA_NOT_SAVE_IV 0
  83. #define SA_SAVE_IV 1
  84. #define SA_HEADER_PROC 1
  85. #define SA_NO_HEADER_PROC 0
  86. union sa_command_0 {
  87. struct {
  88. u32 scatter:1;
  89. u32 gather:1;
  90. u32 save_hash_state:1;
  91. u32 save_iv:1;
  92. u32 load_hash_state:2;
  93. u32 load_iv:2;
  94. u32 digest_len:4;
  95. u32 hdr_proc:1;
  96. u32 extend_pad:1;
  97. u32 stream_cipher_pad:1;
  98. u32 rsv:1;
  99. u32 hash_alg:4;
  100. u32 cipher_alg:4;
  101. u32 pad_type:2;
  102. u32 op_group:2;
  103. u32 dir:1;
  104. u32 opcode:3;
  105. } bf;
  106. u32 w;
  107. } __attribute__((packed));
  108. #define CRYPTO_MODE_ECB 0
  109. #define CRYPTO_MODE_CBC 1
  110. #define CRYPTO_MODE_OFB 2
  111. #define CRYPTO_MODE_CFB 3
  112. #define CRYPTO_MODE_CTR 4
  113. #define CRYPTO_FEEDBACK_MODE_NO_FB 0
  114. #define CRYPTO_FEEDBACK_MODE_64BIT_OFB 0
  115. #define CRYPTO_FEEDBACK_MODE_8BIT_CFB 1
  116. #define CRYPTO_FEEDBACK_MODE_1BIT_CFB 2
  117. #define CRYPTO_FEEDBACK_MODE_128BIT_CFB 3
  118. #define SA_AES_KEY_LEN_128 2
  119. #define SA_AES_KEY_LEN_192 3
  120. #define SA_AES_KEY_LEN_256 4
  121. #define SA_REV2 1
  122. /**
  123. * The follow defines bits sa_command_1
  124. * In Basic hash mode this bit define simple hash or hmac.
  125. * In IPsec mode, this bit define muting control.
  126. */
  127. #define SA_HASH_MODE_HASH 0
  128. #define SA_HASH_MODE_HMAC 1
  129. #define SA_MC_ENABLE 0
  130. #define SA_MC_DISABLE 1
  131. #define SA_NOT_COPY_HDR 0
  132. #define SA_COPY_HDR 1
  133. #define SA_NOT_COPY_PAD 0
  134. #define SA_COPY_PAD 1
  135. #define SA_NOT_COPY_PAYLOAD 0
  136. #define SA_COPY_PAYLOAD 1
  137. #define SA_EXTENDED_SN_OFF 0
  138. #define SA_EXTENDED_SN_ON 1
  139. #define SA_SEQ_MASK_OFF 0
  140. #define SA_SEQ_MASK_ON 1
  141. union sa_command_1 {
  142. struct {
  143. u32 crypto_mode31:1;
  144. u32 save_arc4_state:1;
  145. u32 arc4_stateful:1;
  146. u32 key_len:5;
  147. u32 hash_crypto_offset:8;
  148. u32 sa_rev:2;
  149. u32 byte_offset:1;
  150. u32 hmac_muting:1;
  151. u32 feedback_mode:2;
  152. u32 crypto_mode9_8:2;
  153. u32 extended_seq_num:1;
  154. u32 seq_num_mask:1;
  155. u32 mutable_bit_proc:1;
  156. u32 ip_version:1;
  157. u32 copy_pad:1;
  158. u32 copy_payload:1;
  159. u32 copy_hdr:1;
  160. u32 rsv1:1;
  161. } bf;
  162. u32 w;
  163. } __attribute__((packed));
  164. struct dynamic_sa_ctl {
  165. union dynamic_sa_contents sa_contents;
  166. union sa_command_0 sa_command_0;
  167. union sa_command_1 sa_command_1;
  168. } __attribute__((packed));
  169. /**
  170. * State Record for Security Association (SA)
  171. */
  172. struct sa_state_record {
  173. __le32 save_iv[4];
  174. __le32 save_hash_byte_cnt[2];
  175. union {
  176. u32 save_digest[16]; /* for MD5/SHA */
  177. __le32 save_digest_le32[16]; /* GHASH / CBC */
  178. };
  179. } __attribute__((packed));
  180. /**
  181. * Security Association (SA) for AES128
  182. *
  183. */
  184. struct dynamic_sa_aes128 {
  185. struct dynamic_sa_ctl ctrl;
  186. __le32 key[4];
  187. __le32 iv[4]; /* for CBC, OFC, and CFB mode */
  188. u32 state_ptr;
  189. u32 reserved;
  190. } __attribute__((packed));
  191. #define SA_AES128_LEN (sizeof(struct dynamic_sa_aes128)/4)
  192. #define SA_AES128_CONTENTS 0x3e000042
  193. /*
  194. * Security Association (SA) for AES192
  195. */
  196. struct dynamic_sa_aes192 {
  197. struct dynamic_sa_ctl ctrl;
  198. __le32 key[6];
  199. __le32 iv[4]; /* for CBC, OFC, and CFB mode */
  200. u32 state_ptr;
  201. u32 reserved;
  202. } __attribute__((packed));
  203. #define SA_AES192_LEN (sizeof(struct dynamic_sa_aes192)/4)
  204. #define SA_AES192_CONTENTS 0x3e000062
  205. /**
  206. * Security Association (SA) for AES256
  207. */
  208. struct dynamic_sa_aes256 {
  209. struct dynamic_sa_ctl ctrl;
  210. __le32 key[8];
  211. __le32 iv[4]; /* for CBC, OFC, and CFB mode */
  212. u32 state_ptr;
  213. u32 reserved;
  214. } __attribute__((packed));
  215. #define SA_AES256_LEN (sizeof(struct dynamic_sa_aes256)/4)
  216. #define SA_AES256_CONTENTS 0x3e000082
  217. #define SA_AES_CONTENTS 0x3e000002
  218. /**
  219. * Security Association (SA) for AES128 CCM
  220. */
  221. struct dynamic_sa_aes128_ccm {
  222. struct dynamic_sa_ctl ctrl;
  223. __le32 key[4];
  224. __le32 iv[4];
  225. u32 state_ptr;
  226. u32 reserved;
  227. } __packed;
  228. #define SA_AES128_CCM_LEN (sizeof(struct dynamic_sa_aes128_ccm)/4)
  229. #define SA_AES128_CCM_CONTENTS 0x3e000042
  230. #define SA_AES_CCM_CONTENTS 0x3e000002
  231. /**
  232. * Security Association (SA) for AES128_GCM
  233. */
  234. struct dynamic_sa_aes128_gcm {
  235. struct dynamic_sa_ctl ctrl;
  236. __le32 key[4];
  237. __le32 inner_digest[4];
  238. __le32 iv[4];
  239. u32 state_ptr;
  240. u32 reserved;
  241. } __packed;
  242. #define SA_AES128_GCM_LEN (sizeof(struct dynamic_sa_aes128_gcm)/4)
  243. #define SA_AES128_GCM_CONTENTS 0x3e000442
  244. #define SA_AES_GCM_CONTENTS 0x3e000402
  245. /**
  246. * Security Association (SA) for HASH160: HMAC-SHA1
  247. */
  248. struct dynamic_sa_hash160 {
  249. struct dynamic_sa_ctl ctrl;
  250. __le32 inner_digest[5];
  251. __le32 outer_digest[5];
  252. u32 state_ptr;
  253. u32 reserved;
  254. } __attribute__((packed));
  255. #define SA_HASH160_LEN (sizeof(struct dynamic_sa_hash160)/4)
  256. #define SA_HASH160_CONTENTS 0x2000a502
  257. static inline u32
  258. get_dynamic_sa_offset_state_ptr_field(struct dynamic_sa_ctl *cts)
  259. {
  260. u32 offset;
  261. offset = cts->sa_contents.bf.key_size
  262. + cts->sa_contents.bf.inner_size
  263. + cts->sa_contents.bf.outer_size
  264. + cts->sa_contents.bf.spi
  265. + cts->sa_contents.bf.seq_num0
  266. + cts->sa_contents.bf.seq_num1
  267. + cts->sa_contents.bf.seq_num_mask0
  268. + cts->sa_contents.bf.seq_num_mask1
  269. + cts->sa_contents.bf.seq_num_mask2
  270. + cts->sa_contents.bf.seq_num_mask3
  271. + cts->sa_contents.bf.iv0
  272. + cts->sa_contents.bf.iv1
  273. + cts->sa_contents.bf.iv2
  274. + cts->sa_contents.bf.iv3;
  275. return sizeof(struct dynamic_sa_ctl) + offset * 4;
  276. }
  277. static inline __le32 *get_dynamic_sa_key_field(struct dynamic_sa_ctl *cts)
  278. {
  279. return (__le32 *) ((unsigned long)cts + sizeof(struct dynamic_sa_ctl));
  280. }
  281. static inline __le32 *get_dynamic_sa_inner_digest(struct dynamic_sa_ctl *cts)
  282. {
  283. return (__le32 *) ((unsigned long)cts +
  284. sizeof(struct dynamic_sa_ctl) +
  285. cts->sa_contents.bf.key_size * 4);
  286. }
  287. #endif