Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*! \file xtea.c
  2. \author Thomas.Hoehenleitner [at] seerose.net
  3. *******************************************************************************/
  4. #include "xtea.h"
  5. #include "trice.h"
  6. #if ((TRICE_DIRECT_XTEA_ENCRYPT == 1) || (TRICE_DEFERRED_XTEA_ENCRYPT == 1)) && TRICE_OFF == 0
  7. //! golang XTEA works with 64 rounds
  8. static const unsigned int numRounds = 64;
  9. //! 128 bit static key
  10. static const uint32_t k[4] = XTEA_ENCRYPT_KEY;
  11. //! internal constant
  12. static const uint32_t delta = 0x9E3779B9;
  13. //! precomputed values for faster execution
  14. static uint32_t table[64];
  15. //! XTEAInitTable precalculates the table.
  16. //! It is possible to put this table completely into FLASH by precomputing it during compile time.
  17. void XTEAInitTable(void) {
  18. uint32_t sum = 0;
  19. unsigned i;
  20. // Two rounds of XTEA applied per loop
  21. for (i = 0; i < numRounds;) {
  22. table[i] = sum + k[sum & 3];
  23. i++;
  24. sum += delta;
  25. table[i] = sum + k[(sum >> 11) & 3]; // lint !e661 Warning 661: Possible access of out-of-bounds pointer (1 beyond end of data) by operator '['
  26. i++;
  27. }
  28. }
  29. // encipher converts 64 bits.
  30. //! Code taken and adapted from xtea\block.go
  31. //!\param v 64 bits of data in v[0] and v[1] are encoded in place
  32. static void encipher(uint32_t v[2]) {
  33. uint32_t v0 = v[0], v1 = v[1];
  34. unsigned i;
  35. for (i = 0; i < numRounds;) {
  36. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ table[i];
  37. i++;
  38. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ table[i]; // lint !e661 Warning 661: Possible access of out-of-bounds pointer (1 beyond end of data) by operator '['
  39. i++;
  40. }
  41. v[0] = v0;
  42. v[1] = v1;
  43. }
  44. #if XTEA_DECRYPT == 1
  45. //! decipher reverses encipher action.
  46. //! Code taken and adapted from xtea\block.go
  47. //!\param v 64 bits of data in v[0] and v[1] are decoded in place
  48. static void decipher(uint32_t v[2]) {
  49. uint32_t v0 = v[0], v1 = v[1];
  50. for (int i = numRounds; i > 0;) {
  51. i--;
  52. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ table[i];
  53. i--;
  54. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ table[i];
  55. }
  56. v[0] = v0;
  57. v[1] = v1;
  58. }
  59. //! XTEADecrypt re-converts from xtea cipher.
  60. //! \param p pointer to 8 byte buffer
  61. //! count is expected to be an even number.
  62. void XTEADecrypt(uint32_t* p, unsigned count) {
  63. for (int i = 0; i < count; i += 2) {
  64. decipher(&p[i]); // byte swapping is done inside receiver according to endianness.
  65. }
  66. }
  67. #endif // #if XTEA_DECRYPT == 1
  68. //! XTEAEncrypt converts to xtea cipher.
  69. //! \param p pointer to 8 byte buffer.
  70. //! count is expected to be an even number.
  71. void XTEAEncrypt(uint32_t* p, unsigned count) {
  72. unsigned i;
  73. for (i = 0; i < count; i += 2) {
  74. encipher(&p[i]); // byte swap is done inside receiver
  75. }
  76. }
  77. #endif // #if ((TRICE_DIRECT_XTEA_ENCRYPT == 1) || (TRICE_DEFERRED_XTEA_ENCRYPT == 1)) && TRICE_OFF == 0