Debian Codegraph @

mpi/ec-hw-s390x.c

raw
1 /* ec-hw-s390x.c - zSeries ECC acceleration
2 * Copyright (C) 2021 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <errno.h>
24
25 #ifdef HAVE_GCC_INLINE_ASM_S390X
26
27 #include "mpi-internal.h"
28 #include "g10lib.h"
29 #include "context.h"
30 #include "ec-context.h"
31 #include "ec-internal.h"
32
33 #include "../cipher/bufhelp.h"
34 #include "../cipher/asm-inline-s390x.h"
35
36
37 #define S390X_PCC_PARAM_BLOCK_SIZE 4096
38
39
40 extern void reverse_buffer (unsigned char *buffer, unsigned int length);
41
42 static int s390_mul_point_montgomery (mpi_point_t result, gcry_mpi_t scalar,
43 mpi_point_t point, mpi_ec_t ctx,
44 byte *param_block_buf);
45
46
47 static int
48 mpi_copy_to_raw(byte *raw, unsigned int raw_nbytes, gcry_mpi_t a)
49 {
50 unsigned int num_to_zero;
51 unsigned int nbytes;
52 int i, j;
53
54 if (mpi_has_sign (a))
55 return -1;
56
57 if (mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE))
58 {
59 unsigned int nbits;
60 byte *buf;
61
62 buf = mpi_get_opaque (a, &nbits);
63 nbytes = (nbits + 7) / 8;
64
65 if (raw_nbytes < nbytes)
66 return -1;
67
68 num_to_zero = raw_nbytes - nbytes;
69 if (num_to_zero > 0)
70 memset (raw, 0, num_to_zero);
71 if (nbytes > 0)
72 memcpy (raw + num_to_zero, buf, nbytes);
73
74 return 0;
75 }
76
77 nbytes = a->nlimbs * BYTES_PER_MPI_LIMB;
78 if (raw_nbytes < nbytes)
79 return -1;
80
81 num_to_zero = raw_nbytes - nbytes;
82 if (num_to_zero > 0)
83 memset (raw, 0, num_to_zero);
84
85 for (j = a->nlimbs - 1, i = 0; i < a->nlimbs; i++, j--)
86 {
87 buf_put_be64(raw + num_to_zero + i * BYTES_PER_MPI_LIMB, a->d[j]);
88 }
89
90 return 0;
91 }
92
93 int
94 _gcry_s390x_ec_hw_mul_point (mpi_point_t result, gcry_mpi_t scalar,
95 mpi_point_t point, mpi_ec_t ctx)
96 {
97 byte param_block_buf[S390X_PCC_PARAM_BLOCK_SIZE];
98 byte *param_out_x = NULL;
99 byte *param_out_y = NULL;
100 byte *param_in_x = NULL;
101 byte *param_in_y = NULL;
102 byte *param_scalar = NULL;
103 unsigned int field_nbits;
104 unsigned int pcc_func;
105 gcry_mpi_t x, y;
106 gcry_mpi_t d = NULL;
107 int rc = -1;
108
109 if (ctx->name == NULL)
110 return -1;
111
112 if (!(_gcry_get_hw_features () & HWF_S390X_MSA_9))
113 return -1; /* ECC acceleration not supported by HW. */
114
115 if (ctx->model == MPI_EC_MONTGOMERY)
116 return s390_mul_point_montgomery (result, scalar, point, ctx,
117 param_block_buf);
118
119 if (ctx->model == MPI_EC_WEIERSTRASS && ctx->nbits == 256 &&
120 strcmp (ctx->name, "NIST P-256") == 0)
121 {
122 struct pcc_param_block_nistp256_s
123 {
124 byte out_x[256 / 8];
125 byte out_y[256 / 8];
126 byte in_x[256 / 8];
127 byte in_y[256 / 8];
128 byte scalar[256 / 8];
129 byte c_and_ribm[64];
130 } *params = (void *)param_block_buf;
131
132 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
133
134 pcc_func = PCC_FUNCTION_NIST_P256;
135 field_nbits = 256;
136 param_out_x = params->out_x;
137 param_out_y = params->out_y;
138 param_in_x = params->in_x;
139 param_in_y = params->in_y;
140 param_scalar = params->scalar;
141 }
142 else if (ctx->model == MPI_EC_WEIERSTRASS && ctx->nbits == 384 &&
143 strcmp (ctx->name, "NIST P-384") == 0)
144 {
145 struct pcc_param_block_nistp384_s
146 {
147 byte out_x[384 / 8];
148 byte out_y[384 / 8];
149 byte in_x[384 / 8];
150 byte in_y[384 / 8];
151 byte scalar[384 / 8];
152 byte c_and_ribm[64];
153 } *params = (void *)param_block_buf;
154
155 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
156
157 pcc_func = PCC_FUNCTION_NIST_P384;
158 field_nbits = 384;
159 param_out_x = params->out_x;
160 param_out_y = params->out_y;
161 param_in_x = params->in_x;
162 param_in_y = params->in_y;
163 param_scalar = params->scalar;
164 }
165 else if (ctx->model == MPI_EC_WEIERSTRASS && ctx->nbits == 521 &&
166 strcmp (ctx->name, "NIST P-521") == 0)
167 {
168 struct pcc_param_block_nistp521_s
169 {
170 byte out_x[640 / 8]; /* note: first 14 bytes not modified by pcc */
171 byte out_y[640 / 8]; /* note: first 14 bytes not modified by pcc */
172 byte in_x[640 / 8];
173 byte in_y[640 / 8];
174 byte scalar[640 / 8];
175 byte c_and_ribm[64];
176 } *params = (void *)param_block_buf;
177
178 memset (params->out_x, 0, 14);
179 memset (params->out_y, 0, 14);
180 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
181
182 pcc_func = PCC_FUNCTION_NIST_P521;
183 field_nbits = 640;
184 param_out_x = params->out_x;
185 param_out_y = params->out_y;
186 param_in_x = params->in_x;
187 param_in_y = params->in_y;
188 param_scalar = params->scalar;
189 }
190 else if (ctx->model == MPI_EC_EDWARDS && ctx->nbits == 255 &&
191 strcmp (ctx->name, "Ed25519") == 0)
192 {
193 struct pcc_param_block_ed25519_s
194 {
195 byte out_x[256 / 8];
196 byte out_y[256 / 8];
197 byte in_x[256 / 8];
198 byte in_y[256 / 8];
199 byte scalar[256 / 8];
200 byte c_and_ribm[64];
201 } *params = (void *)param_block_buf;
202
203 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
204
205 pcc_func = PCC_FUNCTION_ED25519;
206 field_nbits = 256;
207 param_out_x = params->out_x;
208 param_out_y = params->out_y;
209 param_in_x = params->in_x;
210 param_in_y = params->in_y;
211 param_scalar = params->scalar;
212 }
213 else if (ctx->model == MPI_EC_EDWARDS && ctx->nbits == 448 &&
214 strcmp (ctx->name, "Ed448") == 0)
215 {
216 struct pcc_param_block_ed448_s
217 {
218 byte out_x[512 / 8]; /* note: first 8 bytes not modified by pcc */
219 byte out_y[512 / 8]; /* note: first 8 bytes not modified by pcc */
220 byte in_x[512 / 8];
221 byte in_y[512 / 8];
222 byte scalar[512 / 8];
223 byte c_and_ribm[64];
224 } *params = (void *)param_block_buf;
225
226 memset (params->out_x, 0, 8);
227 memset (params->out_y, 0, 8);
228 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
229
230 pcc_func = PCC_FUNCTION_ED448;
231 field_nbits = 512;
232 param_out_x = params->out_x;
233 param_out_y = params->out_y;
234 param_in_x = params->in_x;
235 param_in_y = params->in_y;
236 param_scalar = params->scalar;
237 }
238
239 if (param_scalar == NULL)
240 return -1; /* No curve match. */
241
242 if (!(pcc_query () & km_function_to_mask (pcc_func)))
243 return -1; /* HW does not support acceleration for this curve. */
244
245 x = mpi_new (ctx->nbits);
246 y = mpi_new (ctx->nbits);
247
248 if (_gcry_mpi_ec_get_affine (x, y, point, ctx) < 0)
249 {
250 /* Point at infinity. */
251 goto out;
252 }
253
254 if (mpi_has_sign (scalar) || mpi_cmp (scalar, ctx->n) >= 0)
255 {
256 d = mpi_is_secure (scalar) ? mpi_snew (ctx->nbits) : mpi_new (ctx->nbits);
257 _gcry_mpi_mod (d, scalar, ctx->n);
258 }
259 else
260 {
261 d = scalar;
262 }
263
264 if (mpi_copy_to_raw (param_in_x, field_nbits / 8, x) < 0)
265 goto out;
266
267 if (mpi_copy_to_raw (param_in_y, field_nbits / 8, y) < 0)
268 goto out;
269
270 if (mpi_copy_to_raw (param_scalar, field_nbits / 8, d) < 0)
271 goto out;
272
273 if (pcc_scalar_multiply (pcc_func, param_block_buf) != 0)
274 goto out;
275
276 _gcry_mpi_set_buffer (result->x, param_out_x, field_nbits / 8, 0);
277 _gcry_mpi_set_buffer (result->y, param_out_y, field_nbits / 8, 0);
278 mpi_set_ui (result->z, 1);
279 mpi_normalize (result->x);
280 mpi_normalize (result->y);
281 if (ctx->model == MPI_EC_EDWARDS)
282 mpi_point_resize (result, ctx);
283
284 rc = 0;
285
286 out:
287 if (d != scalar)
288 mpi_release (d);
289 mpi_release (y);
290 mpi_release (x);
291 wipememory (param_block_buf, S390X_PCC_PARAM_BLOCK_SIZE);
292
293 return rc;
294 }
295
296
297 static int
298 s390_mul_point_montgomery (mpi_point_t result, gcry_mpi_t scalar,
299 mpi_point_t point, mpi_ec_t ctx,
300 byte *param_block_buf)
301 {
302 byte *param_out_x = NULL;
303 byte *param_in_x = NULL;
304 byte *param_scalar = NULL;
305 unsigned int field_nbits;
306 unsigned int pcc_func;
307 gcry_mpi_t x;
308 gcry_mpi_t d = NULL;
309 int rc = -1;
310
311 if (ctx->nbits == 255 && strcmp (ctx->name, "Curve25519") == 0)
312 {
313 struct pcc_param_block_x25519_s
314 {
315 byte out_x[256 / 8];
316 byte in_x[256 / 8];
317 byte scalar[256 / 8];
318 byte c_and_ribm[64];
319 } *params = (void *)param_block_buf;
320
321 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
322
323 pcc_func = PCC_FUNCTION_X25519;
324 field_nbits = 256;
325 param_out_x = params->out_x;
326 param_in_x = params->in_x;
327 param_scalar = params->scalar;
328 }
329 else if (ctx->nbits == 448 && strcmp (ctx->name, "X448") == 0)
330 {
331 struct pcc_param_block_x448_s
332 {
333 byte out_x[512 / 8]; /* note: first 8 bytes not modified by pcc */
334 byte in_x[512 / 8];
335 byte scalar[512 / 8];
336 byte c_and_ribm[64];
337 } *params = (void *)param_block_buf;
338
339 memset (params->out_x, 0, 8);
340 memset (params->c_and_ribm, 0, sizeof(params->c_and_ribm));
341
342 pcc_func = PCC_FUNCTION_X448;
343 field_nbits = 512;
344 param_out_x = params->out_x;
345 param_in_x = params->in_x;
346 param_scalar = params->scalar;
347 }
348
349 if (param_scalar == NULL)
350 return -1; /* No curve match. */
351
352 if (!(pcc_query () & km_function_to_mask (pcc_func)))
353 return -1; /* HW does not support acceleration for this curve. */
354
355 x = mpi_new (ctx->nbits);
356
357 if (mpi_is_opaque (scalar))
358 {
359 const unsigned int pbits = ctx->nbits;
360 unsigned int n;
361 unsigned char *raw;
362
363 raw = _gcry_mpi_get_opaque_copy (scalar, &n);
364 if ((n + 7) / 8 != (pbits + 7) / 8)
365 log_fatal ("scalar size (%d) != prime size (%d)\n",
366 (n + 7) / 8, (pbits + 7) / 8);
367
368 reverse_buffer (raw, (n + 7 ) / 8);
369 if ((pbits % 8))
370 raw[0] &= (1 << (pbits % 8)) - 1;
371 raw[0] |= (1 << ((pbits + 7) % 8));
372 raw[(pbits + 7) / 8 - 1] &= (256 - ctx->h);
373 d = mpi_is_secure (scalar) ? mpi_snew (pbits) : mpi_new (pbits);
374 _gcry_mpi_set_buffer (d, raw, (n + 7) / 8, 0);
375 xfree (raw);
376 }
377 else
378 {
379 d = scalar;
380 }
381
382 if (_gcry_mpi_ec_get_affine (x, NULL, point, ctx) < 0)
383 {
384 /* Point at infinity. */
385 goto out;
386 }
387
388 if (mpi_copy_to_raw (param_in_x, field_nbits / 8, x) < 0)
389 goto out;
390
391 if (mpi_copy_to_raw (param_scalar, field_nbits / 8, d) < 0)
392 goto out;
393
394 if (pcc_scalar_multiply (pcc_func, param_block_buf) != 0)
395 goto out;
396
397 _gcry_mpi_set_buffer (result->x, param_out_x, field_nbits / 8, 0);
398 mpi_set_ui (result->z, 1);
399 mpi_point_resize (result, ctx);
400
401 rc = 0;
402
403 out:
404 if (d != scalar)
405 mpi_release (d);
406 mpi_release (x);
407 wipememory (param_block_buf, S390X_PCC_PARAM_BLOCK_SIZE);
408
409 return rc;
410 }
411
412 #endif /* HAVE_GCC_INLINE_ASM_S390X */