2
0

s390xcap.c 32 KB

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