ecp_s390x_nistp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright 2019 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
  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(param + S390X_OFF_RN(len), len) != 1) {
  154. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
  155. EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  156. goto ret;
  157. }
  158. } else {
  159. /* Reconstruct k = (k^-1)^-1. */
  160. if (ec_group_do_inverse_ord(group, k, kinv, NULL) == 0
  161. || BN_bn2binpad(k, param + S390X_OFF_RN(len), len) == -1) {
  162. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
  163. goto ret;
  164. }
  165. /* Turns KDSA internal nonce-generation off. */
  166. fc |= S390X_KDSA_D;
  167. }
  168. if (s390x_kdsa(fc, param, NULL, 0) != 0) {
  169. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_ECDSA_LIB);
  170. goto ret;
  171. }
  172. if (BN_bin2bn(param + S390X_OFF_R(len), len, sig->r) == NULL
  173. || BN_bin2bn(param + S390X_OFF_S(len), len, sig->s) == NULL) {
  174. ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
  175. goto ret;
  176. }
  177. ok = 1;
  178. ret:
  179. OPENSSL_cleanse(param + S390X_OFF_K(len), 2 * len);
  180. if (ok != 1) {
  181. ECDSA_SIG_free(sig);
  182. sig = NULL;
  183. }
  184. BN_clear_free(k);
  185. return sig;
  186. }
  187. static int ecdsa_s390x_nistp_verify_sig(const unsigned char *dgst, int dgstlen,
  188. const ECDSA_SIG *sig, EC_KEY *eckey,
  189. unsigned int fc, int len)
  190. {
  191. unsigned char param[S390X_SIZE_PARAM];
  192. int rc = -1;
  193. BN_CTX *ctx;
  194. BIGNUM *x, *y;
  195. const EC_GROUP *group;
  196. const EC_POINT *pubkey;
  197. int off;
  198. group = EC_KEY_get0_group(eckey);
  199. pubkey = EC_KEY_get0_public_key(eckey);
  200. if (eckey == NULL || group == NULL || pubkey == NULL || sig == NULL) {
  201. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
  202. return -1;
  203. }
  204. if (!EC_KEY_can_sign(eckey)) {
  205. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG,
  206. EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  207. return -1;
  208. }
  209. ctx = BN_CTX_new_ex(group->libctx);
  210. if (ctx == NULL) {
  211. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
  212. return -1;
  213. }
  214. BN_CTX_start(ctx);
  215. x = BN_CTX_get(ctx);
  216. y = BN_CTX_get(ctx);
  217. if (x == NULL || y == NULL) {
  218. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
  219. goto ret;
  220. }
  221. memset(param, 0, sizeof(param));
  222. off = len - (dgstlen > len ? len : dgstlen);
  223. memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
  224. if (group->meth->point_get_affine_coordinates(group, pubkey,
  225. x, y, ctx) != 1
  226. || BN_bn2binpad(sig->r, param + S390X_OFF_R(len), len) == -1
  227. || BN_bn2binpad(sig->s, param + S390X_OFF_S(len), len) == -1
  228. || BN_bn2binpad(x, param + S390X_OFF_X(len), len) == -1
  229. || BN_bn2binpad(y, param + S390X_OFF_Y(len), len) == -1) {
  230. ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_BN_LIB);
  231. goto ret;
  232. }
  233. rc = s390x_kdsa(fc, param, NULL, 0) == 0 ? 1 : 0;
  234. ret:
  235. BN_CTX_end(ctx);
  236. BN_CTX_free(ctx);
  237. return rc;
  238. }
  239. #define EC_GFP_S390X_NISTP_METHOD(bits) \
  240. \
  241. static int ec_GFp_s390x_nistp##bits##_mul(const EC_GROUP *group, \
  242. EC_POINT *r, \
  243. const BIGNUM *scalar, \
  244. size_t num, \
  245. const EC_POINT *points[], \
  246. const BIGNUM *scalars[], \
  247. BN_CTX *ctx) \
  248. { \
  249. return ec_GFp_s390x_nistp_mul(group, r, scalar, num, points, \
  250. scalars, ctx, \
  251. S390X_SCALAR_MULTIPLY_P##bits, \
  252. S390X_SIZE_P##bits); \
  253. } \
  254. \
  255. static ECDSA_SIG *ecdsa_s390x_nistp##bits##_sign_sig(const unsigned \
  256. char *dgst, \
  257. int dgstlen, \
  258. const BIGNUM *kinv,\
  259. const BIGNUM *r, \
  260. EC_KEY *eckey) \
  261. { \
  262. return ecdsa_s390x_nistp_sign_sig(dgst, dgstlen, kinv, r, eckey, \
  263. S390X_ECDSA_SIGN_P##bits, \
  264. S390X_SIZE_P##bits); \
  265. } \
  266. \
  267. static int ecdsa_s390x_nistp##bits##_verify_sig(const \
  268. unsigned char *dgst, \
  269. int dgstlen, \
  270. const ECDSA_SIG *sig, \
  271. EC_KEY *eckey) \
  272. { \
  273. return ecdsa_s390x_nistp_verify_sig(dgst, dgstlen, sig, eckey, \
  274. S390X_ECDSA_VERIFY_P##bits, \
  275. S390X_SIZE_P##bits); \
  276. } \
  277. \
  278. const EC_METHOD *EC_GFp_s390x_nistp##bits##_method(void) \
  279. { \
  280. static const EC_METHOD EC_GFp_s390x_nistp##bits##_meth = { \
  281. EC_FLAGS_DEFAULT_OCT, \
  282. NID_X9_62_prime_field, \
  283. ec_GFp_simple_group_init, \
  284. ec_GFp_simple_group_finish, \
  285. ec_GFp_simple_group_clear_finish, \
  286. ec_GFp_simple_group_copy, \
  287. ec_GFp_simple_group_set_curve, \
  288. ec_GFp_simple_group_get_curve, \
  289. ec_GFp_simple_group_get_degree, \
  290. ec_group_simple_order_bits, \
  291. ec_GFp_simple_group_check_discriminant, \
  292. ec_GFp_simple_point_init, \
  293. ec_GFp_simple_point_finish, \
  294. ec_GFp_simple_point_clear_finish, \
  295. ec_GFp_simple_point_copy, \
  296. ec_GFp_simple_point_set_to_infinity, \
  297. ec_GFp_simple_set_Jprojective_coordinates_GFp, \
  298. ec_GFp_simple_get_Jprojective_coordinates_GFp, \
  299. ec_GFp_simple_point_set_affine_coordinates, \
  300. ec_GFp_simple_point_get_affine_coordinates, \
  301. NULL, /* point_set_compressed_coordinates */ \
  302. NULL, /* point2oct */ \
  303. NULL, /* oct2point */ \
  304. ec_GFp_simple_add, \
  305. ec_GFp_simple_dbl, \
  306. ec_GFp_simple_invert, \
  307. ec_GFp_simple_is_at_infinity, \
  308. ec_GFp_simple_is_on_curve, \
  309. ec_GFp_simple_cmp, \
  310. ec_GFp_simple_make_affine, \
  311. ec_GFp_simple_points_make_affine, \
  312. ec_GFp_s390x_nistp##bits##_mul, \
  313. NULL, /* precompute_mult */ \
  314. NULL, /* have_precompute_mult */ \
  315. ec_GFp_simple_field_mul, \
  316. ec_GFp_simple_field_sqr, \
  317. NULL, /* field_div */ \
  318. ec_GFp_simple_field_inv, \
  319. NULL, /* field_encode */ \
  320. NULL, /* field_decode */ \
  321. NULL, /* field_set_to_one */ \
  322. ec_key_simple_priv2oct, \
  323. ec_key_simple_oct2priv, \
  324. NULL, /* set_private */ \
  325. ec_key_simple_generate_key, \
  326. ec_key_simple_check_key, \
  327. ec_key_simple_generate_public_key, \
  328. NULL, /* keycopy */ \
  329. NULL, /* keyfinish */ \
  330. ecdh_simple_compute_key, \
  331. ecdsa_simple_sign_setup, \
  332. ecdsa_s390x_nistp##bits##_sign_sig, \
  333. ecdsa_s390x_nistp##bits##_verify_sig, \
  334. NULL, /* field_inverse_mod_ord */ \
  335. ec_GFp_simple_blind_coordinates, \
  336. ec_GFp_simple_ladder_pre, \
  337. ec_GFp_simple_ladder_step, \
  338. ec_GFp_simple_ladder_post \
  339. }; \
  340. static const EC_METHOD *ret; \
  341. \
  342. if ((OPENSSL_s390xcap_P.pcc[1] \
  343. & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P##bits)) \
  344. && (OPENSSL_s390xcap_P.kdsa[0] \
  345. & S390X_CAPBIT(S390X_ECDSA_VERIFY_P##bits)) \
  346. && (OPENSSL_s390xcap_P.kdsa[0] \
  347. & S390X_CAPBIT(S390X_ECDSA_SIGN_P##bits))) \
  348. ret = &EC_GFp_s390x_nistp##bits##_meth; \
  349. else \
  350. ret = EC_GFp_mont_method(); \
  351. \
  352. return ret; \
  353. }
  354. EC_GFP_S390X_NISTP_METHOD(256)
  355. EC_GFP_S390X_NISTP_METHOD(384)
  356. EC_GFP_S390X_NISTP_METHOD(521)