groestl_internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 sph_bswap32(sph_u32 x) {
  106. x = SPH_T32((x << 16) | (x >> 16));
  107. x = ((x & SPH_C32(0xFF00FF00)) >> 8) | ((x & SPH_C32(0x00FF00FF)) << 8);
  108. return x;
  109. }
  110. /**
  111. * Byte-swap a 64-bit value.
  112. *
  113. * @param x the input value
  114. * @return the byte-swapped value
  115. */
  116. static inline sph_u64 sph_bswap64(sph_u64 x) {
  117. x = SPH_T64((x << 32) | (x >> 32));
  118. x = ((x & SPH_C64(0xFFFF0000FFFF0000)) >> 16) | ((x & SPH_C64(0x0000FFFF0000FFFF)) << 16);
  119. x = ((x & SPH_C64(0xFF00FF00FF00FF00)) >> 8) | ((x & SPH_C64(0x00FF00FF00FF00FF)) << 8);
  120. return x;
  121. }
  122. static inline void sph_enc16be(void* dst, unsigned val) {
  123. ((unsigned char*)dst)[0] = (val >> 8);
  124. ((unsigned char*)dst)[1] = val;
  125. }
  126. static inline unsigned sph_dec16be(const void* src) {
  127. return ((unsigned)(((const unsigned char*)src)[0]) << 8) |
  128. (unsigned)(((const unsigned char*)src)[1]);
  129. }
  130. static inline void sph_enc16le(void* dst, unsigned val) {
  131. ((unsigned char*)dst)[0] = val;
  132. ((unsigned char*)dst)[1] = val >> 8;
  133. }
  134. static inline unsigned sph_dec16le(const void* src) {
  135. return (unsigned)(((const unsigned char*)src)[0]) |
  136. ((unsigned)(((const unsigned char*)src)[1]) << 8);
  137. }
  138. /**
  139. * Encode a 32-bit value into the provided buffer (big endian convention).
  140. *
  141. * @param dst the destination buffer
  142. * @param val the 32-bit value to encode
  143. */
  144. static inline void sph_enc32be(void* dst, sph_u32 val) {
  145. ((unsigned char*)dst)[0] = (val >> 24);
  146. ((unsigned char*)dst)[1] = (val >> 16);
  147. ((unsigned char*)dst)[2] = (val >> 8);
  148. ((unsigned char*)dst)[3] = val;
  149. }
  150. /**
  151. * Encode a 32-bit value into the provided buffer (big endian convention).
  152. * The destination buffer must be properly aligned.
  153. *
  154. * @param dst the destination buffer (32-bit aligned)
  155. * @param val the value to encode
  156. */
  157. static inline void sph_enc32be_aligned(void* dst, sph_u32 val) {
  158. #if SPH_LITTLE_ENDIAN
  159. *(sph_u32*)dst = sph_bswap32(val);
  160. #elif SPH_BIG_ENDIAN
  161. *(sph_u32*)dst = val;
  162. #else
  163. ((unsigned char*)dst)[0] = (val >> 24);
  164. ((unsigned char*)dst)[1] = (val >> 16);
  165. ((unsigned char*)dst)[2] = (val >> 8);
  166. ((unsigned char*)dst)[3] = val;
  167. #endif
  168. }
  169. /**
  170. * Decode a 32-bit value from the provided buffer (big endian convention).
  171. *
  172. * @param src the source buffer
  173. * @return the decoded value
  174. */
  175. static inline sph_u32 sph_dec32be(const void* src) {
  176. return ((sph_u32)(((const unsigned char*)src)[0]) << 24) |
  177. ((sph_u32)(((const unsigned char*)src)[1]) << 16) |
  178. ((sph_u32)(((const unsigned char*)src)[2]) << 8) |
  179. (sph_u32)(((const unsigned char*)src)[3]);
  180. }
  181. /**
  182. * Decode a 32-bit value from the provided buffer (big endian convention).
  183. * The source buffer must be properly aligned.
  184. *
  185. * @param src the source buffer (32-bit aligned)
  186. * @return the decoded value
  187. */
  188. static inline sph_u32 sph_dec32be_aligned(const void* src) {
  189. #if SPH_LITTLE_ENDIAN
  190. return sph_bswap32(*(const sph_u32*)src);
  191. #elif SPH_BIG_ENDIAN
  192. return *(const sph_u32*)src;
  193. #else
  194. return ((sph_u32)(((const unsigned char*)src)[0]) << 24) |
  195. ((sph_u32)(((const unsigned char*)src)[1]) << 16) |
  196. ((sph_u32)(((const unsigned char*)src)[2]) << 8) |
  197. (sph_u32)(((const unsigned char*)src)[3]);
  198. #endif
  199. }
  200. /**
  201. * Encode a 32-bit value into the provided buffer (little endian convention).
  202. *
  203. * @param dst the destination buffer
  204. * @param val the 32-bit value to encode
  205. */
  206. static inline void sph_enc32le(void* dst, sph_u32 val) {
  207. ((unsigned char*)dst)[0] = val;
  208. ((unsigned char*)dst)[1] = (val >> 8);
  209. ((unsigned char*)dst)[2] = (val >> 16);
  210. ((unsigned char*)dst)[3] = (val >> 24);
  211. }
  212. /**
  213. * Encode a 32-bit value into the provided buffer (little endian convention).
  214. * The destination buffer must be properly aligned.
  215. *
  216. * @param dst the destination buffer (32-bit aligned)
  217. * @param val the value to encode
  218. */
  219. static inline void sph_enc32le_aligned(void* dst, sph_u32 val) {
  220. #if SPH_LITTLE_ENDIAN
  221. *(sph_u32*)dst = val;
  222. #elif SPH_BIG_ENDIAN
  223. *(sph_u32*)dst = sph_bswap32(val);
  224. #else
  225. ((unsigned char*)dst)[0] = val;
  226. ((unsigned char*)dst)[1] = (val >> 8);
  227. ((unsigned char*)dst)[2] = (val >> 16);
  228. ((unsigned char*)dst)[3] = (val >> 24);
  229. #endif
  230. }
  231. /**
  232. * Decode a 32-bit value from the provided buffer (little endian convention).
  233. *
  234. * @param src the source buffer
  235. * @return the decoded value
  236. */
  237. static inline sph_u32 sph_dec32le(const void* src) {
  238. return (sph_u32)(((const unsigned char*)src)[0]) |
  239. ((sph_u32)(((const unsigned char*)src)[1]) << 8) |
  240. ((sph_u32)(((const unsigned char*)src)[2]) << 16) |
  241. ((sph_u32)(((const unsigned char*)src)[3]) << 24);
  242. }
  243. /**
  244. * Decode a 32-bit value from the provided buffer (little endian convention).
  245. * The source buffer must be properly aligned.
  246. *
  247. * @param src the source buffer (32-bit aligned)
  248. * @return the decoded value
  249. */
  250. static inline sph_u32 sph_dec32le_aligned(const void* src) {
  251. #if SPH_LITTLE_ENDIAN
  252. return *(const sph_u32*)src;
  253. #elif SPH_BIG_ENDIAN
  254. return sph_bswap32(*(const sph_u32*)src);
  255. #else
  256. return (sph_u32)(((const unsigned char*)src)[0]) |
  257. ((sph_u32)(((const unsigned char*)src)[1]) << 8) |
  258. ((sph_u32)(((const unsigned char*)src)[2]) << 16) |
  259. ((sph_u32)(((const unsigned char*)src)[3]) << 24);
  260. #endif
  261. }
  262. /**
  263. * Encode a 64-bit value into the provided buffer (big endian convention).
  264. *
  265. * @param dst the destination buffer
  266. * @param val the 64-bit value to encode
  267. */
  268. static inline void sph_enc64be(void* dst, sph_u64 val) {
  269. ((unsigned char*)dst)[0] = (val >> 56);
  270. ((unsigned char*)dst)[1] = (val >> 48);
  271. ((unsigned char*)dst)[2] = (val >> 40);
  272. ((unsigned char*)dst)[3] = (val >> 32);
  273. ((unsigned char*)dst)[4] = (val >> 24);
  274. ((unsigned char*)dst)[5] = (val >> 16);
  275. ((unsigned char*)dst)[6] = (val >> 8);
  276. ((unsigned char*)dst)[7] = val;
  277. }
  278. /**
  279. * Encode a 64-bit value into the provided buffer (big endian convention).
  280. * The destination buffer must be properly aligned.
  281. *
  282. * @param dst the destination buffer (64-bit aligned)
  283. * @param val the value to encode
  284. */
  285. static inline void sph_enc64be_aligned(void* dst, sph_u64 val) {
  286. #if SPH_LITTLE_ENDIAN
  287. *(sph_u64*)dst = sph_bswap64(val);
  288. #elif SPH_BIG_ENDIAN
  289. *(sph_u64*)dst = val;
  290. #else
  291. ((unsigned char*)dst)[0] = (val >> 56);
  292. ((unsigned char*)dst)[1] = (val >> 48);
  293. ((unsigned char*)dst)[2] = (val >> 40);
  294. ((unsigned char*)dst)[3] = (val >> 32);
  295. ((unsigned char*)dst)[4] = (val >> 24);
  296. ((unsigned char*)dst)[5] = (val >> 16);
  297. ((unsigned char*)dst)[6] = (val >> 8);
  298. ((unsigned char*)dst)[7] = val;
  299. #endif
  300. }
  301. /**
  302. * Decode a 64-bit value from the provided buffer (big endian convention).
  303. *
  304. * @param src the source buffer
  305. * @return the decoded value
  306. */
  307. static inline sph_u64 sph_dec64be(const void* src) {
  308. return ((sph_u64)(((const unsigned char*)src)[0]) << 56) |
  309. ((sph_u64)(((const unsigned char*)src)[1]) << 48) |
  310. ((sph_u64)(((const unsigned char*)src)[2]) << 40) |
  311. ((sph_u64)(((const unsigned char*)src)[3]) << 32) |
  312. ((sph_u64)(((const unsigned char*)src)[4]) << 24) |
  313. ((sph_u64)(((const unsigned char*)src)[5]) << 16) |
  314. ((sph_u64)(((const unsigned char*)src)[6]) << 8) |
  315. (sph_u64)(((const unsigned char*)src)[7]);
  316. }
  317. /**
  318. * Decode a 64-bit value from the provided buffer (big endian convention).
  319. * The source buffer must be properly aligned.
  320. *
  321. * @param src the source buffer (64-bit aligned)
  322. * @return the decoded value
  323. */
  324. static inline sph_u64 sph_dec64be_aligned(const void* src) {
  325. #if SPH_LITTLE_ENDIAN
  326. return sph_bswap64(*(const sph_u64*)src);
  327. #elif SPH_BIG_ENDIAN
  328. return *(const sph_u64*)src;
  329. #else
  330. return ((sph_u64)(((const unsigned char*)src)[0]) << 56) |
  331. ((sph_u64)(((const unsigned char*)src)[1]) << 48) |
  332. ((sph_u64)(((const unsigned char*)src)[2]) << 40) |
  333. ((sph_u64)(((const unsigned char*)src)[3]) << 32) |
  334. ((sph_u64)(((const unsigned char*)src)[4]) << 24) |
  335. ((sph_u64)(((const unsigned char*)src)[5]) << 16) |
  336. ((sph_u64)(((const unsigned char*)src)[6]) << 8) |
  337. (sph_u64)(((const unsigned char*)src)[7]);
  338. #endif
  339. }
  340. /**
  341. * Encode a 64-bit value into the provided buffer (little endian convention).
  342. *
  343. * @param dst the destination buffer
  344. * @param val the 64-bit value to encode
  345. */
  346. static inline void sph_enc64le(void* dst, sph_u64 val) {
  347. ((unsigned char*)dst)[0] = val;
  348. ((unsigned char*)dst)[1] = (val >> 8);
  349. ((unsigned char*)dst)[2] = (val >> 16);
  350. ((unsigned char*)dst)[3] = (val >> 24);
  351. ((unsigned char*)dst)[4] = (val >> 32);
  352. ((unsigned char*)dst)[5] = (val >> 40);
  353. ((unsigned char*)dst)[6] = (val >> 48);
  354. ((unsigned char*)dst)[7] = (val >> 56);
  355. }
  356. /**
  357. * Encode a 64-bit value into the provided buffer (little endian convention).
  358. * The destination buffer must be properly aligned.
  359. *
  360. * @param dst the destination buffer (64-bit aligned)
  361. * @param val the value to encode
  362. */
  363. static inline void sph_enc64le_aligned(void* dst, sph_u64 val) {
  364. #if SPH_LITTLE_ENDIAN
  365. *(sph_u64*)dst = val;
  366. #elif SPH_BIG_ENDIAN
  367. *(sph_u64*)dst = sph_bswap64(val);
  368. #else
  369. ((unsigned char*)dst)[0] = val;
  370. ((unsigned char*)dst)[1] = (val >> 8);
  371. ((unsigned char*)dst)[2] = (val >> 16);
  372. ((unsigned char*)dst)[3] = (val >> 24);
  373. ((unsigned char*)dst)[4] = (val >> 32);
  374. ((unsigned char*)dst)[5] = (val >> 40);
  375. ((unsigned char*)dst)[6] = (val >> 48);
  376. ((unsigned char*)dst)[7] = (val >> 56);
  377. #endif
  378. }
  379. /**
  380. * Decode a 64-bit value from the provided buffer (little endian convention).
  381. *
  382. * @param src the source buffer
  383. * @return the decoded value
  384. */
  385. static inline sph_u64 sph_dec64le(const void* src) {
  386. return (sph_u64)(((const unsigned char*)src)[0]) |
  387. ((sph_u64)(((const unsigned char*)src)[1]) << 8) |
  388. ((sph_u64)(((const unsigned char*)src)[2]) << 16) |
  389. ((sph_u64)(((const unsigned char*)src)[3]) << 24) |
  390. ((sph_u64)(((const unsigned char*)src)[4]) << 32) |
  391. ((sph_u64)(((const unsigned char*)src)[5]) << 40) |
  392. ((sph_u64)(((const unsigned char*)src)[6]) << 48) |
  393. ((sph_u64)(((const unsigned char*)src)[7]) << 56);
  394. }
  395. /**
  396. * Decode a 64-bit value from the provided buffer (little endian convention).
  397. * The source buffer must be properly aligned.
  398. *
  399. * @param src the source buffer (64-bit aligned)
  400. * @return the decoded value
  401. */
  402. static inline sph_u64 sph_dec64le_aligned(const void* src) {
  403. #if SPH_LITTLE_ENDIAN
  404. return *(const sph_u64*)src;
  405. #elif SPH_BIG_ENDIAN
  406. return sph_bswap64(*(const sph_u64*)src);
  407. #else
  408. return (sph_u64)(((const unsigned char*)src)[0]) |
  409. ((sph_u64)(((const unsigned char*)src)[1]) << 8) |
  410. ((sph_u64)(((const unsigned char*)src)[2]) << 16) |
  411. ((sph_u64)(((const unsigned char*)src)[3]) << 24) |
  412. ((sph_u64)(((const unsigned char*)src)[4]) << 32) |
  413. ((sph_u64)(((const unsigned char*)src)[5]) << 40) |
  414. ((sph_u64)(((const unsigned char*)src)[6]) << 48) |
  415. ((sph_u64)(((const unsigned char*)src)[7]) << 56);
  416. #endif
  417. }
  418. #endif