crypt_s390.h 11 KB

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