2
0

s390xcap.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Copyright 2010-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <setjmp.h>
  13. #include <signal.h>
  14. #include "internal/cryptlib.h"
  15. #include "crypto/ctype.h"
  16. #include "s390x_arch.h"
  17. #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  18. # if __GLIBC_PREREQ(2, 16)
  19. # include <sys/auxv.h>
  20. # if defined(HWCAP_S390_STFLE) && defined(HWCAP_S390_VX)
  21. # define OSSL_IMPLEMENT_GETAUXVAL
  22. # endif
  23. # endif
  24. #endif
  25. #define LEN 128
  26. #define STR_(S) #S
  27. #define STR(S) STR_(S)
  28. #define TOK_FUNC(NAME) \
  29. (sscanf(tok_begin, \
  30. " " STR(NAME) " : %" STR(LEN) "[^:] : " \
  31. "%" STR(LEN) "s %" STR(LEN) "s ", \
  32. tok[0], tok[1], tok[2]) == 2) { \
  33. \
  34. off = (tok[0][0] == '~') ? 1 : 0; \
  35. if (sscanf(tok[0] + off, "%llx", &cap->NAME[0]) != 1) \
  36. goto ret; \
  37. if (off) \
  38. cap->NAME[0] = ~cap->NAME[0]; \
  39. \
  40. off = (tok[1][0] == '~') ? 1 : 0; \
  41. if (sscanf(tok[1] + off, "%llx", &cap->NAME[1]) != 1) \
  42. goto ret; \
  43. if (off) \
  44. cap->NAME[1] = ~cap->NAME[1]; \
  45. }
  46. #define TOK_CPU(NAME) \
  47. (sscanf(tok_begin, \
  48. " %" STR(LEN) "s %" STR(LEN) "s ", \
  49. tok[0], tok[1]) == 1 \
  50. && !strcmp(tok[0], #NAME)) { \
  51. memcpy(cap, &NAME, sizeof(*cap)); \
  52. }
  53. #ifndef OSSL_IMPLEMENT_GETAUXVAL
  54. static sigjmp_buf ill_jmp;
  55. static void ill_handler(int sig)
  56. {
  57. siglongjmp(ill_jmp, sig);
  58. }
  59. void OPENSSL_vx_probe(void);
  60. #endif
  61. static const char *env;
  62. static int parse_env(struct OPENSSL_s390xcap_st *cap);
  63. void OPENSSL_s390x_facilities(void);
  64. void OPENSSL_s390x_functions(void);
  65. struct OPENSSL_s390xcap_st OPENSSL_s390xcap_P;
  66. #if defined(__GNUC__) && defined(__linux)
  67. __attribute__ ((visibility("hidden")))
  68. #endif
  69. void OPENSSL_cpuid_setup(void)
  70. {
  71. struct OPENSSL_s390xcap_st cap;
  72. if (OPENSSL_s390xcap_P.stfle[0])
  73. return;
  74. /* set a bit that will not be tested later */
  75. OPENSSL_s390xcap_P.stfle[0] |= S390X_CAPBIT(0);
  76. #if defined(OSSL_IMPLEMENT_GETAUXVAL)
  77. {
  78. const unsigned long hwcap = getauxval(AT_HWCAP);
  79. /* protection against missing store-facility-list-extended */
  80. if (hwcap & HWCAP_S390_STFLE)
  81. OPENSSL_s390x_facilities();
  82. /* protection against disabled vector facility */
  83. if (!(hwcap & HWCAP_S390_VX)) {
  84. OPENSSL_s390xcap_P.stfle[2] &= ~(S390X_CAPBIT(S390X_VX)
  85. | S390X_CAPBIT(S390X_VXD)
  86. | S390X_CAPBIT(S390X_VXE));
  87. }
  88. }
  89. #else
  90. {
  91. sigset_t oset;
  92. struct sigaction ill_act, oact_ill, oact_fpe;
  93. memset(&ill_act, 0, sizeof(ill_act));
  94. ill_act.sa_handler = ill_handler;
  95. sigfillset(&ill_act.sa_mask);
  96. sigdelset(&ill_act.sa_mask, SIGILL);
  97. sigdelset(&ill_act.sa_mask, SIGFPE);
  98. sigdelset(&ill_act.sa_mask, SIGTRAP);
  99. sigprocmask(SIG_SETMASK, &ill_act.sa_mask, &oset);
  100. sigaction(SIGILL, &ill_act, &oact_ill);
  101. sigaction(SIGFPE, &ill_act, &oact_fpe);
  102. /* protection against missing store-facility-list-extended */
  103. if (sigsetjmp(ill_jmp, 1) == 0)
  104. OPENSSL_s390x_facilities();
  105. /* protection against disabled vector facility */
  106. if ((OPENSSL_s390xcap_P.stfle[2] & S390X_CAPBIT(S390X_VX))
  107. && (sigsetjmp(ill_jmp, 1) == 0)) {
  108. OPENSSL_vx_probe();
  109. } else {
  110. OPENSSL_s390xcap_P.stfle[2] &= ~(S390X_CAPBIT(S390X_VX)
  111. | S390X_CAPBIT(S390X_VXD)
  112. | S390X_CAPBIT(S390X_VXE));
  113. }
  114. sigaction(SIGFPE, &oact_fpe, NULL);
  115. sigaction(SIGILL, &oact_ill, NULL);
  116. sigprocmask(SIG_SETMASK, &oset, NULL);
  117. }
  118. #endif
  119. env = getenv("OPENSSL_s390xcap");
  120. if (env != NULL) {
  121. if (!parse_env(&cap))
  122. env = NULL;
  123. }
  124. if (env != NULL) {
  125. OPENSSL_s390xcap_P.stfle[0] &= cap.stfle[0];
  126. OPENSSL_s390xcap_P.stfle[1] &= cap.stfle[1];
  127. OPENSSL_s390xcap_P.stfle[2] &= cap.stfle[2];
  128. }
  129. OPENSSL_s390x_functions(); /* check OPENSSL_s390xcap_P.stfle */
  130. if (env != NULL) {
  131. OPENSSL_s390xcap_P.kimd[0] &= cap.kimd[0];
  132. OPENSSL_s390xcap_P.kimd[1] &= cap.kimd[1];
  133. OPENSSL_s390xcap_P.klmd[0] &= cap.klmd[0];
  134. OPENSSL_s390xcap_P.klmd[1] &= cap.klmd[1];
  135. OPENSSL_s390xcap_P.km[0] &= cap.km[0];
  136. OPENSSL_s390xcap_P.km[1] &= cap.km[1];
  137. OPENSSL_s390xcap_P.kmc[0] &= cap.kmc[0];
  138. OPENSSL_s390xcap_P.kmc[1] &= cap.kmc[1];
  139. OPENSSL_s390xcap_P.kmac[0] &= cap.kmac[0];
  140. OPENSSL_s390xcap_P.kmac[1] &= cap.kmac[1];
  141. OPENSSL_s390xcap_P.kmctr[0] &= cap.kmctr[0];
  142. OPENSSL_s390xcap_P.kmctr[1] &= cap.kmctr[1];
  143. OPENSSL_s390xcap_P.kmo[0] &= cap.kmo[0];
  144. OPENSSL_s390xcap_P.kmo[1] &= cap.kmo[1];
  145. OPENSSL_s390xcap_P.kmf[0] &= cap.kmf[0];
  146. OPENSSL_s390xcap_P.kmf[1] &= cap.kmf[1];
  147. OPENSSL_s390xcap_P.prno[0] &= cap.prno[0];
  148. OPENSSL_s390xcap_P.prno[1] &= cap.prno[1];
  149. OPENSSL_s390xcap_P.kma[0] &= cap.kma[0];
  150. OPENSSL_s390xcap_P.kma[1] &= cap.kma[1];
  151. OPENSSL_s390xcap_P.pcc[0] &= cap.pcc[0];
  152. OPENSSL_s390xcap_P.pcc[1] &= cap.pcc[1];
  153. OPENSSL_s390xcap_P.kdsa[0] &= cap.kdsa[0];
  154. OPENSSL_s390xcap_P.kdsa[1] &= cap.kdsa[1];
  155. }
  156. }
  157. static int parse_env(struct OPENSSL_s390xcap_st *cap)
  158. {
  159. /*-
  160. * CPU model data
  161. * (only the STFLE- and QUERY-bits relevant to libcrypto are set)
  162. */
  163. /*-
  164. * z900 (2000) - z/Architecture POP SA22-7832-00
  165. * Facility detection would fail on real hw (no STFLE).
  166. */
  167. static const struct OPENSSL_s390xcap_st z900 = {
  168. /*.stfle = */{0ULL, 0ULL, 0ULL, 0ULL},
  169. /*.kimd = */{0ULL, 0ULL},
  170. /*.klmd = */{0ULL, 0ULL},
  171. /*.km = */{0ULL, 0ULL},
  172. /*.kmc = */{0ULL, 0ULL},
  173. /*.kmac = */{0ULL, 0ULL},
  174. /*.kmctr = */{0ULL, 0ULL},
  175. /*.kmo = */{0ULL, 0ULL},
  176. /*.kmf = */{0ULL, 0ULL},
  177. /*.prno = */{0ULL, 0ULL},
  178. /*.kma = */{0ULL, 0ULL},
  179. /*.pcc = */{0ULL, 0ULL},
  180. /*.kdsa = */{0ULL, 0ULL},
  181. };
  182. /*-
  183. * z990 (2003) - z/Architecture POP SA22-7832-02
  184. * Implements MSA. Facility detection would fail on real hw (no STFLE).
  185. */
  186. static const struct OPENSSL_s390xcap_st z990 = {
  187. /*.stfle = */{S390X_CAPBIT(S390X_MSA),
  188. 0ULL, 0ULL, 0ULL},
  189. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  190. | S390X_CAPBIT(S390X_SHA_1),
  191. 0ULL},
  192. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  193. | S390X_CAPBIT(S390X_SHA_1),
  194. 0ULL},
  195. /*.km = */{S390X_CAPBIT(S390X_QUERY),
  196. 0ULL},
  197. /*.kmc = */{S390X_CAPBIT(S390X_QUERY),
  198. 0ULL},
  199. /*.kmac = */{S390X_CAPBIT(S390X_QUERY),
  200. 0ULL},
  201. /*.kmctr = */{0ULL, 0ULL},
  202. /*.kmo = */{0ULL, 0ULL},
  203. /*.kmf = */{0ULL, 0ULL},
  204. /*.prno = */{0ULL, 0ULL},
  205. /*.kma = */{0ULL, 0ULL},
  206. /*.pcc = */{0ULL, 0ULL},
  207. /*.kdsa = */{0ULL, 0ULL},
  208. };
  209. /*-
  210. * z9 (2005) - z/Architecture POP SA22-7832-04
  211. * Implements MSA and MSA1.
  212. */
  213. static const struct OPENSSL_s390xcap_st z9 = {
  214. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  215. | S390X_CAPBIT(S390X_STCKF),
  216. 0ULL, 0ULL, 0ULL},
  217. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  218. | S390X_CAPBIT(S390X_SHA_1)
  219. | S390X_CAPBIT(S390X_SHA_256),
  220. 0ULL},
  221. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  222. | S390X_CAPBIT(S390X_SHA_1)
  223. | S390X_CAPBIT(S390X_SHA_256),
  224. 0ULL},
  225. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  226. | S390X_CAPBIT(S390X_AES_128),
  227. 0ULL},
  228. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  229. | S390X_CAPBIT(S390X_AES_128),
  230. 0ULL},
  231. /*.kmac = */{S390X_CAPBIT(S390X_QUERY),
  232. 0ULL},
  233. /*.kmctr = */{0ULL, 0ULL},
  234. /*.kmo = */{0ULL, 0ULL},
  235. /*.kmf = */{0ULL, 0ULL},
  236. /*.prno = */{0ULL, 0ULL},
  237. /*.kma = */{0ULL, 0ULL},
  238. /*.pcc = */{0ULL, 0ULL},
  239. /*.kdsa = */{0ULL, 0ULL},
  240. };
  241. /*-
  242. * z10 (2008) - z/Architecture POP SA22-7832-06
  243. * Implements MSA and MSA1-2.
  244. */
  245. static const struct OPENSSL_s390xcap_st z10 = {
  246. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  247. | S390X_CAPBIT(S390X_STCKF),
  248. 0ULL, 0ULL, 0ULL},
  249. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  250. | S390X_CAPBIT(S390X_SHA_1)
  251. | S390X_CAPBIT(S390X_SHA_256)
  252. | S390X_CAPBIT(S390X_SHA_512),
  253. 0ULL},
  254. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  255. | S390X_CAPBIT(S390X_SHA_1)
  256. | S390X_CAPBIT(S390X_SHA_256)
  257. | S390X_CAPBIT(S390X_SHA_512),
  258. 0ULL},
  259. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  260. | S390X_CAPBIT(S390X_AES_128)
  261. | S390X_CAPBIT(S390X_AES_192)
  262. | S390X_CAPBIT(S390X_AES_256),
  263. 0ULL},
  264. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  265. | S390X_CAPBIT(S390X_AES_128)
  266. | S390X_CAPBIT(S390X_AES_192)
  267. | S390X_CAPBIT(S390X_AES_256),
  268. 0ULL},
  269. /*.kmac = */{S390X_CAPBIT(S390X_QUERY),
  270. 0ULL},
  271. /*.kmctr = */{0ULL, 0ULL},
  272. /*.kmo = */{0ULL, 0ULL},
  273. /*.kmf = */{0ULL, 0ULL},
  274. /*.prno = */{0ULL, 0ULL},
  275. /*.kma = */{0ULL, 0ULL},
  276. /*.pcc = */{0ULL, 0ULL},
  277. /*.kdsa = */{0ULL, 0ULL},
  278. };
  279. /*-
  280. * z196 (2010) - z/Architecture POP SA22-7832-08
  281. * Implements MSA and MSA1-4.
  282. */
  283. static const struct OPENSSL_s390xcap_st z196 = {
  284. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  285. | S390X_CAPBIT(S390X_STCKF),
  286. S390X_CAPBIT(S390X_MSA3)
  287. | S390X_CAPBIT(S390X_MSA4),
  288. 0ULL, 0ULL},
  289. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  290. | S390X_CAPBIT(S390X_SHA_1)
  291. | S390X_CAPBIT(S390X_SHA_256)
  292. | S390X_CAPBIT(S390X_SHA_512),
  293. S390X_CAPBIT(S390X_GHASH)},
  294. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  295. | S390X_CAPBIT(S390X_SHA_1)
  296. | S390X_CAPBIT(S390X_SHA_256)
  297. | S390X_CAPBIT(S390X_SHA_512),
  298. 0ULL},
  299. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  300. | S390X_CAPBIT(S390X_AES_128)
  301. | S390X_CAPBIT(S390X_AES_192)
  302. | S390X_CAPBIT(S390X_AES_256)
  303. | S390X_CAPBIT(S390X_XTS_AES_128)
  304. | S390X_CAPBIT(S390X_XTS_AES_256),
  305. 0ULL},
  306. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  307. | S390X_CAPBIT(S390X_AES_128)
  308. | S390X_CAPBIT(S390X_AES_192)
  309. | S390X_CAPBIT(S390X_AES_256),
  310. 0ULL},
  311. /*.kmac = */{S390X_CAPBIT(S390X_QUERY)
  312. | S390X_CAPBIT(S390X_AES_128)
  313. | S390X_CAPBIT(S390X_AES_192)
  314. | S390X_CAPBIT(S390X_AES_256),
  315. 0ULL},
  316. /*.kmctr = */{S390X_CAPBIT(S390X_QUERY)
  317. | S390X_CAPBIT(S390X_AES_128)
  318. | S390X_CAPBIT(S390X_AES_192)
  319. | S390X_CAPBIT(S390X_AES_256),
  320. 0ULL},
  321. /*.kmo = */{S390X_CAPBIT(S390X_QUERY)
  322. | S390X_CAPBIT(S390X_AES_128)
  323. | S390X_CAPBIT(S390X_AES_192)
  324. | S390X_CAPBIT(S390X_AES_256),
  325. 0ULL},
  326. /*.kmf = */{S390X_CAPBIT(S390X_QUERY)
  327. | S390X_CAPBIT(S390X_AES_128)
  328. | S390X_CAPBIT(S390X_AES_192)
  329. | S390X_CAPBIT(S390X_AES_256),
  330. 0ULL},
  331. /*.prno = */{0ULL, 0ULL},
  332. /*.kma = */{0ULL, 0ULL},
  333. /*.pcc = */{S390X_CAPBIT(S390X_QUERY),
  334. 0ULL},
  335. /*.kdsa = */{0ULL, 0ULL},
  336. };
  337. /*-
  338. * zEC12 (2012) - z/Architecture POP SA22-7832-09
  339. * Implements MSA and MSA1-4.
  340. */
  341. static const struct OPENSSL_s390xcap_st zEC12 = {
  342. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  343. | S390X_CAPBIT(S390X_STCKF),
  344. S390X_CAPBIT(S390X_MSA3)
  345. | S390X_CAPBIT(S390X_MSA4),
  346. 0ULL, 0ULL},
  347. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  348. | S390X_CAPBIT(S390X_SHA_1)
  349. | S390X_CAPBIT(S390X_SHA_256)
  350. | S390X_CAPBIT(S390X_SHA_512),
  351. S390X_CAPBIT(S390X_GHASH)},
  352. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  353. | S390X_CAPBIT(S390X_SHA_1)
  354. | S390X_CAPBIT(S390X_SHA_256)
  355. | S390X_CAPBIT(S390X_SHA_512),
  356. 0ULL},
  357. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  358. | S390X_CAPBIT(S390X_AES_128)
  359. | S390X_CAPBIT(S390X_AES_192)
  360. | S390X_CAPBIT(S390X_AES_256)
  361. | S390X_CAPBIT(S390X_XTS_AES_128)
  362. | S390X_CAPBIT(S390X_XTS_AES_256),
  363. 0ULL},
  364. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  365. | S390X_CAPBIT(S390X_AES_128)
  366. | S390X_CAPBIT(S390X_AES_192)
  367. | S390X_CAPBIT(S390X_AES_256),
  368. 0ULL},
  369. /*.kmac = */{S390X_CAPBIT(S390X_QUERY)
  370. | S390X_CAPBIT(S390X_AES_128)
  371. | S390X_CAPBIT(S390X_AES_192)
  372. | S390X_CAPBIT(S390X_AES_256),
  373. 0ULL},
  374. /*.kmctr = */{S390X_CAPBIT(S390X_QUERY)
  375. | S390X_CAPBIT(S390X_AES_128)
  376. | S390X_CAPBIT(S390X_AES_192)
  377. | S390X_CAPBIT(S390X_AES_256),
  378. 0ULL},
  379. /*.kmo = */{S390X_CAPBIT(S390X_QUERY)
  380. | S390X_CAPBIT(S390X_AES_128)
  381. | S390X_CAPBIT(S390X_AES_192)
  382. | S390X_CAPBIT(S390X_AES_256),
  383. 0ULL},
  384. /*.kmf = */{S390X_CAPBIT(S390X_QUERY)
  385. | S390X_CAPBIT(S390X_AES_128)
  386. | S390X_CAPBIT(S390X_AES_192)
  387. | S390X_CAPBIT(S390X_AES_256),
  388. 0ULL},
  389. /*.prno = */{0ULL, 0ULL},
  390. /*.kma = */{0ULL, 0ULL},
  391. /*.pcc = */{S390X_CAPBIT(S390X_QUERY),
  392. 0ULL},
  393. /*.kdsa = */{0ULL, 0ULL},
  394. };
  395. /*-
  396. * z13 (2015) - z/Architecture POP SA22-7832-10
  397. * Implements MSA and MSA1-5.
  398. */
  399. static const struct OPENSSL_s390xcap_st z13 = {
  400. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  401. | S390X_CAPBIT(S390X_STCKF)
  402. | S390X_CAPBIT(S390X_MSA5),
  403. S390X_CAPBIT(S390X_MSA3)
  404. | S390X_CAPBIT(S390X_MSA4),
  405. S390X_CAPBIT(S390X_VX),
  406. 0ULL},
  407. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  408. | S390X_CAPBIT(S390X_SHA_1)
  409. | S390X_CAPBIT(S390X_SHA_256)
  410. | S390X_CAPBIT(S390X_SHA_512),
  411. S390X_CAPBIT(S390X_GHASH)},
  412. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  413. | S390X_CAPBIT(S390X_SHA_1)
  414. | S390X_CAPBIT(S390X_SHA_256)
  415. | S390X_CAPBIT(S390X_SHA_512),
  416. 0ULL},
  417. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  418. | S390X_CAPBIT(S390X_AES_128)
  419. | S390X_CAPBIT(S390X_AES_192)
  420. | S390X_CAPBIT(S390X_AES_256)
  421. | S390X_CAPBIT(S390X_XTS_AES_128)
  422. | S390X_CAPBIT(S390X_XTS_AES_256),
  423. 0ULL},
  424. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  425. | S390X_CAPBIT(S390X_AES_128)
  426. | S390X_CAPBIT(S390X_AES_192)
  427. | S390X_CAPBIT(S390X_AES_256),
  428. 0ULL},
  429. /*.kmac = */{S390X_CAPBIT(S390X_QUERY)
  430. | S390X_CAPBIT(S390X_AES_128)
  431. | S390X_CAPBIT(S390X_AES_192)
  432. | S390X_CAPBIT(S390X_AES_256),
  433. 0ULL},
  434. /*.kmctr = */{S390X_CAPBIT(S390X_QUERY)
  435. | S390X_CAPBIT(S390X_AES_128)
  436. | S390X_CAPBIT(S390X_AES_192)
  437. | S390X_CAPBIT(S390X_AES_256),
  438. 0ULL},
  439. /*.kmo = */{S390X_CAPBIT(S390X_QUERY)
  440. | S390X_CAPBIT(S390X_AES_128)
  441. | S390X_CAPBIT(S390X_AES_192)
  442. | S390X_CAPBIT(S390X_AES_256),
  443. 0ULL},
  444. /*.kmf = */{S390X_CAPBIT(S390X_QUERY)
  445. | S390X_CAPBIT(S390X_AES_128)
  446. | S390X_CAPBIT(S390X_AES_192)
  447. | S390X_CAPBIT(S390X_AES_256),
  448. 0ULL},
  449. /*.prno = */{S390X_CAPBIT(S390X_QUERY)
  450. | S390X_CAPBIT(S390X_SHA_512_DRNG),
  451. 0ULL},
  452. /*.kma = */{0ULL, 0ULL},
  453. /*.pcc = */{S390X_CAPBIT(S390X_QUERY),
  454. 0ULL},
  455. /*.kdsa = */{0ULL, 0ULL},
  456. };
  457. /*-
  458. * z14 (2017) - z/Architecture POP SA22-7832-11
  459. * Implements MSA and MSA1-8.
  460. */
  461. static const struct OPENSSL_s390xcap_st z14 = {
  462. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  463. | S390X_CAPBIT(S390X_STCKF)
  464. | S390X_CAPBIT(S390X_MSA5),
  465. S390X_CAPBIT(S390X_MSA3)
  466. | S390X_CAPBIT(S390X_MSA4),
  467. S390X_CAPBIT(S390X_VX)
  468. | S390X_CAPBIT(S390X_VXD)
  469. | S390X_CAPBIT(S390X_VXE)
  470. | S390X_CAPBIT(S390X_MSA8),
  471. 0ULL},
  472. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  473. | S390X_CAPBIT(S390X_SHA_1)
  474. | S390X_CAPBIT(S390X_SHA_256)
  475. | S390X_CAPBIT(S390X_SHA_512)
  476. | S390X_CAPBIT(S390X_SHA3_224)
  477. | S390X_CAPBIT(S390X_SHA3_256)
  478. | S390X_CAPBIT(S390X_SHA3_384)
  479. | S390X_CAPBIT(S390X_SHA3_512)
  480. | S390X_CAPBIT(S390X_SHAKE_128)
  481. | S390X_CAPBIT(S390X_SHAKE_256),
  482. S390X_CAPBIT(S390X_GHASH)},
  483. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  484. | S390X_CAPBIT(S390X_SHA_1)
  485. | S390X_CAPBIT(S390X_SHA_256)
  486. | S390X_CAPBIT(S390X_SHA_512)
  487. | S390X_CAPBIT(S390X_SHA3_224)
  488. | S390X_CAPBIT(S390X_SHA3_256)
  489. | S390X_CAPBIT(S390X_SHA3_384)
  490. | S390X_CAPBIT(S390X_SHA3_512)
  491. | S390X_CAPBIT(S390X_SHAKE_128)
  492. | S390X_CAPBIT(S390X_SHAKE_256),
  493. 0ULL},
  494. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  495. | S390X_CAPBIT(S390X_AES_128)
  496. | S390X_CAPBIT(S390X_AES_192)
  497. | S390X_CAPBIT(S390X_AES_256)
  498. | S390X_CAPBIT(S390X_XTS_AES_128)
  499. | S390X_CAPBIT(S390X_XTS_AES_256),
  500. 0ULL},
  501. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  502. | S390X_CAPBIT(S390X_AES_128)
  503. | S390X_CAPBIT(S390X_AES_192)
  504. | S390X_CAPBIT(S390X_AES_256),
  505. 0ULL},
  506. /*.kmac = */{S390X_CAPBIT(S390X_QUERY)
  507. | S390X_CAPBIT(S390X_AES_128)
  508. | S390X_CAPBIT(S390X_AES_192)
  509. | S390X_CAPBIT(S390X_AES_256),
  510. 0ULL},
  511. /*.kmctr = */{S390X_CAPBIT(S390X_QUERY)
  512. | S390X_CAPBIT(S390X_AES_128)
  513. | S390X_CAPBIT(S390X_AES_192)
  514. | S390X_CAPBIT(S390X_AES_256),
  515. 0ULL},
  516. /*.kmo = */{S390X_CAPBIT(S390X_QUERY)
  517. | S390X_CAPBIT(S390X_AES_128)
  518. | S390X_CAPBIT(S390X_AES_192)
  519. | S390X_CAPBIT(S390X_AES_256),
  520. 0ULL},
  521. /*.kmf = */{S390X_CAPBIT(S390X_QUERY)
  522. | S390X_CAPBIT(S390X_AES_128)
  523. | S390X_CAPBIT(S390X_AES_192)
  524. | S390X_CAPBIT(S390X_AES_256),
  525. 0ULL},
  526. /*.prno = */{S390X_CAPBIT(S390X_QUERY)
  527. | S390X_CAPBIT(S390X_SHA_512_DRNG),
  528. S390X_CAPBIT(S390X_TRNG)},
  529. /*.kma = */{S390X_CAPBIT(S390X_QUERY)
  530. | S390X_CAPBIT(S390X_AES_128)
  531. | S390X_CAPBIT(S390X_AES_192)
  532. | S390X_CAPBIT(S390X_AES_256),
  533. 0ULL},
  534. /*.pcc = */{S390X_CAPBIT(S390X_QUERY),
  535. 0ULL},
  536. /*.kdsa = */{0ULL, 0ULL},
  537. };
  538. /*-
  539. * z15 (2019) - z/Architecture POP SA22-7832-12
  540. * Implements MSA and MSA1-9.
  541. */
  542. static const struct OPENSSL_s390xcap_st z15 = {
  543. /*.stfle = */{S390X_CAPBIT(S390X_MSA)
  544. | S390X_CAPBIT(S390X_STCKF)
  545. | S390X_CAPBIT(S390X_MSA5),
  546. S390X_CAPBIT(S390X_MSA3)
  547. | S390X_CAPBIT(S390X_MSA4),
  548. S390X_CAPBIT(S390X_VX)
  549. | S390X_CAPBIT(S390X_VXD)
  550. | S390X_CAPBIT(S390X_VXE)
  551. | S390X_CAPBIT(S390X_MSA8)
  552. | S390X_CAPBIT(S390X_MSA9),
  553. 0ULL},
  554. /*.kimd = */{S390X_CAPBIT(S390X_QUERY)
  555. | S390X_CAPBIT(S390X_SHA_1)
  556. | S390X_CAPBIT(S390X_SHA_256)
  557. | S390X_CAPBIT(S390X_SHA_512)
  558. | S390X_CAPBIT(S390X_SHA3_224)
  559. | S390X_CAPBIT(S390X_SHA3_256)
  560. | S390X_CAPBIT(S390X_SHA3_384)
  561. | S390X_CAPBIT(S390X_SHA3_512)
  562. | S390X_CAPBIT(S390X_SHAKE_128)
  563. | S390X_CAPBIT(S390X_SHAKE_256),
  564. S390X_CAPBIT(S390X_GHASH)},
  565. /*.klmd = */{S390X_CAPBIT(S390X_QUERY)
  566. | S390X_CAPBIT(S390X_SHA_1)
  567. | S390X_CAPBIT(S390X_SHA_256)
  568. | S390X_CAPBIT(S390X_SHA_512)
  569. | S390X_CAPBIT(S390X_SHA3_224)
  570. | S390X_CAPBIT(S390X_SHA3_256)
  571. | S390X_CAPBIT(S390X_SHA3_384)
  572. | S390X_CAPBIT(S390X_SHA3_512)
  573. | S390X_CAPBIT(S390X_SHAKE_128)
  574. | S390X_CAPBIT(S390X_SHAKE_256),
  575. 0ULL},
  576. /*.km = */{S390X_CAPBIT(S390X_QUERY)
  577. | S390X_CAPBIT(S390X_AES_128)
  578. | S390X_CAPBIT(S390X_AES_192)
  579. | S390X_CAPBIT(S390X_AES_256)
  580. | S390X_CAPBIT(S390X_XTS_AES_128)
  581. | S390X_CAPBIT(S390X_XTS_AES_256),
  582. 0ULL},
  583. /*.kmc = */{S390X_CAPBIT(S390X_QUERY)
  584. | S390X_CAPBIT(S390X_AES_128)
  585. | S390X_CAPBIT(S390X_AES_192)
  586. | S390X_CAPBIT(S390X_AES_256),
  587. 0ULL},
  588. /*.kmac = */{S390X_CAPBIT(S390X_QUERY)
  589. | S390X_CAPBIT(S390X_AES_128)
  590. | S390X_CAPBIT(S390X_AES_192)
  591. | S390X_CAPBIT(S390X_AES_256),
  592. 0ULL},
  593. /*.kmctr = */{S390X_CAPBIT(S390X_QUERY)
  594. | S390X_CAPBIT(S390X_AES_128)
  595. | S390X_CAPBIT(S390X_AES_192)
  596. | S390X_CAPBIT(S390X_AES_256),
  597. 0ULL},
  598. /*.kmo = */{S390X_CAPBIT(S390X_QUERY)
  599. | S390X_CAPBIT(S390X_AES_128)
  600. | S390X_CAPBIT(S390X_AES_192)
  601. | S390X_CAPBIT(S390X_AES_256),
  602. 0ULL},
  603. /*.kmf = */{S390X_CAPBIT(S390X_QUERY)
  604. | S390X_CAPBIT(S390X_AES_128)
  605. | S390X_CAPBIT(S390X_AES_192)
  606. | S390X_CAPBIT(S390X_AES_256),
  607. 0ULL},
  608. /*.prno = */{S390X_CAPBIT(S390X_QUERY)
  609. | S390X_CAPBIT(S390X_SHA_512_DRNG),
  610. S390X_CAPBIT(S390X_TRNG)},
  611. /*.kma = */{S390X_CAPBIT(S390X_QUERY)
  612. | S390X_CAPBIT(S390X_AES_128)
  613. | S390X_CAPBIT(S390X_AES_192)
  614. | S390X_CAPBIT(S390X_AES_256),
  615. 0ULL},
  616. /*.pcc = */{S390X_CAPBIT(S390X_QUERY),
  617. S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P256)
  618. | S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P384)
  619. | S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P521)
  620. | S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED25519)
  621. | S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED448)
  622. | S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519)
  623. | S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448)},
  624. /*.kdsa = */{S390X_CAPBIT(S390X_QUERY)
  625. | S390X_CAPBIT(S390X_ECDSA_VERIFY_P256)
  626. | S390X_CAPBIT(S390X_ECDSA_VERIFY_P384)
  627. | S390X_CAPBIT(S390X_ECDSA_VERIFY_P521)
  628. | S390X_CAPBIT(S390X_ECDSA_SIGN_P256)
  629. | S390X_CAPBIT(S390X_ECDSA_SIGN_P384)
  630. | S390X_CAPBIT(S390X_ECDSA_SIGN_P521)
  631. | S390X_CAPBIT(S390X_EDDSA_VERIFY_ED25519)
  632. | S390X_CAPBIT(S390X_EDDSA_VERIFY_ED448)
  633. | S390X_CAPBIT(S390X_EDDSA_SIGN_ED25519)
  634. | S390X_CAPBIT(S390X_EDDSA_SIGN_ED448),
  635. 0ULL},
  636. };
  637. /*-
  638. * z16 (2022) - z/Architecture POP
  639. * Implements MSA and MSA1-9 (same as z15).
  640. */
  641. static const struct OPENSSL_s390xcap_st z16 = z15;
  642. char *tok_begin, *tok_end, *buff, tok[S390X_STFLE_MAX][LEN + 1];
  643. int rc, off, i, n;
  644. buff = malloc(strlen(env) + 1);
  645. if (buff == NULL)
  646. return 0;
  647. rc = 0;
  648. memset(cap, ~0, sizeof(*cap));
  649. strcpy(buff, env);
  650. tok_begin = buff + strspn(buff, ";");
  651. strtok(tok_begin, ";");
  652. tok_end = strtok(NULL, ";");
  653. while (tok_begin != NULL) {
  654. /* stfle token */
  655. if ((n = sscanf(tok_begin,
  656. " stfle : %" STR(LEN) "[^:] : "
  657. "%" STR(LEN) "[^:] : %" STR(LEN) "s ",
  658. tok[0], tok[1], tok[2]))) {
  659. for (i = 0; i < n; i++) {
  660. off = (tok[i][0] == '~') ? 1 : 0;
  661. if (sscanf(tok[i] + off, "%llx", &cap->stfle[i]) != 1)
  662. goto ret;
  663. if (off)
  664. cap->stfle[i] = ~cap->stfle[i];
  665. }
  666. }
  667. /* query function tokens */
  668. else if TOK_FUNC(kimd)
  669. else if TOK_FUNC(klmd)
  670. else if TOK_FUNC(km)
  671. else if TOK_FUNC(kmc)
  672. else if TOK_FUNC(kmac)
  673. else if TOK_FUNC(kmctr)
  674. else if TOK_FUNC(kmo)
  675. else if TOK_FUNC(kmf)
  676. else if TOK_FUNC(prno)
  677. else if TOK_FUNC(kma)
  678. else if TOK_FUNC(pcc)
  679. else if TOK_FUNC(kdsa)
  680. /* CPU model tokens */
  681. else if TOK_CPU(z900)
  682. else if TOK_CPU(z990)
  683. else if TOK_CPU(z9)
  684. else if TOK_CPU(z10)
  685. else if TOK_CPU(z196)
  686. else if TOK_CPU(zEC12)
  687. else if TOK_CPU(z13)
  688. else if TOK_CPU(z14)
  689. else if TOK_CPU(z15)
  690. else if TOK_CPU(z16)
  691. /* whitespace(ignored) or invalid tokens */
  692. else {
  693. while (*tok_begin != '\0') {
  694. if (!ossl_isspace(*tok_begin))
  695. goto ret;
  696. tok_begin++;
  697. }
  698. }
  699. tok_begin = tok_end;
  700. tok_end = strtok(NULL, ";");
  701. }
  702. rc = 1;
  703. ret:
  704. free(buff);
  705. return rc;
  706. }