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

« back to all changes in this revision

Viewing changes to libtomcrypt/demos/test/dh_tests.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 "test.h"
2
 
 
3
 
#ifdef MDH
4
 
 
5
 
int dh_tests (void)
6
 
{
7
 
  unsigned char buf[3][4096];
8
 
  unsigned long x, y, z;
9
 
  int           stat, stat2;
10
 
  dh_key        usera, userb;
11
 
 
12
 
  DO(dh_test());
13
 
 
14
 
  /* make up two keys */
15
 
  DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &usera));
16
 
  DO(dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &userb));
17
 
 
18
 
  /* make the shared secret */
19
 
  x = 4096;
20
 
  DO(dh_shared_secret (&usera, &userb, buf[0], &x));
21
 
 
22
 
  y = 4096;
23
 
  DO(dh_shared_secret (&userb, &usera, buf[1], &y));
24
 
  if (y != x) {
25
 
    printf ("DH Shared keys are not same size.\n");
26
 
    return 1;
27
 
  }
28
 
  if (memcmp (buf[0], buf[1], x)) {
29
 
    printf ("DH Shared keys not same contents.\n");
30
 
    return 1;
31
 
  }
32
 
 
33
 
  /* now export userb */
34
 
  y = 4096;
35
 
  DO(dh_export (buf[1], &y, PK_PUBLIC, &userb));
36
 
          dh_free (&userb);
37
 
 
38
 
  /* import and make the shared secret again */
39
 
  DO(dh_import (buf[1], y, &userb));
40
 
  z = 4096;
41
 
  DO(dh_shared_secret (&usera, &userb, buf[2], &z));
42
 
 
43
 
  if (z != x) {
44
 
    printf ("failed.  Size don't match?\n");
45
 
    return 1;
46
 
  }
47
 
  if (memcmp (buf[0], buf[2], x)) {
48
 
    printf ("Failed.  Content didn't match.\n");
49
 
    return 1;
50
 
  }
51
 
  dh_free (&usera);
52
 
  dh_free (&userb);
53
 
 
54
 
/* test encrypt_key */
55
 
  dh_make_key (&test_yarrow, find_prng ("yarrow"), 512, &usera);
56
 
  for (x = 0; x < 16; x++) {
57
 
    buf[0][x] = x;
58
 
  }
59
 
  y = sizeof (buf[1]);
60
 
  DO(dh_encrypt_key (buf[0], 16, buf[1], &y, &test_yarrow, find_prng ("yarrow"), find_hash ("md5"), &usera));
61
 
  zeromem (buf[0], sizeof (buf[0]));
62
 
  x = sizeof (buf[0]);
63
 
  DO(dh_decrypt_key (buf[1], y, buf[0], &x, &usera));
64
 
  if (x != 16) {
65
 
    printf ("Failed (length)\n");
66
 
    return 1;
67
 
  }
68
 
  for (x = 0; x < 16; x++)
69
 
    if (buf[0][x] != x) {
70
 
      printf ("Failed (contents)\n");
71
 
      return 1;
72
 
    }
73
 
 
74
 
/* test sign_hash */
75
 
  for (x = 0; x < 16; x++) {
76
 
     buf[0][x] = x;
77
 
  }
78
 
  x = sizeof (buf[1]);
79
 
  DO(dh_sign_hash (buf[0], 16, buf[1], &x, &test_yarrow         , find_prng ("yarrow"), &usera));
80
 
  DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat, &usera));
81
 
  buf[0][0] ^= 1;
82
 
  DO(dh_verify_hash (buf[1], x, buf[0], 16, &stat2, &usera));
83
 
  if (!(stat == 1 && stat2 == 0)) { 
84
 
     printf("dh_sign/verify_hash %d %d", stat, stat2);
85
 
     return 1;
86
 
  }
87
 
  dh_free (&usera);
88
 
  return 0;
89
 
}
90
 
 
91
 
#else
92
 
 
93
 
int dh_tests(void)
94
 
{
95
 
   printf("NOP");
96
 
   return 0;
97
 
}
98
 
 
99
 
#endif