s390xcap.c 29 KB

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