crypt_s390.h 12 KB

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