SnP-Relaned.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
  3. Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
  4. denoted as "the implementer".
  5. For more information, feedback or questions, please refer to our websites:
  6. http://keccak.noekeon.org/
  7. http://keyak.noekeon.org/
  8. http://ketje.noekeon.org/
  9. To the extent possible under law, the implementer has waived all copyright
  10. and related or neighboring rights to the source code in this file.
  11. http://creativecommons.org/publicdomain/zero/1.0/
  12. */
  13. #ifndef _SnP_Relaned_h_
  14. #define _SnP_Relaned_h_
  15. #define SnP_AddBytes(state, data, offset, length, SnP_AddLanes, SnP_AddBytesInLane, SnP_laneLengthInBytes) \
  16. { \
  17. if ((offset) == 0) { \
  18. SnP_AddLanes(state, data, (length)/SnP_laneLengthInBytes); \
  19. SnP_AddBytesInLane(state, \
  20. (length)/SnP_laneLengthInBytes, \
  21. (data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
  22. 0, \
  23. (length)%SnP_laneLengthInBytes); \
  24. } \
  25. else { \
  26. unsigned int _sizeLeft = (length); \
  27. unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
  28. unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
  29. const unsigned char *_curData = (data); \
  30. while(_sizeLeft > 0) { \
  31. unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
  32. if (_bytesInLane > _sizeLeft) \
  33. _bytesInLane = _sizeLeft; \
  34. SnP_AddBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
  35. _sizeLeft -= _bytesInLane; \
  36. _lanePosition++; \
  37. _offsetInLane = 0; \
  38. _curData += _bytesInLane; \
  39. } \
  40. } \
  41. }
  42. #define SnP_OverwriteBytes(state, data, offset, length, SnP_OverwriteLanes, SnP_OverwriteBytesInLane, SnP_laneLengthInBytes) \
  43. { \
  44. if ((offset) == 0) { \
  45. SnP_OverwriteLanes(state, data, (length)/SnP_laneLengthInBytes); \
  46. SnP_OverwriteBytesInLane(state, \
  47. (length)/SnP_laneLengthInBytes, \
  48. (data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
  49. 0, \
  50. (length)%SnP_laneLengthInBytes); \
  51. } \
  52. else { \
  53. unsigned int _sizeLeft = (length); \
  54. unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
  55. unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
  56. const unsigned char *_curData = (data); \
  57. while(_sizeLeft > 0) { \
  58. unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
  59. if (_bytesInLane > _sizeLeft) \
  60. _bytesInLane = _sizeLeft; \
  61. SnP_OverwriteBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
  62. _sizeLeft -= _bytesInLane; \
  63. _lanePosition++; \
  64. _offsetInLane = 0; \
  65. _curData += _bytesInLane; \
  66. } \
  67. } \
  68. }
  69. #define SnP_ExtractBytes(state, data, offset, length, SnP_ExtractLanes, SnP_ExtractBytesInLane, SnP_laneLengthInBytes) \
  70. { \
  71. if ((offset) == 0) { \
  72. SnP_ExtractLanes(state, data, (length)/SnP_laneLengthInBytes); \
  73. SnP_ExtractBytesInLane(state, \
  74. (length)/SnP_laneLengthInBytes, \
  75. (data)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
  76. 0, \
  77. (length)%SnP_laneLengthInBytes); \
  78. } \
  79. else { \
  80. unsigned int _sizeLeft = (length); \
  81. unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
  82. unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
  83. unsigned char *_curData = (data); \
  84. while(_sizeLeft > 0) { \
  85. unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
  86. if (_bytesInLane > _sizeLeft) \
  87. _bytesInLane = _sizeLeft; \
  88. SnP_ExtractBytesInLane(state, _lanePosition, _curData, _offsetInLane, _bytesInLane); \
  89. _sizeLeft -= _bytesInLane; \
  90. _lanePosition++; \
  91. _offsetInLane = 0; \
  92. _curData += _bytesInLane; \
  93. } \
  94. } \
  95. }
  96. #define SnP_ExtractAndAddBytes(state, input, output, offset, length, SnP_ExtractAndAddLanes, SnP_ExtractAndAddBytesInLane, SnP_laneLengthInBytes) \
  97. { \
  98. if ((offset) == 0) { \
  99. SnP_ExtractAndAddLanes(state, input, output, (length)/SnP_laneLengthInBytes); \
  100. SnP_ExtractAndAddBytesInLane(state, \
  101. (length)/SnP_laneLengthInBytes, \
  102. (input)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
  103. (output)+((length)/SnP_laneLengthInBytes)*SnP_laneLengthInBytes, \
  104. 0, \
  105. (length)%SnP_laneLengthInBytes); \
  106. } \
  107. else { \
  108. unsigned int _sizeLeft = (length); \
  109. unsigned int _lanePosition = (offset)/SnP_laneLengthInBytes; \
  110. unsigned int _offsetInLane = (offset)%SnP_laneLengthInBytes; \
  111. const unsigned char *_curInput = (input); \
  112. unsigned char *_curOutput = (output); \
  113. while(_sizeLeft > 0) { \
  114. unsigned int _bytesInLane = SnP_laneLengthInBytes - _offsetInLane; \
  115. if (_bytesInLane > _sizeLeft) \
  116. _bytesInLane = _sizeLeft; \
  117. SnP_ExtractAndAddBytesInLane(state, _lanePosition, _curInput, _curOutput, _offsetInLane, _bytesInLane); \
  118. _sizeLeft -= _bytesInLane; \
  119. _lanePosition++; \
  120. _offsetInLane = 0; \
  121. _curInput += _bytesInLane; \
  122. _curOutput += _bytesInLane; \
  123. } \
  124. } \
  125. }
  126. #endif