simple-speck.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "hal.h"
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include "simpleserial.h"
  5. #include "speck.h"
  6. u8 gkey[32] = {0x00};
  7. uint8_t get_key(uint8_t* k, uint8_t len) {
  8. // Load key here
  9. simpleserial_put('o', 32, gkey);
  10. return 0x00;
  11. }
  12. uint8_t set_key(uint8_t* key, uint8_t len) {
  13. // copy keybytes
  14. memcpy(gkey, key, 32);
  15. simpleserial_put('o', 32, gkey);
  16. return 0x00;
  17. }
  18. uint8_t get_pt(uint8_t* pt, uint8_t len) {
  19. /**********************************
  20. * Start user-specific code here. */
  21. trigger_high();
  22. //16 hex bytes held in 'pt' were sent
  23. //from the computer. Store your response
  24. //back into 'pt', which will send 16 bytes
  25. //back to computer. Can ignore of course if
  26. //not needed
  27. volatile uint8_t ppp[] = {0x42, 0x41, 0x41, 0x41,0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 };
  28. trigger_low();
  29. /* End user-specific code here. *
  30. ********************************/
  31. simpleserial_put('r', 16, ppp);
  32. return 0x00;
  33. }
  34. uint8_t encrypt_block(uint8_t* pt, uint8_t len) {
  35. trigger_high(); // TRIGGER START
  36. //u8 pt[16] = {0x70, 0x6f, 0x6f, 0x6e, 0x65, 0x72, 0x2e, 0x20, 0x49, 0x6e, 0x20, 0x74, 0x68, 0x6f, 0x73, 0x65};
  37. u8 key[32] = {0x00};
  38. memcpy(key, gkey, 32); // copy the globally set encryption key
  39. u8 ct[16] = {0x00};
  40. EncryptBlock(pt, key, ct);
  41. trigger_low(); // TRIGGER STOP
  42. simpleserial_put('c', 16, ct);
  43. return 0x00;
  44. }
  45. uint8_t reset(uint8_t* x, uint8_t len) {
  46. simpleserial_put('r', 0, NULL);
  47. // Reset key here if needed
  48. return 0x00;
  49. }
  50. int main(void) {
  51. platform_init();
  52. init_uart();
  53. trigger_setup();
  54. /* Uncomment this to get a HELLO message for debug */
  55. putch('h');
  56. putch('e');
  57. putch('l');
  58. putch('l');
  59. putch('o');
  60. putch('\n');
  61. simpleserial_init();
  62. simpleserial_addcmd('p', 16, get_pt);
  63. simpleserial_addcmd('l', 0, get_pt);
  64. simpleserial_addcmd('e', 16, encrypt_block);
  65. simpleserial_addcmd('k', 16, get_key);
  66. simpleserial_addcmd('s', 32, set_key);
  67. simpleserial_addcmd('x', 0, reset);
  68. while(1)
  69. simpleserial_get();
  70. }