crypt_s390.h 10.0 KB

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