cpacf.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * CP Assist for Cryptographic Functions (CPACF)
  3. *
  4. * Copyright IBM Corp. 2003, 2016
  5. * Author(s): Thomas Spatzier
  6. * Jan Glauber
  7. * Harald Freudenberger (freude@de.ibm.com)
  8. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  9. */
  10. #ifndef _ASM_S390_CPACF_H
  11. #define _ASM_S390_CPACF_H
  12. #include <asm/facility.h>
  13. /*
  14. * Instruction opcodes for the CPACF instructions
  15. */
  16. #define CPACF_KMAC 0xb91e /* MSA */
  17. #define CPACF_KM 0xb92e /* MSA */
  18. #define CPACF_KMC 0xb92f /* MSA */
  19. #define CPACF_KIMD 0xb93e /* MSA */
  20. #define CPACF_KLMD 0xb93f /* MSA */
  21. #define CPACF_PCKMO 0xb928 /* MSA3 */
  22. #define CPACF_KMF 0xb92a /* MSA4 */
  23. #define CPACF_KMO 0xb92b /* MSA4 */
  24. #define CPACF_PCC 0xb92c /* MSA4 */
  25. #define CPACF_KMCTR 0xb92d /* MSA4 */
  26. #define CPACF_PPNO 0xb93c /* MSA5 */
  27. /*
  28. * Decryption modifier bit
  29. */
  30. #define CPACF_DECRYPT 0x80
  31. /*
  32. * Function codes for the KM (CIPHER MESSAGE) instruction
  33. */
  34. #define CPACF_KM_QUERY 0x00
  35. #define CPACF_KM_DEA 0x01
  36. #define CPACF_KM_TDEA_128 0x02
  37. #define CPACF_KM_TDEA_192 0x03
  38. #define CPACF_KM_AES_128 0x12
  39. #define CPACF_KM_AES_192 0x13
  40. #define CPACF_KM_AES_256 0x14
  41. #define CPACF_KM_XTS_128 0x32
  42. #define CPACF_KM_XTS_256 0x34
  43. /*
  44. * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
  45. * instruction
  46. */
  47. #define CPACF_KMC_QUERY 0x00
  48. #define CPACF_KMC_DEA 0x01
  49. #define CPACF_KMC_TDEA_128 0x02
  50. #define CPACF_KMC_TDEA_192 0x03
  51. #define CPACF_KMC_AES_128 0x12
  52. #define CPACF_KMC_AES_192 0x13
  53. #define CPACF_KMC_AES_256 0x14
  54. #define CPACF_KMC_PRNG 0x43
  55. /*
  56. * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
  57. * instruction
  58. */
  59. #define CPACF_KMCTR_QUERY 0x00
  60. #define CPACF_KMCTR_DEA 0x01
  61. #define CPACF_KMCTR_TDEA_128 0x02
  62. #define CPACF_KMCTR_TDEA_192 0x03
  63. #define CPACF_KMCTR_AES_128 0x12
  64. #define CPACF_KMCTR_AES_192 0x13
  65. #define CPACF_KMCTR_AES_256 0x14
  66. /*
  67. * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
  68. * instruction
  69. */
  70. #define CPACF_KIMD_QUERY 0x00
  71. #define CPACF_KIMD_SHA_1 0x01
  72. #define CPACF_KIMD_SHA_256 0x02
  73. #define CPACF_KIMD_SHA_512 0x03
  74. #define CPACF_KIMD_GHASH 0x41
  75. /*
  76. * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
  77. * instruction
  78. */
  79. #define CPACF_KLMD_QUERY 0x00
  80. #define CPACF_KLMD_SHA_1 0x01
  81. #define CPACF_KLMD_SHA_256 0x02
  82. #define CPACF_KLMD_SHA_512 0x03
  83. /*
  84. * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
  85. * instruction
  86. */
  87. #define CPACF_KMAC_QUERY 0x00
  88. #define CPACF_KMAC_DEA 0x01
  89. #define CPACF_KMAC_TDEA_128 0x02
  90. #define CPACF_KMAC_TDEA_192 0x03
  91. /*
  92. * Function codes for the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
  93. * instruction
  94. */
  95. #define CPACF_PPNO_QUERY 0x00
  96. #define CPACF_PPNO_SHA512_DRNG_GEN 0x03
  97. #define CPACF_PPNO_SHA512_DRNG_SEED 0x83
  98. typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
  99. /**
  100. * cpacf_query() - check if a specific CPACF function is available
  101. * @opcode: the opcode of the crypto instruction
  102. * @func: the function code to test for
  103. *
  104. * Executes the query function for the given crypto instruction @opcode
  105. * and checks if @func is available
  106. *
  107. * Returns 1 if @func is available for @opcode, 0 otherwise
  108. */
  109. static inline void __cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
  110. {
  111. register unsigned long r0 asm("0") = 0; /* query function */
  112. register unsigned long r1 asm("1") = (unsigned long) mask;
  113. asm volatile(
  114. " spm 0\n" /* pckmo doesn't change the cc */
  115. /* Parameter registers are ignored, but may not be 0 */
  116. "0: .insn rrf,%[opc] << 16,2,2,2,0\n"
  117. " brc 1,0b\n" /* handle partial completion */
  118. : "=m" (*mask)
  119. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
  120. : "cc");
  121. }
  122. static inline int __cpacf_check_opcode(unsigned int opcode)
  123. {
  124. switch (opcode) {
  125. case CPACF_KMAC:
  126. case CPACF_KM:
  127. case CPACF_KMC:
  128. case CPACF_KIMD:
  129. case CPACF_KLMD:
  130. return test_facility(17); /* check for MSA */
  131. case CPACF_PCKMO:
  132. return test_facility(76); /* check for MSA3 */
  133. case CPACF_KMF:
  134. case CPACF_KMO:
  135. case CPACF_PCC:
  136. case CPACF_KMCTR:
  137. return test_facility(77); /* check for MSA4 */
  138. case CPACF_PPNO:
  139. return test_facility(57); /* check for MSA5 */
  140. default:
  141. BUG();
  142. }
  143. }
  144. static inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
  145. {
  146. if (__cpacf_check_opcode(opcode)) {
  147. __cpacf_query(opcode, mask);
  148. return 1;
  149. }
  150. memset(mask, 0, sizeof(*mask));
  151. return 0;
  152. }
  153. static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
  154. {
  155. return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
  156. }
  157. static inline int cpacf_query_func(unsigned int opcode, unsigned int func)
  158. {
  159. cpacf_mask_t mask;
  160. if (cpacf_query(opcode, &mask))
  161. return cpacf_test_func(&mask, func);
  162. return 0;
  163. }
  164. /**
  165. * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
  166. * @func: the function code passed to KM; see CPACF_KM_xxx defines
  167. * @param: address of parameter block; see POP for details on each func
  168. * @dest: address of destination memory area
  169. * @src: address of source memory area
  170. * @src_len: length of src operand in bytes
  171. *
  172. * Returns 0 for the query func, number of processed bytes for
  173. * encryption/decryption funcs
  174. */
  175. static inline int cpacf_km(unsigned long func, void *param,
  176. u8 *dest, const u8 *src, long src_len)
  177. {
  178. register unsigned long r0 asm("0") = (unsigned long) func;
  179. register unsigned long r1 asm("1") = (unsigned long) param;
  180. register unsigned long r2 asm("2") = (unsigned long) src;
  181. register unsigned long r3 asm("3") = (unsigned long) src_len;
  182. register unsigned long r4 asm("4") = (unsigned long) dest;
  183. asm volatile(
  184. "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
  185. " brc 1,0b\n" /* handle partial completion */
  186. : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
  187. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
  188. : "cc", "memory");
  189. return src_len - r3;
  190. }
  191. /**
  192. * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
  193. * @func: the function code passed to KM; see CPACF_KMC_xxx defines
  194. * @param: address of parameter block; see POP for details on each func
  195. * @dest: address of destination memory area
  196. * @src: address of source memory area
  197. * @src_len: length of src operand in bytes
  198. *
  199. * Returns 0 for the query func, number of processed bytes for
  200. * encryption/decryption funcs
  201. */
  202. static inline int cpacf_kmc(unsigned long func, void *param,
  203. u8 *dest, const u8 *src, long src_len)
  204. {
  205. register unsigned long r0 asm("0") = (unsigned long) func;
  206. register unsigned long r1 asm("1") = (unsigned long) param;
  207. register unsigned long r2 asm("2") = (unsigned long) src;
  208. register unsigned long r3 asm("3") = (unsigned long) src_len;
  209. register unsigned long r4 asm("4") = (unsigned long) dest;
  210. asm volatile(
  211. "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
  212. " brc 1,0b\n" /* handle partial completion */
  213. : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
  214. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
  215. : "cc", "memory");
  216. return src_len - r3;
  217. }
  218. /**
  219. * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
  220. * instruction
  221. * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
  222. * @param: address of parameter block; see POP for details on each func
  223. * @src: address of source memory area
  224. * @src_len: length of src operand in bytes
  225. */
  226. static inline void cpacf_kimd(unsigned long func, void *param,
  227. const u8 *src, long src_len)
  228. {
  229. register unsigned long r0 asm("0") = (unsigned long) func;
  230. register unsigned long r1 asm("1") = (unsigned long) param;
  231. register unsigned long r2 asm("2") = (unsigned long) src;
  232. register unsigned long r3 asm("3") = (unsigned long) src_len;
  233. asm volatile(
  234. "0: .insn rre,%[opc] << 16,0,%[src]\n"
  235. " brc 1,0b\n" /* handle partial completion */
  236. : [src] "+a" (r2), [len] "+d" (r3)
  237. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
  238. : "cc", "memory");
  239. }
  240. /**
  241. * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
  242. * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
  243. * @param: address of parameter block; see POP for details on each func
  244. * @src: address of source memory area
  245. * @src_len: length of src operand in bytes
  246. */
  247. static inline void cpacf_klmd(unsigned long func, void *param,
  248. const u8 *src, long src_len)
  249. {
  250. register unsigned long r0 asm("0") = (unsigned long) func;
  251. register unsigned long r1 asm("1") = (unsigned long) param;
  252. register unsigned long r2 asm("2") = (unsigned long) src;
  253. register unsigned long r3 asm("3") = (unsigned long) src_len;
  254. asm volatile(
  255. "0: .insn rre,%[opc] << 16,0,%[src]\n"
  256. " brc 1,0b\n" /* handle partial completion */
  257. : [src] "+a" (r2), [len] "+d" (r3)
  258. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
  259. : "cc", "memory");
  260. }
  261. /**
  262. * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
  263. * instruction
  264. * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
  265. * @param: address of parameter block; see POP for details on each func
  266. * @src: address of source memory area
  267. * @src_len: length of src operand in bytes
  268. *
  269. * Returns 0 for the query func, number of processed bytes for digest funcs
  270. */
  271. static inline int cpacf_kmac(unsigned long func, void *param,
  272. const u8 *src, long src_len)
  273. {
  274. register unsigned long r0 asm("0") = (unsigned long) func;
  275. register unsigned long r1 asm("1") = (unsigned long) param;
  276. register unsigned long r2 asm("2") = (unsigned long) src;
  277. register unsigned long r3 asm("3") = (unsigned long) src_len;
  278. asm volatile(
  279. "0: .insn rre,%[opc] << 16,0,%[src]\n"
  280. " brc 1,0b\n" /* handle partial completion */
  281. : [src] "+a" (r2), [len] "+d" (r3)
  282. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
  283. : "cc", "memory");
  284. return src_len - r3;
  285. }
  286. /**
  287. * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
  288. * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
  289. * @param: address of parameter block; see POP for details on each func
  290. * @dest: address of destination memory area
  291. * @src: address of source memory area
  292. * @src_len: length of src operand in bytes
  293. * @counter: address of counter value
  294. *
  295. * Returns 0 for the query func, number of processed bytes for
  296. * encryption/decryption funcs
  297. */
  298. static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
  299. const u8 *src, long src_len, u8 *counter)
  300. {
  301. register unsigned long r0 asm("0") = (unsigned long) func;
  302. register unsigned long r1 asm("1") = (unsigned long) param;
  303. register unsigned long r2 asm("2") = (unsigned long) src;
  304. register unsigned long r3 asm("3") = (unsigned long) src_len;
  305. register unsigned long r4 asm("4") = (unsigned long) dest;
  306. register unsigned long r6 asm("6") = (unsigned long) counter;
  307. asm volatile(
  308. "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
  309. " brc 1,0b\n" /* handle partial completion */
  310. : [src] "+a" (r2), [len] "+d" (r3),
  311. [dst] "+a" (r4), [ctr] "+a" (r6)
  312. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
  313. : "cc", "memory");
  314. return src_len - r3;
  315. }
  316. /**
  317. * cpacf_ppno() - executes the PPNO (PERFORM PSEUDORANDOM NUMBER OPERATION)
  318. * instruction
  319. * @func: the function code passed to PPNO; see CPACF_PPNO_xxx defines
  320. * @param: address of parameter block; see POP for details on each func
  321. * @dest: address of destination memory area
  322. * @dest_len: size of destination memory area in bytes
  323. * @seed: address of seed data
  324. * @seed_len: size of seed data in bytes
  325. */
  326. static inline void cpacf_ppno(unsigned long func, void *param,
  327. u8 *dest, long dest_len,
  328. const u8 *seed, long seed_len)
  329. {
  330. register unsigned long r0 asm("0") = (unsigned long) func;
  331. register unsigned long r1 asm("1") = (unsigned long) param;
  332. register unsigned long r2 asm("2") = (unsigned long) dest;
  333. register unsigned long r3 asm("3") = (unsigned long) dest_len;
  334. register unsigned long r4 asm("4") = (unsigned long) seed;
  335. register unsigned long r5 asm("5") = (unsigned long) seed_len;
  336. asm volatile (
  337. "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
  338. " brc 1,0b\n" /* handle partial completion */
  339. : [dst] "+a" (r2), [dlen] "+d" (r3)
  340. : [fc] "d" (r0), [pba] "a" (r1),
  341. [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PPNO)
  342. : "cc", "memory");
  343. }
  344. /**
  345. * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
  346. * instruction
  347. * @func: the function code passed to PCC; see CPACF_KM_xxx defines
  348. * @param: address of parameter block; see POP for details on each func
  349. */
  350. static inline void cpacf_pcc(unsigned long func, void *param)
  351. {
  352. register unsigned long r0 asm("0") = (unsigned long) func;
  353. register unsigned long r1 asm("1") = (unsigned long) param;
  354. asm volatile(
  355. "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
  356. " brc 1,0b\n" /* handle partial completion */
  357. :
  358. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
  359. : "cc", "memory");
  360. }
  361. #endif /* _ASM_S390_CPACF_H */