crypt_s390.h 10 KB

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