ecp_s390x_nistp.c 17 KB

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