2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/err.h>
13 #include <openssl/rand.h>
15 #include "s390x_arch.h"
17 /* Size of parameter blocks */
18 #define S390X_SIZE_PARAM 4096
20 /* Size of fields in parameter blocks */
21 #define S390X_SIZE_P256 32
22 #define S390X_SIZE_P384 48
23 #define S390X_SIZE_P521 80
25 /* Offsets of fields in PCC parameter blocks */
26 #define S390X_OFF_RES_X(n) (0 * n)
27 #define S390X_OFF_RES_Y(n) (1 * n)
28 #define S390X_OFF_SRC_X(n) (2 * n)
29 #define S390X_OFF_SRC_Y(n) (3 * n)
30 #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)
41 static int ec_GFp_s390x_nistp_mul(const EC_GROUP *group, EC_POINT *r,
43 size_t num, const EC_POINT *points[],
44 const BIGNUM *scalars[],
45 BN_CTX *ctx, unsigned int fc, int len)
47 unsigned char param[S390X_SIZE_PARAM];
49 const EC_POINT *point_ptr = NULL;
50 const BIGNUM *scalar_ptr = NULL;
51 BN_CTX *new_ctx = NULL;
55 ctx = new_ctx = BN_CTX_new_ex(group->libctx);
64 if (x == NULL || y == NULL) {
70 * Use PCC for EC keygen and ECDH key derivation:
71 * scalar * generator and scalar * peer public key,
72 * scalar in [0,order).
74 if ((scalar != NULL && num == 0 && BN_is_negative(scalar) == 0)
75 || (scalar == NULL && num == 1 && BN_is_negative(scalars[0]) == 0)) {
78 point_ptr = EC_GROUP_get0_generator(group);
81 point_ptr = points[0];
82 scalar_ptr = scalars[0];
85 if (EC_POINT_is_at_infinity(group, point_ptr) == 1
86 || BN_is_zero(scalar_ptr)) {
87 rc = EC_POINT_set_to_infinity(group, r);
91 memset(¶m, 0, sizeof(param));
93 if (group->meth->point_get_affine_coordinates(group, point_ptr,
95 || BN_bn2binpad(x, param + S390X_OFF_SRC_X(len), len) == -1
96 || BN_bn2binpad(y, param + S390X_OFF_SRC_Y(len), len) == -1
97 || BN_bn2binpad(scalar_ptr,
98 param + S390X_OFF_SCALAR(len), len) == -1
99 || s390x_pcc(fc, param) != 0
100 || BN_bin2bn(param + S390X_OFF_RES_X(len), len, x) == NULL
101 || BN_bin2bn(param + S390X_OFF_RES_Y(len), len, y) == NULL
102 || group->meth->point_set_affine_coordinates(group, r,
110 /* Otherwise use default. */
112 rc = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
113 OPENSSL_cleanse(param + S390X_OFF_SCALAR(len), len);
115 BN_CTX_free(new_ctx);
119 static ECDSA_SIG *ecdsa_s390x_nistp_sign_sig(const unsigned char *dgst,
124 unsigned int fc, int len)
126 unsigned char param[S390X_SIZE_PARAM];
130 const EC_GROUP *group;
131 const BIGNUM *privkey;
134 group = EC_KEY_get0_group(eckey);
135 privkey = EC_KEY_get0_private_key(eckey);
136 if (group == NULL || privkey == NULL) {
137 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, EC_R_MISSING_PARAMETERS);
141 if (!EC_KEY_can_sign(eckey)) {
142 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
143 EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
148 sig = ECDSA_SIG_new();
149 if (k == NULL || sig == NULL) {
150 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
156 if (sig->r == NULL || sig->s == NULL) {
157 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
161 memset(param, 0, sizeof(param));
162 off = len - (dgstlen > len ? len : dgstlen);
163 memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
165 if (BN_bn2binpad(privkey, param + S390X_OFF_K(len), len) == -1) {
166 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
170 if (r == NULL || kinv == NULL) {
172 * Generate random k and copy to param param block. RAND_priv_bytes_ex
173 * is used instead of BN_priv_rand_range or BN_generate_dsa_nonce
174 * because kdsa instruction constructs an in-range, invertible nonce
175 * internally implementing counter-measures for RNG weakness.
177 if (RAND_priv_bytes_ex(eckey->libctx, param + S390X_OFF_RN(len),
179 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
180 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
184 /* Reconstruct k = (k^-1)^-1. */
185 if (ec_group_do_inverse_ord(group, k, kinv, NULL) == 0
186 || BN_bn2binpad(k, param + S390X_OFF_RN(len), len) == -1) {
187 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
190 /* Turns KDSA internal nonce-generation off. */
194 if (s390x_kdsa(fc, param, NULL, 0) != 0) {
195 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_ECDSA_LIB);
199 if (BN_bin2bn(param + S390X_OFF_R(len), len, sig->r) == NULL
200 || BN_bin2bn(param + S390X_OFF_S(len), len, sig->s) == NULL) {
201 ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
207 OPENSSL_cleanse(param + S390X_OFF_K(len), 2 * len);
216 static int ecdsa_s390x_nistp_verify_sig(const unsigned char *dgst, int dgstlen,
217 const ECDSA_SIG *sig, EC_KEY *eckey,
218 unsigned int fc, int len)
220 unsigned char param[S390X_SIZE_PARAM];
224 const EC_GROUP *group;
225 const EC_POINT *pubkey;
228 group = EC_KEY_get0_group(eckey);
229 pubkey = EC_KEY_get0_public_key(eckey);
230 if (eckey == NULL || group == NULL || pubkey == NULL || sig == NULL) {
231 ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
235 if (!EC_KEY_can_sign(eckey)) {
236 ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG,
237 EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
241 ctx = BN_CTX_new_ex(group->libctx);
243 ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
251 if (x == NULL || y == NULL) {
252 ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
256 memset(param, 0, sizeof(param));
257 off = len - (dgstlen > len ? len : dgstlen);
258 memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
260 if (group->meth->point_get_affine_coordinates(group, pubkey,
262 || BN_bn2binpad(sig->r, param + S390X_OFF_R(len), len) == -1
263 || BN_bn2binpad(sig->s, param + S390X_OFF_S(len), len) == -1
264 || BN_bn2binpad(x, param + S390X_OFF_X(len), len) == -1
265 || BN_bn2binpad(y, param + S390X_OFF_Y(len), len) == -1) {
266 ECerr(EC_F_ECDSA_S390X_NISTP_VERIFY_SIG, ERR_R_BN_LIB);
270 rc = s390x_kdsa(fc, param, NULL, 0) == 0 ? 1 : 0;
277 #define EC_GFP_S390X_NISTP_METHOD(bits) \
279 static int ec_GFp_s390x_nistp##bits##_mul(const EC_GROUP *group, \
281 const BIGNUM *scalar, \
283 const EC_POINT *points[], \
284 const BIGNUM *scalars[], \
287 return ec_GFp_s390x_nistp_mul(group, r, scalar, num, points, \
289 S390X_SCALAR_MULTIPLY_P##bits, \
290 S390X_SIZE_P##bits); \
293 static ECDSA_SIG *ecdsa_s390x_nistp##bits##_sign_sig(const unsigned \
300 return ecdsa_s390x_nistp_sign_sig(dgst, dgstlen, kinv, r, eckey, \
301 S390X_ECDSA_SIGN_P##bits, \
302 S390X_SIZE_P##bits); \
305 static int ecdsa_s390x_nistp##bits##_verify_sig(const \
306 unsigned char *dgst, \
308 const ECDSA_SIG *sig, \
311 return ecdsa_s390x_nistp_verify_sig(dgst, dgstlen, sig, eckey, \
312 S390X_ECDSA_VERIFY_P##bits, \
313 S390X_SIZE_P##bits); \
316 const EC_METHOD *EC_GFp_s390x_nistp##bits##_method(void) \
318 static const EC_METHOD EC_GFp_s390x_nistp##bits##_meth = { \
319 EC_FLAGS_DEFAULT_OCT, \
320 NID_X9_62_prime_field, \
321 ec_GFp_simple_group_init, \
322 ec_GFp_simple_group_finish, \
323 ec_GFp_simple_group_clear_finish, \
324 ec_GFp_simple_group_copy, \
325 ec_GFp_simple_group_set_curve, \
326 ec_GFp_simple_group_get_curve, \
327 ec_GFp_simple_group_get_degree, \
328 ec_group_simple_order_bits, \
329 ec_GFp_simple_group_check_discriminant, \
330 ec_GFp_simple_point_init, \
331 ec_GFp_simple_point_finish, \
332 ec_GFp_simple_point_clear_finish, \
333 ec_GFp_simple_point_copy, \
334 ec_GFp_simple_point_set_to_infinity, \
335 ec_GFp_simple_set_Jprojective_coordinates_GFp, \
336 ec_GFp_simple_get_Jprojective_coordinates_GFp, \
337 ec_GFp_simple_point_set_affine_coordinates, \
338 ec_GFp_simple_point_get_affine_coordinates, \
339 NULL, /* point_set_compressed_coordinates */ \
340 NULL, /* point2oct */ \
341 NULL, /* oct2point */ \
344 ec_GFp_simple_invert, \
345 ec_GFp_simple_is_at_infinity, \
346 ec_GFp_simple_is_on_curve, \
348 ec_GFp_simple_make_affine, \
349 ec_GFp_simple_points_make_affine, \
350 ec_GFp_s390x_nistp##bits##_mul, \
351 NULL, /* precompute_mult */ \
352 NULL, /* have_precompute_mult */ \
353 ec_GFp_simple_field_mul, \
354 ec_GFp_simple_field_sqr, \
355 NULL, /* field_div */ \
356 ec_GFp_simple_field_inv, \
357 NULL, /* field_encode */ \
358 NULL, /* field_decode */ \
359 NULL, /* field_set_to_one */ \
360 ec_key_simple_priv2oct, \
361 ec_key_simple_oct2priv, \
362 NULL, /* set_private */ \
363 ec_key_simple_generate_key, \
364 ec_key_simple_check_key, \
365 ec_key_simple_generate_public_key, \
366 NULL, /* keycopy */ \
367 NULL, /* keyfinish */ \
368 ecdh_simple_compute_key, \
369 ecdsa_simple_sign_setup, \
370 ecdsa_s390x_nistp##bits##_sign_sig, \
371 ecdsa_s390x_nistp##bits##_verify_sig, \
372 NULL, /* field_inverse_mod_ord */ \
373 ec_GFp_simple_blind_coordinates, \
374 ec_GFp_simple_ladder_pre, \
375 ec_GFp_simple_ladder_step, \
376 ec_GFp_simple_ladder_post \
378 static const EC_METHOD *ret; \
380 if ((OPENSSL_s390xcap_P.pcc[1] \
381 & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_P##bits)) \
382 && (OPENSSL_s390xcap_P.kdsa[0] \
383 & S390X_CAPBIT(S390X_ECDSA_VERIFY_P##bits)) \
384 && (OPENSSL_s390xcap_P.kdsa[0] \
385 & S390X_CAPBIT(S390X_ECDSA_SIGN_P##bits))) \
386 ret = &EC_GFp_s390x_nistp##bits##_meth; \
388 ret = EC_GFp_mont_method(); \
393 EC_GFP_S390X_NISTP_METHOD(256)
394 EC_GFP_S390X_NISTP_METHOD(384)
395 EC_GFP_S390X_NISTP_METHOD(521)