crypt_s390.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Support for s390 cryptographic instructions.
  5. *
  6. * Copyright IBM Corp. 2003,2007
  7. * Author(s): Thomas Spatzier
  8. * Jan Glauber (jan.glauber@de.ibm.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #ifndef _CRYPTO_ARCH_S390_CRYPT_S390_H
  17. #define _CRYPTO_ARCH_S390_CRYPT_S390_H
  18. #include <asm/errno.h>
  19. #define CRYPT_S390_OP_MASK 0xFF00
  20. #define CRYPT_S390_FUNC_MASK 0x00FF
  21. #define CRYPT_S390_PRIORITY 300
  22. #define CRYPT_S390_COMPOSITE_PRIORITY 400
  23. #define CRYPT_S390_MSA 0x1
  24. #define CRYPT_S390_MSA3 0x2
  25. #define CRYPT_S390_MSA4 0x4
  26. /* s390 cryptographic operations */
  27. enum crypt_s390_operations {
  28. CRYPT_S390_KM = 0x0100,
  29. CRYPT_S390_KMC = 0x0200,
  30. CRYPT_S390_KIMD = 0x0300,
  31. CRYPT_S390_KLMD = 0x0400,
  32. CRYPT_S390_KMAC = 0x0500,
  33. CRYPT_S390_KMCTR = 0x0600
  34. };
  35. /*
  36. * function codes for KM (CIPHER MESSAGE) instruction
  37. * 0x80 is the decipher modifier bit
  38. */
  39. enum crypt_s390_km_func {
  40. KM_QUERY = CRYPT_S390_KM | 0x0,
  41. KM_DEA_ENCRYPT = CRYPT_S390_KM | 0x1,
  42. KM_DEA_DECRYPT = CRYPT_S390_KM | 0x1 | 0x80,
  43. KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2,
  44. KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80,
  45. KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3,
  46. KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80,
  47. KM_AES_128_ENCRYPT = CRYPT_S390_KM | 0x12,
  48. KM_AES_128_DECRYPT = CRYPT_S390_KM | 0x12 | 0x80,
  49. KM_AES_192_ENCRYPT = CRYPT_S390_KM | 0x13,
  50. KM_AES_192_DECRYPT = CRYPT_S390_KM | 0x13 | 0x80,
  51. KM_AES_256_ENCRYPT = CRYPT_S390_KM | 0x14,
  52. KM_AES_256_DECRYPT = CRYPT_S390_KM | 0x14 | 0x80,
  53. KM_XTS_128_ENCRYPT = CRYPT_S390_KM | 0x32,
  54. KM_XTS_128_DECRYPT = CRYPT_S390_KM | 0x32 | 0x80,
  55. KM_XTS_256_ENCRYPT = CRYPT_S390_KM | 0x34,
  56. KM_XTS_256_DECRYPT = CRYPT_S390_KM | 0x34 | 0x80,
  57. };
  58. /*
  59. * function codes for KMC (CIPHER MESSAGE WITH CHAINING)
  60. * instruction
  61. */
  62. enum crypt_s390_kmc_func {
  63. KMC_QUERY = CRYPT_S390_KMC | 0x0,
  64. KMC_DEA_ENCRYPT = CRYPT_S390_KMC | 0x1,
  65. KMC_DEA_DECRYPT = CRYPT_S390_KMC | 0x1 | 0x80,
  66. KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2,
  67. KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80,
  68. KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3,
  69. KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80,
  70. KMC_AES_128_ENCRYPT = CRYPT_S390_KMC | 0x12,
  71. KMC_AES_128_DECRYPT = CRYPT_S390_KMC | 0x12 | 0x80,
  72. KMC_AES_192_ENCRYPT = CRYPT_S390_KMC | 0x13,
  73. KMC_AES_192_DECRYPT = CRYPT_S390_KMC | 0x13 | 0x80,
  74. KMC_AES_256_ENCRYPT = CRYPT_S390_KMC | 0x14,
  75. KMC_AES_256_DECRYPT = CRYPT_S390_KMC | 0x14 | 0x80,
  76. KMC_PRNG = CRYPT_S390_KMC | 0x43,
  77. };
  78. /*
  79. * function codes for KMCTR (CIPHER MESSAGE WITH COUNTER)
  80. * instruction
  81. */
  82. enum crypt_s390_kmctr_func {
  83. KMCTR_QUERY = CRYPT_S390_KMCTR | 0x0,
  84. KMCTR_DEA_ENCRYPT = CRYPT_S390_KMCTR | 0x1,
  85. KMCTR_DEA_DECRYPT = CRYPT_S390_KMCTR | 0x1 | 0x80,
  86. KMCTR_TDEA_128_ENCRYPT = CRYPT_S390_KMCTR | 0x2,
  87. KMCTR_TDEA_128_DECRYPT = CRYPT_S390_KMCTR | 0x2 | 0x80,
  88. KMCTR_TDEA_192_ENCRYPT = CRYPT_S390_KMCTR | 0x3,
  89. KMCTR_TDEA_192_DECRYPT = CRYPT_S390_KMCTR | 0x3 | 0x80,
  90. KMCTR_AES_128_ENCRYPT = CRYPT_S390_KMCTR | 0x12,
  91. KMCTR_AES_128_DECRYPT = CRYPT_S390_KMCTR | 0x12 | 0x80,
  92. KMCTR_AES_192_ENCRYPT = CRYPT_S390_KMCTR | 0x13,
  93. KMCTR_AES_192_DECRYPT = CRYPT_S390_KMCTR | 0x13 | 0x80,
  94. KMCTR_AES_256_ENCRYPT = CRYPT_S390_KMCTR | 0x14,
  95. KMCTR_AES_256_DECRYPT = CRYPT_S390_KMCTR | 0x14 | 0x80,
  96. };
  97. /*
  98. * function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
  99. * instruction
  100. */
  101. enum crypt_s390_kimd_func {
  102. KIMD_QUERY = CRYPT_S390_KIMD | 0,
  103. KIMD_SHA_1 = CRYPT_S390_KIMD | 1,
  104. KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
  105. KIMD_SHA_512 = CRYPT_S390_KIMD | 3,
  106. KIMD_GHASH = CRYPT_S390_KIMD | 65,
  107. };
  108. /*
  109. * function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
  110. * instruction
  111. */
  112. enum crypt_s390_klmd_func {
  113. KLMD_QUERY = CRYPT_S390_KLMD | 0,
  114. KLMD_SHA_1 = CRYPT_S390_KLMD | 1,
  115. KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
  116. KLMD_SHA_512 = CRYPT_S390_KLMD | 3,
  117. };
  118. /*
  119. * function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
  120. * instruction
  121. */
  122. enum crypt_s390_kmac_func {
  123. KMAC_QUERY = CRYPT_S390_KMAC | 0,
  124. KMAC_DEA = CRYPT_S390_KMAC | 1,
  125. KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
  126. KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
  127. };
  128. /**
  129. * crypt_s390_km:
  130. * @func: the function code passed to KM; see crypt_s390_km_func
  131. * @param: address of parameter block; see POP for details on each func
  132. * @dest: address of destination memory area
  133. * @src: address of source memory area
  134. * @src_len: length of src operand in bytes
  135. *
  136. * Executes the KM (CIPHER MESSAGE) operation of the CPU.
  137. *
  138. * Returns -1 for failure, 0 for the query func, number of processed
  139. * bytes for encryption/decryption funcs
  140. */
  141. static inline int crypt_s390_km(long func, void *param,
  142. u8 *dest, const u8 *src, long src_len)
  143. {
  144. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  145. register void *__param asm("1") = param;
  146. register const u8 *__src asm("2") = src;
  147. register long __src_len asm("3") = src_len;
  148. register u8 *__dest asm("4") = dest;
  149. int ret;
  150. asm volatile(
  151. "0: .insn rre,0xb92e0000,%3,%1 \n" /* KM opcode */
  152. "1: brc 1,0b \n" /* handle partial completion */
  153. " la %0,0\n"
  154. "2:\n"
  155. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  156. : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
  157. : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
  158. if (ret < 0)
  159. return ret;
  160. return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
  161. }
  162. /**
  163. * crypt_s390_kmc:
  164. * @func: the function code passed to KM; see crypt_s390_kmc_func
  165. * @param: address of parameter block; see POP for details on each func
  166. * @dest: address of destination memory area
  167. * @src: address of source memory area
  168. * @src_len: length of src operand in bytes
  169. *
  170. * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
  171. *
  172. * Returns -1 for failure, 0 for the query func, number of processed
  173. * bytes for encryption/decryption funcs
  174. */
  175. static inline int crypt_s390_kmc(long func, void *param,
  176. u8 *dest, const u8 *src, long src_len)
  177. {
  178. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  179. register void *__param asm("1") = param;
  180. register const u8 *__src asm("2") = src;
  181. register long __src_len asm("3") = src_len;
  182. register u8 *__dest asm("4") = dest;
  183. int ret;
  184. asm volatile(
  185. "0: .insn rre,0xb92f0000,%3,%1 \n" /* KMC opcode */
  186. "1: brc 1,0b \n" /* handle partial completion */
  187. " la %0,0\n"
  188. "2:\n"
  189. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  190. : "=d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest)
  191. : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
  192. if (ret < 0)
  193. return ret;
  194. return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
  195. }
  196. /**
  197. * crypt_s390_kimd:
  198. * @func: the function code passed to KM; see crypt_s390_kimd_func
  199. * @param: address of parameter block; see POP for details on each func
  200. * @src: address of source memory area
  201. * @src_len: length of src operand in bytes
  202. *
  203. * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
  204. * of the CPU.
  205. *
  206. * Returns -1 for failure, 0 for the query func, number of processed
  207. * bytes for digest funcs
  208. */
  209. static inline int crypt_s390_kimd(long func, void *param,
  210. const u8 *src, long src_len)
  211. {
  212. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  213. register void *__param asm("1") = param;
  214. register const u8 *__src asm("2") = src;
  215. register long __src_len asm("3") = src_len;
  216. int ret;
  217. asm volatile(
  218. "0: .insn rre,0xb93e0000,%1,%1 \n" /* KIMD opcode */
  219. "1: brc 1,0b \n" /* handle partial completion */
  220. " la %0,0\n"
  221. "2:\n"
  222. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  223. : "=d" (ret), "+a" (__src), "+d" (__src_len)
  224. : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
  225. if (ret < 0)
  226. return ret;
  227. return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
  228. }
  229. /**
  230. * crypt_s390_klmd:
  231. * @func: the function code passed to KM; see crypt_s390_klmd_func
  232. * @param: address of parameter block; see POP for details on each func
  233. * @src: address of source memory area
  234. * @src_len: length of src operand in bytes
  235. *
  236. * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
  237. *
  238. * Returns -1 for failure, 0 for the query func, number of processed
  239. * bytes for digest funcs
  240. */
  241. static inline int crypt_s390_klmd(long func, void *param,
  242. const u8 *src, long src_len)
  243. {
  244. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  245. register void *__param asm("1") = param;
  246. register const u8 *__src asm("2") = src;
  247. register long __src_len asm("3") = src_len;
  248. int ret;
  249. asm volatile(
  250. "0: .insn rre,0xb93f0000,%1,%1 \n" /* KLMD opcode */
  251. "1: brc 1,0b \n" /* handle partial completion */
  252. " la %0,0\n"
  253. "2:\n"
  254. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  255. : "=d" (ret), "+a" (__src), "+d" (__src_len)
  256. : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
  257. if (ret < 0)
  258. return ret;
  259. return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
  260. }
  261. /**
  262. * crypt_s390_kmac:
  263. * @func: the function code passed to KM; see crypt_s390_klmd_func
  264. * @param: address of parameter block; see POP for details on each func
  265. * @src: address of source memory area
  266. * @src_len: length of src operand in bytes
  267. *
  268. * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
  269. * of the CPU.
  270. *
  271. * Returns -1 for failure, 0 for the query func, number of processed
  272. * bytes for digest funcs
  273. */
  274. static inline int crypt_s390_kmac(long func, void *param,
  275. const u8 *src, long src_len)
  276. {
  277. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  278. register void *__param asm("1") = param;
  279. register const u8 *__src asm("2") = src;
  280. register long __src_len asm("3") = src_len;
  281. int ret;
  282. asm volatile(
  283. "0: .insn rre,0xb91e0000,%1,%1 \n" /* KLAC opcode */
  284. "1: brc 1,0b \n" /* handle partial completion */
  285. " la %0,0\n"
  286. "2:\n"
  287. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  288. : "=d" (ret), "+a" (__src), "+d" (__src_len)
  289. : "d" (__func), "a" (__param), "0" (-1) : "cc", "memory");
  290. if (ret < 0)
  291. return ret;
  292. return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
  293. }
  294. /**
  295. * crypt_s390_kmctr:
  296. * @func: the function code passed to KMCTR; see crypt_s390_kmctr_func
  297. * @param: address of parameter block; see POP for details on each func
  298. * @dest: address of destination memory area
  299. * @src: address of source memory area
  300. * @src_len: length of src operand in bytes
  301. * @counter: address of counter value
  302. *
  303. * Executes the KMCTR (CIPHER MESSAGE WITH COUNTER) operation of the CPU.
  304. *
  305. * Returns -1 for failure, 0 for the query func, number of processed
  306. * bytes for encryption/decryption funcs
  307. */
  308. static inline int crypt_s390_kmctr(long func, void *param, u8 *dest,
  309. const u8 *src, long src_len, u8 *counter)
  310. {
  311. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  312. register void *__param asm("1") = param;
  313. register const u8 *__src asm("2") = src;
  314. register long __src_len asm("3") = src_len;
  315. register u8 *__dest asm("4") = dest;
  316. register u8 *__ctr asm("6") = counter;
  317. int ret = -1;
  318. asm volatile(
  319. "0: .insn rrf,0xb92d0000,%3,%1,%4,0 \n" /* KMCTR opcode */
  320. "1: brc 1,0b \n" /* handle partial completion */
  321. " la %0,0\n"
  322. "2:\n"
  323. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  324. : "+d" (ret), "+a" (__src), "+d" (__src_len), "+a" (__dest),
  325. "+a" (__ctr)
  326. : "d" (__func), "a" (__param) : "cc", "memory");
  327. if (ret < 0)
  328. return ret;
  329. return (func & CRYPT_S390_FUNC_MASK) ? src_len - __src_len : __src_len;
  330. }
  331. /**
  332. * crypt_s390_func_available:
  333. * @func: the function code of the specific function; 0 if op in general
  334. *
  335. * Tests if a specific crypto function is implemented on the machine.
  336. *
  337. * Returns 1 if func available; 0 if func or op in general not available
  338. */
  339. static inline int crypt_s390_func_available(int func,
  340. unsigned int facility_mask)
  341. {
  342. unsigned char status[16];
  343. int ret;
  344. if (facility_mask & CRYPT_S390_MSA && !test_facility(17))
  345. return 0;
  346. if (facility_mask & CRYPT_S390_MSA3 && !test_facility(76))
  347. return 0;
  348. if (facility_mask & CRYPT_S390_MSA4 && !test_facility(77))
  349. return 0;
  350. switch (func & CRYPT_S390_OP_MASK) {
  351. case CRYPT_S390_KM:
  352. ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
  353. break;
  354. case CRYPT_S390_KMC:
  355. ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
  356. break;
  357. case CRYPT_S390_KIMD:
  358. ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
  359. break;
  360. case CRYPT_S390_KLMD:
  361. ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
  362. break;
  363. case CRYPT_S390_KMAC:
  364. ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
  365. break;
  366. case CRYPT_S390_KMCTR:
  367. ret = crypt_s390_kmctr(KMCTR_QUERY, &status, NULL, NULL, 0,
  368. NULL);
  369. break;
  370. default:
  371. return 0;
  372. }
  373. if (ret < 0)
  374. return 0;
  375. func &= CRYPT_S390_FUNC_MASK;
  376. func &= 0x7f; /* mask modifier bit */
  377. return (status[func >> 3] & (0x80 >> (func & 7))) != 0;
  378. }
  379. /**
  380. * crypt_s390_pcc:
  381. * @func: the function code passed to KM; see crypt_s390_km_func
  382. * @param: address of parameter block; see POP for details on each func
  383. *
  384. * Executes the PCC (PERFORM CRYPTOGRAPHIC COMPUTATION) operation of the CPU.
  385. *
  386. * Returns -1 for failure, 0 for success.
  387. */
  388. static inline int crypt_s390_pcc(long func, void *param)
  389. {
  390. register long __func asm("0") = func & 0x7f; /* encrypt or decrypt */
  391. register void *__param asm("1") = param;
  392. int ret = -1;
  393. asm volatile(
  394. "0: .insn rre,0xb92c0000,0,0 \n" /* PCC opcode */
  395. "1: brc 1,0b \n" /* handle partial completion */
  396. " la %0,0\n"
  397. "2:\n"
  398. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  399. : "+d" (ret)
  400. : "d" (__func), "a" (__param) : "cc", "memory");
  401. return ret;
  402. }
  403. #endif /* _CRYPTO_ARCH_S390_CRYPT_S390_H */