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.
 
 
 
 
 
 

119 lines
3.7 KiB

  1. /**
  2. * @cond internal
  3. * @file keccak_f.c.inc
  4. * @copyright
  5. * Copyright (c) 2015-2016 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. *
  8. * Loosely based on CC0 implementations of Keccak-F:
  9. * Keccak-Tiny:
  10. * David Leon Gil
  11. * TweetFIPS202:
  12. * Dan J Bernstein
  13. * Peter Schwabe
  14. * Gilles van Assche
  15. *
  16. * @author Mike Hamburg
  17. *
  18. * @brief Keccak-f[n] implementation. Designed to be included in another C
  19. * file, so no headers.
  20. */
  21. /* Could lose this to save size, maybe, depends on arch */
  22. #ifndef STROBE_OPT_RC_TABLE
  23. #define STROBE_OPT_RC_TABLE 1
  24. #endif
  25. /* Helper macros to unroll the permutation. */
  26. #define REPEAT5(e) e e e e e
  27. #if STROBE_OPT_FOR_SIZE // Size + 0 bytes, speed x 1/2
  28. # define FOR51(v, e) v = 0; REPEAT5(e; v += 1;)
  29. # define FOR55(v, e) for (v=0; v<25; v+= 5) { e; }
  30. # define REPEAT24(e) {int _j=0; for (_j=0; _j<24; _j++) { e }}
  31. #elif STROBE_OPT_FOR_SPEED // Size + 600 bytes, speed x1
  32. # define FOR51(v, e) v = 0; REPEAT5(e; v += 1;)
  33. # define FOR55(v, e) v = 0; REPEAT5(e; v += 5;)
  34. # define REPEAT24(e) e e e e e e e e e e e e e e e e e e e e e e e e
  35. #elif STROBE_OPT_FOR_SIZE_AGGRESSIVE // Terrible. Actually makes things bigger
  36. # define FOR51(v, e) for (v=0; v<5; v++) { e; }
  37. # define FOR55(v, e) for (v=0; v<25; v+= 5) { e; }
  38. # define REPEAT24(e) {int _j=0; for (_j=0; _j<24; _j++) { e }}
  39. #else // Size + 100 bytes, speed x 3/4
  40. # define FOR51(v, e) v = 0; REPEAT5(e; v += 1;)
  41. # define FOR55(v, e) for (v=0; v<25; v+= 5) { e; }
  42. # define REPEAT24(e) e e e e e e e e e e e e e e e e e e e e e e e e
  43. #endif
  44. #if STROBE_INTEROP_F_BITS == 1600
  45. #define NROUNDS 24
  46. #elif STROBE_INTEROP_F_BITS == 800
  47. #define NROUNDS 22
  48. #elif STROBE_INTEROP_F_BITS == 400
  49. #define NROUNDS 20
  50. #elif sSTROBE_INTEROP_F_BITS == 200
  51. #define NROUNDS 18
  52. #else
  53. #error "Only implementing KeccakF[200,400,800,1600]'"
  54. #endif
  55. /** Rotate left */
  56. static inline kword_t rol(kword_t x, int s) {
  57. static const int WBITS = 8*sizeof(kword_t);
  58. s %= WBITS;
  59. return (x << s) | (x >> (WBITS - s));
  60. }
  61. /*** The keccak-f[] permutation ***/
  62. static void keccak_f(kdomain_s *state) {
  63. const uint8_t pi[24] = {
  64. 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
  65. 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1
  66. };
  67. #define RC_B(x,n) ((((x##ull)>>n)&1)<<((1<<n)-1))
  68. #define RC_X(x) ((kword_t)(RC_B(x,0)|RC_B(x,1)|RC_B(x,2)|RC_B(x,3)|RC_B(x,4)|RC_B(x,5)|RC_B(x,6)))
  69. const kword_t RC[NROUNDS] = {
  70. #if NROUNDS >= 24
  71. RC_X(0x74), RC_X(0x21),
  72. #endif
  73. #if NROUNDS >= 22
  74. RC_X(0x58), RC_X(0x79),
  75. #endif
  76. #if NROUNDS >= 20
  77. RC_X(0x66), RC_X(0x16),
  78. #endif
  79. RC_X(0x48), RC_X(0x52), RC_X(0x53), RC_X(0x5d), RC_X(0x4f), RC_X(0x3f),
  80. RC_X(0x26), RC_X(0x35), RC_X(0x0c), RC_X(0x0e), RC_X(0x55), RC_X(0x79),
  81. RC_X(0x21), RC_X(0x1f), RC_X(0x70), RC_X(0x5e), RC_X(0x1a), RC_X(0x01)
  82. };
  83. kword_t* a = state->w;
  84. kword_t b[5] = {0}, t, u;
  85. unsigned int x, y;
  86. int i;
  87. for (i=0; i<25; i++) a[i] = eswap_letoh(a[i]);
  88. for (i = NROUNDS-1; i >=0; i--) {
  89. // Theta
  90. FOR51(x, b[x] = 0;)
  91. FOR55(y, FOR51(x, b[x] ^= a[x + y];))
  92. FOR55(y, FOR51(x,
  93. a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1);
  94. ))
  95. // Rho and pi
  96. t = a[1];
  97. x = y = 0;
  98. REPEAT24(u = a[pi[x]]; y += x+1; a[pi[x]] = rol(t, y); t = u; x++; )
  99. // Chi
  100. FOR55(y,
  101. FOR51(x, b[x] = a[y + x];)
  102. FOR51(x, a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]);)
  103. )
  104. // Iota
  105. a[0] ^= RC[i];
  106. }
  107. for (i=0; i<25; i++) a[i] = eswap_htole(a[i]);
  108. }