~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/testprof/pkcs_1_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <tomcrypt_test.h>
 
2
 
 
3
#ifdef PKCS_1
 
4
 
 
5
int pkcs_1_test(void)
 
6
{
 
7
   unsigned char buf[3][128];
 
8
   int res1, res2, res3, prng_idx, hash_idx;
 
9
   unsigned long x, y, l1, l2, l3, i1, i2, lparamlen, saltlen, modlen;
 
10
   static const unsigned char lparam[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
 
11
 
 
12
   /* get hash/prng  */
 
13
   hash_idx = find_hash("sha1");
 
14
   prng_idx = find_prng("yarrow");
 
15
   
 
16
   if (hash_idx == -1 || prng_idx == -1) {
 
17
      fprintf(stderr, "pkcs_1 tests require sha1/yarrow");
 
18
      return 1;
 
19
   }   
 
20
 
 
21
   /* do many tests */
 
22
   for (x = 0; x < 100; x++) {
 
23
      zeromem(buf, sizeof(buf));
 
24
 
 
25
      /* make a dummy message (of random length) */
 
26
      l3 = (rand() & 31) + 8;
 
27
      for (y = 0; y < l3; y++) buf[0][y] = rand() & 255;
 
28
 
 
29
      /* random modulus len (v1.5 must be multiple of 8 though arbitrary sizes seem to work) */
 
30
      modlen = 800 + 8 * (abs(rand()) % 28);
 
31
 
 
32
      /* pick a random lparam len [0..16] */
 
33
      lparamlen = abs(rand()) % 17;
 
34
 
 
35
      /* pick a random saltlen 0..16 */
 
36
      saltlen   = abs(rand()) % 17;
 
37
 
 
38
      /* PKCS #1 v2.0 supports modlens not multiple of 8 */
 
39
      modlen = 800 + (abs(rand()) % 224);
 
40
 
 
41
      /* encode it */
 
42
      l1 = sizeof(buf[1]);
 
43
      DO(pkcs_1_oaep_encode(buf[0], l3, lparam, lparamlen, modlen, &yarrow_prng, prng_idx, hash_idx, buf[1], &l1));
 
44
 
 
45
      /* decode it */
 
46
      l2 = sizeof(buf[2]);
 
47
      DO(pkcs_1_oaep_decode(buf[1], l1, lparam, lparamlen, modlen, hash_idx, buf[2], &l2, &res1));
 
48
 
 
49
      if (res1 != 1 || l2 != l3 || memcmp(buf[2], buf[0], l3) != 0) {
 
50
         fprintf(stderr, "Outsize == %lu, should have been %lu, res1 = %d, lparamlen = %lu, msg contents follow.\n", l2, l3, res1, lparamlen);
 
51
         fprintf(stderr, "ORIGINAL:\n");
 
52
         for (x = 0; x < l3; x++) {
 
53
             fprintf(stderr, "%02x ", buf[0][x]);
 
54
         }
 
55
         fprintf(stderr, "\nRESULT:\n");
 
56
         for (x = 0; x < l2; x++) {
 
57
             fprintf(stderr, "%02x ", buf[2][x]);
 
58
         }
 
59
         fprintf(stderr, "\n\n");
 
60
         return 1;
 
61
      }
 
62
 
 
63
      /* test PSS */
 
64
      l1 = sizeof(buf[1]);
 
65
      DO(pkcs_1_pss_encode(buf[0], l3, saltlen, &yarrow_prng, prng_idx, hash_idx, modlen, buf[1], &l1));
 
66
      DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res1));
 
67
      
 
68
      buf[0][i1 = abs(rand()) % l3] ^= 1;
 
69
      DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res2));
 
70
 
 
71
      buf[0][i1] ^= 1;
 
72
      buf[1][i2 = abs(rand()) % l1] ^= 1;
 
73
      DO(pkcs_1_pss_decode(buf[0], l3, buf[1], l1, saltlen, hash_idx, modlen, &res3));
 
74
 
 
75
      if (!(res1 == 1 && res2 == 0 && res3 == 0)) {
 
76
         fprintf(stderr, "PSS failed: %d, %d, %d, %lu, %lu\n", res1, res2, res3, l3, saltlen);
 
77
         return 1;
 
78
      }
 
79
   }
 
80
   return 0;
 
81
}
 
82
 
 
83
#else
 
84
 
 
85
int pkcs_1_test(void)
 
86
{
 
87
   fprintf(stderr, "NOP");
 
88
   return 0;
 
89
}
 
90
 
 
91
#endif
 
92
 
 
93
 
 
94
/* $Source: /cvs/libtom/libtomcrypt/testprof/pkcs_1_test.c,v $ */
 
95
/* $Revision: 1.6 $ */
 
96
/* $Date: 2005/05/21 12:51:25 $ */