summaryrefslogtreecommitdiff
path: root/attic/historic/anode/libanode/tests/ec-test.c
blob: 49f04265ba460f4a81449249fc5aaa2a499601e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* libanode: the Anode C reference implementation
 * Copyright (C) 2009 Adam Ierymenko <adam.ierymenko@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../impl/ec.h"
#include "../impl/misc.h"

#define TEST_KEY_LEN 128
#define AnodeEC_key_to_hex(k,b,l) Anode_to_hex((k)->key,(k)->bytes,(b),l)

int main(int argc,char **argv)
{
  struct AnodeECKeyPair pair1;
  struct AnodeECKeyPair pair2;
  struct AnodeECKeyPair pair3;
  unsigned char key[TEST_KEY_LEN];
  char str[16384];

  printf("Creating key pair #1...\n");
  if (!AnodeECKeyPair_generate(&pair1)) {
    printf("Could not create key pair.\n");
    return 1;
  }
  AnodeEC_key_to_hex(&pair1.pub,str,sizeof(str));
  printf("Public:  %s\n",str);
  AnodeEC_key_to_hex(&pair1.priv,str,sizeof(str));
  printf("Private: %s\n\n",str);

  printf("Creating key pair #2...\n");
  if (!AnodeECKeyPair_generate(&pair2)) {
    printf("Could not create key pair.\n");
    return 1;
  }
  AnodeEC_key_to_hex(&pair2.pub,str,sizeof(str));
  printf("Public:  %s\n",str);
  AnodeEC_key_to_hex(&pair2.priv,str,sizeof(str));
  printf("Private: %s\n\n",str);

  printf("Key agreement between public #2 and private #1...\n");
  if (!AnodeECKeyPair_agree(&pair1,&pair2.pub,key,TEST_KEY_LEN)) {
    printf("Agreement failed.\n");
    return 1;
  }
  Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  printf("Agreed secret: %s\n\n",str);

  printf("Key agreement between public #1 and private #2...\n");
  if (!AnodeECKeyPair_agree(&pair2,&pair1.pub,key,TEST_KEY_LEN)) {
    printf("Agreement failed.\n");
    return 1;
  }
  Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  printf("Agreed secret: %s\n\n",str);

  printf("Testing key pair init function (init #3 from #2's parts)...\n");
  if (!AnodeECKeyPair_init(&pair3,&(pair2.pub),&(pair2.priv))) {
    printf("Init failed.\n");
    return 1;
  }

  printf("Key agreement between public #1 and private #3...\n");
  if (!AnodeECKeyPair_agree(&pair3,&pair1.pub,key,TEST_KEY_LEN)) {
    printf("Agreement failed.\n");
    return 1;
  }
  Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  printf("Agreed secret: %s\n\n",str);

  printf("Key agreement between public #1 and private #1...\n");
  if (!AnodeECKeyPair_agree(&pair1,&pair1.pub,key,TEST_KEY_LEN)) {
    printf("Agreement failed.\n");
    return 1;
  }
  Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  printf("Agreed secret (should not match): %s\n\n",str);

  AnodeECKeyPair_destroy(&pair1);
  AnodeECKeyPair_destroy(&pair2);
  AnodeECKeyPair_destroy(&pair3);

  return 0;
}