2
0

ec-test.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include "../impl/ec.h"
  20. #include "../impl/misc.h"
  21. #define TEST_KEY_LEN 128
  22. #define AnodeEC_key_to_hex(k,b,l) Anode_to_hex((k)->key,(k)->bytes,(b),l)
  23. int main(int argc,char **argv)
  24. {
  25. struct AnodeECKeyPair pair1;
  26. struct AnodeECKeyPair pair2;
  27. struct AnodeECKeyPair pair3;
  28. unsigned char key[TEST_KEY_LEN];
  29. char str[16384];
  30. printf("Creating key pair #1...\n");
  31. if (!AnodeECKeyPair_generate(&pair1)) {
  32. printf("Could not create key pair.\n");
  33. return 1;
  34. }
  35. AnodeEC_key_to_hex(&pair1.pub,str,sizeof(str));
  36. printf("Public: %s\n",str);
  37. AnodeEC_key_to_hex(&pair1.priv,str,sizeof(str));
  38. printf("Private: %s\n\n",str);
  39. printf("Creating key pair #2...\n");
  40. if (!AnodeECKeyPair_generate(&pair2)) {
  41. printf("Could not create key pair.\n");
  42. return 1;
  43. }
  44. AnodeEC_key_to_hex(&pair2.pub,str,sizeof(str));
  45. printf("Public: %s\n",str);
  46. AnodeEC_key_to_hex(&pair2.priv,str,sizeof(str));
  47. printf("Private: %s\n\n",str);
  48. printf("Key agreement between public #2 and private #1...\n");
  49. if (!AnodeECKeyPair_agree(&pair1,&pair2.pub,key,TEST_KEY_LEN)) {
  50. printf("Agreement failed.\n");
  51. return 1;
  52. }
  53. Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  54. printf("Agreed secret: %s\n\n",str);
  55. printf("Key agreement between public #1 and private #2...\n");
  56. if (!AnodeECKeyPair_agree(&pair2,&pair1.pub,key,TEST_KEY_LEN)) {
  57. printf("Agreement failed.\n");
  58. return 1;
  59. }
  60. Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  61. printf("Agreed secret: %s\n\n",str);
  62. printf("Testing key pair init function (init #3 from #2's parts)...\n");
  63. if (!AnodeECKeyPair_init(&pair3,&(pair2.pub),&(pair2.priv))) {
  64. printf("Init failed.\n");
  65. return 1;
  66. }
  67. printf("Key agreement between public #1 and private #3...\n");
  68. if (!AnodeECKeyPair_agree(&pair3,&pair1.pub,key,TEST_KEY_LEN)) {
  69. printf("Agreement failed.\n");
  70. return 1;
  71. }
  72. Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  73. printf("Agreed secret: %s\n\n",str);
  74. printf("Key agreement between public #1 and private #1...\n");
  75. if (!AnodeECKeyPair_agree(&pair1,&pair1.pub,key,TEST_KEY_LEN)) {
  76. printf("Agreement failed.\n");
  77. return 1;
  78. }
  79. Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str));
  80. printf("Agreed secret (should not match): %s\n\n",str);
  81. AnodeECKeyPair_destroy(&pair1);
  82. AnodeECKeyPair_destroy(&pair2);
  83. AnodeECKeyPair_destroy(&pair3);
  84. return 0;
  85. }