s390xcap.c 29 KB

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