crypt_s390.h 9.8 KB

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