crypt_s390.h 9.7 KB

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