crypt_z990.h 11 KB

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