crypt_s390.h 11 KB

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