crypt_s390.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. /* s930 cryptographic operations */
  21. enum crypt_s390_operations {
  22. CRYPT_S390_KM = 0x0100,
  23. CRYPT_S390_KMC = 0x0200,
  24. CRYPT_S390_KIMD = 0x0300,
  25. CRYPT_S390_KLMD = 0x0400,
  26. CRYPT_S390_KMAC = 0x0500
  27. };
  28. /* function codes for KM (CIPHER MESSAGE) instruction
  29. * 0x80 is the decipher modifier bit
  30. */
  31. enum crypt_s390_km_func {
  32. KM_QUERY = CRYPT_S390_KM | 0x0,
  33. KM_DEA_ENCRYPT = CRYPT_S390_KM | 0x1,
  34. KM_DEA_DECRYPT = CRYPT_S390_KM | 0x1 | 0x80,
  35. KM_TDEA_128_ENCRYPT = CRYPT_S390_KM | 0x2,
  36. KM_TDEA_128_DECRYPT = CRYPT_S390_KM | 0x2 | 0x80,
  37. KM_TDEA_192_ENCRYPT = CRYPT_S390_KM | 0x3,
  38. KM_TDEA_192_DECRYPT = CRYPT_S390_KM | 0x3 | 0x80,
  39. KM_AES_128_ENCRYPT = CRYPT_S390_KM | 0x12,
  40. KM_AES_128_DECRYPT = CRYPT_S390_KM | 0x12 | 0x80,
  41. KM_AES_192_ENCRYPT = CRYPT_S390_KM | 0x13,
  42. KM_AES_192_DECRYPT = CRYPT_S390_KM | 0x13 | 0x80,
  43. KM_AES_256_ENCRYPT = CRYPT_S390_KM | 0x14,
  44. KM_AES_256_DECRYPT = CRYPT_S390_KM | 0x14 | 0x80,
  45. };
  46. /* function codes for KMC (CIPHER MESSAGE WITH CHAINING)
  47. * instruction
  48. */
  49. enum crypt_s390_kmc_func {
  50. KMC_QUERY = CRYPT_S390_KMC | 0x0,
  51. KMC_DEA_ENCRYPT = CRYPT_S390_KMC | 0x1,
  52. KMC_DEA_DECRYPT = CRYPT_S390_KMC | 0x1 | 0x80,
  53. KMC_TDEA_128_ENCRYPT = CRYPT_S390_KMC | 0x2,
  54. KMC_TDEA_128_DECRYPT = CRYPT_S390_KMC | 0x2 | 0x80,
  55. KMC_TDEA_192_ENCRYPT = CRYPT_S390_KMC | 0x3,
  56. KMC_TDEA_192_DECRYPT = CRYPT_S390_KMC | 0x3 | 0x80,
  57. KMC_AES_128_ENCRYPT = CRYPT_S390_KMC | 0x12,
  58. KMC_AES_128_DECRYPT = CRYPT_S390_KMC | 0x12 | 0x80,
  59. KMC_AES_192_ENCRYPT = CRYPT_S390_KMC | 0x13,
  60. KMC_AES_192_DECRYPT = CRYPT_S390_KMC | 0x13 | 0x80,
  61. KMC_AES_256_ENCRYPT = CRYPT_S390_KMC | 0x14,
  62. KMC_AES_256_DECRYPT = CRYPT_S390_KMC | 0x14 | 0x80,
  63. };
  64. /* function codes for KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST)
  65. * instruction
  66. */
  67. enum crypt_s390_kimd_func {
  68. KIMD_QUERY = CRYPT_S390_KIMD | 0,
  69. KIMD_SHA_1 = CRYPT_S390_KIMD | 1,
  70. KIMD_SHA_256 = CRYPT_S390_KIMD | 2,
  71. };
  72. /* function codes for KLMD (COMPUTE LAST MESSAGE DIGEST)
  73. * instruction
  74. */
  75. enum crypt_s390_klmd_func {
  76. KLMD_QUERY = CRYPT_S390_KLMD | 0,
  77. KLMD_SHA_1 = CRYPT_S390_KLMD | 1,
  78. KLMD_SHA_256 = CRYPT_S390_KLMD | 2,
  79. };
  80. /* function codes for KMAC (COMPUTE MESSAGE AUTHENTICATION CODE)
  81. * instruction
  82. */
  83. enum crypt_s390_kmac_func {
  84. KMAC_QUERY = CRYPT_S390_KMAC | 0,
  85. KMAC_DEA = CRYPT_S390_KMAC | 1,
  86. KMAC_TDEA_128 = CRYPT_S390_KMAC | 2,
  87. KMAC_TDEA_192 = CRYPT_S390_KMAC | 3
  88. };
  89. /* status word for s390 crypto instructions' QUERY functions */
  90. struct crypt_s390_query_status {
  91. u64 high;
  92. u64 low;
  93. };
  94. /*
  95. * Standard fixup and ex_table sections for crypt_s390 inline functions.
  96. * label 0: the s390 crypto operation
  97. * label 1: just after 1 to catch illegal operation exception
  98. * (unsupported model)
  99. * label 6: the return point after fixup
  100. * label 7: set error value if exception _in_ crypto operation
  101. * label 8: set error value if illegal operation exception
  102. * [ret] is the variable to receive the error code
  103. * [ERR] is the error code value
  104. */
  105. #ifndef CONFIG_64BIT
  106. #define __crypt_s390_fixup \
  107. ".section .fixup,\"ax\" \n" \
  108. "7: lhi %0,%h[e1] \n" \
  109. " bras 1,9f \n" \
  110. " .long 6b \n" \
  111. "8: lhi %0,%h[e2] \n" \
  112. " bras 1,9f \n" \
  113. " .long 6b \n" \
  114. "9: l 1,0(1) \n" \
  115. " br 1 \n" \
  116. ".previous \n" \
  117. ".section __ex_table,\"a\" \n" \
  118. " .align 4 \n" \
  119. " .long 0b,7b \n" \
  120. " .long 1b,8b \n" \
  121. ".previous"
  122. #else /* CONFIG_64BIT */
  123. #define __crypt_s390_fixup \
  124. ".section .fixup,\"ax\" \n" \
  125. "7: lhi %0,%h[e1] \n" \
  126. " jg 6b \n" \
  127. "8: lhi %0,%h[e2] \n" \
  128. " jg 6b \n" \
  129. ".previous\n" \
  130. ".section __ex_table,\"a\" \n" \
  131. " .align 8 \n" \
  132. " .quad 0b,7b \n" \
  133. " .quad 1b,8b \n" \
  134. ".previous"
  135. #endif /* CONFIG_64BIT */
  136. /*
  137. * Standard code for setting the result of s390 crypto instructions.
  138. * %0: the register which will receive the result
  139. * [result]: the register containing the result (e.g. second operand length
  140. * to compute number of processed bytes].
  141. */
  142. #ifndef CONFIG_64BIT
  143. #define __crypt_s390_set_result \
  144. " lr %0,%[result] \n"
  145. #else /* CONFIG_64BIT */
  146. #define __crypt_s390_set_result \
  147. " lgr %0,%[result] \n"
  148. #endif
  149. /*
  150. * Executes the KM (CIPHER MESSAGE) operation of the CPU.
  151. * @param func: the function code passed to KM; see crypt_s390_km_func
  152. * @param param: address of parameter block; see POP for details on each func
  153. * @param dest: address of destination memory area
  154. * @param src: address of source memory area
  155. * @param src_len: length of src operand in bytes
  156. * @returns < zero for failure, 0 for the query func, number of processed bytes
  157. * for encryption/decryption funcs
  158. */
  159. static inline int
  160. crypt_s390_km(long func, void* param, u8* dest, const u8* src, long src_len)
  161. {
  162. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  163. register void* __param asm("1") = param;
  164. register u8* __dest asm("4") = dest;
  165. register const u8* __src asm("2") = src;
  166. register long __src_len asm("3") = src_len;
  167. int ret;
  168. ret = 0;
  169. __asm__ __volatile__ (
  170. "0: .insn rre,0xB92E0000,%1,%2 \n" /* KM opcode */
  171. "1: brc 1,0b \n" /* handle partial completion */
  172. __crypt_s390_set_result
  173. "6: \n"
  174. __crypt_s390_fixup
  175. : "+d" (ret), "+a" (__dest), "+a" (__src),
  176. [result] "+d" (__src_len)
  177. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  178. "a" (__param)
  179. : "cc", "memory"
  180. );
  181. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  182. ret = src_len - ret;
  183. }
  184. return ret;
  185. }
  186. /*
  187. * Executes the KMC (CIPHER MESSAGE WITH CHAINING) operation of the CPU.
  188. * @param func: the function code passed to KM; see crypt_s390_kmc_func
  189. * @param param: address of parameter block; see POP for details on each func
  190. * @param dest: address of destination memory area
  191. * @param src: address of source memory area
  192. * @param src_len: length of src operand in bytes
  193. * @returns < zero for failure, 0 for the query func, number of processed bytes
  194. * for encryption/decryption funcs
  195. */
  196. static inline int
  197. crypt_s390_kmc(long func, void* param, u8* dest, const u8* src, long src_len)
  198. {
  199. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  200. register void* __param asm("1") = param;
  201. register u8* __dest asm("4") = dest;
  202. register const u8* __src asm("2") = src;
  203. register long __src_len asm("3") = src_len;
  204. int ret;
  205. ret = 0;
  206. __asm__ __volatile__ (
  207. "0: .insn rre,0xB92F0000,%1,%2 \n" /* KMC opcode */
  208. "1: brc 1,0b \n" /* handle partial completion */
  209. __crypt_s390_set_result
  210. "6: \n"
  211. __crypt_s390_fixup
  212. : "+d" (ret), "+a" (__dest), "+a" (__src),
  213. [result] "+d" (__src_len)
  214. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  215. "a" (__param)
  216. : "cc", "memory"
  217. );
  218. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  219. ret = src_len - ret;
  220. }
  221. return ret;
  222. }
  223. /*
  224. * Executes the KIMD (COMPUTE INTERMEDIATE MESSAGE DIGEST) operation
  225. * of the CPU.
  226. * @param func: the function code passed to KM; see crypt_s390_kimd_func
  227. * @param param: address of parameter block; see POP for details on each func
  228. * @param src: address of source memory area
  229. * @param src_len: length of src operand in bytes
  230. * @returns < zero for failure, 0 for the query func, number of processed bytes
  231. * for digest funcs
  232. */
  233. static inline int
  234. crypt_s390_kimd(long func, void* param, const u8* src, long src_len)
  235. {
  236. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  237. register void* __param asm("1") = param;
  238. register const u8* __src asm("2") = src;
  239. register long __src_len asm("3") = src_len;
  240. int ret;
  241. ret = 0;
  242. __asm__ __volatile__ (
  243. "0: .insn rre,0xB93E0000,%1,%1 \n" /* KIMD opcode */
  244. "1: brc 1,0b \n" /* handle partical completion */
  245. __crypt_s390_set_result
  246. "6: \n"
  247. __crypt_s390_fixup
  248. : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
  249. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  250. "a" (__param)
  251. : "cc", "memory"
  252. );
  253. if (ret >= 0 && (func & CRYPT_S390_FUNC_MASK)){
  254. ret = src_len - ret;
  255. }
  256. return ret;
  257. }
  258. /*
  259. * Executes the KLMD (COMPUTE LAST MESSAGE DIGEST) operation of the CPU.
  260. * @param func: the function code passed to KM; see crypt_s390_klmd_func
  261. * @param param: address of parameter block; see POP for details on each func
  262. * @param src: address of source memory area
  263. * @param src_len: length of src operand in bytes
  264. * @returns < zero for failure, 0 for the query func, number of processed bytes
  265. * for digest funcs
  266. */
  267. static inline int
  268. crypt_s390_klmd(long func, void* param, const u8* src, long src_len)
  269. {
  270. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  271. register void* __param asm("1") = param;
  272. register const u8* __src asm("2") = src;
  273. register long __src_len asm("3") = src_len;
  274. int ret;
  275. ret = 0;
  276. __asm__ __volatile__ (
  277. "0: .insn rre,0xB93F0000,%1,%1 \n" /* KLMD opcode */
  278. "1: brc 1,0b \n" /* handle partical completion */
  279. __crypt_s390_set_result
  280. "6: \n"
  281. __crypt_s390_fixup
  282. : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
  283. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  284. "a" (__param)
  285. : "cc", "memory"
  286. );
  287. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  288. ret = src_len - ret;
  289. }
  290. return ret;
  291. }
  292. /*
  293. * Executes the KMAC (COMPUTE MESSAGE AUTHENTICATION CODE) operation
  294. * of the CPU.
  295. * @param func: the function code passed to KM; see crypt_s390_klmd_func
  296. * @param param: address of parameter block; see POP for details on each func
  297. * @param src: address of source memory area
  298. * @param src_len: length of src operand in bytes
  299. * @returns < zero for failure, 0 for the query func, number of processed bytes
  300. * for digest funcs
  301. */
  302. static inline int
  303. crypt_s390_kmac(long func, void* param, const u8* src, long src_len)
  304. {
  305. register long __func asm("0") = func & CRYPT_S390_FUNC_MASK;
  306. register void* __param asm("1") = param;
  307. register const u8* __src asm("2") = src;
  308. register long __src_len asm("3") = src_len;
  309. int ret;
  310. ret = 0;
  311. __asm__ __volatile__ (
  312. "0: .insn rre,0xB91E0000,%5,%5 \n" /* KMAC opcode */
  313. "1: brc 1,0b \n" /* handle partical completion */
  314. __crypt_s390_set_result
  315. "6: \n"
  316. __crypt_s390_fixup
  317. : "+d" (ret), "+a" (__src), [result] "+d" (__src_len)
  318. : [e1] "K" (-EFAULT), [e2] "K" (-ENOSYS), "d" (__func),
  319. "a" (__param)
  320. : "cc", "memory"
  321. );
  322. if (ret >= 0 && func & CRYPT_S390_FUNC_MASK){
  323. ret = src_len - ret;
  324. }
  325. return ret;
  326. }
  327. /**
  328. * Tests if a specific crypto function is implemented on the machine.
  329. * @param func: the function code of the specific function; 0 if op in general
  330. * @return 1 if func available; 0 if func or op in general not available
  331. */
  332. static inline int
  333. crypt_s390_func_available(int func)
  334. {
  335. int ret;
  336. struct crypt_s390_query_status status = {
  337. .high = 0,
  338. .low = 0
  339. };
  340. switch (func & CRYPT_S390_OP_MASK){
  341. case CRYPT_S390_KM:
  342. ret = crypt_s390_km(KM_QUERY, &status, NULL, NULL, 0);
  343. break;
  344. case CRYPT_S390_KMC:
  345. ret = crypt_s390_kmc(KMC_QUERY, &status, NULL, NULL, 0);
  346. break;
  347. case CRYPT_S390_KIMD:
  348. ret = crypt_s390_kimd(KIMD_QUERY, &status, NULL, 0);
  349. break;
  350. case CRYPT_S390_KLMD:
  351. ret = crypt_s390_klmd(KLMD_QUERY, &status, NULL, 0);
  352. break;
  353. case CRYPT_S390_KMAC:
  354. ret = crypt_s390_kmac(KMAC_QUERY, &status, NULL, 0);
  355. break;
  356. default:
  357. ret = 0;
  358. return ret;
  359. }
  360. if (ret >= 0){
  361. func &= CRYPT_S390_FUNC_MASK;
  362. func &= 0x7f; //mask modifier bit
  363. if (func < 64){
  364. ret = (status.high >> (64 - func - 1)) & 0x1;
  365. } else {
  366. ret = (status.low >> (128 - func - 1)) & 0x1;
  367. }
  368. } else {
  369. ret = 0;
  370. }
  371. return ret;
  372. }
  373. #endif // _CRYPTO_ARCH_S390_CRYPT_S390_H