crypt_s390.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. * Standard fixup and ex_table sections for crypt_s390 inline functions.
  98. * label 0: the s390 crypto operation
  99. * label 1: just after 1 to catch illegal operation exception
  100. * (unsupported model)
  101. * label 6: the return point after fixup
  102. * label 7: set error value if exception _in_ crypto operation
  103. * label 8: set error value if illegal operation exception
  104. * [ret] is the variable to receive the error code
  105. * [ERR] is the error code value
  106. */
  107. #ifndef CONFIG_64BIT
  108. #define __crypt_s390_fixup \
  109. ".section .fixup,\"ax\" \n" \
  110. "7: lhi %0,%h[e1] \n" \
  111. " bras 1,9f \n" \
  112. " .long 6b \n" \
  113. "8: lhi %0,%h[e2] \n" \
  114. " bras 1,9f \n" \
  115. " .long 6b \n" \
  116. "9: l 1,0(1) \n" \
  117. " br 1 \n" \
  118. ".previous \n" \
  119. ".section __ex_table,\"a\" \n" \
  120. " .align 4 \n" \
  121. " .long 0b,7b \n" \
  122. " .long 1b,8b \n" \
  123. ".previous"
  124. #else /* CONFIG_64BIT */
  125. #define __crypt_s390_fixup \
  126. ".section .fixup,\"ax\" \n" \
  127. "7: lhi %0,%h[e1] \n" \
  128. " jg 6b \n" \
  129. "8: lhi %0,%h[e2] \n" \
  130. " jg 6b \n" \
  131. ".previous\n" \
  132. ".section __ex_table,\"a\" \n" \
  133. " .align 8 \n" \
  134. " .quad 0b,7b \n" \
  135. " .quad 1b,8b \n" \
  136. ".previous"
  137. #endif /* CONFIG_64BIT */
  138. /*
  139. * Standard code for setting the result of s390 crypto instructions.
  140. * %0: the register which will receive the result
  141. * [result]: the register containing the result (e.g. second operand length
  142. * to compute number of processed bytes].
  143. */
  144. #ifndef CONFIG_64BIT
  145. #define __crypt_s390_set_result \
  146. " lr %0,%[result] \n"
  147. #else /* CONFIG_64BIT */
  148. #define __crypt_s390_set_result \
  149. " lgr %0,%[result] \n"
  150. #endif
  151. /*
  152. * Executes the KM (CIPHER MESSAGE) operation of the CPU.
  153. * @param func: the function code passed to KM; see crypt_s390_km_func
  154. * @param param: address of parameter block; see POP for details on each func
  155. * @param dest: address of destination memory area
  156. * @param src: address of source memory area
  157. * @param src_len: length of src operand in bytes
  158. * @returns < zero for failure, 0 for the query func, number of processed bytes
  159. * for encryption/decryption funcs
  160. */
  161. static inline int
  162. crypt_s390_km(long func, void* param, u8* dest, const u8* src, long src_len)
  163. {
  164. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  165. register void* __param asm("1") = param;
  166. register u8* __dest asm("4") = dest;
  167. register const u8* __src asm("2") = src;
  168. register long __src_len asm("3") = src_len;
  169. int ret;
  170. ret = 0;
  171. __asm__ __volatile__ (
  172. "0: .insn rre,0xB92E0000,%1,%2 \n" /* KM opcode */
  173. "1: brc 1,0b \n" /* handle partial completion */
  174. __crypt_s390_set_result
  175. "6: \n"
  176. __crypt_s390_fixup
  177. : "+d" (ret), "+a" (__dest), "+a" (__src),
  178. [result] "+d" (__src_len)
  179. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  180. "a" (__param)
  181. : "cc", "memory"
  182. );
  183. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  184. ret = src_len - ret;
  185. }
  186. return ret;
  187. }
  188. /*
  189. * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
  190. * @param func: the function code passed to KM; see crypt_s390_kmc_func
  191. * @param param: address of parameter block; see POP for details on each func
  192. * @param dest: address of destination memory area
  193. * @param src: address of source memory area
  194. * @param src_len: length of src operand in bytes
  195. * @returns < zero for failure, 0 for the query func, number of processed bytes
  196. * for encryption/decryption funcs
  197. */
  198. static inline int
  199. crypt_s390_kmc(long func, void* param, u8* dest, const u8* src, long src_len)
  200. {
  201. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  202. register void* __param asm("1") = param;
  203. register u8* __dest asm("4") = dest;
  204. register const u8* __src asm("2") = src;
  205. register long __src_len asm("3") = src_len;
  206. int ret;
  207. ret = 0;
  208. __asm__ __volatile__ (
  209. "0: .insn rre,0xB92F0000,%1,%2 \n" /* KMC opcode */
  210. "1: brc 1,0b \n" /* handle partial completion */
  211. __crypt_s390_set_result
  212. "6: \n"
  213. __crypt_s390_fixup
  214. : "+d" (ret), "+a" (__dest), "+a" (__src),
  215. [result] "+d" (__src_len)
  216. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  217. "a" (__param)
  218. : "cc", "memory"
  219. );
  220. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  221. ret = src_len - ret;
  222. }
  223. return ret;
  224. }
  225. /*
  226. * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
  227. * of the CPU.
  228. * @param func: the function code passed to KM; see crypt_s390_kimd_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_kimd(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. ret = 0;
  244. __asm__ __volatile__ (
  245. "0: .insn rre,0xB93E0000,%1,%1 \n" /* KIMD opcode */
  246. "1: brc 1,0b \n" /* handle partical completion */
  247. __crypt_s390_set_result
  248. "6: \n"
  249. __crypt_s390_fixup
  250. : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
  251. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  252. "a" (__param)
  253. : "cc", "memory"
  254. );
  255. if (ret >= 0 && (func & CRYPT_S390_FUNC_MASK)){
  256. ret = src_len - ret;
  257. }
  258. return ret;
  259. }
  260. /*
  261. * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
  262. * @param func: the function code passed to KM; see crypt_s390_klmd_func
  263. * @param param: address of parameter block; see POP for details on each func
  264. * @param src: address of source memory area
  265. * @param src_len: length of src operand in bytes
  266. * @returns < zero for failure, 0 for the query func, number of processed bytes
  267. * for digest funcs
  268. */
  269. static inline int
  270. crypt_s390_klmd(long func, void* param, const u8* src, long src_len)
  271. {
  272. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  273. register void* __param asm("1") = param;
  274. register const u8* __src asm("2") = src;
  275. register long __src_len asm("3") = src_len;
  276. int ret;
  277. ret = 0;
  278. __asm__ __volatile__ (
  279. "0: .insn rre,0xB93F0000,%1,%1 \n" /* KLMD opcode */
  280. "1: brc 1,0b \n" /* handle partical completion */
  281. __crypt_s390_set_result
  282. "6: \n"
  283. __crypt_s390_fixup
  284. : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
  285. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  286. "a" (__param)
  287. : "cc", "memory"
  288. );
  289. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  290. ret = src_len - ret;
  291. }
  292. return ret;
  293. }
  294. /*
  295. * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
  296. * of the CPU.
  297. * @param func: the function code passed to KM; see crypt_s390_klmd_func
  298. * @param param: address of parameter block; see POP for details on each func
  299. * @param src: address of source memory area
  300. * @param src_len: length of src operand in bytes
  301. * @returns < zero for failure, 0 for the query func, number of processed bytes
  302. * for digest funcs
  303. */
  304. static inline int
  305. crypt_s390_kmac(long func, void* param, const u8* src, long src_len)
  306. {
  307. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  308. register void* __param asm("1") = param;
  309. register const u8* __src asm("2") = src;
  310. register long __src_len asm("3") = src_len;
  311. int ret;
  312. ret = 0;
  313. __asm__ __volatile__ (
  314. "0: .insn rre,0xB91E0000,%5,%5 \n" /* KMAC opcode */
  315. "1: brc 1,0b \n" /* handle partical completion */
  316. __crypt_s390_set_result
  317. "6: \n"
  318. __crypt_s390_fixup
  319. : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
  320. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  321. "a" (__param)
  322. : "cc", "memory"
  323. );
  324. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  325. ret = src_len - ret;
  326. }
  327. return ret;
  328. }
  329. /**
  330. * Tests if a specific crypto function is implemented on the machine.
  331. * @param func: the function code of the specific function; 0 if op in general
  332. * @return 1 if func available; 0 if func or op in general not available
  333. */
  334. static inline int
  335. crypt_s390_func_available(int func)
  336. {
  337. int ret;
  338. struct crypt_s390_query_status status = {
  339. .high = 0,
  340. .low = 0
  341. };
  342. switch (func & CRYPT_S390_OP_MASK){
  343. case CRYPT_S390_KM:
  344. ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
  345. break;
  346. case CRYPT_S390_KMC:
  347. ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
  348. break;
  349. case CRYPT_S390_KIMD:
  350. ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
  351. break;
  352. case CRYPT_S390_KLMD:
  353. ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
  354. break;
  355. case CRYPT_S390_KMAC:
  356. ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
  357. break;
  358. default:
  359. ret = 0;
  360. return ret;
  361. }
  362. if (ret >= 0){
  363. func &= CRYPT_S390_FUNC_MASK;
  364. func &= 0x7f; //mask modifier bit
  365. if (func < 64){
  366. ret = (status.high >> (64 - func - 1)) & 0x1;
  367. } else {
  368. ret = (status.low >> (128 - func - 1)) & 0x1;
  369. }
  370. } else {
  371. ret = 0;
  372. }
  373. return ret;
  374. }
  375. #endif // _CRYPTO_ARCH_S390_CRYPT_S390_H