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.
 
 
 
 
 
 

88 lines
2.6 KiB

  1. /*-
  2. * Copyright 2022 John-Mark Gurney.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. *
  25. */
  26. #include <stdint.h>
  27. #include <x25519.h>
  28. #include <board/simpflash.h>
  29. #include <strobe_rng_init.h> /* roundup */
  30. #include <strobe_pki.h>
  31. uint8_t keyvalid;
  32. static const uint8_t privkey[roundup(EC_PRIVATE_BYTES, sizeof(uint8_t)) / sizeof(uint8_t)] __attribute__ ((section (".savekeys"))) = {
  33. #if 0
  34. 0xae, 0xe7, 0xdd, 0x04, 0x84, 0xb3, 0xcd, 0x3c,
  35. 0xef, 0x25, 0x71, 0x83, 0xc4, 0x6c, 0x5d, 0x3c,
  36. 0xee, 0x98, 0xee, 0x79, 0xf2, 0x97, 0x6a, 0xe8,
  37. 0x39, 0xec, 0x7d, 0xe8, 0x23, 0xe7, 0x20, 0xdb,
  38. #endif
  39. };
  40. #include <public_key.h>
  41. void
  42. get_pubkey(uint8_t pubkey[EC_PUBLIC_BYTES])
  43. {
  44. x25519_base(pubkey, privkey, 1);
  45. }
  46. struct strobepkikey
  47. get_key(void)
  48. {
  49. struct strobepkikey spk;
  50. uint8_t key[sizeof privkey] = {};
  51. uint8_t keyf[sizeof privkey] = {};
  52. int r;
  53. memset(keyf, 0xff, sizeof keyf);
  54. if (memcmp(key, privkey, sizeof privkey) == 0 ||
  55. memcmp(keyf, privkey, sizeof privkey) == 0) {
  56. /* Generate new key */
  57. do {
  58. r = strobe_randomize((uint8_t *)key, sizeof key);
  59. if (r < 0)
  60. continue;
  61. } while (r != 0);
  62. /* and write it. */
  63. doflash(privkey, key, sizeof key);
  64. }
  65. spk = (struct strobepkikey){
  66. .privkey.pkt = (void *)(uintptr_t)privkey,
  67. .privkey.pktlen = EC_PRIVATE_BYTES,
  68. .pubkey.pkt = (void *)(uintptr_t)pubkey,
  69. .pubkey.pktlen = EC_PUBLIC_BYTES,
  70. };
  71. return spk;
  72. }