crypt_s390.h 13 KB

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