1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/*
* EC_METHOD low level APIs are deprecated for public use, but still ok for
* internal use.
*/
#include "internal/deprecated.h"
#include <stdlib.h>
#include <string.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include "ec_local.h"
#include "s390x_arch.h"
/* Size of parameter blocks */
#define S390X_SIZE_PARAM 4096
/* Size of fields in parameter blocks */
#define S390X_SIZE_P256 32
#define S390X_SIZE_P384 48
#define S390X_SIZE_P521 80
/* Offsets of fields in PCC parameter blocks */
#define S390X_OFF_RES_X(n) (0 * n)
#define S390X_OFF_RES_Y(n) (1 * n)
#define S390X_OFF_SRC_X(n) (2 * n)
#define S390X_OFF_SRC_Y(n) (3 * n)
#define S390X_OFF_SCALAR(n) (4 * n)
/* Offsets of fields in KDSA parameter blocks */
#define S390X_OFF_R(n) (0 * n)
#define S390X_OFF_S(n) (1 * n)
#define S390X_OFF_H(n) (2 * n)
#define S390X_OFF_K(n) (3 * n)
#define S390X_OFF_X(n) (3 * n)
#define S390X_OFF_RN(n) (4 * n)
#define S390X_OFF_Y(n) (4 * n)
static int ec_GFp_s390x_nistp_mul(const EC_GROUP *group, EC_POINT *r,
const BIGNUM *scalar,
size_t num, const EC_POINT *points[],
const BIGNUM *scalars[],
BN_CTX *ctx, unsigned int fc, int len)
{
unsigned char param[S390X_SIZE_PARAM];
BIGNUM *x, *y;
const EC_POINT *point_ptr = NULL;
const BIGNUM *scalar_ptr = NULL;
BN_CTX *new_ctx = NULL;
int rc = -1;
if (ctx == NULL) {
ctx = new_ctx = BN_CTX_new_ex(group->libctx);
if (ctx == NULL)
return 0;
}
BN_CTX_start(ctx);
x = BN_CTX_get(ctx);
y = BN_CTX_get(ctx);
if (x == NULL || y == NULL) {
rc = 0;
goto ret;
}
/*
* Use PCC for EC keygen and ECDH key derivation:
* scalar * generator and scalar * peer public key,
* scalar in [0,order).
*/
if ((scalar != NULL && num == 0 && BN_is_negative(scalar) == 0)
|| (scalar == NULL && num == 1 && BN_is_negative(scalars[0]) == 0)) {
if (num == 0) {
point_ptr = EC_GROUP_get0_generator(group);
scalar_ptr = scalar;
} else {
point_ptr = points[0];
scalar_ptr = scalars[0];
}
if (EC_POINT_is_at_infinity(group, point_ptr) == 1
|| BN_is_zero(scalar_ptr)) {
rc = EC_POINT_set_to_infinity(group, r);
goto ret;
}
memset(¶m, 0, sizeof(param));
if (group->meth->point_get_affine_coordinates(group, point_ptr,
x, y, ctx) != 1
|| BN_bn2binpad(x, param + S390X_OFF_SRC_X(len), len) == -1
|| BN_bn2binpad(y, param + S390X_OFF_SRC_Y(len), len) == -1
|| BN_bn2binpad(scalar_ptr,
param + S390X_OFF_SCALAR(len), len) == -1
|| s390x_pcc(fc, param) != 0
|| BN_bin2bn(param + S390X_OFF_RES_X(len), len, x) == NULL
|| BN_bin2bn(param + S390X_OFF_RES_Y(len), len, y) == NULL
|| group->meth->point_set_affine_coordinates(group, r,
x, y, ctx) != 1)
goto ret;
rc = 1;
}
ret:
/* Otherwise use default. */
if (rc == -1)
rc = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
OPENSSL_cleanse(param + S390X_OFF_SCALAR(len), len);
BN_CTX_end(ctx);
BN_CTX_free(new_ctx);
return rc;
}
static ECDSA_SIG *ecdsa_s390x_nistp_sign_sig(const unsigned char *dgst,
int dgstlen,
const BIGNUM *kinv,
const BIGNUM *r,
EC_KEY *eckey,
unsigned int fc, int len)
{
unsigned char param[S390X_SIZE_PARAM];
int ok = 0;
BIGNUM *k;
ECDSA_SIG *sig;
const EC_GROUP *group;
const BIGNUM *privkey;
int off;
group = EC_KEY_get0_group(eckey);
privkey = EC_KEY_get0_private_key(eckey);
if (group == NULL || privkey == NULL) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, EC_R_MISSING_PARAMETERS);
return NULL;
}
if (!EC_KEY_can_sign(eckey)) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
return NULL;
}
k = BN_secure_new();
sig = ECDSA_SIG_new();
if (k == NULL || sig == NULL) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
goto ret;
}
sig->r = BN_new();
sig->s = BN_new();
if (sig->r == NULL || sig->s == NULL) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_MALLOC_FAILURE);
goto ret;
}
memset(param, 0, sizeof(param));
off = len - (dgstlen > len ? len : dgstlen);
memcpy(param + S390X_OFF_H(len) + off, dgst, len - off);
if (BN_bn2binpad(privkey, param + S390X_OFF_K(len), len) == -1) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
goto ret;
}
if (r == NULL || kinv == NULL) {
/*
* Generate random k and copy to param param block. RAND_priv_bytes_ex
* is used instead of BN_priv_rand_range or BN_generate_dsa_nonce
* because kdsa instruction constructs an in-range, invertible nonce
* internally implementing counter-measures for RNG weakness.
*/
if (RAND_priv_bytes_ex(eckey->libctx, param + S390X_OFF_RN(len),
len) != 1) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG,
EC_R_RANDOM_NUMBER_GENERATION_FAILED);
goto ret;
}
} else {
/* Reconstruct k = (k^-1)^-1. */
if (ec_group_do_inverse_ord(group, k, kinv, NULL) == 0
|| BN_bn2binpad(k, param + S390X_OFF_RN(len), len) == -1) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
goto ret;
}
/* Turns KDSA internal nonce-generation off. */
fc |= S390X_KDSA_D;
}
if (s390x_kdsa(fc, param, NULL, 0) != 0) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_ECDSA_LIB);
goto ret;
}
if (BN_bin2bn(param + S390X_OFF_R(len), len, sig->r) == NULL
|| BN_bin2bn(param + S390X_OFF_S(len), len, sig->s) == NULL) {
ECerr(EC_F_ECDSA_S390X_NISTP_SIGN_SIG, ERR_R_BN_LIB);
goto ret;
}
ok = 1;
ret:
OPENSSL_cleanse(param + S390X_OFF_K(len), 2 * len);
if (ok != 1) {
ECDSA_SIG_free(sig);
sig = NULL;
}
BN_clear_free(k);
return sig;
}
static int ecdsa_s390x_nistp_verify_sig(const unsigned char *dgst,
|