cpacf.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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_PRNO 0xb93c /* MSA5 */
  27. #define CPACF_KMA 0xb929 /* MSA8 */
  28. /*
  29. * En/decryption modifier bits
  30. */
  31. #define CPACF_ENCRYPT 0x00
  32. #define CPACF_DECRYPT 0x80
  33. /*
  34. * Function codes for the KM (CIPHER MESSAGE) instruction
  35. */
  36. #define CPACF_KM_QUERY 0x00
  37. #define CPACF_KM_DEA 0x01
  38. #define CPACF_KM_TDEA_128 0x02
  39. #define CPACF_KM_TDEA_192 0x03
  40. #define CPACF_KM_AES_128 0x12
  41. #define CPACF_KM_AES_192 0x13
  42. #define CPACF_KM_AES_256 0x14
  43. #define CPACF_KM_PAES_128 0x1a
  44. #define CPACF_KM_PAES_192 0x1b
  45. #define CPACF_KM_PAES_256 0x1c
  46. #define CPACF_KM_XTS_128 0x32
  47. #define CPACF_KM_XTS_256 0x34
  48. #define CPACF_KM_PXTS_128 0x3a
  49. #define CPACF_KM_PXTS_256 0x3c
  50. /*
  51. * Function codes for the KMC (CIPHER MESSAGE WITH CHAINING)
  52. * instruction
  53. */
  54. #define CPACF_KMC_QUERY 0x00
  55. #define CPACF_KMC_DEA 0x01
  56. #define CPACF_KMC_TDEA_128 0x02
  57. #define CPACF_KMC_TDEA_192 0x03
  58. #define CPACF_KMC_AES_128 0x12
  59. #define CPACF_KMC_AES_192 0x13
  60. #define CPACF_KMC_AES_256 0x14
  61. #define CPACF_KMC_PAES_128 0x1a
  62. #define CPACF_KMC_PAES_192 0x1b
  63. #define CPACF_KMC_PAES_256 0x1c
  64. #define CPACF_KMC_PRNG 0x43
  65. /*
  66. * Function codes for the KMCTR (CIPHER MESSAGE WITH COUNTER)
  67. * instruction
  68. */
  69. #define CPACF_KMCTR_QUERY 0x00
  70. #define CPACF_KMCTR_DEA 0x01
  71. #define CPACF_KMCTR_TDEA_128 0x02
  72. #define CPACF_KMCTR_TDEA_192 0x03
  73. #define CPACF_KMCTR_AES_128 0x12
  74. #define CPACF_KMCTR_AES_192 0x13
  75. #define CPACF_KMCTR_AES_256 0x14
  76. #define CPACF_KMCTR_PAES_128 0x1a
  77. #define CPACF_KMCTR_PAES_192 0x1b
  78. #define CPACF_KMCTR_PAES_256 0x1c
  79. /*
  80. * Function codes for the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
  81. * instruction
  82. */
  83. #define CPACF_KIMD_QUERY 0x00
  84. #define CPACF_KIMD_SHA_1 0x01
  85. #define CPACF_KIMD_SHA_256 0x02
  86. #define CPACF_KIMD_SHA_512 0x03
  87. #define CPACF_KIMD_GHASH 0x41
  88. /*
  89. * Function codes for the KLMD (COMPUTE LAST MESSAGE DIGEST)
  90. * instruction
  91. */
  92. #define CPACF_KLMD_QUERY 0x00
  93. #define CPACF_KLMD_SHA_1 0x01
  94. #define CPACF_KLMD_SHA_256 0x02
  95. #define CPACF_KLMD_SHA_512 0x03
  96. /*
  97. * function codes for the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
  98. * instruction
  99. */
  100. #define CPACF_KMAC_QUERY 0x00
  101. #define CPACF_KMAC_DEA 0x01
  102. #define CPACF_KMAC_TDEA_128 0x02
  103. #define CPACF_KMAC_TDEA_192 0x03
  104. /*
  105. * Function codes for the PCKMO (PERFORM CRYPTOGRAPHIC KEY MANAGEMENT)
  106. * instruction
  107. */
  108. #define CPACF_PCKMO_QUERY 0x00
  109. #define CPACF_PCKMO_ENC_DES_KEY 0x01
  110. #define CPACF_PCKMO_ENC_TDES_128_KEY 0x02
  111. #define CPACF_PCKMO_ENC_TDES_192_KEY 0x03
  112. #define CPACF_PCKMO_ENC_AES_128_KEY 0x12
  113. #define CPACF_PCKMO_ENC_AES_192_KEY 0x13
  114. #define CPACF_PCKMO_ENC_AES_256_KEY 0x14
  115. /*
  116. * Function codes for the PRNO (PERFORM RANDOM NUMBER OPERATION)
  117. * instruction
  118. */
  119. #define CPACF_PRNO_QUERY 0x00
  120. #define CPACF_PRNO_SHA512_DRNG_GEN 0x03
  121. #define CPACF_PRNO_SHA512_DRNG_SEED 0x83
  122. #define CPACF_PRNO_TRNG_Q_R2C_RATIO 0x70
  123. #define CPACF_PRNO_TRNG 0x72
  124. typedef struct { unsigned char bytes[16]; } cpacf_mask_t;
  125. /**
  126. * cpacf_query() - check if a specific CPACF function is available
  127. * @opcode: the opcode of the crypto instruction
  128. * @func: the function code to test for
  129. *
  130. * Executes the query function for the given crypto instruction @opcode
  131. * and checks if @func is available
  132. *
  133. * Returns 1 if @func is available for @opcode, 0 otherwise
  134. */
  135. static inline void __cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
  136. {
  137. register unsigned long r0 asm("0") = 0; /* query function */
  138. register unsigned long r1 asm("1") = (unsigned long) mask;
  139. asm volatile(
  140. " spm 0\n" /* pckmo doesn't change the cc */
  141. /* Parameter regs are ignored, but must be nonzero and unique */
  142. "0: .insn rrf,%[opc] << 16,2,4,6,0\n"
  143. " brc 1,0b\n" /* handle partial completion */
  144. : "=m" (*mask)
  145. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (opcode)
  146. : "cc");
  147. }
  148. static inline int __cpacf_check_opcode(unsigned int opcode)
  149. {
  150. switch (opcode) {
  151. case CPACF_KMAC:
  152. case CPACF_KM:
  153. case CPACF_KMC:
  154. case CPACF_KIMD:
  155. case CPACF_KLMD:
  156. return test_facility(17); /* check for MSA */
  157. case CPACF_PCKMO:
  158. return test_facility(76); /* check for MSA3 */
  159. case CPACF_KMF:
  160. case CPACF_KMO:
  161. case CPACF_PCC:
  162. case CPACF_KMCTR:
  163. return test_facility(77); /* check for MSA4 */
  164. case CPACF_PRNO:
  165. return test_facility(57); /* check for MSA5 */
  166. default:
  167. BUG();
  168. }
  169. }
  170. static inline int cpacf_query(unsigned int opcode, cpacf_mask_t *mask)
  171. {
  172. if (__cpacf_check_opcode(opcode)) {
  173. __cpacf_query(opcode, mask);
  174. return 1;
  175. }
  176. memset(mask, 0, sizeof(*mask));
  177. return 0;
  178. }
  179. static inline int cpacf_test_func(cpacf_mask_t *mask, unsigned int func)
  180. {
  181. return (mask->bytes[func >> 3] & (0x80 >> (func & 7))) != 0;
  182. }
  183. static inline int cpacf_query_func(unsigned int opcode, unsigned int func)
  184. {
  185. cpacf_mask_t mask;
  186. if (cpacf_query(opcode, &mask))
  187. return cpacf_test_func(&mask, func);
  188. return 0;
  189. }
  190. /**
  191. * cpacf_km() - executes the KM (CIPHER MESSAGE) instruction
  192. * @func: the function code passed to KM; see CPACF_KM_xxx defines
  193. * @param: address of parameter block; see POP for details on each func
  194. * @dest: address of destination memory area
  195. * @src: address of source memory area
  196. * @src_len: length of src operand in bytes
  197. *
  198. * Returns 0 for the query func, number of processed bytes for
  199. * encryption/decryption funcs
  200. */
  201. static inline int cpacf_km(unsigned long func, void *param,
  202. u8 *dest, const u8 *src, long src_len)
  203. {
  204. register unsigned long r0 asm("0") = (unsigned long) func;
  205. register unsigned long r1 asm("1") = (unsigned long) param;
  206. register unsigned long r2 asm("2") = (unsigned long) src;
  207. register unsigned long r3 asm("3") = (unsigned long) src_len;
  208. register unsigned long r4 asm("4") = (unsigned long) dest;
  209. asm volatile(
  210. "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
  211. " brc 1,0b\n" /* handle partial completion */
  212. : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
  213. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KM)
  214. : "cc", "memory");
  215. return src_len - r3;
  216. }
  217. /**
  218. * cpacf_kmc() - executes the KMC (CIPHER MESSAGE WITH CHAINING) instruction
  219. * @func: the function code passed to KM; see CPACF_KMC_xxx defines
  220. * @param: address of parameter block; see POP for details on each func
  221. * @dest: address of destination memory area
  222. * @src: address of source memory area
  223. * @src_len: length of src operand in bytes
  224. *
  225. * Returns 0 for the query func, number of processed bytes for
  226. * encryption/decryption funcs
  227. */
  228. static inline int cpacf_kmc(unsigned long func, void *param,
  229. u8 *dest, const u8 *src, long src_len)
  230. {
  231. register unsigned long r0 asm("0") = (unsigned long) func;
  232. register unsigned long r1 asm("1") = (unsigned long) param;
  233. register unsigned long r2 asm("2") = (unsigned long) src;
  234. register unsigned long r3 asm("3") = (unsigned long) src_len;
  235. register unsigned long r4 asm("4") = (unsigned long) dest;
  236. asm volatile(
  237. "0: .insn rre,%[opc] << 16,%[dst],%[src]\n"
  238. " brc 1,0b\n" /* handle partial completion */
  239. : [src] "+a" (r2), [len] "+d" (r3), [dst] "+a" (r4)
  240. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMC)
  241. : "cc", "memory");
  242. return src_len - r3;
  243. }
  244. /**
  245. * cpacf_kimd() - executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
  246. * instruction
  247. * @func: the function code passed to KM; see CPACF_KIMD_xxx defines
  248. * @param: address of parameter block; see POP for details on each func
  249. * @src: address of source memory area
  250. * @src_len: length of src operand in bytes
  251. */
  252. static inline void cpacf_kimd(unsigned long func, void *param,
  253. const u8 *src, long src_len)
  254. {
  255. register unsigned long r0 asm("0") = (unsigned long) func;
  256. register unsigned long r1 asm("1") = (unsigned long) param;
  257. register unsigned long r2 asm("2") = (unsigned long) src;
  258. register unsigned long r3 asm("3") = (unsigned long) src_len;
  259. asm volatile(
  260. "0: .insn rre,%[opc] << 16,0,%[src]\n"
  261. " brc 1,0b\n" /* handle partial completion */
  262. : [src] "+a" (r2), [len] "+d" (r3)
  263. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KIMD)
  264. : "cc", "memory");
  265. }
  266. /**
  267. * cpacf_klmd() - executes the KLMD (COMPUTE LAST MESSAGE DIGEST) instruction
  268. * @func: the function code passed to KM; see CPACF_KLMD_xxx defines
  269. * @param: address of parameter block; see POP for details on each func
  270. * @src: address of source memory area
  271. * @src_len: length of src operand in bytes
  272. */
  273. static inline void cpacf_klmd(unsigned long func, void *param,
  274. const u8 *src, long src_len)
  275. {
  276. register unsigned long r0 asm("0") = (unsigned long) func;
  277. register unsigned long r1 asm("1") = (unsigned long) param;
  278. register unsigned long r2 asm("2") = (unsigned long) src;
  279. register unsigned long r3 asm("3") = (unsigned long) src_len;
  280. asm volatile(
  281. "0: .insn rre,%[opc] << 16,0,%[src]\n"
  282. " brc 1,0b\n" /* handle partial completion */
  283. : [src] "+a" (r2), [len] "+d" (r3)
  284. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KLMD)
  285. : "cc", "memory");
  286. }
  287. /**
  288. * cpacf_kmac() - executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
  289. * instruction
  290. * @func: the function code passed to KM; see CPACF_KMAC_xxx defines
  291. * @param: address of parameter block; see POP for details on each func
  292. * @src: address of source memory area
  293. * @src_len: length of src operand in bytes
  294. *
  295. * Returns 0 for the query func, number of processed bytes for digest funcs
  296. */
  297. static inline int cpacf_kmac(unsigned long func, void *param,
  298. const u8 *src, long src_len)
  299. {
  300. register unsigned long r0 asm("0") = (unsigned long) func;
  301. register unsigned long r1 asm("1") = (unsigned long) param;
  302. register unsigned long r2 asm("2") = (unsigned long) src;
  303. register unsigned long r3 asm("3") = (unsigned long) src_len;
  304. asm volatile(
  305. "0: .insn rre,%[opc] << 16,0,%[src]\n"
  306. " brc 1,0b\n" /* handle partial completion */
  307. : [src] "+a" (r2), [len] "+d" (r3)
  308. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMAC)
  309. : "cc", "memory");
  310. return src_len - r3;
  311. }
  312. /**
  313. * cpacf_kmctr() - executes the KMCTR (CIPHER MESSAGE WITH COUNTER) instruction
  314. * @func: the function code passed to KMCTR; see CPACF_KMCTR_xxx defines
  315. * @param: address of parameter block; see POP for details on each func
  316. * @dest: address of destination memory area
  317. * @src: address of source memory area
  318. * @src_len: length of src operand in bytes
  319. * @counter: address of counter value
  320. *
  321. * Returns 0 for the query func, number of processed bytes for
  322. * encryption/decryption funcs
  323. */
  324. static inline int cpacf_kmctr(unsigned long func, void *param, u8 *dest,
  325. const u8 *src, long src_len, u8 *counter)
  326. {
  327. register unsigned long r0 asm("0") = (unsigned long) func;
  328. register unsigned long r1 asm("1") = (unsigned long) param;
  329. register unsigned long r2 asm("2") = (unsigned long) src;
  330. register unsigned long r3 asm("3") = (unsigned long) src_len;
  331. register unsigned long r4 asm("4") = (unsigned long) dest;
  332. register unsigned long r6 asm("6") = (unsigned long) counter;
  333. asm volatile(
  334. "0: .insn rrf,%[opc] << 16,%[dst],%[src],%[ctr],0\n"
  335. " brc 1,0b\n" /* handle partial completion */
  336. : [src] "+a" (r2), [len] "+d" (r3),
  337. [dst] "+a" (r4), [ctr] "+a" (r6)
  338. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_KMCTR)
  339. : "cc", "memory");
  340. return src_len - r3;
  341. }
  342. /**
  343. * cpacf_prno() - executes the PRNO (PERFORM RANDOM NUMBER OPERATION)
  344. * instruction
  345. * @func: the function code passed to PRNO; see CPACF_PRNO_xxx defines
  346. * @param: address of parameter block; see POP for details on each func
  347. * @dest: address of destination memory area
  348. * @dest_len: size of destination memory area in bytes
  349. * @seed: address of seed data
  350. * @seed_len: size of seed data in bytes
  351. */
  352. static inline void cpacf_prno(unsigned long func, void *param,
  353. u8 *dest, unsigned long dest_len,
  354. const u8 *seed, unsigned long seed_len)
  355. {
  356. register unsigned long r0 asm("0") = (unsigned long) func;
  357. register unsigned long r1 asm("1") = (unsigned long) param;
  358. register unsigned long r2 asm("2") = (unsigned long) dest;
  359. register unsigned long r3 asm("3") = (unsigned long) dest_len;
  360. register unsigned long r4 asm("4") = (unsigned long) seed;
  361. register unsigned long r5 asm("5") = (unsigned long) seed_len;
  362. asm volatile (
  363. "0: .insn rre,%[opc] << 16,%[dst],%[seed]\n"
  364. " brc 1,0b\n" /* handle partial completion */
  365. : [dst] "+a" (r2), [dlen] "+d" (r3)
  366. : [fc] "d" (r0), [pba] "a" (r1),
  367. [seed] "a" (r4), [slen] "d" (r5), [opc] "i" (CPACF_PRNO)
  368. : "cc", "memory");
  369. }
  370. /**
  371. * cpacf_trng() - executes the TRNG subfunction of the PRNO instruction
  372. * @ucbuf: buffer for unconditioned data
  373. * @ucbuf_len: amount of unconditioned data to fetch in bytes
  374. * @cbuf: buffer for conditioned data
  375. * @cbuf_len: amount of conditioned data to fetch in bytes
  376. */
  377. static inline void cpacf_trng(u8 *ucbuf, unsigned long ucbuf_len,
  378. u8 *cbuf, unsigned long cbuf_len)
  379. {
  380. register unsigned long r0 asm("0") = (unsigned long) CPACF_PRNO_TRNG;
  381. register unsigned long r2 asm("2") = (unsigned long) ucbuf;
  382. register unsigned long r3 asm("3") = (unsigned long) ucbuf_len;
  383. register unsigned long r4 asm("4") = (unsigned long) cbuf;
  384. register unsigned long r5 asm("5") = (unsigned long) cbuf_len;
  385. asm volatile (
  386. "0: .insn rre,%[opc] << 16,%[ucbuf],%[cbuf]\n"
  387. " brc 1,0b\n" /* handle partial completion */
  388. : [ucbuf] "+a" (r2), [ucbuflen] "+d" (r3),
  389. [cbuf] "+a" (r4), [cbuflen] "+d" (r5)
  390. : [fc] "d" (r0), [opc] "i" (CPACF_PRNO)
  391. : "cc", "memory");
  392. }
  393. /**
  394. * cpacf_pcc() - executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION)
  395. * instruction
  396. * @func: the function code passed to PCC; see CPACF_KM_xxx defines
  397. * @param: address of parameter block; see POP for details on each func
  398. */
  399. static inline void cpacf_pcc(unsigned long func, void *param)
  400. {
  401. register unsigned long r0 asm("0") = (unsigned long) func;
  402. register unsigned long r1 asm("1") = (unsigned long) param;
  403. asm volatile(
  404. "0: .insn rre,%[opc] << 16,0,0\n" /* PCC opcode */
  405. " brc 1,0b\n" /* handle partial completion */
  406. :
  407. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCC)
  408. : "cc", "memory");
  409. }
  410. /**
  411. * cpacf_pckmo() - executes the PCKMO (PERFORM CRYPTOGRAPHIC KEY
  412. * MANAGEMENT) instruction
  413. * @func: the function code passed to PCKMO; see CPACF_PCKMO_xxx defines
  414. * @param: address of parameter block; see POP for details on each func
  415. *
  416. * Returns 0.
  417. */
  418. static inline void cpacf_pckmo(long func, void *param)
  419. {
  420. register unsigned long r0 asm("0") = (unsigned long) func;
  421. register unsigned long r1 asm("1") = (unsigned long) param;
  422. asm volatile(
  423. " .insn rre,%[opc] << 16,0,0\n" /* PCKMO opcode */
  424. :
  425. : [fc] "d" (r0), [pba] "a" (r1), [opc] "i" (CPACF_PCKMO)
  426. : "cc", "memory");
  427. }
  428. #endif /* _ASM_S390_CPACF_H */