Debian Codegraph @

cipher/pubkey.c

raw
1 /* pubkey.c - pubkey dispatcher
2 * Copyright (C) 1998, 1999, 2000, 2002, 2003, 2005,
3 * 2007, 2008, 2011 Free Software Foundation, Inc.
4 * Copyright (C) 2013 g10 Code GmbH
5 *
6 * This file is part of Libgcrypt.
7 *
8 * Libgcrypt is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * Libgcrypt is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "g10lib.h"
29 #include "mpi.h"
30 #include "cipher.h"
31 #include "context.h"
32 #include "pubkey-internal.h"
33
34
35 /* This is the list of the public-key algorithms included in
36 Libgcrypt. */
37 static gcry_pk_spec_t * const pubkey_list[] =
38 {
39 #if USE_ECC
40 &_gcry_pubkey_spec_ecc,
41 #endif
42 #if USE_RSA
43 &_gcry_pubkey_spec_rsa,
44 #endif
45 #if USE_DSA
46 &_gcry_pubkey_spec_dsa,
47 #endif
48 #if USE_ELGAMAL
49 &_gcry_pubkey_spec_elg,
50 #endif
51 #if USE_DILITHIUM
52 &_gcry_pubkey_spec_mldsa,
53 #endif
54 &_gcry_pubkey_spec_kem,
55 NULL
56 };
57
58
59 static int
60 map_algo (int algo)
61 {
62 switch (algo)
63 {
64 case GCRY_PK_RSA_E: return GCRY_PK_RSA;
65 case GCRY_PK_RSA_S: return GCRY_PK_RSA;
66 case GCRY_PK_ELG_E: return GCRY_PK_ELG;
67 case GCRY_PK_ECDSA: return GCRY_PK_ECC;
68 case GCRY_PK_EDDSA: return GCRY_PK_ECC;
69 case GCRY_PK_ECDH: return GCRY_PK_ECC;
70 default: return algo;
71 }
72 }
73
74
75 /* Return the spec structure for the public key algorithm ALGO. For
76 an unknown algorithm NULL is returned. */
77 static gcry_pk_spec_t *
78 spec_from_algo (int algo)
79 {
80 int idx;
81 gcry_pk_spec_t *spec;
82
83 algo = map_algo (algo);
84
85 for (idx = 0; (spec = pubkey_list[idx]); idx++)
86 if (algo == spec->algo)
87 return spec;
88 return NULL;
89 }
90
91
92 /* Return the spec structure for the public key algorithm with NAME.
93 For an unknown name NULL is returned. */
94 static gcry_pk_spec_t *
95 spec_from_name (const char *name)
96 {
97 gcry_pk_spec_t *spec;
98 int idx;
99 const char **aliases;
100
101 for (idx=0; (spec = pubkey_list[idx]); idx++)
102 {
103 if (!stricmp (name, spec->name))
104 return spec;
105 for (aliases = spec->aliases; *aliases; aliases++)
106 if (!stricmp (name, *aliases))
107 return spec;
108 }
109
110 return NULL;
111 }
112
113
114
115 /* Given the s-expression SEXP with the first element be either
116 * "private-key" or "public-key" return the spec structure for it. We
117 * look through the list to find a list beginning with "private-key"
118 * or "public-key" - the first one found is used. If WANT_PRIVATE is
119 * set the function will only succeed if a private key has been given.
120 * On success the spec is stored at R_SPEC. On error NULL is stored
121 * at R_SPEC and an error code returned. If R_PARMS is not NULL and
122 * the function returns success, the parameter list below
123 * "private-key" or "public-key" is stored there and the caller must
124 * call gcry_sexp_release on it.
125 */
126 static gcry_err_code_t
127 spec_from_sexp (gcry_sexp_t sexp, int want_private,
128 gcry_pk_spec_t **r_spec, gcry_sexp_t *r_parms)
129 {
130 gcry_sexp_t list, l2;
131 char *name;
132 gcry_pk_spec_t *spec;
133
134 *r_spec = NULL;
135 if (r_parms)
136 *r_parms = NULL;
137
138 /* Check that the first element is valid. If we are looking for a
139 public key but a private key was supplied, we allow the use of
140 the private key anyway. The rationale for this is that the
141 private key is a superset of the public key. */
142 list = sexp_find_token (sexp, want_private? "private-key":"public-key", 0);
143 if (!list && !want_private)
144 list = sexp_find_token (sexp, "private-key", 0);
145 if (!list)
146 return GPG_ERR_INV_OBJ; /* Does not contain a key object. */
147
148 l2 = sexp_cadr (list);
149 sexp_release (list);
150 list = l2;
151 name = sexp_nth_string (list, 0);
152 if (!name)
153 {
154 sexp_release ( list );
155 return GPG_ERR_INV_OBJ; /* Invalid structure of object. */
156 }
157 spec = spec_from_name (name);
158 xfree (name);
159 if (!spec)
160 {
161 sexp_release (list);
162 return GPG_ERR_PUBKEY_ALGO; /* Unknown algorithm. */
163 }
164 *r_spec = spec;
165 if (r_parms)
166 *r_parms = list;
167 else
168 sexp_release (list);
169 return 0;
170 }
171
172
173
174 /* Disable the use of the algorithm ALGO. This is not thread safe and
175 should thus be called early. */
176 static void
177 disable_pubkey_algo (int algo)
178 {
179 gcry_pk_spec_t *spec = spec_from_algo (algo);
180
181 if (spec)
182 spec->flags.disabled = 1;
183 }
184
185
186
187 /*
188 * Map a string to the pubkey algo
189 */
190 int
191 _gcry_pk_map_name (const char *string)
192 {
193 gcry_pk_spec_t *spec;
194
195 if (!string)
196 return 0;
197 spec = spec_from_name (string);
198 if (!spec)
199 return 0;
200 if (spec->flags.disabled)
201 return 0;
202 if (!spec->flags.fips && fips_mode ())
203 return 0;
204 return spec->algo;
205 }
206
207
208 /* Map the public key algorithm whose ID is contained in ALGORITHM to
209 a string representation of the algorithm name. For unknown
210 algorithm IDs this functions returns "?". */
211 const char *
212 _gcry_pk_algo_name (int algo)
213 {
214 gcry_pk_spec_t *spec;
215
216 spec = spec_from_algo (algo);
217 if (spec)
218 return spec->name;
219 return "?";
220 }
221
222
223 /****************
224 * A USE of 0 means: don't care.
225 */
226 static gcry_err_code_t
227 check_pubkey_algo (int algo, unsigned use)
228 {
229 gcry_err_code_t err = 0;
230 gcry_pk_spec_t *spec;
231
232 spec = spec_from_algo (algo);
233 if (spec && !spec->flags.disabled && (spec->flags.fips || !fips_mode ()))
234 {
235 if (((use & GCRY_PK_USAGE_SIGN)
236 && (! (spec->use & GCRY_PK_USAGE_SIGN)))
237 || ((use & GCRY_PK_USAGE_ENCR)
238 && (! (spec->use & GCRY_PK_USAGE_ENCR))))
239 err = GPG_ERR_WRONG_PUBKEY_ALGO;
240 }
241 else
242 err = GPG_ERR_PUBKEY_ALGO;
243
244 return err;
245 }
246
247
248 /****************
249 * Return the number of public key material numbers
250 */
251 static int
252 pubkey_get_npkey (int algo)
253 {
254 gcry_pk_spec_t *spec = spec_from_algo (algo);
255
256 return spec? strlen (spec->elements_pkey) : 0;
257 }
258
259
260 /****************
261 * Return the number of secret key material numbers
262 */
263 static int
264 pubkey_get_nskey (int algo)
265 {
266 gcry_pk_spec_t *spec = spec_from_algo (algo);
267
268 return spec? strlen (spec->elements_skey) : 0;
269 }
270
271
272 /****************
273 * Return the number of signature material numbers
274 */
275 static int
276 pubkey_get_nsig (int algo)
277 {
278 gcry_pk_spec_t *spec = spec_from_algo (algo);
279
280 return spec? strlen (spec->elements_sig) : 0;
281 }
282
283 /****************
284 * Return the number of encryption material numbers
285 */
286 static int
287 pubkey_get_nenc (int algo)
288 {
289 gcry_pk_spec_t *spec = spec_from_algo (algo);
290
291 return spec? strlen (spec->elements_enc) : 0;
292 }
293
294
295
296 /*
297 Do a PK encrypt operation
298
299 Caller has to provide a public key as the SEXP pkey and data as a
300 SEXP with just one MPI in it. Alternatively S_DATA might be a
301 complex S-Expression, similar to the one used for signature
302 verification. This provides a flag which allows handling PKCS#1
303 block type 2 padding. The function returns a sexp which may be
304 passed to to pk_decrypt.
305
306 Returns: 0 or an errorcode.
307
308 s_data = See comment for _gcry_pk_util_data_to_mpi
309 s_pkey = <key-as-defined-in-sexp_to_key>
310 r_ciph = (enc-val
311 (<algo>
312 (<param_name1> <mpi>)
313 ...
314 (<param_namen> <mpi>)
315 ))
316
317 */
318 gcry_err_code_t
319 _gcry_pk_encrypt (gcry_sexp_t *r_ciph, gcry_sexp_t s_data, gcry_sexp_t s_pkey)
320 {
321 gcry_err_code_t rc;
322 gcry_pk_spec_t *spec;
323 gcry_sexp_t keyparms;
324
325 *r_ciph = NULL;
326
327 rc = spec_from_sexp (s_pkey, 0, &spec, &keyparms);
328 if (rc)
329 goto leave;
330
331 if (spec->flags.disabled)
332 {
333 rc = GPG_ERR_PUBKEY_ALGO;
334 goto leave;
335 }
336 else if (!spec->flags.fips && fips_mode ())
337 {
338 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
339 {
340 rc = GPG_ERR_PUBKEY_ALGO;
341 goto leave;
342 }
343 else
344 fips_service_indicator_mark_non_compliant ();
345 }
346
347 if (spec->encrypt)
348 rc = spec->encrypt (r_ciph, s_data, keyparms);
349 else
350 rc = GPG_ERR_NOT_IMPLEMENTED;
351
352 leave:
353 sexp_release (keyparms);
354 return rc;
355 }
356
357
358 /*
359 Do a PK decrypt operation
360
361 Caller has to provide a secret key as the SEXP skey and data in a
362 format as created by gcry_pk_encrypt. For historic reasons the
363 function returns simply an MPI as an S-expression part; this is
364 deprecated and the new method should be used which returns a real
365 S-expressionl this is selected by adding at least an empty flags
366 list to S_DATA.
367
368 Returns: 0 or an errorcode.
369
370 s_data = (enc-val
371 [(flags [raw, pkcs1, oaep])]
372 (<algo>
373 (<param_name1> <mpi>)
374 ...
375 (<param_namen> <mpi>)
376 ))
377 s_skey = <key-as-defined-in-sexp_to_key>
378 r_plain= Either an incomplete S-expression without the parentheses
379 or if the flags list is used (even if empty) a real S-expression:
380 (value PLAIN). In raw mode (or no flags given) the returned value
381 is to be interpreted as a signed MPI, thus it may have an extra
382 leading zero octet even if not included in the original data.
383 With pkcs1 or oaep decoding enabled the returned value is a
384 verbatim octet string.
385 */
386 gcry_err_code_t
387 _gcry_pk_decrypt (gcry_sexp_t *r_plain, gcry_sexp_t s_data, gcry_sexp_t s_skey)
388 {
389 gcry_err_code_t rc;
390 gcry_pk_spec_t *spec;
391 gcry_sexp_t keyparms;
392
393 *r_plain = NULL;
394
395 rc = spec_from_sexp (s_skey, 1, &spec, &keyparms);
396 if (rc)
397 goto leave;
398
399 if (spec->flags.disabled)
400 {
401 rc = GPG_ERR_PUBKEY_ALGO;
402 goto leave;
403 }
404 else if (!spec->flags.fips && fips_mode ())
405 {
406 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
407 {
408 rc = GPG_ERR_PUBKEY_ALGO;
409 goto leave;
410 }
411 else
412 fips_service_indicator_mark_non_compliant ();
413 }
414
415 if (spec->decrypt)
416 rc = spec->decrypt (r_plain, s_data, keyparms);
417 else
418 rc = GPG_ERR_NOT_IMPLEMENTED;
419
420 leave:
421 sexp_release (keyparms);
422 return rc;
423 }
424
425
426
427 /*
428 Create a signature.
429
430 Caller has to provide a secret key as the SEXP skey and data
431 expressed as a SEXP list hash with only one element which should
432 instantly be available as a MPI. Alternatively the structure given
433 below may be used for S_HASH, it provides the abiliy to pass flags
434 to the operation; the flags defined by now are "pkcs1" which does
435 PKCS#1 block type 1 style padding and "pss" for PSS encoding.
436
437 Returns: 0 or an errorcode.
438 In case of 0 the function returns a new SEXP with the
439 signature value; the structure of this signature depends on the
440 other arguments but is always suitable to be passed to
441 gcry_pk_verify
442
443 s_hash = See comment for _gcry-pk_util_data_to_mpi
444
445 s_skey = <key-as-defined-in-sexp_to_key>
446 r_sig = (sig-val
447 (<algo>
448 (<param_name1> <mpi>)
449 ...
450 (<param_namen> <mpi>))
451 [(hash algo)])
452
453 Note that (hash algo) in R_SIG is not used.
454 */
455 gcry_err_code_t
456 _gcry_pk_sign (gcry_sexp_t *r_sig, gcry_sexp_t s_hash, gcry_sexp_t s_skey)
457 {
458 gcry_err_code_t rc;
459 gcry_pk_spec_t *spec;
460 gcry_sexp_t keyparms;
461
462 *r_sig = NULL;
463
464 rc = spec_from_sexp (s_skey, 1, &spec, &keyparms);
465 if (rc)
466 goto leave;
467
468 if (spec->flags.disabled)
469 {
470 rc = GPG_ERR_PUBKEY_ALGO;
471 goto leave;
472 }
473 else if (!spec->flags.fips && fips_mode ())
474 {
475 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
476 {
477 rc = GPG_ERR_PUBKEY_ALGO;
478 goto leave;
479 }
480 else
481 fips_service_indicator_mark_non_compliant ();
482 }
483
484 if (spec->sign)
485 rc = spec->sign (r_sig, s_hash, keyparms);
486 else
487 rc = GPG_ERR_NOT_IMPLEMENTED;
488
489 leave:
490 sexp_release (keyparms);
491 return rc;
492 }
493
494
495 #define MAX_CONTEXTS 2 /* Currently, input data and random_override */
496
497 static gcry_err_code_t
498 prepare_datasexp_to_be_signed (const char *tmpl, gcry_md_hd_t hd,
499 gcry_ctx_t ctx, gcry_sexp_t *s_data_p)
500 {
501 const char *s;
502 const char *digest_name = NULL;
503 const unsigned char *digest;
504 int digest_size;
505 int algo;
506 gcry_err_code_t rc;
507
508 if (hd == NULL)
509 {
510 const unsigned char *data[MAX_CONTEXTS];
511 int data_size[MAX_CONTEXTS];
512 int i = 0;
513 void *argv[MAX_CONTEXTS*2];
514
515 while (1)
516 {
517 size_t len;
518
519 rc = _gcry_pk_get_single_data (&ctx, &data[i/2], &len);
520 if (rc)
521 return rc;
522
523 data_size[i/2] = (int)len;
524
525 argv[i] = (void *)&data_size[i/2];
526 argv[i+1] = (void *)&data[i/2];
527
528 i += 2;
529
530 if (!ctx)
531 break;
532
533 if (i >= MAX_CONTEXTS*2)
534 return GPG_ERR_EINVAL;
535 }
536
537 rc = _gcry_sexp_build_array (s_data_p, NULL, tmpl, argv);
538 return rc;
539 }
540
541 /* Check if it has fixed hash name or %s */
542 s = strstr (tmpl, "(hash ");
543 if (s == NULL)
544 {
545 _gcry_md_close (hd);
546 return GPG_ERR_DIGEST_ALGO;
547 }
548
549 s += 6;
550 if (!strncmp (s, "%s", 2))
551 {
552 algo = _gcry_md_get_algo (hd);
553
554 if (fips_mode () && algo == GCRY_MD_SHA1)
555 {
556 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
557 {
558 _gcry_md_close (hd);
559 return GPG_ERR_DIGEST_ALGO;
560 }
561 else
562 fips_service_indicator_mark_non_compliant ();
563 }
564
565 digest_name = _gcry_md_algo_name (algo);
566 digest_size = (int)_gcry_md_get_algo_dlen (algo);
567 digest = _gcry_md_read (hd, 0);
568 }
569 else
570 {
571 const char *p;
572 char *digest_name_supplied;
573
574 for (p = s; *p && *p != ' '; p++)
575 ;
576
577 digest_name_supplied = xtrymalloc (p - s + 1);
578 if (!digest_name_supplied)
579 {
580 rc = gpg_err_code_from_syserror ();
581 _gcry_md_close (hd);
582 return rc;
583 }
584 memcpy (digest_name_supplied, s, p - s);
585 digest_name_supplied[p - s] = 0;
586
587 algo = _gcry_md_map_name (digest_name_supplied);
588 xfree (digest_name_supplied);
589 if (algo == 0)
590 {
591 _gcry_md_close (hd);
592 return GPG_ERR_DIGEST_ALGO;
593 }
594 else if (fips_mode () && algo == GCRY_MD_SHA1)
595 {
596 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
597 {
598 _gcry_md_close (hd);
599 return GPG_ERR_DIGEST_ALGO;
600 }
601 else
602 fips_service_indicator_mark_non_compliant ();
603 }
604
605 digest_size = (int)_gcry_md_get_algo_dlen (algo);
606 digest = _gcry_md_read (hd, algo);
607 }
608
609 if (!digest)
610 {
611 _gcry_md_close (hd);
612 return GPG_ERR_NOT_IMPLEMENTED;
613 }
614
615 if (!ctx)
616 {
617 if (!digest_name)
618 rc = _gcry_sexp_build (s_data_p, NULL, tmpl,
619 digest_size, digest);
620 else
621 rc = _gcry_sexp_build (s_data_p, NULL, tmpl, digest_name,
622 digest_size, digest);
623 }
624 else
625 {
626 const unsigned char *p;
627 size_t len;
628
629 rc = _gcry_pk_get_single_data (&ctx, &p, &len);
630 if (rc)
631 return rc;
632
633 if (!digest_name)
634 rc = _gcry_sexp_build (s_data_p, NULL, tmpl,
635 digest_size, digest, (int) len, p);
636 else
637 rc = _gcry_sexp_build (s_data_p, NULL, tmpl, digest_name,
638 digest_size, digest, (int) len, p);
639 }
640
641 _gcry_md_close (hd);
642 return rc;
643 }
644
645
646 gcry_err_code_t
647 _gcry_pk_sign_md (gcry_sexp_t *r_sig, const char *tmpl, gcry_md_hd_t hd_orig,
648 gcry_sexp_t s_skey, gcry_ctx_t ctx)
649 {
650 gcry_err_code_t rc;
651 gcry_pk_spec_t *spec;
652 gcry_sexp_t keyparms = NULL;
653 gcry_sexp_t s_data = NULL;
654 gcry_error_t err;
655 gcry_md_hd_t hd;
656
657 *r_sig = NULL;
658
659 if (!hd_orig)
660 hd = NULL;
661 else
662 {
663 err = _gcry_md_copy (&hd, hd_orig);
664 if (err)
665 return gpg_err_code (err);
666 }
667
668 rc = prepare_datasexp_to_be_signed (tmpl, hd, ctx, &s_data);
669 if (rc)
670 return rc;
671
672 rc = spec_from_sexp (s_skey, 1, &spec, &keyparms);
673 if (rc)
674 goto leave;
675
676 if (spec->flags.disabled)
677 {
678 rc = GPG_ERR_PUBKEY_ALGO;
679 goto leave;
680 }
681 else if (!spec->flags.fips && fips_mode ())
682 {
683 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
684 {
685 rc = GPG_ERR_PUBKEY_ALGO;
686 goto leave;
687 }
688 else
689 fips_service_indicator_mark_non_compliant ();
690 }
691
692 if (spec->sign)
693 rc = spec->sign (r_sig, s_data, keyparms);
694 else
695 rc = GPG_ERR_NOT_IMPLEMENTED;
696
697 leave:
698 sexp_release (s_data);
699 sexp_release (keyparms);
700 return rc;
701 }
702
703
704 /*
705 Verify a signature.
706
707 Caller has to supply the public key pkey, the signature sig and his
708 hashvalue data. Public key has to be a standard public key given
709 as an S-Exp, sig is a S-Exp as returned from gcry_pk_sign and data
710 must be an S-Exp like the one in sign too. */
711 gcry_err_code_t
712 _gcry_pk_verify (gcry_sexp_t s_sig, gcry_sexp_t s_hash, gcry_sexp_t s_pkey)
713 {
714 gcry_err_code_t rc;
715 gcry_pk_spec_t *spec;
716 gcry_sexp_t keyparms;
717
718 rc = spec_from_sexp (s_pkey, 0, &spec, &keyparms);
719 if (rc)
720 goto leave;
721
722 if (spec->flags.disabled)
723 {
724 rc = GPG_ERR_PUBKEY_ALGO;
725 goto leave;
726 }
727 else if (!spec->flags.fips && fips_mode ())
728 {
729 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
730 {
731 rc = GPG_ERR_PUBKEY_ALGO;
732 goto leave;
733 }
734 else
735 fips_service_indicator_mark_non_compliant ();
736 }
737
738 if (spec->verify)
739 rc = spec->verify (s_sig, s_hash, keyparms);
740 else
741 rc = GPG_ERR_NOT_IMPLEMENTED;
742
743 leave:
744 sexp_release (keyparms);
745 return rc;
746 }
747
748
749 gcry_err_code_t
750 _gcry_pk_verify_md (gcry_sexp_t s_sig, const char *tmpl, gcry_md_hd_t hd_orig,
751 gcry_sexp_t s_pkey, gcry_ctx_t ctx)
752 {
753 gcry_err_code_t rc;
754 gcry_pk_spec_t *spec;
755 gcry_sexp_t keyparms = NULL;
756 gcry_sexp_t s_data = NULL;
757 gcry_error_t err;
758 gcry_md_hd_t hd;
759
760 if (!hd_orig)
761 hd = NULL;
762 else
763 {
764 err = _gcry_md_copy (&hd, hd_orig);
765 if (err)
766 return gpg_err_code (err);
767 }
768
769 rc = prepare_datasexp_to_be_signed (tmpl, hd, ctx, &s_data);
770 if (rc)
771 return rc;
772
773 rc = spec_from_sexp (s_pkey, 0, &spec, &keyparms);
774 if (rc)
775 goto leave;
776
777 if (spec->flags.disabled)
778 {
779 rc = GPG_ERR_PUBKEY_ALGO;
780 goto leave;
781 }
782 else if (!spec->flags.fips && fips_mode ())
783 {
784 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
785 {
786 rc = GPG_ERR_PUBKEY_ALGO;
787 goto leave;
788 }
789 else
790 fips_service_indicator_mark_non_compliant ();
791 }
792
793 if (spec->verify)
794 rc = spec->verify (s_sig, s_data, keyparms);
795 else
796 rc = GPG_ERR_NOT_IMPLEMENTED;
797
798 leave:
799 sexp_release (s_data);
800 sexp_release (keyparms);
801 return rc;
802 }
803
804
805 /*
806 Test a key.
807
808 This may be used either for a public or a secret key to see whether
809 the internal structure is okay.
810
811 Returns: 0 or an errorcode.
812
813 NOTE: We currently support only secret key checking. */
814 gcry_err_code_t
815 _gcry_pk_testkey (gcry_sexp_t s_key)
816 {
817 gcry_err_code_t rc;
818 gcry_pk_spec_t *spec;
819 gcry_sexp_t keyparms;
820
821 rc = spec_from_sexp (s_key, 1, &spec, &keyparms);
822 if (rc)
823 goto leave;
824
825 if (spec->flags.disabled)
826 {
827 rc = GPG_ERR_PUBKEY_ALGO;
828 goto leave;
829 }
830 else if (!spec->flags.fips && fips_mode ())
831 {
832 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
833 {
834 rc = GPG_ERR_PUBKEY_ALGO;
835 goto leave;
836 }
837 else
838 fips_service_indicator_mark_non_compliant ();
839 }
840
841 if (spec->check_secret_key)
842 rc = spec->check_secret_key (keyparms);
843 else
844 rc = GPG_ERR_NOT_IMPLEMENTED;
845
846 leave:
847 sexp_release (keyparms);
848 return rc;
849 }
850
851
852
853 /* Create a public key pair and return it as R_KEY.
854 * How the key is created depends on s_parms:
855 *
856 * (genkey
857 * (algo
858 * (parameter_name_1 ....)
859 * ....
860 * (parameter_name_n ....)))
861 *
862 * The key is returned in a format depending on the algorithm. Both,
863 * private and secret keys are returned and optionally some additional
864 * information. For example for Elgamal this structure is returned:
865 *
866 * (key-data
867 * (public-key
868 * (elg
869 * (p <mpi>)
870 * (g <mpi>)
871 * (y <mpi>)))
872 * (private-key
873 * (elg
874 * (p <mpi>)
875 * (g <mpi>)
876 * (y <mpi>)
877 * (x <mpi>)))
878 * (misc-key-info
879 * (pm1-factors n1 n2 ... nn)))
880 */
881 gcry_err_code_t
882 _gcry_pk_genkey (gcry_sexp_t *r_key, gcry_sexp_t s_parms)
883 {
884 gcry_pk_spec_t *spec = NULL;
885 gcry_sexp_t list = NULL;
886 gcry_sexp_t l2 = NULL;
887 char *name = NULL;
888 gcry_err_code_t rc;
889
890 *r_key = NULL;
891
892 list = sexp_find_token (s_parms, "genkey", 0);
893 if (!list)
894 {
895 rc = GPG_ERR_INV_OBJ; /* Does not contain genkey data. */
896 goto leave;
897 }
898
899 l2 = sexp_cadr (list);
900 sexp_release (list);
901 list = l2;
902 l2 = NULL;
903 if (! list)
904 {
905 rc = GPG_ERR_NO_OBJ; /* No cdr for the genkey. */
906 goto leave;
907 }
908
909 name = _gcry_sexp_nth_string (list, 0);
910 if (!name)
911 {
912 rc = GPG_ERR_INV_OBJ; /* Algo string missing. */
913 goto leave;
914 }
915
916 spec = spec_from_name (name);
917 xfree (name);
918 name = NULL;
919 if (!spec || spec->flags.disabled)
920 {
921 rc = GPG_ERR_PUBKEY_ALGO; /* Unknown algorithm. */
922 goto leave;
923 }
924 else if (!spec->flags.fips && fips_mode ())
925 {
926 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
927 {
928 rc = GPG_ERR_PUBKEY_ALGO;
929 goto leave;
930 }
931 else
932 fips_service_indicator_mark_non_compliant ();
933 }
934
935 if (spec->generate)
936 rc = spec->generate (list, r_key);
937 else
938 rc = GPG_ERR_NOT_IMPLEMENTED;
939
940 leave:
941 sexp_release (list);
942 xfree (name);
943 sexp_release (l2);
944
945 return rc;
946 }
947
948
949 /*
950 Get the number of nbits from the public key.
951
952 Hmmm: Should we have really this function or is it better to have a
953 more general function to retrieve different properties of the key? */
954 unsigned int
955 _gcry_pk_get_nbits (gcry_sexp_t key)
956 {
957 gcry_pk_spec_t *spec;
958 gcry_sexp_t parms;
959 unsigned int nbits;
960
961 /* Parsing KEY might be considered too much overhead. For example
962 for RSA we would only need to look at P and stop parsing right
963 away. However, with ECC things are more complicate in that only
964 a curve name might be specified. Thus we need to tear the sexp
965 apart. */
966
967 if (spec_from_sexp (key, 0, &spec, &parms))
968 return 0; /* Error - 0 is a suitable indication for that. */
969
970 if (spec->flags.disabled)
971 nbits = 0; /* Error */
972 else if (!spec->flags.fips && fips_mode ())
973 {
974 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
975 nbits = 0; /* Error */
976 else
977 {
978 fips_service_indicator_mark_non_compliant ();
979 nbits = spec->get_nbits (parms);
980 }
981 }
982 else
983 nbits = spec->get_nbits (parms);
984
985 sexp_release (parms);
986 return nbits;
987 }
988
989
990 /* Return the so called KEYGRIP which is the SHA-1 hash of the public
991 key parameters expressed in a way depending on the algorithm.
992
993 ARRAY must either be 20 bytes long or NULL; in the latter case a
994 newly allocated array of that size is returned, otherwise ARRAY or
995 NULL is returned to indicate an error which is most likely an
996 unknown algorithm. The function accepts public or secret keys. */
997 unsigned char *
998 _gcry_pk_get_keygrip (gcry_sexp_t key, unsigned char *array)
999 {
1000 gcry_sexp_t list = NULL;
1001 gcry_sexp_t l2 = NULL;
1002 gcry_pk_spec_t *spec = NULL;
1003 const char *s;
1004 char *name = NULL;
1005 const char *elems;
1006 gcry_md_hd_t md = NULL;
1007 int okay = 0;
1008
1009 /* Check that the first element is valid. */
1010 list = sexp_find_token (key, "public-key", 0);
1011 if (! list)
1012 list = sexp_find_token (key, "private-key", 0);
1013 if (! list)
1014 list = sexp_find_token (key, "protected-private-key", 0);
1015 if (! list)
1016 list = sexp_find_token (key, "shadowed-private-key", 0);
1017 if (! list)
1018 return NULL; /* No public- or private-key object. */
1019
1020 l2 = sexp_cadr (list);
1021 sexp_release (list);
1022 list = l2;
1023 l2 = NULL;
1024
1025 name = _gcry_sexp_nth_string (list, 0);
1026 if (!name)
1027 goto fail; /* Invalid structure of object. */
1028
1029 spec = spec_from_name (name);
1030 if (!spec)
1031 goto fail; /* Unknown algorithm. */
1032
1033 elems = spec->elements_grip;
1034 if (!elems)
1035 goto fail; /* No grip parameter. */
1036
1037 if (_gcry_md_open (&md, GCRY_MD_SHA1, 0))
1038 goto fail;
1039
1040 if (spec->comp_keygrip)
1041 {
1042 /* Module specific method to compute a keygrip. */
1043 if (spec->comp_keygrip (md, list))
1044 goto fail;
1045 }
1046 else
1047 {
1048 /* Generic method to compute a keygrip. */
1049 for (s = elems; *s; s++)
1050 {
1051 const char *data;
1052 size_t datalen;
1053 char buf[30];
1054
1055 l2 = sexp_find_token (list, s, 1);
1056 if (! l2)
1057 goto fail;
1058 data = sexp_nth_data (l2, 1, &datalen);
1059 if (! data)
1060 goto fail;
1061
1062 snprintf (buf, sizeof buf, "(1:%c%u:", *s, (unsigned int)datalen);
1063 _gcry_md_write (md, buf, strlen (buf));
1064 _gcry_md_write (md, data, datalen);
1065 sexp_release (l2);
1066 l2 = NULL;
1067 _gcry_md_write (md, ")", 1);
1068 }
1069 }
1070
1071 if (!array)
1072 {
1073 array = xtrymalloc (20);
1074 if (! array)
1075 goto fail;
1076 }
1077
1078 memcpy (array, _gcry_md_read (md, GCRY_MD_SHA1), 20);
1079 okay = 1;
1080
1081 fail:
1082 xfree (name);
1083 sexp_release (l2);
1084 _gcry_md_close (md);
1085 sexp_release (list);
1086 return okay? array : NULL;
1087 }
1088
1089
1090
1091 const char *
1092 _gcry_pk_get_curve (gcry_sexp_t key, int iterator, unsigned int *r_nbits)
1093 {
1094 const char *result = NULL;
1095 gcry_pk_spec_t *spec;
1096 gcry_sexp_t keyparms = NULL;
1097
1098 if (r_nbits)
1099 *r_nbits = 0;
1100
1101 if (key)
1102 {
1103 iterator = 0;
1104
1105 if (spec_from_sexp (key, 0, &spec, &keyparms))
1106 return NULL;
1107 }
1108 else
1109 {
1110 spec = spec_from_name ("ecc");
1111 if (!spec)
1112 return NULL;
1113 }
1114
1115 if (spec->flags.disabled)
1116 result = NULL;
1117 else if (!spec->flags.fips && fips_mode ())
1118 {
1119 if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_PK))
1120 result = NULL;
1121 else
1122 {
1123 fips_service_indicator_mark_non_compliant ();
1124 result = spec->get_curve (keyparms, iterator, r_nbits);
1125 }
1126 }
1127 else if (spec->get_curve)
1128 result = spec->get_curve (keyparms, iterator, r_nbits);
1129
1130 sexp_release (keyparms);
1131 return result;
1132 }
1133
1134
1135
1136 gcry_sexp_t
1137 _gcry_pk_get_param (int algo, const char *name)
1138 {
1139 gcry_sexp_t result = NULL;
1140 gcry_pk_spec_t *spec = NULL;
1141
1142 algo = map_algo (algo);
1143
1144 if (algo != GCRY_PK_ECC)
1145 return NULL;
1146
1147 spec = spec_from_name ("ecc");
1148 if (spec)
1149 {
1150 if (spec && spec->get_curve_param)
1151 result = spec->get_curve_param (name);
1152 }
1153 return result;
1154 }
1155
1156
1157
1158 gcry_err_code_t
1159 _gcry_pk_ctl (int cmd, void *buffer, size_t buflen)
1160 {
1161 gcry_err_code_t rc = 0;
1162
1163 switch (cmd)
1164 {
1165 case GCRYCTL_DISABLE_ALGO:
1166 /* This one expects a buffer pointing to an integer with the
1167 algo number. */
1168 if ((! buffer) || (buflen != sizeof (int)))
1169 rc = GPG_ERR_INV_ARG;
1170 else
1171 disable_pubkey_algo (*((int *) buffer));
1172 break;
1173
1174 default:
1175 rc = GPG_ERR_INV_OP;
1176 }
1177
1178 return rc;
1179 }
1180
1181
1182 /* Return information about the given algorithm
1183
1184 WHAT selects the kind of information returned:
1185
1186 GCRYCTL_TEST_ALGO:
1187 Returns 0 when the specified algorithm is available for use.
1188 Buffer must be NULL, nbytes may have the address of a variable
1189 with the required usage of the algorithm. It may be 0 for don't
1190 care or a combination of the GCRY_PK_USAGE_xxx flags;
1191
1192 GCRYCTL_GET_ALGO_USAGE:
1193 Return the usage flags for the given algo. An invalid algo
1194 returns 0. Disabled algos are ignored here because we
1195 only want to know whether the algo is at all capable of
1196 the usage.
1197
1198 Note: Because this function is in most cases used to return an
1199 integer value, we can make it easier for the caller to just look at
1200 the return value. The caller will in all cases consult the value
1201 and thereby detecting whether a error occurred or not (i.e. while
1202 checking the block size) */
1203 gcry_err_code_t
1204 _gcry_pk_algo_info (int algorithm, int what, void *buffer, size_t *nbytes)
1205 {
1206 gcry_err_code_t rc = 0;
1207
1208 switch (what)
1209 {
1210 case GCRYCTL_TEST_ALGO:
1211 {
1212 int use = nbytes ? *nbytes : 0;
1213 if (buffer)
1214 rc = GPG_ERR_INV_ARG;
1215 else if (check_pubkey_algo (algorithm, use))
1216 rc = GPG_ERR_PUBKEY_ALGO;
1217 break;
1218 }
1219
1220 case GCRYCTL_GET_ALGO_USAGE:
1221 {
1222 gcry_pk_spec_t *spec;
1223
1224 spec = spec_from_algo (algorithm);
1225 *nbytes = spec? spec->use : 0;
1226 break;
1227 }
1228
1229 case GCRYCTL_GET_ALGO_NPKEY:
1230 {
1231 /* FIXME? */
1232 int npkey = pubkey_get_npkey (algorithm);
1233 *nbytes = npkey;
1234 break;
1235 }
1236 case GCRYCTL_GET_ALGO_NSKEY:
1237 {
1238 /* FIXME? */
1239 int nskey = pubkey_get_nskey (algorithm);
1240 *nbytes = nskey;
1241 break;
1242 }
1243 case GCRYCTL_GET_ALGO_NSIGN:
1244 {
1245 /* FIXME? */
1246 int nsign = pubkey_get_nsig (algorithm);
1247 *nbytes = nsign;
1248 break;
1249 }
1250 case GCRYCTL_GET_ALGO_NENCR:
1251 {
1252 /* FIXME? */
1253 int nencr = pubkey_get_nenc (algorithm);
1254 *nbytes = nencr;
1255 break;
1256 }
1257
1258 default:
1259 rc = GPG_ERR_INV_OP;
1260 }
1261
1262 return rc;
1263 }
1264
1265
1266 /* Return an S-expression representing the context CTX. Depending on
1267 the state of that context, the S-expression may either be a public
1268 key, a private key or any other object used with public key
1269 operations. On success a new S-expression is stored at R_SEXP and
1270 0 is returned, on error NULL is store there and an error code is
1271 returned. MODE is either 0 or one of the GCRY_PK_GET_xxx values.
1272
1273 As of now it only support certain ECC operations because a context
1274 object is right now only defined for ECC. Over time this function
1275 will be extended to cover more algorithms. Note also that the name
1276 of the function is gcry_pubkey_xxx and not gcry_pk_xxx. The idea
1277 is that we will eventually provide variants of the existing
1278 gcry_pk_xxx functions which will take a context parameter. */
1279 gcry_err_code_t
1280 _gcry_pubkey_get_sexp (gcry_sexp_t *r_sexp, int mode, gcry_ctx_t ctx)
1281 {
1282 mpi_ec_t ec;
1283
1284 if (!r_sexp)
1285 return GPG_ERR_INV_VALUE;
1286 *r_sexp = NULL;
1287 switch (mode)
1288 {
1289 case 0:
1290 case GCRY_PK_GET_PUBKEY:
1291 case GCRY_PK_GET_SECKEY:
1292 break;
1293 default:
1294 return GPG_ERR_INV_VALUE;
1295 }
1296 if (!ctx)
1297 return GPG_ERR_NO_CRYPT_CTX;
1298
1299 ec = _gcry_ctx_find_pointer (ctx, CONTEXT_TYPE_EC);
1300 if (ec)
1301 return _gcry_pk_ecc_get_sexp (r_sexp, mode, ec);
1302
1303 return GPG_ERR_WRONG_CRYPT_CTX;
1304 }
1305
1306
1307
1308 /* Explicitly initialize this module. */
1309 gcry_err_code_t
1310 _gcry_pk_init (void)
1311 {
1312 return 0;
1313 }
1314
1315
1316 /* Run the selftests for pubkey algorithm ALGO with optional reporting
1317 function REPORT. */
1318 gpg_error_t
1319 _gcry_pk_selftest (int algo, int extended, selftest_report_func_t report)
1320 {
1321 gcry_err_code_t ec;
1322 gcry_pk_spec_t *spec;
1323
1324 algo = map_algo (algo);
1325 spec = spec_from_algo (algo);
1326 if (spec && !spec->flags.disabled
1327 && (spec->flags.fips || !fips_mode ())
1328 && spec->selftest)
1329 ec = spec->selftest (algo, extended, report);
1330 else
1331 {
1332 ec = GPG_ERR_PUBKEY_ALGO;
1333 /* Fixme: We need to change the report function to allow passing
1334 of an encryption mode (e.g. pkcs1, ecdsa, or ecdh). */
1335 if (report)
1336 report ("pubkey", algo, "module",
1337 spec && !spec->flags.disabled
1338 && (spec->flags.fips || !fips_mode ())?
1339 "no selftest available" :
1340 spec? "algorithm disabled" :
1341 "algorithm not found");
1342 }
1343
1344 return gpg_error (ec);
1345 }
1346
1347
1348 struct pk_single_data {
1349 size_t len;
1350 unsigned char area[1]; /* In future, we may use flexible array member. */
1351 };
1352
1353 gpg_err_code_t
1354 _gcry_pk_single_data_push (gcry_ctx_t *r_ctx,
1355 const unsigned char *p, size_t len)
1356 {
1357 gcry_ctx_t ctx;
1358 struct pk_single_data *psd;
1359 int data_type = CONTEXT_TYPE_SINGLE_DATA;
1360
1361 if (!p)
1362 return GPG_ERR_EINVAL;
1363
1364 ctx = _gcry_ctx_alloc (data_type,
1365 offsetof (struct pk_single_data, area) + len,
1366 NULL, *r_ctx);
1367 if (!ctx)
1368 return gpg_err_code_from_syserror ();
1369 psd = _gcry_ctx_get_pointer (ctx, data_type);
1370 psd->len = len;
1371 memcpy (psd->area, p, len);
1372
1373 *r_ctx = ctx;
1374 return 0;
1375 }
1376
1377 gpg_err_code_t
1378 _gcry_pk_get_single_data (gcry_ctx_t *r_ctx,
1379 const unsigned char **r_p, size_t *r_len)
1380 {
1381 struct pk_single_data *psd;
1382 int data_type = CONTEXT_TYPE_SINGLE_DATA;
1383 gcry_ctx_t ctx = *r_ctx;
1384
1385 psd = _gcry_ctx_find_pointer (ctx, data_type);
1386 if (!psd)
1387 return GPG_ERR_EINVAL;
1388
1389 *r_p = psd->area;
1390 *r_len = psd->len;
1391 *r_ctx = _gcry_ctx_get_pointer (ctx, 0);
1392
1393 return 0;
1394 }