crypt_s390.h 10 KB

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