groestl_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* Groestl hash from https://github.com/Groestlcoin/vanitygen
  2. * Trezor adaptation by Yura Pakhuchiy <pakhuchiy@gmail.com>. */
  3. /**
  4. * Basic type definitions.
  5. *
  6. * This header file defines the generic integer types that will be used
  7. * for the implementation of hash functions; it also contains helper
  8. * functions which encode and decode multi-byte integer values, using
  9. * either little-endian or big-endian conventions.
  10. *
  11. * This file contains a compile-time test on the size of a byte
  12. * (the <code>unsigned char</code> C type). If bytes are not octets,
  13. * i.e. if they do not have a size of exactly 8 bits, then compilation
  14. * is aborted. Architectures where bytes are not octets are relatively
  15. * rare, even in the embedded devices market. We forbid non-octet bytes
  16. * because there is no clear convention on how octet streams are encoded
  17. * on such systems.
  18. *
  19. * ==========================(LICENSE BEGIN)============================
  20. *
  21. * Copyright (c) 2007-2010 Projet RNRT SAPHIR
  22. *
  23. * Permission is hereby granted, free of charge, to any person obtaining
  24. * a copy of this software and associated documentation files (the
  25. * "Software"), to deal in the Software without restriction, including
  26. * without limitation the rights to use, copy, modify, merge, publish,
  27. * distribute, sublicense, and/or sell copies of the Software, and to
  28. * permit persons to whom the Software is furnished to do so, subject to
  29. * the following conditions:
  30. *
  31. * The above copyright notice and this permission notice shall be
  32. * included in all copies or substantial portions of the Software.
  33. *
  34. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  35. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  36. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  37. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  38. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  39. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  40. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  41. *
  42. * ===========================(LICENSE END)=============================
  43. *
  44. * @file sph_types.h
  45. * @author Thomas Pornin <thomas.pornin@cryptolog.com>
  46. */
  47. #ifndef GROESTL_INTERNAL_H__
  48. #define GROESTL_INTERNAL_H__
  49. #include <limits.h>
  50. /*
  51. * All our I/O functions are defined over octet streams. We do not know
  52. * how to handle input data if bytes are not octets.
  53. */
  54. #if CHAR_BIT != 8
  55. #error This code requires 8-bit bytes
  56. #endif
  57. #if defined __STDC__ && __STDC_VERSION__ >= 199901L
  58. #include <stdint.h>
  59. typedef uint32_t sph_u32;
  60. typedef int32_t sph_s32;
  61. typedef uint64_t sph_u64;
  62. typedef int64_t sph_s64;
  63. #define SPH_C32(x) ((sph_u32)(x))
  64. #define SPH_C64(x) ((sph_u64)(x))
  65. #else
  66. #error We need at least C99 compiler
  67. #endif
  68. #define SPH_T32(x) ((x) & SPH_C32(0xFFFFFFFF))
  69. #define SPH_ROTL32(x, n) SPH_T32(((x) << (n)) | ((x) >> (32 - (n))))
  70. #define SPH_ROTR32(x, n) SPH_ROTL32(x, (32 - (n)))
  71. #define SPH_T64(x) ((x) & SPH_C64(0xFFFFFFFFFFFFFFFF))
  72. #define SPH_ROTL64(x, n) SPH_T64(((x) << (n)) | ((x) >> (64 - (n))))
  73. #define SPH_ROTR64(x, n) SPH_ROTL64(x, (64 - (n)))
  74. /*
  75. * 32-bit x86, aka "i386 compatible".
  76. */
  77. #if defined __i386__ || defined _M_IX86
  78. #define SPH_DETECT_LITTLE_ENDIAN 1
  79. #define SPH_DETECT_BIG_ENDIAN 0
  80. /*
  81. * 64-bit x86, hereafter known as "amd64".
  82. */
  83. #elif defined __x86_64 || defined _M_X64
  84. #define SPH_DETECT_LITTLE_ENDIAN 1
  85. #define SPH_DETECT_BIG_ENDIAN 0
  86. /*
  87. * ARM, little-endian.
  88. */
  89. #elif defined __arm__ && __ARMEL__
  90. #define SPH_DETECT_LITTLE_ENDIAN 1
  91. #define SPH_DETECT_BIG_ENDIAN 0
  92. /*
  93. * ARM64, little-endian.
  94. */
  95. #elif defined __aarch64__
  96. #define SPH_DETECT_LITTLE_ENDIAN 1
  97. #define SPH_DETECT_BIG_ENDIAN 0
  98. #endif
  99. #if defined SPH_DETECT_LITTLE_ENDIAN && !defined SPH_LITTLE_ENDIAN
  100. #define SPH_LITTLE_ENDIAN SPH_DETECT_LITTLE_ENDIAN
  101. #endif
  102. #if defined SPH_DETECT_BIG_ENDIAN && !defined SPH_BIG_ENDIAN
  103. #define SPH_BIG_ENDIAN SPH_DETECT_BIG_ENDIAN
  104. #endif
  105. static inline sph_u32
  106. sph_bswap32(sph_u32 x)
  107. {
  108. x = SPH_T32((x << 16) | (x >> 16));
  109. x = ((x & SPH_C32(0xFF00FF00)) >> 8)
  110. | ((x & SPH_C32(0x00FF00FF)) << 8);
  111. return x;
  112. }
  113. /**
  114. * Byte-swap a 64-bit value.
  115. *
  116. * @param x the input value
  117. * @return the byte-swapped value
  118. */
  119. static inline sph_u64
  120. sph_bswap64(sph_u64 x)
  121. {
  122. x = SPH_T64((x << 32) | (x >> 32));
  123. x = ((x & SPH_C64(0xFFFF0000FFFF0000)) >> 16)
  124. | ((x & SPH_C64(0x0000FFFF0000FFFF)) << 16);
  125. x = ((x & SPH_C64(0xFF00FF00FF00FF00)) >> 8)
  126. | ((x & SPH_C64(0x00FF00FF00FF00FF)) << 8);
  127. return x;
  128. }
  129. static inline void
  130. sph_enc16be(void *dst, unsigned val)
  131. {
  132. ((unsigned char *)dst)[0] = (val >> 8);
  133. ((unsigned char *)dst)[1] = val;
  134. }
  135. static inline unsigned
  136. sph_dec16be(const void *src)
  137. {
  138. return ((unsigned)(((const unsigned char *)src)[0]) << 8)
  139. | (unsigned)(((const unsigned char *)src)[1]);
  140. }
  141. static inline void
  142. sph_enc16le(void *dst, unsigned val)
  143. {
  144. ((unsigned char *)dst)[0] = val;
  145. ((unsigned char *)dst)[1] = val >> 8;
  146. }
  147. static inline unsigned
  148. sph_dec16le(const void *src)
  149. {
  150. return (unsigned)(((const unsigned char *)src)[0])
  151. | ((unsigned)(((const unsigned char *)src)[1]) << 8);
  152. }
  153. /**
  154. * Encode a 32-bit value into the provided buffer (big endian convention).
  155. *
  156. * @param dst the destination buffer
  157. * @param val the 32-bit value to encode
  158. */
  159. static inline void
  160. sph_enc32be(void *dst, sph_u32 val)
  161. {
  162. ((unsigned char *)dst)[0] = (val >> 24);
  163. ((unsigned char *)dst)[1] = (val >> 16);
  164. ((unsigned char *)dst)[2] = (val >> 8);
  165. ((unsigned char *)dst)[3] = val;
  166. }
  167. /**
  168. * Encode a 32-bit value into the provided buffer (big endian convention).
  169. * The destination buffer must be properly aligned.
  170. *
  171. * @param dst the destination buffer (32-bit aligned)
  172. * @param val the value to encode
  173. */
  174. static inline void
  175. sph_enc32be_aligned(void *dst, sph_u32 val)
  176. {
  177. #if SPH_LITTLE_ENDIAN
  178. *(sph_u32 *)dst = sph_bswap32(val);
  179. #elif SPH_BIG_ENDIAN
  180. *(sph_u32 *)dst = val;
  181. #else
  182. ((unsigned char *)dst)[0] = (val >> 24);
  183. ((unsigned char *)dst)[1] = (val >> 16);
  184. ((unsigned char *)dst)[2] = (val >> 8);
  185. ((unsigned char *)dst)[3] = val;
  186. #endif
  187. }
  188. /**
  189. * Decode a 32-bit value from the provided buffer (big endian convention).
  190. *
  191. * @param src the source buffer
  192. * @return the decoded value
  193. */
  194. static inline sph_u32
  195. sph_dec32be(const void *src)
  196. {
  197. return ((sph_u32)(((const unsigned char *)src)[0]) << 24)
  198. | ((sph_u32)(((const unsigned char *)src)[1]) << 16)
  199. | ((sph_u32)(((const unsigned char *)src)[2]) << 8)
  200. | (sph_u32)(((const unsigned char *)src)[3]);
  201. }
  202. /**
  203. * Decode a 32-bit value from the provided buffer (big endian convention).
  204. * The source buffer must be properly aligned.
  205. *
  206. * @param src the source buffer (32-bit aligned)
  207. * @return the decoded value
  208. */
  209. static inline sph_u32
  210. sph_dec32be_aligned(const void *src)
  211. {
  212. #if SPH_LITTLE_ENDIAN
  213. return sph_bswap32(*(const sph_u32 *)src);
  214. #elif SPH_BIG_ENDIAN
  215. return *(const sph_u32 *)src;
  216. #else
  217. return ((sph_u32)(((const unsigned char *)src)[0]) << 24)
  218. | ((sph_u32)(((const unsigned char *)src)[1]) << 16)
  219. | ((sph_u32)(((const unsigned char *)src)[2]) << 8)
  220. | (sph_u32)(((const unsigned char *)src)[3]);
  221. #endif
  222. }
  223. /**
  224. * Encode a 32-bit value into the provided buffer (little endian convention).
  225. *
  226. * @param dst the destination buffer
  227. * @param val the 32-bit value to encode
  228. */
  229. static inline void
  230. sph_enc32le(void *dst, sph_u32 val)
  231. {
  232. ((unsigned char *)dst)[0] = val;
  233. ((unsigned char *)dst)[1] = (val >> 8);
  234. ((unsigned char *)dst)[2] = (val >> 16);
  235. ((unsigned char *)dst)[3] = (val >> 24);
  236. }
  237. /**
  238. * Encode a 32-bit value into the provided buffer (little endian convention).
  239. * The destination buffer must be properly aligned.
  240. *
  241. * @param dst the destination buffer (32-bit aligned)
  242. * @param val the value to encode
  243. */
  244. static inline void
  245. sph_enc32le_aligned(void *dst, sph_u32 val)
  246. {
  247. #if SPH_LITTLE_ENDIAN
  248. *(sph_u32 *)dst = val;
  249. #elif SPH_BIG_ENDIAN
  250. *(sph_u32 *)dst = sph_bswap32(val);
  251. #else
  252. ((unsigned char *)dst)[0] = val;
  253. ((unsigned char *)dst)[1] = (val >> 8);
  254. ((unsigned char *)dst)[2] = (val >> 16);
  255. ((unsigned char *)dst)[3] = (val >> 24);
  256. #endif
  257. }
  258. /**
  259. * Decode a 32-bit value from the provided buffer (little endian convention).
  260. *
  261. * @param src the source buffer
  262. * @return the decoded value
  263. */
  264. static inline sph_u32
  265. sph_dec32le(const void *src)
  266. {
  267. return (sph_u32)(((const unsigned char *)src)[0])
  268. | ((sph_u32)(((const unsigned char *)src)[1]) << 8)
  269. | ((sph_u32)(((const unsigned char *)src)[2]) << 16)
  270. | ((sph_u32)(((const unsigned char *)src)[3]) << 24);
  271. }
  272. /**
  273. * Decode a 32-bit value from the provided buffer (little endian convention).
  274. * The source buffer must be properly aligned.
  275. *
  276. * @param src the source buffer (32-bit aligned)
  277. * @return the decoded value
  278. */
  279. static inline sph_u32
  280. sph_dec32le_aligned(const void *src)
  281. {
  282. #if SPH_LITTLE_ENDIAN
  283. return *(const sph_u32 *)src;
  284. #elif SPH_BIG_ENDIAN
  285. return sph_bswap32(*(const sph_u32 *)src);
  286. #else
  287. return (sph_u32)(((const unsigned char *)src)[0])
  288. | ((sph_u32)(((const unsigned char *)src)[1]) << 8)
  289. | ((sph_u32)(((const unsigned char *)src)[2]) << 16)
  290. | ((sph_u32)(((const unsigned char *)src)[3]) << 24);
  291. #endif
  292. }
  293. /**
  294. * Encode a 64-bit value into the provided buffer (big endian convention).
  295. *
  296. * @param dst the destination buffer
  297. * @param val the 64-bit value to encode
  298. */
  299. static inline void
  300. sph_enc64be(void *dst, sph_u64 val)
  301. {
  302. ((unsigned char *)dst)[0] = (val >> 56);
  303. ((unsigned char *)dst)[1] = (val >> 48);
  304. ((unsigned char *)dst)[2] = (val >> 40);
  305. ((unsigned char *)dst)[3] = (val >> 32);
  306. ((unsigned char *)dst)[4] = (val >> 24);
  307. ((unsigned char *)dst)[5] = (val >> 16);
  308. ((unsigned char *)dst)[6] = (val >> 8);
  309. ((unsigned char *)dst)[7] = val;
  310. }
  311. /**
  312. * Encode a 64-bit value into the provided buffer (big endian convention).
  313. * The destination buffer must be properly aligned.
  314. *
  315. * @param dst the destination buffer (64-bit aligned)
  316. * @param val the value to encode
  317. */
  318. static inline void
  319. sph_enc64be_aligned(void *dst, sph_u64 val)
  320. {
  321. #if SPH_LITTLE_ENDIAN
  322. *(sph_u64 *)dst = sph_bswap64(val);
  323. #elif SPH_BIG_ENDIAN
  324. *(sph_u64 *)dst = val;
  325. #else
  326. ((unsigned char *)dst)[0] = (val >> 56);
  327. ((unsigned char *)dst)[1] = (val >> 48);
  328. ((unsigned char *)dst)[2] = (val >> 40);
  329. ((unsigned char *)dst)[3] = (val >> 32);
  330. ((unsigned char *)dst)[4] = (val >> 24);
  331. ((unsigned char *)dst)[5] = (val >> 16);
  332. ((unsigned char *)dst)[6] = (val >> 8);
  333. ((unsigned char *)dst)[7] = val;
  334. #endif
  335. }
  336. /**
  337. * Decode a 64-bit value from the provided buffer (big endian convention).
  338. *
  339. * @param src the source buffer
  340. * @return the decoded value
  341. */
  342. static inline sph_u64
  343. sph_dec64be(const void *src)
  344. {
  345. return ((sph_u64)(((const unsigned char *)src)[0]) << 56)
  346. | ((sph_u64)(((const unsigned char *)src)[1]) << 48)
  347. | ((sph_u64)(((const unsigned char *)src)[2]) << 40)
  348. | ((sph_u64)(((const unsigned char *)src)[3]) << 32)
  349. | ((sph_u64)(((const unsigned char *)src)[4]) << 24)
  350. | ((sph_u64)(((const unsigned char *)src)[5]) << 16)
  351. | ((sph_u64)(((const unsigned char *)src)[6]) << 8)
  352. | (sph_u64)(((const unsigned char *)src)[7]);
  353. }
  354. /**
  355. * Decode a 64-bit value from the provided buffer (big endian convention).
  356. * The source buffer must be properly aligned.
  357. *
  358. * @param src the source buffer (64-bit aligned)
  359. * @return the decoded value
  360. */
  361. static inline sph_u64
  362. sph_dec64be_aligned(const void *src)
  363. {
  364. #if SPH_LITTLE_ENDIAN
  365. return sph_bswap64(*(const sph_u64 *)src);
  366. #elif SPH_BIG_ENDIAN
  367. return *(const sph_u64 *)src;
  368. #else
  369. return ((sph_u64)(((const unsigned char *)src)[0]) << 56)
  370. | ((sph_u64)(((const unsigned char *)src)[1]) << 48)
  371. | ((sph_u64)(((const unsigned char *)src)[2]) << 40)
  372. | ((sph_u64)(((const unsigned char *)src)[3]) << 32)
  373. | ((sph_u64)(((const unsigned char *)src)[4]) << 24)
  374. | ((sph_u64)(((const unsigned char *)src)[5]) << 16)
  375. | ((sph_u64)(((const unsigned char *)src)[6]) << 8)
  376. | (sph_u64)(((const unsigned char *)src)[7]);
  377. #endif
  378. }
  379. /**
  380. * Encode a 64-bit value into the provided buffer (little endian convention).
  381. *
  382. * @param dst the destination buffer
  383. * @param val the 64-bit value to encode
  384. */
  385. static inline void
  386. sph_enc64le(void *dst, sph_u64 val)
  387. {
  388. ((unsigned char *)dst)[0] = val;
  389. ((unsigned char *)dst)[1] = (val >> 8);
  390. ((unsigned char *)dst)[2] = (val >> 16);
  391. ((unsigned char *)dst)[3] = (val >> 24);
  392. ((unsigned char *)dst)[4] = (val >> 32);
  393. ((unsigned char *)dst)[5] = (val >> 40);
  394. ((unsigned char *)dst)[6] = (val >> 48);
  395. ((unsigned char *)dst)[7] = (val >> 56);
  396. }
  397. /**
  398. * Encode a 64-bit value into the provided buffer (little endian convention).
  399. * The destination buffer must be properly aligned.
  400. *
  401. * @param dst the destination buffer (64-bit aligned)
  402. * @param val the value to encode
  403. */
  404. static inline void
  405. sph_enc64le_aligned(void *dst, sph_u64 val)
  406. {
  407. #if SPH_LITTLE_ENDIAN
  408. *(sph_u64 *)dst = val;
  409. #elif SPH_BIG_ENDIAN
  410. *(sph_u64 *)dst = sph_bswap64(val);
  411. #else
  412. ((unsigned char *)dst)[0] = val;
  413. ((unsigned char *)dst)[1] = (val >> 8);
  414. ((unsigned char *)dst)[2] = (val >> 16);
  415. ((unsigned char *)dst)[3] = (val >> 24);
  416. ((unsigned char *)dst)[4] = (val >> 32);
  417. ((unsigned char *)dst)[5] = (val >> 40);
  418. ((unsigned char *)dst)[6] = (val >> 48);
  419. ((unsigned char *)dst)[7] = (val >> 56);
  420. #endif
  421. }
  422. /**
  423. * Decode a 64-bit value from the provided buffer (little endian convention).
  424. *
  425. * @param src the source buffer
  426. * @return the decoded value
  427. */
  428. static inline sph_u64
  429. sph_dec64le(const void *src)
  430. {
  431. return (sph_u64)(((const unsigned char *)src)[0])
  432. | ((sph_u64)(((const unsigned char *)src)[1]) << 8)
  433. | ((sph_u64)(((const unsigned char *)src)[2]) << 16)
  434. | ((sph_u64)(((const unsigned char *)src)[3]) << 24)
  435. | ((sph_u64)(((const unsigned char *)src)[4]) << 32)
  436. | ((sph_u64)(((const unsigned char *)src)[5]) << 40)
  437. | ((sph_u64)(((const unsigned char *)src)[6]) << 48)
  438. | ((sph_u64)(((const unsigned char *)src)[7]) << 56);
  439. }
  440. /**
  441. * Decode a 64-bit value from the provided buffer (little endian convention).
  442. * The source buffer must be properly aligned.
  443. *
  444. * @param src the source buffer (64-bit aligned)
  445. * @return the decoded value
  446. */
  447. static inline sph_u64
  448. sph_dec64le_aligned(const void *src)
  449. {
  450. #if SPH_LITTLE_ENDIAN
  451. return *(const sph_u64 *)src;
  452. #elif SPH_BIG_ENDIAN
  453. return sph_bswap64(*(const sph_u64 *)src);
  454. #else
  455. return (sph_u64)(((const unsigned char *)src)[0])
  456. | ((sph_u64)(((const unsigned char *)src)[1]) << 8)
  457. | ((sph_u64)(((const unsigned char *)src)[2]) << 16)
  458. | ((sph_u64)(((const unsigned char *)src)[3]) << 24)
  459. | ((sph_u64)(((const unsigned char *)src)[4]) << 32)
  460. | ((sph_u64)(((const unsigned char *)src)[5]) << 40)
  461. | ((sph_u64)(((const unsigned char *)src)[6]) << 48)
  462. | ((sph_u64)(((const unsigned char *)src)[7]) << 56);
  463. #endif
  464. }
  465. #endif