simple-speck.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. u8 rand_data[8] = {0x00};
  8. uint8_t get_key(uint8_t* k, uint8_t len) {
  9. simpleserial_put('o', 8, gkey);
  10. return 0x00;
  11. }
  12. uint8_t set_key(uint8_t* key, uint8_t len) {
  13. memcpy(gkey, key, 8);
  14. int sum = 0;
  15. for(int i=0; i < 8; i++) {
  16. sum += key[i];
  17. }
  18. // initialize random value
  19. srand(sum);
  20. for(int i=0; i < 8; i++) {
  21. rand_data[i] = rand() & 0xFF;
  22. }
  23. return 0x00;
  24. }
  25. uint8_t get_pt(uint8_t* pt, uint8_t len) {
  26. /**********************************
  27. * Start user-specific code here. */
  28. trigger_high();
  29. // Only for testing purposes
  30. volatile uint8_t testing_output[] = {0x42, 0x41, 0x41, 0x41,0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 };
  31. trigger_low();
  32. /* End user-specific code here. *
  33. ********************************/
  34. simpleserial_put('r', 16, testing_output);
  35. return 0x00;
  36. }
  37. uint8_t encrypt_block(uint8_t* pt, uint8_t len) {
  38. trigger_high(); // TRIGGER START
  39. u8 key[8] = {0x00};
  40. memcpy(key, gkey, 8); // copy the globally set encryption key
  41. u8 ct[4] = {0x00};
  42. Speck3264_EncryptBlock(pt, key, ct, rand_data); // the encryption happens here
  43. trigger_low(); // TRIGGER STOP
  44. simpleserial_put('c', 4, ct);
  45. return 0x00;
  46. }
  47. uint8_t reset(uint8_t* x, uint8_t len) {
  48. simpleserial_put('r', 0, NULL);
  49. // Reset key here if needed
  50. return 0x00;
  51. }
  52. int main(void) {
  53. platform_init();
  54. init_uart();
  55. trigger_setup();
  56. /* Uncomment this to get a HELLO message for debug */
  57. /*
  58. putch('h');
  59. putch('e');
  60. putch('l');
  61. putch('l');
  62. putch('o');
  63. putch('\n');
  64. */
  65. simpleserial_init();
  66. simpleserial_addcmd('p', 16, get_pt);
  67. simpleserial_addcmd('e', 4, encrypt_block);
  68. simpleserial_addcmd('k', 4, get_key);
  69. simpleserial_addcmd('s', 8, set_key);
  70. simpleserial_addcmd('x', 0, reset);
  71. while(1)
  72. simpleserial_get();
  73. }