~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

Viewing changes to grub-core/lib/libgcrypt/cipher/dsa.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
"      42CAA7DC289F0C5A9D155F02D3D551DB741A81695B74D4C8F477F9C7838EB0FB#)"
75
75
"  (x #11D54E4ADBD3034160F2CED4B7CD292A4EBF3EC0#)))";
76
76
/* A sample 1024 bit DSA key used for the selftests (public only).  */
77
 
static const char sample_public_key[] = 
 
77
static const char sample_public_key[] =
78
78
"(public-key"
79
79
" (dsa"
80
80
"  (p #00AD7C0025BA1A15F775F3F2D673718391D00456978D347B33D7B49E7F32EDAB"
141
141
  unsigned int nbytes = (nbits+7)/8;
142
142
  char *rndbuf = NULL;
143
143
 
 
144
  /* To learn why we don't use mpi_mod to get the requested bit size,
 
145
     read the paper: "The Insecurity of the Digital Signature
 
146
     Algorithm with Partially Known Nonces" by Nguyen and Shparlinski.
 
147
     Journal of Cryptology, New York. Vol 15, nr 3 (2003)  */
 
148
 
144
149
  if ( DBG_CIPHER )
145
150
    log_debug("choosing a random k ");
146
 
  for (;;) 
 
151
  for (;;)
147
152
    {
148
153
      if( DBG_CIPHER )
149
154
        progress('.');
150
155
 
151
 
      if ( !rndbuf || nbits < 32 ) 
 
156
      if ( !rndbuf || nbits < 32 )
152
157
        {
153
158
          gcry_free(rndbuf);
154
159
          rndbuf = gcry_random_bytes_secure( (nbits+7)/8, GCRY_STRONG_RANDOM );
156
161
      else
157
162
        { /* Change only some of the higher bits.  We could improve
158
163
             this by directly requesting more memory at the first call
159
 
             to get_random_bytes() and use this the here maybe it is
160
 
             easier to do this directly in random.c. */
 
164
             to get_random_bytes() and use these extra bytes here.
 
165
             However the required management code is more complex and
 
166
             thus we better use this simple method.  */
161
167
          char *pp = gcry_random_bytes_secure( 4, GCRY_STRONG_RANDOM );
162
168
          memcpy( rndbuf,pp, 4 );
163
169
          gcry_free(pp);
164
170
        }
165
171
      _gcry_mpi_set_buffer( k, rndbuf, nbytes, 0 );
 
172
 
 
173
      /* Make sure we have the requested number of bits.  This code
 
174
         looks a bit funny but it is easy to understand if you
 
175
         consider that mpi_set_highbit clears all higher bits.  We
 
176
         don't have a clear_highbit, thus we first set the high bit
 
177
         and then clear it again.  */
166
178
      if ( mpi_test_bit( k, nbits-1 ) )
167
179
        mpi_set_highbit( k, nbits-1 );
168
180
      else
172
184
        }
173
185
 
174
186
      if( !(mpi_cmp( k, q ) < 0) ) /* check: k < q */
175
 
        {       
 
187
        {
176
188
          if( DBG_CIPHER )
177
189
            progress('+');
178
190
          continue; /* no  */
188
200
  gcry_free(rndbuf);
189
201
  if( DBG_CIPHER )
190
202
    progress('\n');
191
 
  
 
203
 
192
204
  return k;
193
205
}
194
206
 
315
327
          mpi_add_ui (h, h, 1);
316
328
          /* g = h^e mod p */
317
329
          gcry_mpi_powm (g, h, e, p);
318
 
        } 
 
330
        }
319
331
      while (!mpi_cmp_ui (g, 1));  /* Continue until g != 1. */
320
332
    }
321
333
 
330
342
  x = mpi_alloc_secure( mpi_get_nlimbs(q) );
331
343
  mpi_sub_ui( h, q, 1 );  /* put q-1 into h */
332
344
  rndbuf = NULL;
333
 
  do 
 
345
  do
334
346
    {
335
347
      if( DBG_CIPHER )
336
348
        progress('.');
337
349
      if( !rndbuf )
338
350
        rndbuf = gcry_random_bytes_secure ((qbits+7)/8, random_level);
339
 
      else 
 
351
      else
340
352
        { /* Change only some of the higher bits (= 2 bytes)*/
341
353
          char *r = gcry_random_bytes_secure (2, random_level);
342
354
          memcpy(rndbuf, r, 2 );
345
357
 
346
358
      _gcry_mpi_set_buffer( x, rndbuf, (qbits+7)/8, 0 );
347
359
      mpi_clear_highbit( x, qbits+1 );
348
 
    } 
 
360
    }
349
361
  while ( !( mpi_cmp_ui( x, 0 )>0 && mpi_cmp( x, h )<0 ) );
350
362
  gcry_free(rndbuf);
351
363
  mpi_free( e );
355
367
  y = mpi_alloc( mpi_get_nlimbs(p) );
356
368
  gcry_mpi_powm( y, g, x, p );
357
369
 
358
 
  if( DBG_CIPHER ) 
 
370
  if( DBG_CIPHER )
359
371
    {
360
372
      progress('\n');
361
373
      log_mpidump("dsa  p", p );
406
418
    const void *seed;
407
419
    size_t seedlen;
408
420
  } initial_seed = { NULL, NULL, 0 };
409
 
  gcry_mpi_t prime_q = NULL; 
410
 
  gcry_mpi_t prime_p = NULL; 
 
421
  gcry_mpi_t prime_q = NULL;
 
422
  gcry_mpi_t prime_p = NULL;
411
423
  gcry_mpi_t value_g = NULL; /* The generator. */
412
424
  gcry_mpi_t value_y = NULL; /* g^x mod p */
413
425
  gcry_mpi_t value_x = NULL; /* The secret exponent. */
467
479
            initial_seed.seed = gcry_sexp_nth_data (initial_seed.sexp, 1,
468
480
                                                    &initial_seed.seedlen);
469
481
        }
470
 
      
 
482
 
471
483
      /* Fixme: Enable 186-3 after it has been approved and after fixing
472
484
         the generation function.  */
473
485
      /*   if (use_fips186_2) */
474
486
      (void)use_fips186_2;
475
 
      ec = _gcry_generate_fips186_2_prime (nbits, qbits, 
476
 
                                           initial_seed.seed, 
 
487
      ec = _gcry_generate_fips186_2_prime (nbits, qbits,
 
488
                                           initial_seed.seed,
477
489
                                           initial_seed.seedlen,
478
 
                                           &prime_q, &prime_p, 
 
490
                                           &prime_q, &prime_p,
479
491
                                           r_counter,
480
492
                                           r_seed, r_seedlen);
481
493
      /*   else */
493
505
      mpi_sub_ui (value_e, prime_p, 1);
494
506
      mpi_fdiv_q (value_e, value_e, prime_q );
495
507
      value_g = mpi_alloc_like (prime_p);
496
 
      value_h = mpi_alloc_set_ui (1); 
 
508
      value_h = mpi_alloc_set_ui (1);
497
509
      do
498
510
        {
499
511
          mpi_add_ui (value_h, value_h, 1);
500
512
          /* g = h^e mod p */
501
513
          mpi_powm (value_g, value_h, value_e, prime_p);
502
 
        } 
 
514
        }
503
515
      while (!mpi_cmp_ui (value_g, 1));  /* Continue until g != 1.  */
504
516
    }
505
517
 
506
518
 
507
519
  /* Select a random number x with:  0 < x < q  */
508
520
  value_x = gcry_mpi_snew (qbits);
509
 
  do 
 
521
  do
510
522
    {
511
523
      if( DBG_CIPHER )
512
524
        progress('.');
513
525
      gcry_mpi_randomize (value_x, qbits, GCRY_VERY_STRONG_RANDOM);
514
526
      mpi_clear_highbit (value_x, qbits+1);
515
 
    } 
 
527
    }
516
528
  while (!(mpi_cmp_ui (value_x, 0) > 0 && mpi_cmp (value_x, prime_q) < 0));
517
529
 
518
530
  /* y = g^x mod p */
519
531
  value_y = mpi_alloc_like (prime_p);
520
532
  gcry_mpi_powm (value_y, value_g, value_x, prime_p);
521
533
 
522
 
  if (DBG_CIPHER) 
 
534
  if (DBG_CIPHER)
523
535
    {
524
536
      progress('\n');
525
537
      log_mpidump("dsa  p", prime_p );
691
703
  int use_fips186_2 = 0;
692
704
  int use_fips186 = 0;
693
705
  dsa_domain_t domain;
694
 
 
 
706
 
695
707
  (void)algo;    /* No need to check it.  */
696
708
  (void)evalue;  /* Not required for DSA. */
697
709
 
700
712
  if (genparms)
701
713
    {
702
714
      gcry_sexp_t domainsexp;
703
 
  
 
715
 
704
716
      /* Parse the optional qbits element.  */
705
717
      l1 = gcry_sexp_find_token (genparms, "qbits", 0);
706
718
      if (l1)
708
720
          char buf[50];
709
721
          const char *s;
710
722
          size_t n;
711
 
          
 
723
 
712
724
          s = gcry_sexp_nth_data (l1, 1, &n);
713
725
          if (!s || n >= DIM (buf) - 1 )
714
726
            {
760
772
              gcry_sexp_release (deriveparms);
761
773
              return GPG_ERR_INV_VALUE;
762
774
            }
763
 
          
 
775
 
764
776
          /* Put all domain parameters into the domain object.  */
765
777
          l1 = gcry_sexp_find_token (domainsexp, "p", 0);
766
778
          domain.p = gcry_sexp_nth_mpi (l1, 1, GCRYMPI_FMT_USG);
804
816
        {
805
817
          /* Format the seed-values unless domain parameters are used
806
818
             for which a H_VALUE of NULL is an indication.  */
807
 
          ec = gpg_err_code (gcry_sexp_build 
 
819
          ec = gpg_err_code (gcry_sexp_build
808
820
                             (&seedinfo, NULL,
809
821
                              "(seed-values(counter %d)(seed %b)(h %m))",
810
822
                              counter, (int)seedlen, seed, h_value));
879
891
                  p = stpcpy (p, ")");
880
892
                }
881
893
              p = stpcpy (p, ")");
882
 
              
 
894
 
883
895
              /* Allocate space for the list of factors plus one for
884
896
                 an S-expression plus an extra NULL entry for safety
885
897
                 and fill it with the factors.  */
894
906
                  for (j=0; j < nfactors; j++)
895
907
                    arg_list[i++] = (*retfactors) + j;
896
908
                  arg_list[i] = NULL;
897
 
                  
898
 
                  ec = gpg_err_code (gcry_sexp_build_array 
 
909
 
 
910
                  ec = gpg_err_code (gcry_sexp_build_array
899
911
                                     (r_extrainfo, NULL, format, arg_list));
900
912
                }
901
913
            }
907
919
              gcry_mpi_release ((*retfactors)[i]);
908
920
              (*retfactors)[i] = NULL;
909
921
            }
 
922
          gcry_free (*retfactors);
910
923
          *retfactors = NULL;
911
924
          if (ec)
912
925
            {
1022
1035
 
1023
1036
 
1024
1037
 
1025
 
/* 
 
1038
/*
1026
1039
     Self-test section.
1027
1040
 */
1028
1041
 
1029
1042
static const char *
1030
1043
selftest_sign_1024 (gcry_sexp_t pkey, gcry_sexp_t skey)
1031
1044
{
1032
 
  static const char sample_data[] = 
1033
 
    "(data (flags pkcs1)"
1034
 
    " (hash sha1 #a0b1c2d3e4f500102030405060708090a1b2c3d4#))";
1035
 
  static const char sample_data_bad[] = 
1036
 
    "(data (flags pkcs1)"
1037
 
    " (hash sha1 #a0b1c2d3e4f510102030405060708090a1b2c3d4#))";
 
1045
  static const char sample_data[] =
 
1046
    "(data (flags raw)"
 
1047
    " (value #a0b1c2d3e4f500102030405060708090a1b2c3d4#))";
 
1048
  static const char sample_data_bad[] =
 
1049
    "(data (flags raw)"
 
1050
    " (value #a0b1c2d3e4f510102030405060708090a1b2c3d4#))";
1038
1051
 
1039
1052
  const char *errtxt = NULL;
1040
1053
  gcry_error_t err;
1045
1058
  err = gcry_sexp_sscan (&data, NULL,
1046
1059
                         sample_data, strlen (sample_data));
1047
1060
  if (!err)
1048
 
    err = gcry_sexp_sscan (&data_bad, NULL, 
 
1061
    err = gcry_sexp_sscan (&data_bad, NULL,
1049
1062
                           sample_data_bad, strlen (sample_data_bad));
1050
1063
  if (err)
1051
1064
    {
1092
1105
 
1093
1106
  /* Convert the S-expressions into the internal representation.  */
1094
1107
  what = "convert";
1095
 
  err = gcry_sexp_sscan (&skey, NULL, 
 
1108
  err = gcry_sexp_sscan (&skey, NULL,
1096
1109
                         sample_secret_key, strlen (sample_secret_key));
1097
1110
  if (!err)
1098
 
    err = gcry_sexp_sscan (&pkey, NULL, 
 
1111
    err = gcry_sexp_sscan (&pkey, NULL,
1099
1112
                           sample_public_key, strlen (sample_public_key));
1100
1113
  if (err)
1101
1114
    {
1145
1158
    default:
1146
1159
      ec = GPG_ERR_PUBKEY_ALGO;
1147
1160
      break;
1148
 
        
 
1161
 
1149
1162
    }
1150
1163
  return ec;
1151
1164
}
1162
1175
 
1163
1176
gcry_pk_spec_t _gcry_pubkey_spec_dsa =
1164
1177
  {
1165
 
    "DSA", dsa_names, 
 
1178
    "DSA", dsa_names,
1166
1179
    "pqgy", "pqgyx", "", "rs", "pqgy",
1167
1180
    GCRY_PK_USAGE_SIGN,
1168
1181
    dsa_generate,
1173
1186
    dsa_verify,
1174
1187
    dsa_get_nbits
1175
1188
  };
1176
 
pk_extra_spec_t _gcry_pubkey_extraspec_dsa = 
 
1189
pk_extra_spec_t _gcry_pubkey_extraspec_dsa =
1177
1190
  {
1178
1191
    run_selftests,
1179
1192
    dsa_generate_ext
1180
1193
  };
1181