hash_sha3.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Sara Golemon <pollita@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php_hash.h"
  17. #include "php_hash_sha3.h"
  18. #ifdef HAVE_SLOW_HASH3
  19. // ================= slow algo ==============================================
  20. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && \
  21. (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  22. # if defined(__LITTLE_ENDIAN__)
  23. # undef WORDS_BIGENDIAN
  24. # else
  25. # if defined(__BIG_ENDIAN__)
  26. # define WORDS_BIGENDIAN
  27. # endif
  28. # endif
  29. #endif
  30. static inline uint64_t rol64(uint64_t v, unsigned char b) {
  31. return (v << b) | (v >> (64 - b));
  32. }
  33. static inline unsigned char idx(unsigned char x, unsigned char y) {
  34. return x + (5 * y);
  35. }
  36. #ifdef WORDS_BIGENDIAN
  37. static inline uint64_t load64(const unsigned char* x) {
  38. signed char i;
  39. uint64_t ret = 0;
  40. for (i = 7; i >= 0; --i) {
  41. ret <<= 8;
  42. ret |= x[i];
  43. }
  44. return ret;
  45. }
  46. static inline void store64(unsigned char* x, uint64_t val) {
  47. size_t i;
  48. for (i = 0; i < 8; ++i) {
  49. x[i] = val & 0xFF;
  50. val >>= 8;
  51. }
  52. }
  53. static inline void xor64(unsigned char* x, uint64_t val) {
  54. size_t i;
  55. for (i = 0; i < 8; ++i) {
  56. x[i] ^= val & 0xFF;
  57. val >>= 8;
  58. }
  59. }
  60. # define readLane(x, y) load64(ctx->state+sizeof(uint64_t)*idx(x, y))
  61. # define writeLane(x, y, v) store64(ctx->state+sizeof(uint64_t)*idx(x, y), v)
  62. # define XORLane(x, y, v) xor64(ctx->state+sizeof(uint64_t)*idx(x, y), v)
  63. #else
  64. # define readLane(x, y) (((uint64_t*)ctx->state)[idx(x,y)])
  65. # define writeLane(x, y, v) (((uint64_t*)ctx->state)[idx(x,y)] = v)
  66. # define XORLane(x, y, v) (((uint64_t*)ctx->state)[idx(x,y)] ^= v)
  67. #endif
  68. static inline char LFSR86540(unsigned char* pLFSR)
  69. {
  70. unsigned char LFSR = *pLFSR;
  71. char result = LFSR & 0x01;
  72. if (LFSR & 0x80) {
  73. // Primitive polynomial over GF(2): x^8+x^6+x^5+x^4+1
  74. LFSR = (LFSR << 1) ^ 0x71;
  75. } else {
  76. LFSR <<= 1;
  77. }
  78. *pLFSR = LFSR;
  79. return result;
  80. }
  81. static void permute(PHP_SHA3_CTX* ctx) {
  82. unsigned char LFSRstate = 0x01;
  83. unsigned char round;
  84. for (round = 0; round < 24; ++round) {
  85. { // Theta step (see [Keccak Reference, Section 2.3.2])
  86. uint64_t C[5], D;
  87. unsigned char x, y;
  88. for (x = 0; x < 5; ++x) {
  89. C[x] = readLane(x, 0) ^ readLane(x, 1) ^
  90. readLane(x, 2) ^ readLane(x, 3) ^ readLane(x, 4);
  91. }
  92. for (x = 0; x < 5; ++x) {
  93. D = C[(x+4)%5] ^ rol64(C[(x+1)%5], 1);
  94. for (y = 0; y < 5; ++y) {
  95. XORLane(x, y, D);
  96. }
  97. }
  98. }
  99. { // p and Pi steps (see [Keccak Reference, Sections 2.3.3 and 2.3.4])
  100. unsigned char x = 1, y = 0, t;
  101. uint64_t current = readLane(x, y);
  102. for (t = 0; t < 24; ++t) {
  103. unsigned char r = ((t + 1) * (t + 2) / 2) % 64;
  104. unsigned char Y = (2*x + 3*y) % 5;
  105. uint64_t temp;
  106. x = y;
  107. y = Y;
  108. temp = readLane(x, y);
  109. writeLane(x, y, rol64(current, r));
  110. current = temp;
  111. }
  112. }
  113. { // X step (see [Keccak Reference, Section 2.3.1])
  114. unsigned char x, y;
  115. for (y = 0; y < 5; ++y) {
  116. uint64_t temp[5];
  117. for (x = 0; x < 5; ++x) {
  118. temp[x] = readLane(x, y);
  119. }
  120. for (x = 0; x < 5; ++x) {
  121. writeLane(x, y, temp[x] ^((~temp[(x+1)%5]) & temp[(x+2)%5]));
  122. }
  123. }
  124. }
  125. { // i step (see [Keccak Reference, Section 2.3.5])
  126. unsigned char j;
  127. for (j = 0; j < 7; ++j) {
  128. if (LFSR86540(&LFSRstate)) {
  129. uint64_t bitPos = (1<<j) - 1;
  130. XORLane(0, 0, (uint64_t)1 << bitPos);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. // ==========================================================================
  137. static void PHP_SHA3_Init(PHP_SHA3_CTX* ctx,
  138. int bits) {
  139. memset(ctx, 0, sizeof(PHP_SHA3_CTX));
  140. }
  141. static void PHP_SHA3_Update(PHP_SHA3_CTX* ctx,
  142. const unsigned char* buf,
  143. size_t count,
  144. size_t block_size) {
  145. while (count > 0) {
  146. size_t len = block_size - ctx->pos;
  147. if (len > count) {
  148. len = count;
  149. }
  150. count -= len;
  151. while (len-- > 0) {
  152. ctx->state[ctx->pos++] ^= *(buf++);
  153. }
  154. if (ctx->pos >= block_size) {
  155. permute(ctx);
  156. ctx->pos = 0;
  157. }
  158. }
  159. }
  160. static void PHP_SHA3_Final(unsigned char* digest,
  161. PHP_SHA3_CTX* ctx,
  162. size_t block_size,
  163. size_t digest_size) {
  164. size_t len = digest_size;
  165. // Pad state to finalize
  166. ctx->state[ctx->pos++] ^= 0x06;
  167. ctx->state[block_size-1] ^= 0x80;
  168. permute(ctx);
  169. // Square output for digest
  170. for(;;) {
  171. int bs = (len < block_size) ? len : block_size;
  172. memcpy(digest, ctx->state, bs);
  173. digest += bs;
  174. len -= bs;
  175. if (!len) break;
  176. permute(ctx);
  177. }
  178. // Zero out context
  179. ZEND_SECURE_ZERO(ctx, sizeof(PHP_SHA3_CTX));
  180. }
  181. static int php_sha3_unserialize(php_hashcontext_object *hash,
  182. zend_long magic,
  183. const zval *zv,
  184. size_t block_size)
  185. {
  186. PHP_SHA3_CTX *ctx = (PHP_SHA3_CTX *) hash->context;
  187. int r = FAILURE;
  188. if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
  189. && (r = php_hash_unserialize_spec(hash, zv, PHP_SHA3_SPEC)) == SUCCESS
  190. && ctx->pos < block_size) {
  191. return SUCCESS;
  192. } else {
  193. return r != SUCCESS ? r : -2000;
  194. }
  195. }
  196. // ==========================================================================
  197. #define DECLARE_SHA3_OPS(bits) \
  198. void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { \
  199. PHP_SHA3_Init(ctx, bits); \
  200. } \
  201. void PHP_SHA3##bits##Update(PHP_SHA3_##bits##_CTX* ctx, \
  202. const unsigned char* input, \
  203. size_t inputLen) { \
  204. PHP_SHA3_Update(ctx, input, inputLen, \
  205. (1600 - (2 * bits)) >> 3); \
  206. } \
  207. void PHP_SHA3##bits##Final(unsigned char* digest, \
  208. PHP_SHA3_##bits##_CTX* ctx) { \
  209. PHP_SHA3_Final(digest, ctx, \
  210. (1600 - (2 * bits)) >> 3, \
  211. bits >> 3); \
  212. } \
  213. static int php_sha3##bits##_unserialize(php_hashcontext_object *hash, \
  214. zend_long magic, \
  215. const zval *zv) { \
  216. return php_sha3_unserialize(hash, magic, zv, (1600 - (2 * bits)) >> 3); \
  217. } \
  218. const php_hash_ops php_hash_sha3_##bits##_ops = { \
  219. "sha3-" #bits, \
  220. (php_hash_init_func_t) PHP_SHA3##bits##Init, \
  221. (php_hash_update_func_t) PHP_SHA3##bits##Update, \
  222. (php_hash_final_func_t) PHP_SHA3##bits##Final, \
  223. php_hash_copy, \
  224. php_hash_serialize, \
  225. php_sha3##bits##_unserialize, \
  226. PHP_SHA3_SPEC, \
  227. bits >> 3, \
  228. (1600 - (2 * bits)) >> 3, \
  229. sizeof(PHP_SHA3_##bits##_CTX), \
  230. 1 \
  231. }
  232. #else
  233. // ================= fast algo ==============================================
  234. #define SUCCESS SHA3_SUCCESS /* Avoid conflict between KeccacHash.h and zend_types.h */
  235. #include "KeccakHash.h"
  236. /* KECCAK SERIALIZATION
  237. Keccak_HashInstance consists of:
  238. KeccakWidth1600_SpongeInstance {
  239. unsigned char state[200];
  240. unsigned int rate; -- fixed for digest size
  241. unsigned int byteIOIndex; -- in range [0, rate/8)
  242. int squeezing; -- 0 normally, 1 only during finalize
  243. } sponge;
  244. unsigned int fixedOutputLength; -- fixed for digest size
  245. unsigned char delimitedSuffix; -- fixed for digest size
  246. NB If the external sha3/ library is updated, the serialization code
  247. may need to be updated.
  248. The simpler SHA3 code's serialization states are not interchangeable with
  249. Keccak. Furthermore, the Keccak sponge state is sensitive to architecture
  250. -- 32-bit and 64-bit implementations produce different states. It does not
  251. appear that the state is sensitive to endianness. */
  252. #if Keccak_HashInstance_ImplType == 64
  253. /* corresponds to sha3/generic64lc */
  254. # define PHP_HASH_SERIALIZE_MAGIC_KECCAK 100
  255. #elif Keccak_HashInstance_ImplType == 32
  256. /* corresponds to sha3/generic32lc */
  257. # define PHP_HASH_SERIALIZE_MAGIC_KECCAK 101
  258. #else
  259. # error "Unknown Keccak_HashInstance_ImplType"
  260. #endif
  261. #define PHP_KECCAK_SPEC "b200IiIIB"
  262. static int php_keccak_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv)
  263. {
  264. *magic = PHP_HASH_SERIALIZE_MAGIC_KECCAK;
  265. return php_hash_serialize_spec(hash, zv, PHP_KECCAK_SPEC);
  266. }
  267. static int php_keccak_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
  268. {
  269. Keccak_HashInstance *ctx = (Keccak_HashInstance *) hash->context;
  270. int r = FAILURE;
  271. if (magic == PHP_HASH_SERIALIZE_MAGIC_KECCAK
  272. && (r = php_hash_unserialize_spec(hash, zv, PHP_KECCAK_SPEC)) == SUCCESS
  273. && ctx->sponge.byteIOIndex < ctx->sponge.rate / 8) {
  274. return SUCCESS;
  275. } else {
  276. return r != SUCCESS ? r : -2000;
  277. }
  278. }
  279. // ==========================================================================
  280. #define DECLARE_SHA3_OPS(bits) \
  281. void PHP_SHA3##bits##Init(PHP_SHA3_##bits##_CTX* ctx, ZEND_ATTRIBUTE_UNUSED HashTable *args) { \
  282. ZEND_ASSERT(sizeof(Keccak_HashInstance) <= sizeof(PHP_SHA3_##bits##_CTX)); \
  283. Keccak_HashInitialize_SHA3_##bits((Keccak_HashInstance *)ctx); \
  284. } \
  285. void PHP_SHA3##bits##Update(PHP_SHA3_##bits##_CTX* ctx, \
  286. const unsigned char* input, \
  287. size_t inputLen) { \
  288. Keccak_HashUpdate((Keccak_HashInstance *)ctx, input, inputLen * 8); \
  289. } \
  290. void PHP_SHA3##bits##Final(unsigned char* digest, \
  291. PHP_SHA3_##bits##_CTX* ctx) { \
  292. Keccak_HashFinal((Keccak_HashInstance *)ctx, digest); \
  293. } \
  294. const php_hash_ops php_hash_sha3_##bits##_ops = { \
  295. "sha3-" #bits, \
  296. (php_hash_init_func_t) PHP_SHA3##bits##Init, \
  297. (php_hash_update_func_t) PHP_SHA3##bits##Update, \
  298. (php_hash_final_func_t) PHP_SHA3##bits##Final, \
  299. php_hash_copy, \
  300. php_keccak_serialize, \
  301. php_keccak_unserialize, \
  302. PHP_KECCAK_SPEC, \
  303. bits >> 3, \
  304. (1600 - (2 * bits)) >> 3, \
  305. sizeof(PHP_SHA3_CTX), \
  306. 1 \
  307. }
  308. #endif
  309. // ================= both algo ==============================================
  310. DECLARE_SHA3_OPS(224);
  311. DECLARE_SHA3_OPS(256);
  312. DECLARE_SHA3_OPS(384);
  313. DECLARE_SHA3_OPS(512);
  314. #undef DECLARE_SHA3_OPS