"Fossies" - the Fresh Open Source Software Archive

Member "nettle-3.7.3/testsuite/rsa-encrypt-test.c" (6 Jun 2021, 4933 Bytes) of package /linux/privat/nettle-3.7.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "rsa-encrypt-test.c": 3.7.2_vs_3.7.3.

    1 #include "testutils.h"
    2 
    3 #include "rsa.h"
    4 #include "knuth-lfib.h"
    5 
    6 void
    7 test_main(void)
    8 {
    9   struct rsa_public_key pub;
   10   struct rsa_private_key key;
   11   struct knuth_lfib_ctx lfib;
   12 
   13   /* FIXME: How is this spelled? */
   14   const unsigned char msg[] = "Squemish ossifrage";
   15   size_t msg_length = LLENGTH(msg);
   16 
   17   uint8_t *decrypted;
   18   size_t decrypted_length;
   19   uint8_t after;
   20 
   21   mpz_t gibberish;
   22   mpz_t bad_input;
   23 
   24   rsa_private_key_init(&key);
   25   rsa_public_key_init(&pub);
   26   mpz_init(gibberish);
   27   mpz_init(bad_input);
   28 
   29   knuth_lfib_init(&lfib, 17);
   30   
   31   test_rsa_set_key_1(&pub, &key);
   32 
   33   if (verbose)
   34     fprintf(stderr, "msg: `%s', length = %d\n", msg, (int) msg_length);
   35 
   36   ASSERT(msg_length <= key.size);
   37   
   38   ASSERT(rsa_encrypt(&pub,
   39              &lfib, (nettle_random_func *) knuth_lfib_random,
   40              msg_length, msg,
   41              gibberish));
   42 
   43   if (verbose)
   44     {
   45       fprintf(stderr, "encrypted: ");
   46       mpz_out_str(stderr, 10, gibberish);
   47     }
   48   
   49   decrypted = xalloc(key.size + 1);
   50 
   51   knuth_lfib_random (&lfib, msg_length + 1, decrypted);
   52   after = decrypted[msg_length];
   53   
   54   decrypted_length = msg_length - 1;
   55   ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
   56 
   57   decrypted_length = msg_length;
   58   ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
   59   ASSERT(decrypted_length == msg_length);
   60   ASSERT(MEMEQ(msg_length, msg, decrypted));
   61   ASSERT(decrypted[msg_length] == after);
   62 
   63   knuth_lfib_random (&lfib, key.size + 1, decrypted);
   64   after = decrypted[key.size];
   65 
   66   decrypted_length = key.size;
   67   ASSERT(rsa_decrypt(&key, &decrypted_length, decrypted, gibberish));
   68   ASSERT(decrypted_length == msg_length);
   69   ASSERT(MEMEQ(msg_length, msg, decrypted));
   70   ASSERT(decrypted[key.size] == after);
   71   
   72   knuth_lfib_random (&lfib, msg_length + 1, decrypted);
   73   after = decrypted[msg_length];
   74 
   75   decrypted_length = msg_length;
   76   ASSERT(rsa_decrypt_tr(&pub, &key,
   77             &lfib, (nettle_random_func *) knuth_lfib_random,
   78             &decrypted_length, decrypted, gibberish));
   79   ASSERT(decrypted_length == msg_length);
   80   ASSERT(MEMEQ(msg_length, msg, decrypted));
   81   ASSERT(decrypted[msg_length] == after);
   82 
   83   /* test side channel resistant variant */
   84   knuth_lfib_random (&lfib, msg_length + 1, decrypted);
   85   after = decrypted[msg_length];
   86   decrypted_length = msg_length;
   87 
   88   ASSERT(rsa_sec_decrypt(&pub, &key,
   89                          &lfib, (nettle_random_func *) knuth_lfib_random,
   90                          decrypted_length, decrypted, gibberish));
   91   ASSERT(MEMEQ(msg_length, msg, decrypted));
   92   ASSERT(decrypted[msg_length] == after);
   93 
   94   /* test invalid length to rsa_sec_decrypt */
   95   knuth_lfib_random (&lfib, msg_length + 1, decrypted);
   96   decrypted_length = msg_length - 1;
   97   after = decrypted[decrypted_length] = 'X';
   98   decrypted[0] = 'A';
   99 
  100   ASSERT(!rsa_sec_decrypt(&pub, &key,
  101                           &lfib, (nettle_random_func *) knuth_lfib_random,
  102                           decrypted_length, decrypted, gibberish));
  103   ASSERT(decrypted[decrypted_length] == after);
  104   ASSERT(decrypted[0] == 'A');
  105 
  106   /* Test zero input. */
  107   mpz_set_ui (bad_input, 0);
  108   decrypted_length = msg_length;
  109   ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
  110   ASSERT(!rsa_decrypt_tr(&pub, &key,
  111              &lfib, (nettle_random_func *) knuth_lfib_random,
  112              &decrypted_length, decrypted, bad_input));
  113   ASSERT(!rsa_sec_decrypt(&pub, &key,
  114               &lfib, (nettle_random_func *) knuth_lfib_random,
  115               decrypted_length, decrypted, bad_input));
  116   ASSERT(decrypted_length == msg_length);
  117 
  118   /* Test input that is slightly larger than n */
  119   mpz_add(bad_input, gibberish, pub.n);
  120   decrypted_length = msg_length;
  121   ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
  122   ASSERT(!rsa_decrypt_tr(&pub, &key,
  123              &lfib, (nettle_random_func *) knuth_lfib_random,
  124              &decrypted_length, decrypted, bad_input));
  125   ASSERT(!rsa_sec_decrypt(&pub, &key,
  126               &lfib, (nettle_random_func *) knuth_lfib_random,
  127               decrypted_length, decrypted, bad_input));
  128   ASSERT(decrypted_length == msg_length);
  129 
  130   /* Test input that is considerably larger than n */
  131   mpz_mul_2exp (bad_input, pub.n, 100);
  132   mpz_add (bad_input, bad_input, gibberish);
  133   decrypted_length = msg_length;
  134   ASSERT(!rsa_decrypt(&key, &decrypted_length, decrypted, bad_input));
  135   ASSERT(!rsa_decrypt_tr(&pub, &key,
  136              &lfib, (nettle_random_func *) knuth_lfib_random,
  137              &decrypted_length, decrypted, bad_input));
  138   ASSERT(!rsa_sec_decrypt(&pub, &key,
  139               &lfib, (nettle_random_func *) knuth_lfib_random,
  140               decrypted_length, decrypted, bad_input));
  141   ASSERT(decrypted_length == msg_length);
  142 
  143   /* Test invalid key. */
  144   mpz_add_ui (key.q, key.q, 2);
  145   decrypted_length = key.size;
  146   ASSERT(!rsa_decrypt_tr(&pub, &key,
  147              &lfib, (nettle_random_func *) knuth_lfib_random,
  148              &decrypted_length, decrypted, gibberish));
  149 
  150   rsa_private_key_clear(&key);
  151   rsa_public_key_clear(&pub);
  152   mpz_clear(gibberish);
  153   mpz_clear(bad_input);
  154   free(decrypted);
  155 }