Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

92 lines
2.3 KiB

  1. /**
  2. * @cond internal
  3. * @file test_x25519.c
  4. * @copyright
  5. * Copyright (c) 2016 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. * @brief Tests for x25519 key exchange and signatures.
  9. */
  10. #include "x25519.h"
  11. #include "strobe_config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. static void __attribute__((unused))
  17. randomize(uint8_t foo[X25519_BYTES]) {
  18. unsigned i;
  19. static unsigned int seed = 0x12345678;
  20. for (i=0; i<X25519_BYTES; i++) {
  21. seed += seed*seed | 5;
  22. foo[i] = seed>>24;
  23. }
  24. }
  25. int main(int argc, char **argv) {
  26. (void)argc; (void)argv;
  27. int i;
  28. unsigned char
  29. secret1[X25519_BYTES],
  30. public1[X25519_BYTES],
  31. secret2[X25519_BYTES],
  32. public2[X25519_BYTES],
  33. shared1[X25519_BYTES],
  34. shared2[X25519_BYTES];
  35. for (i=0; i<1000; i++) {
  36. randomize(secret1);
  37. x25519_base(public1,secret1,i%2);
  38. randomize(secret2);
  39. x25519_base(public2,secret2,i%2);
  40. x25519(shared1,secret1,public2,i%2);
  41. x25519(shared2,secret2,public1,i%2);
  42. if (memcmp(shared1,shared2,sizeof(shared1))) {
  43. printf("FAIL shared %d\n",i);
  44. }
  45. }
  46. #if X25519_SUPPORT_SIGN && X25519_SUPPORT_VERIFY
  47. unsigned char
  48. eph_secret[X25519_BYTES],
  49. eph_public[X25519_BYTES],
  50. challenge[X25519_BYTES],
  51. response[X25519_BYTES];
  52. for (i=0; i<1000; i++) {
  53. randomize(secret1);
  54. x25519_base(public1,secret1,0);
  55. randomize(eph_secret);
  56. x25519_base(eph_public,eph_secret,0);
  57. randomize(challenge);
  58. x25519_sign_p2(response,challenge,eph_secret,secret1);
  59. if (0 != x25519_verify_p2(response,challenge,eph_public,public1)) {
  60. printf("FAIL sign %d\n",i);
  61. }
  62. challenge[4] ^= 1;
  63. if (0 == x25519_verify_p2(response,challenge,eph_public,public1)) {
  64. printf("FAIL unsign %d\n",i);
  65. }
  66. }
  67. #endif
  68. unsigned char base[X25519_BYTES] = {9};
  69. unsigned char key[X25519_BYTES] = {9};
  70. unsigned char *b = base, *k = key, *tmp;
  71. for (i=0; i<1000; i++) {
  72. x25519(b,k,b,1);
  73. tmp = b; b = k; k = tmp;
  74. }
  75. for (i=0; i<X25519_BYTES; i++) printf("%02x", k[i]);
  76. printf("\n");
  77. return 0;
  78. }