simple-speck.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "hal.h"
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include "simpleserial.h"
  5. #include "speck.h"
  6. u8 gkey[8] = {0x00};
  7. uint8_t get_key(uint8_t* k, uint8_t len) {
  8. simpleserial_put('o', 8, gkey);
  9. return 0x00;
  10. }
  11. uint8_t set_key(uint8_t* key, uint8_t len) {
  12. memcpy(gkey, key, 8);
  13. return 0x00;
  14. }
  15. uint8_t get_pt(uint8_t* pt, uint8_t len) {
  16. /**********************************
  17. * Start user-specific code here. */
  18. trigger_high();
  19. // Only for testing purposes
  20. volatile uint8_t testing_output[] = {0x42, 0x41, 0x41, 0x41,0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 };
  21. trigger_low();
  22. /* End user-specific code here. *
  23. ********************************/
  24. simpleserial_put('r', 16, testing_output);
  25. return 0x00;
  26. }
  27. uint8_t encrypt_block(uint8_t* pt, uint8_t len) {
  28. trigger_high(); // TRIGGER START
  29. u8 key[8] = {0x00};
  30. memcpy(key, gkey, 8); // copy the globally set encryption key
  31. u8 ct[4] = {0x00};
  32. Speck3264_EncryptBlock(pt, key, ct); // the encryption happens here
  33. trigger_low(); // TRIGGER STOP
  34. simpleserial_put('c', 4, ct);
  35. return 0x00;
  36. }
  37. uint8_t reset(uint8_t* x, uint8_t len) {
  38. simpleserial_put('r', 0, NULL);
  39. // Reset key here if needed
  40. return 0x00;
  41. }
  42. int main(void) {
  43. platform_init();
  44. init_uart();
  45. trigger_setup();
  46. /* Uncomment this to get a HELLO message for debug */
  47. /*
  48. putch('h');
  49. putch('e');
  50. putch('l');
  51. putch('l');
  52. putch('o');
  53. putch('\n');
  54. */
  55. simpleserial_init();
  56. simpleserial_addcmd('p', 16, get_pt);
  57. simpleserial_addcmd('e', 4, encrypt_block);
  58. simpleserial_addcmd('k', 4, get_key);
  59. simpleserial_addcmd('s', 8, set_key);
  60. simpleserial_addcmd('x', 0, reset);
  61. while(1)
  62. simpleserial_get();
  63. }