ecp_s390x_nistp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 2019-2020 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 <stdlib.h>
  10. #include <string.h>
  11. #include <openssl/err.h>
  12. #include <openssl/rand.h>
  13. #include "ec_local.h"
  14. #include "s390x_arch.h"
  15. /* Size of parameter blocks */
  16. #define S390X_SIZE_PARAM 4096
  17. /* Size of fields in parameter blocks */
  18. #define S390X_SIZE_P256 32
  19. #define S390X_SIZE_P384 48
  20. #define S390X_SIZE_P521 80
  21. /* Offsets of fields in PCC parameter blocks */
  22. #define S390X_OFF_RES_X(n) (0 * n)
  23. #define S390X_OFF_RES_Y(n) (1 * n)
  24. #define S390X_OFF_SRC_X(n) (2 * n)
  25. #define S390X_OFF_SRC_Y(n) (3 * n)
  26. #define S390X_OFF_SCALAR(n) (4 * n)
  27. /* Offsets of fields in KDSA parameter blocks */
  28. #define S390X_OFF_R(n) (0 * n)
  29. #define S390X_OFF_S(n) (1 * n)
  30. #define S390X_OFF_H(n) (2 * n)
  31. #define S390X_OFF_K(n) (3 * n)
  32. #define S390X_OFF_X(n) (3 * n)
  33. #define S390X_OFF_RN(n) (4 * n)
  34. #define S390X_OFF_Y(n) (4 * n)
  35. static int ec_GFp_s390x_nistp_mul(const EC_GROUP *group, EC_POINT *r,
  36. const BIGNUM *scalar,
  37. size_t num, const EC_POINT *points[],
  38. const BIGNUM *scalars[],
  39. BN_CTX *ctx, unsigned int fc, int len)
  40. {
  41. unsigned char param[S390X_SIZE_PARAM];
  42. BIGNUM *x, *y;
  43. const EC_POINT *point_ptr = NULL;
  44. const BIGNUM *scalar_ptr = NULL;
  45. BN_CTX *new_ctx = NULL;
  46. int rc = -1;
  47. if (ctx == NULL) {
  48. ctx = new_ctx = BN_CTX_new_ex(group->libctx);
  49. if (ctx == NULL)
  50. return 0;
  51. }
  52. BN_CTX_start(ctx);
  53. x = BN_CTX_get(ctx);
  54. y = BN_CTX_get(ctx);
  55. if (x == NULL || y == NULL) {
  56. rc = 0;
  57. goto ret;
  58. }
  59. /*
  60. * Use PCC for EC keygen and ECDH key derivation:
  61. * scalar * generator and scalar * peer public key,
  62. * scalar in [0,order).
  63. */
  64. if ((scalar != NULL && num == 0 && BN_is_negative(scalar) == 0)
  65. || (scalar == NULL && num == 1 && BN_is_negative(scalars[0]) == 0)) {
  66. if (num == 0) {
  67. point_ptr = EC_GROUP_get0_generator(group);
  68. scalar_ptr = scalar;
  69. } else {
  70. point_ptr = points[0];
  71. scalar_ptr = scalars[0];
  72. }
  73. if (EC_POINT_is_at_infinity(group, point_ptr) == 1
  74. || BN_is_zero(scalar_ptr)) {
  75. rc = EC_POINT_set_to_infinity(group, r);
  76. goto ret;
  77. }
  78. memset(&param, 0, sizeof(param));
  79. if (group->meth->point_get_affine_coordinates(group, point_ptr,
  80. x, y, ctx) != 1
  81. || BN_bn2binpad(x, param + S390X_OFF_SRC_X(len), len) == -1
  82. || BN_bn2binpad(y, param + S390X_OFF_SRC_Y(len), len) == -1
  83. || BN_bn2binpad(scalar_ptr,
  84. param + S390X_OFF_SCALAR(len), len) == -1
  85. || s390x_pcc(fc, param) != 0
  86. || BN_bin2bn(param + S390X_OFF_RES_X(len), len, x) == NULL
  87. || BN_bin2bn(param + S390X_OFF_RES_Y(len), len, y) == NULL
  88. || group->meth->point_set_affine_coordinates(group, r,
  89. x, y, ctx) != 1)
  90. goto ret;
  91. rc = 1;
  92. }
  93. ret:
  94. /* Otherwise use default. */
  95. if (rc == -1)
  96. rc = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
  97. OPENSSL_cleanse(param + S390X_OFF_SCALAR(len), len);
  98. BN_CTX_end(ctx);
  99. BN_CTX_free(new_ctx);
  100. return rc;
  101. }
  102. static ECDSA_SIG *ecdsa_s390x_nistp_sign_sig(const unsigned char *dgst,
  103. int dgstlen,
  104. const BIGNUM *kinv,
  105. const BIGNUM *r,
  106. EC_KEY *eckey,
  107. unsigned int fc, int len)
  108. {
  109. unsigned char param[S390X_SIZE_PARAM];
  110. int ok = 0;
  111. BIGNUM *k;
  112. ECDSA_SIG *sig;
  113. const EC_GROUP *group;
  114. const BIGNUM *privkey;
  115. int off;
  116. group = EC_KEY_get0_group(eckey);
  117. privkey = EC_KEY_get0_private_key(eckey);
  118. if (group == NULL || privkey == NULL) {
  119. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, EC_R_MISSING_PARAMETERS);
  120. return NULL;
  121. }
  122. if (!EC_KEY_can_sign(eckey)) {
  123. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
  124. EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  125. return NULL;
  126. }
  127. k = BN_secure_new();
  128. sig = ECDSA_SIG_new();
  129. if (k == NULL || sig == NULL) {
  130. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  131. goto ret;
  132. }
  133. sig->r = BN_new();
  134. sig->s = BN_new();
  135. if (sig->r == NULL || sig->s == NULL) {
  136. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  137. goto ret;
  138. }
  139. memset(param, 0, sizeof(param));
  140. off = len - (dgstlen > len ? len : dgstlen);
  141. memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
  142. if (BN_bn2binpad(privkey, param + S390X_OFF_K(len), len) == -1) {
  143. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
  144. goto ret;
  145. }
  146. if (r == NULL || kinv == NULL) {
  147. /*
  148. * Generate random k and copy to param param block. RAND_priv_bytes_ex
  149. * is used instead of BN_priv_rand_range or BN_generate_dsa_nonce
  150. * because kdsa instruction constructs an in-range, invertible nonce
  151. * internally implementing counter-measures for RNG weakness.
  152. */
  153. if (RAND_priv_bytes_ex(eckey->libctx, param + S390X_OFF_RN(len),
  154. len) != 1) {
  155. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
  156. EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  157. goto ret;
  158. }
  159. } else {
  160. /* Reconstruct k = (k^-1)^-1. */
  161. if (ec_group_do_inverse_ord(group, k, kinv, NULL) == 0
  162. || BN_bn2binpad(k, param + S390X_OFF_RN(len), len) == -1) {
  163. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
  164. goto ret;
  165. }
  166. /* Turns KDSA internal nonce-generation off. */
  167. fc |= S390X_KDSA_D;
  168. }
  169. if (s390x_kdsa(fc, param, NULL, 0) != 0) {
  170. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_ECDSA_LIB);
  171. goto ret;
  172. }
  173. if (BN_bin2bn(param + S390X_OFF_R(len), len, sig->r) == NULL
  174. || BN_bin2bn(param + S390X_OFF_S(len), len, sig->s) == NULL) {
  175. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
  176. goto ret;
  177. }
  178. ok = 1;
  179. ret:
  180. OPENSSL_cleanse(param + S390X_OFF_K(len), 2 * len);
  181. if (ok != 1) {
  182. ECDSA_SIG_free(sig);
  183. sig = NULL;
  184. }
  185. BN_clear_free(k);
  186. return sig;
  187. }
  188. static int ecdsa_s390x_nistp_verify_sig(const unsigned char *dgst, int dgstlen,
  189. const ECDSA_SIG *sig, EC_KEY *eckey,
  190. unsigned int fc, int len)
  191. {
  192. unsigned char param[S390X_SIZE_PARAM];
  193. int rc = -1;
  194. BN_CTX *ctx;
  195. BIGNUM *x, *y;
  196. const EC_GROUP *group;
  197. const EC_POINT *pubkey;
  198. int off;
  199. group = EC_KEY_get0_group(eckey);
  200. pubkey = EC_KEY_get0_public_key(eckey);
  201. if (eckey == NULL || group == NULL || pubkey == NULL || sig == NULL) {
  202. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
  203. return -1;
  204. }
  205. if (!EC_KEY_can_sign(eckey)) {
  206. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG,
  207. EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  208. return -1;
  209. }
  210. ctx = BN_CTX_new_ex(group->libctx);
  211. if (ctx == NULL) {
  212. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
  213. return -1;
  214. }
  215. BN_CTX_start(ctx);
  216. x = BN_CTX_get(ctx);
  217. y = BN_CTX_get(ctx);
  218. if (x == NULL || y == NULL) {
  219. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
  220. goto ret;
  221. }
  222. memset(param, 0, sizeof(param));
  223. off = len - (dgstlen > len ? len : dgstlen);
  224. memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
  225. if (group->meth->point_get_affine_coordinates(group, pubkey,
  226. x, y, ctx) != 1
  227. || BN_bn2binpad(sig->r, param + S390X_OFF_R(len), len) == -1
  228. || BN_bn2binpad(sig->s, param + S390X_OFF_S(len), len) == -1
  229. || BN_bn2binpad(x, param + S390X_OFF_X(len), len) == -1
  230. || BN_bn2binpad(y, param + S390X_OFF_Y(len), len) == -1) {
  231. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_BN_LIB);
  232. goto ret;
  233. }
  234. rc = s390x_kdsa(fc, param, NULL, 0) == 0 ? 1 : 0;
  235. ret:
  236. BN_CTX_end(ctx);
  237. BN_CTX_free(ctx);
  238. return rc;
  239. }
  240. #define EC_GFP_S390X_NISTP_METHOD(bits) \
  241. \
  242. static int ec_GFp_s390x_nistp##bits##_mul(const EC_GROUP *group, \
  243. EC_POINT *r, \
  244. const BIGNUM *scalar, \
  245. size_t num, \
  246. const EC_POINT *points[], \
  247. const BIGNUM *scalars[], \
  248. BN_CTX *ctx) \
  249. { \
  250. return ec_GFp_s390x_nistp_mul(group, r, scalar, num, points, \
  251. scalars, ctx, \
  252. S390X_SCALAR_MULTIPLY_P##bits, \
  253. S390X_SIZE_P##bits); \
  254. } \
  255. \
  256. static ECDSA_SIG *ecdsa_s390x_nistp##bits##_sign_sig(const unsigned \
  257. char *dgst, \
  258. int dgstlen, \
  259. const BIGNUM *kinv,\
  260. const BIGNUM *r, \
  261. EC_KEY *eckey) \
  262. { \
  263. return ecdsa_s390x_nistp_sign_sig(dgst, dgstlen, kinv, r, eckey, \
  264. S390X_ECDSA_SIGN_P##bits, \
  265. S390X_SIZE_P##bits); \
  266. } \
  267. \
  268. static int ecdsa_s390x_nistp##bits##_verify_sig(const \
  269. unsigned char *dgst, \
  270. int dgstlen, \
  271. const ECDSA_SIG *sig, \
  272. EC_KEY *eckey) \
  273. { \
  274. return ecdsa_s390x_nistp_verify_sig(dgst, dgstlen, sig, eckey, \
  275. S390X_ECDSA_VERIFY_P##bits, \
  276. S390X_SIZE_P##bits); \
  277. } \
  278. \
  279. const EC_METHOD *EC_GFp_s390x_nistp##bits##_method(void) \
  280. { \
  281. static const EC_METHOD EC_GFp_s390x_nistp##bits##_meth = { \
  282. EC_FLAGS_DEFAULT_OCT, \
  283. NID_X9_62_prime_field, \
  284. ec_GFp_simple_group_init, \
  285. ec_GFp_simple_group_finish, \
  286. ec_GFp_simple_group_clear_finish, \
  287. ec_GFp_simple_group_copy, \
  288. ec_GFp_simple_group_set_curve, \
  289. ec_GFp_simple_group_get_curve, \
  290. ec_GFp_simple_group_get_degree, \
  291. ec_group_simple_order_bits, \
  292. ec_GFp_simple_group_check_discriminant, \
  293. ec_GFp_simple_point_init, \
  294. ec_GFp_simple_point_finish, \
  295. ec_GFp_simple_point_clear_finish, \
  296. ec_GFp_simple_point_copy, \
  297. ec_GFp_simple_point_set_to_infinity, \
  298. ec_GFp_simple_point_set_affine_coordinates, \
  299. ec_GFp_simple_point_get_affine_coordinates, \
  300. NULL, /* point_set_compressed_coordinates */ \
  301. NULL, /* point2oct */ \
  302. NULL, /* oct2point */ \
  303. ec_GFp_simple_add, \
  304. ec_GFp_simple_dbl, \
  305. ec_GFp_simple_invert, \
  306. ec_GFp_simple_is_at_infinity, \
  307. ec_GFp_simple_is_on_curve, \
  308. ec_GFp_simple_cmp, \
  309. ec_GFp_simple_make_affine, \
  310. ec_GFp_simple_points_make_affine, \
  311. ec_GFp_s390x_nistp##bits##_mul, \
  312. NULL, /* precompute_mult */ \
  313. NULL, /* have_precompute_mult */ \
  314. ec_GFp_simple_field_mul, \
  315. ec_GFp_simple_field_sqr, \
  316. NULL, /* field_div */ \
  317. ec_GFp_simple_field_inv, \
  318. NULL, /* field_encode */ \
  319. NULL, /* field_decode */ \
  320. NULL, /* field_set_to_one */ \
  321. ec_key_simple_priv2oct, \
  322. ec_key_simple_oct2priv, \
  323. NULL, /* set_private */ \
  324. ec_key_simple_generate_key, \
  325. ec_key_simple_check_key, \
  326. ec_key_simple_generate_public_key, \
  327. NULL, /* keycopy */ \
  328. NULL, /* keyfinish */ \
  329. ecdh_simple_compute_key, \
  330. ecdsa_simple_sign_setup, \
  331. ecdsa_s390x_nistp##bits##_sign_sig, \
  332. ecdsa_s390x_nistp##bits##_verify_sig, \
  333. NULL, /* field_inverse_mod_ord */ \
  334. ec_GFp_simple_blind_coordinates, \
  335. ec_GFp_simple_ladder_pre, \
  336. ec_GFp_simple_ladder_step, \
  337. ec_GFp_simple_ladder_post \
  338. }; \
  339. static const EC_METHOD *ret; \
  340. \
  341. if ((OPENSSL_s390xcap_P.pcc[1] \
  342. & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P##bits)) \
  343. && (OPENSSL_s390xcap_P.kdsa[0] \
  344. & S390X_CAPBIT(S390X_ECDSA_VERIFY_P##bits)) \
  345. && (OPENSSL_s390xcap_P.kdsa[0] \
  346. & S390X_CAPBIT(S390X_ECDSA_SIGN_P##bits))) \
  347. ret = &EC_GFp_s390x_nistp##bits##_meth; \
  348. else \
  349. ret = EC_GFp_mont_method(); \
  350. \
  351. return ret; \
  352. }
  353. EC_GFP_S390X_NISTP_METHOD(256)
  354. EC_GFP_S390X_NISTP_METHOD(384)
  355. EC_GFP_S390X_NISTP_METHOD(521)