├── index.js ├── jh.o ├── x13.h ├── x14.h ├── yescrypt ├── yescrypt-best.c ├── sha256_Y.h ├── sysendian.h └── yescrypt-platform.c ├── boolberry.h ├── bcrypt.h ├── c11.h ├── jh.h ├── s3.h ├── nist5.h ├── x11.h ├── x15.h ├── x5.h ├── fresh.h ├── fugue.h ├── quark.h ├── qubit.h ├── sha1.h ├── hefty1.h ├── dcrypt.h ├── shavite3.h ├── timetravel.h ├── blake.h ├── skein.h ├── keccak.h ├── fugue.c ├── .travis.yml ├── cryptonight.h ├── groestl.h ├── keccak.c ├── crypto ├── hash.h ├── c_keccak.h ├── hash.c ├── c_jh.h ├── cryptonote_core │ ├── cryptonote_format_utils.h │ ├── account.cpp │ ├── account.h │ ├── cryptonote_basic_impl.h │ └── cryptonote_basic_impl.cpp ├── c_blake256.h ├── c_groestl.h ├── hash-ops.h ├── c_skein.h ├── oaes_config.h ├── c_keccak.c ├── wild_keccak.cpp ├── cryptonote_protocol │ └── cryptonote_protocol_defs.h ├── wild_keccak.h ├── groestl_tables.h ├── oaes_lib.h ├── skein_port.h ├── int-util.h └── aesb.c ├── jh.c ├── blake.c ├── scryptn.h ├── boolberry.cc ├── shavite3.c ├── skein.c ├── neoscrypt.h ├── package.json ├── s3.c ├── scryptjane ├── scrypt-jane-romix.h ├── scrypt-jane-mix_chacha.h ├── scrypt-jane-mix_salsa.h ├── scrypt-jane-hash.h ├── scrypt-jane-romix-basic.h ├── scrypt-jane-salsa.h ├── scrypt-jane-romix-template.h ├── scrypt-jane-pbkdf2.h ├── scrypt-jane-chacha.h ├── scrypt-jane-hash_sha256.h └── scrypt-jane-hash_keccak.h ├── groestl.c ├── scryptjane.h ├── fresh.c ├── nist5.c ├── x5.c ├── qubit.c ├── sha1.c ├── sha3 ├── sph_fugue.h ├── sph_hefty1.h └── sph_whirlpool.h ├── hefty1.c ├── c11.c ├── x11.c ├── binding.gyp ├── README.md ├── x13.c ├── x14.c ├── x15.c ├── timetravel.c ├── quark.c ├── scryptjane.c └── dcrypt.c /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('bindings')('multihashing.node') -------------------------------------------------------------------------------- /jh.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UNOMP/node-multi-hashing/HEAD/jh.o -------------------------------------------------------------------------------- /x13.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | void x13_hash(const char* input, char* output, uint32_t len); 6 | -------------------------------------------------------------------------------- /x14.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | void x14_hash(const char* input, char* output, uint32_t len); 6 | -------------------------------------------------------------------------------- /yescrypt/yescrypt-best.c: -------------------------------------------------------------------------------- 1 | #ifdef __SSE2__ 2 | #include "yescrypt-simd.c" 3 | #else 4 | #include "yescrypt-opt.c" 5 | #endif 6 | -------------------------------------------------------------------------------- /boolberry.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | void boolberry_hash(const char* input, uint32_t input_len, const char* scratchpad, uint64_t spad_length, char* output, uint64_t height); 7 | -------------------------------------------------------------------------------- /bcrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef BCRYPT_H 2 | #define BCRYPT_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | void bcrypt_hash(const char *input, char *output); 9 | 10 | #ifdef __cplusplus 11 | } 12 | #endif 13 | 14 | #endif -------------------------------------------------------------------------------- /c11.h: -------------------------------------------------------------------------------- 1 | #ifndef C11_H 2 | #define C11_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void c11_hash(const char* input, char* output); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /jh.h: -------------------------------------------------------------------------------- 1 | #ifndef JHA_H 2 | #define JHA_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void jh_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /s3.h: -------------------------------------------------------------------------------- 1 | #ifndef S3_H 2 | #define S3_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void s3_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /nist5.h: -------------------------------------------------------------------------------- 1 | #ifndef NIST5_H 2 | #define NIST5_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void nist5_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif -------------------------------------------------------------------------------- /x11.h: -------------------------------------------------------------------------------- 1 | #ifndef X11_H 2 | #define X11_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void x11_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /x15.h: -------------------------------------------------------------------------------- 1 | #ifndef X15_H 2 | #define X15_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void x15_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /x5.h: -------------------------------------------------------------------------------- 1 | #ifndef X11_H 2 | #define X11_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void x11_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /fresh.h: -------------------------------------------------------------------------------- 1 | #ifndef FRESH_H 2 | #define FRESH_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void fresh_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /fugue.h: -------------------------------------------------------------------------------- 1 | #ifndef FUGUE_H 2 | #define FUGUE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void fugue_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /quark.h: -------------------------------------------------------------------------------- 1 | #ifndef QUARK_H 2 | #define QUARK_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void quark_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /qubit.h: -------------------------------------------------------------------------------- 1 | #ifndef QUBIT_H 2 | #define QUBIT_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void qubit_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /sha1.h: -------------------------------------------------------------------------------- 1 | #ifndef SHA1_H 2 | #define SHA1_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void sha1_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /hefty1.h: -------------------------------------------------------------------------------- 1 | #ifndef HEFTY1_H 2 | #define HEFTY1_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void hefty1_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /dcrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef DCRYPT_H 2 | #define DCRYPT_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void dcrypt_hash(const char* input, char* hash, uint32_t len); 11 | 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /shavite3.h: -------------------------------------------------------------------------------- 1 | #ifndef SHAVITE_H 2 | #define SHAVITE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void shavite3_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /timetravel.h: -------------------------------------------------------------------------------- 1 | #ifndef TIMETRAVEL_H 2 | #define TIMETRAVEL_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void timetravel_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif -------------------------------------------------------------------------------- /blake.h: -------------------------------------------------------------------------------- 1 | #ifndef BLAKE_H 2 | #define BLAKE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void blake_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /skein.h: -------------------------------------------------------------------------------- 1 | #ifndef SKEIN_H 2 | #define SKEIN_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void skein_hash(const char* input, char* output, uint32_t len); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /keccak.h: -------------------------------------------------------------------------------- 1 | #ifndef KECCAK_H 2 | #define KECCAK_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void keccak_hash(const char* input, char* output, uint32_t size); 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /fugue.c: -------------------------------------------------------------------------------- 1 | #include "fugue.h" 2 | 3 | #include "sha3/sph_fugue.h" 4 | 5 | void fugue_hash(const char* input, char* output, uint32_t len) 6 | { 7 | sph_fugue256_context ctx_fugue; 8 | sph_fugue256_init(&ctx_fugue); 9 | sph_fugue256(&ctx_fugue, input, len); 10 | sph_fugue256_close(&ctx_fugue, output); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10.32' 4 | deploy: 5 | provider: npm 6 | sudo: false 7 | email: steven@sigwo.com 8 | api_key: 9 | secure: D9lpUDAx1OudPBji3mapnAhOug3wcEBqFrNgWaFh5XiYesa/f/X0gMOJggLlvzyhLHKs8VdTHShdu3XzlC3EDwr5wCdgYO1JSOyDo93FG7Y/qhPDVFnzdtsKmr813Qtj2UDKIh2ZP+JnjKaITrvUwRmdi/8+B9Enr5o9ulFb/a0= 10 | sudo: false 11 | -------------------------------------------------------------------------------- /cryptonight.h: -------------------------------------------------------------------------------- 1 | #ifndef CRYPTONIGHT_H 2 | #define CRYPTONIGHT_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void cryptonight_hash(const char* input, char* output, uint32_t len); 11 | void cryptonight_fast_hash(const char* input, char* output, uint32_t len); 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /groestl.h: -------------------------------------------------------------------------------- 1 | #ifndef GROESTL_H 2 | #define GROESTL_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | void groestl_hash(const char* input, char* output, uint32_t len); 11 | void groestlmyriad_hash(const char* input, char* output, uint32_t len); 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /keccak.c: -------------------------------------------------------------------------------- 1 | #include "keccak.h" 2 | 3 | #include "sha3/sph_types.h" 4 | #include "sha3/sph_keccak.h" 5 | 6 | 7 | void keccak_hash(const char* input, char* output, uint32_t size) 8 | { 9 | sph_keccak256_context ctx_keccak; 10 | sph_keccak256_init(&ctx_keccak); 11 | sph_keccak256 (&ctx_keccak, input, size);//80); 12 | sph_keccak256_close(&ctx_keccak, output); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /crypto/hash.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "hash-ops.h" 4 | 5 | typedef unsigned char BitSequence; 6 | typedef unsigned long long DataLength; 7 | 8 | #ifdef __cplusplus 9 | 10 | #include 11 | 12 | typedef std::string blobdata; 13 | 14 | namespace crypto { 15 | #pragma pack(push, 1) 16 | class hash { 17 | char data[HASH_SIZE]; 18 | }; 19 | #pragma pack(pop) 20 | } 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /jh.c: -------------------------------------------------------------------------------- 1 | #include "jh.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "sha3/sph_jh.h" 8 | 9 | 10 | void jh_hash(const char* input, char* output, uint32_t len) { 11 | 12 | sph_jh256_context ctx_jh; 13 | sph_jh256_init(&ctx_jh); 14 | sph_jh256 (&ctx_jh, input, len); 15 | sph_jh256_close(&ctx_jh, output); 16 | 17 | } 18 | 19 | -------------------------------------------------------------------------------- /blake.c: -------------------------------------------------------------------------------- 1 | #include "blake.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | 9 | 10 | void blake_hash(const char* input, char* output, uint32_t len) 11 | { 12 | sph_blake256_context ctx_blake; 13 | sph_blake256_init(&ctx_blake); 14 | sph_blake256(&ctx_blake, input, len); 15 | sph_blake256_close(&ctx_blake, output); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /scryptn.h: -------------------------------------------------------------------------------- 1 | #ifndef SCRYPTN_H 2 | #define SCRYPTN_H 3 | #include 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | void scrypt_N_R_1_256(const char* input, char* output, uint32_t N, uint32_t R, uint32_t len); 9 | void scrypt_N_R_1_256_sp(const char* input, char* output, char* scratchpad, uint32_t N, uint32_t R, uint32_t len); 10 | //const int scrypt_scratchpad_size = 131583; 11 | 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /boolberry.cc: -------------------------------------------------------------------------------- 1 | #include "boolberry.h" 2 | #include "crypto/cryptonote_core/cryptonote_format_utils.h" 3 | 4 | #include 5 | 6 | void boolberry_hash(const char* input, uint32_t input_len, const char* scratchpad, uint64_t spad_length, char* output, uint64_t height) { 7 | crypto::hash* spad = (crypto::hash*) scratchpad; 8 | cryptonote::get_blob_longhash_bb(std::string(input, input_len), *((crypto::hash*)output), height, [&](uint64_t index) -> crypto::hash& { 9 | return spad[index%(spad_length / HASH_SIZE)]; 10 | }); 11 | } 12 | -------------------------------------------------------------------------------- /crypto/c_keccak.h: -------------------------------------------------------------------------------- 1 | // keccak.h 2 | // 19-Nov-11 Markku-Juhani O. Saarinen 3 | 4 | #ifndef KECCAK_H 5 | #define KECCAK_H 6 | 7 | #include 8 | #include 9 | 10 | #ifndef KECCAK_ROUNDS 11 | #define KECCAK_ROUNDS 24 12 | #endif 13 | 14 | #ifndef ROTL64 15 | #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) 16 | #endif 17 | 18 | // compute a keccak hash (md) of given byte length from "in" 19 | int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen); 20 | 21 | // update the state 22 | void keccakf(uint64_t st[25], int norounds); 23 | 24 | void keccak1600(const uint8_t *in, int inlen, uint8_t *md); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /shavite3.c: -------------------------------------------------------------------------------- 1 | #include "shavite3.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "sha3/sph_shavite.h" 7 | 8 | void shavite3_hash(const char* input, char* output, uint32_t len) 9 | { 10 | char hash1[64]; 11 | char hash2[64]; 12 | 13 | sph_shavite512_context ctx_shavite; 14 | 15 | sph_shavite512_init(&ctx_shavite); 16 | sph_shavite512(&ctx_shavite, (const void*) input, len); 17 | sph_shavite512_close(&ctx_shavite, (void*) &hash1); 18 | 19 | sph_shavite512(&ctx_shavite, (const void*) &hash1, 64); 20 | sph_shavite512_close(&ctx_shavite, (void*) &hash2); 21 | 22 | memcpy(output, &hash2, 32); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /skein.c: -------------------------------------------------------------------------------- 1 | #include "skein.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_skein.h" 8 | #include "sha256.h" 9 | 10 | #include 11 | 12 | void skein_hash(const char* input, char* output, uint32_t len) 13 | { 14 | char temp[64]; 15 | 16 | sph_skein512_context ctx_skien; 17 | sph_skein512_init(&ctx_skien); 18 | sph_skein512(&ctx_skien, input, len); 19 | sph_skein512_close(&ctx_skien, &temp); 20 | 21 | SHA256_CTX ctx_sha256; 22 | SHA256_Init(&ctx_sha256); 23 | SHA256_Update(&ctx_sha256, &temp, 64); 24 | SHA256_Final((unsigned char*) output, &ctx_sha256); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /crypto/hash.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "hash-ops.h" 10 | #include "c_keccak.h" 11 | 12 | void hash_permutation(union hash_state *state) { 13 | keccakf((uint64_t*)state, 24); 14 | } 15 | 16 | void hash_process(union hash_state *state, const uint8_t *buf, size_t count) { 17 | keccak1600(buf, count, (uint8_t*)state); 18 | } 19 | 20 | void cn_fast_hash(const void *data, size_t length, char *hash) { 21 | union hash_state state; 22 | hash_process(&state, data, length); 23 | memcpy(hash, &state, HASH_SIZE); 24 | } 25 | -------------------------------------------------------------------------------- /crypto/c_jh.h: -------------------------------------------------------------------------------- 1 | /*This program gives the 64-bit optimized bitslice implementation of JH using ANSI C 2 | 3 | -------------------------------- 4 | Performance 5 | 6 | Microprocessor: Intel CORE 2 processor (Core 2 Duo Mobile T6600 2.2GHz) 7 | Operating System: 64-bit Ubuntu 10.04 (Linux kernel 2.6.32-22-generic) 8 | Speed for long message: 9 | 1) 45.8 cycles/byte compiler: Intel C++ Compiler 11.1 compilation option: icc -O2 10 | 2) 56.8 cycles/byte compiler: gcc 4.4.3 compilation option: gcc -O3 11 | 12 | -------------------------------- 13 | Last Modified: January 16, 2011 14 | */ 15 | #pragma once 16 | #include "hash.h" 17 | 18 | typedef enum {SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2} HashReturn; 19 | 20 | HashReturn jh_hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval); 21 | -------------------------------------------------------------------------------- /neoscrypt.h: -------------------------------------------------------------------------------- 1 | #ifdef __cplusplus 2 | extern "C" { 3 | #endif 4 | void neoscrypt(const char *input, char *output, int profile); 5 | #ifdef __cplusplus 6 | } 7 | #else 8 | #define SCRYPT_BLOCK_SIZE 64 9 | #define SCRYPT_HASH_BLOCK_SIZE 64 10 | #define SCRYPT_HASH_DIGEST_SIZE 32 11 | 12 | typedef uint8_t hash_digest[SCRYPT_HASH_DIGEST_SIZE]; 13 | 14 | #define ROTL32(a,b) (((a) << (b)) | ((a) >> (32 - b))) 15 | #define ROTR32(a,b) (((a) >> (b)) | ((a) << (32 - b))) 16 | 17 | #define U8TO32_BE(p) \ 18 | (((uint32_t)((p)[0]) << 24) | ((uint32_t)((p)[1]) << 16) | \ 19 | ((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3]))) 20 | 21 | #define U32TO8_BE(p, v) \ 22 | (p)[0] = (uint8_t)((v) >> 24); (p)[1] = (uint8_t)((v) >> 16); \ 23 | (p)[2] = (uint8_t)((v) >> 8); (p)[3] = (uint8_t)((v) ); 24 | 25 | #define U64TO8_BE(p, v) \ 26 | U32TO8_BE((p), (uint32_t)((v) >> 32)); \ 27 | U32TO8_BE((p) + 4, (uint32_t)((v) )); 28 | 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "unomp-multi-hashing", 3 | "version": "1.0.1", 4 | "main": "multihashing", 5 | "author": { 6 | "name": "zone117x" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/UNOMP/node-multi-hashing.git" 11 | }, 12 | "dependencies": { 13 | "bindings": "*" 14 | }, 15 | "keywords": [ 16 | "scrypt", 17 | "scryptjane", 18 | "script-n", 19 | "x11", 20 | "quark", 21 | "keccak_hash", 22 | "skein", 23 | "bcrypt", 24 | "keccak", 25 | "blake", 26 | "shavite", 27 | "fugue", 28 | "yescrypt", 29 | "sha1", 30 | "neoscrypt", 31 | "decrypt", 32 | "timetravel" 33 | ], 34 | "scripts": { 35 | "install": "node-gyp rebuild" 36 | }, 37 | "gypfile": true, 38 | "readmeFilename": "README.md", 39 | "description": "node-multi-hashing", 40 | "bugs": { 41 | "url": "https://github.com/UNOMP/node-multi-hashing/issues" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /s3.c: -------------------------------------------------------------------------------- 1 | #include "s3.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_skein.h" 8 | #include "sha3/sph_shavite.h" 9 | #include "sha3/sph_simd.h" 10 | 11 | void s3_hash(const char* input, char* output, uint32_t len) 12 | { 13 | sph_shavite512_context ctx_shavite1; 14 | sph_simd512_context ctx_simd1; 15 | sph_skein512_context ctx_skein; 16 | 17 | //these uint512 in the c++ source of the client are backed by an array of uint32 18 | uint32_t hashA[16], hashB[16]; 19 | 20 | sph_shavite512_init (&ctx_shavite1); 21 | sph_shavite512 (&ctx_shavite1, input, 80); 22 | sph_shavite512_close(&ctx_shavite1, hashA); 23 | 24 | sph_simd512_init (&ctx_simd1); 25 | sph_simd512 (&ctx_simd1, hashA, 64); 26 | sph_simd512_close(&ctx_simd1, hashB); 27 | 28 | sph_skein512_init(&ctx_skein); 29 | sph_skein512 (&ctx_skein, hashB, 64); 30 | sph_skein512_close (&ctx_skein, hashA); 31 | 32 | memcpy(output, hashA, 32); 33 | } 34 | -------------------------------------------------------------------------------- /crypto/cryptonote_core/cryptonote_format_utils.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #pragma once 6 | #include "../hash.h" 7 | #include "../wild_keccak.h" 8 | 9 | 10 | namespace cryptonote 11 | { 12 | template 13 | bool get_blob_longhash_bb(const blobdata& bd, crypto::hash& res, uint64_t height, callback_t accessor) 14 | { 15 | crypto::wild_keccak_dbl(reinterpret_cast(bd.data()), bd.size(), reinterpret_cast(&res), sizeof(res), [&](crypto::state_t_m& st, crypto::mixin_t& mix) 16 | { 17 | if(!height) 18 | { 19 | memset(&mix, 0, sizeof(mix)); 20 | return; 21 | } 22 | #define GET_H(index) accessor(st[index]) 23 | for(size_t i = 0; i!=6; i++) 24 | { 25 | *(crypto::hash*)&mix[i*4] = XOR_4(GET_H(i*4), GET_H(i*4+1), GET_H(i*4+2), GET_H(i*4+3)); 26 | } 27 | }); 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-romix.h: -------------------------------------------------------------------------------- 1 | #if defined(SCRYPT_CHACHA) 2 | #include "scrypt-jane-chacha.h" 3 | #elif defined(SCRYPT_SALSA) 4 | #include "scrypt-jane-salsa.h" 5 | #elif defined(SCRYPT_SALSA64) 6 | #include "scrypt-jane-salsa64.h" 7 | #else 8 | #define SCRYPT_MIX_BASE "ERROR" 9 | typedef uint32_t scrypt_mix_word_t; 10 | #define SCRYPT_WORDTO8_LE U32TO8_LE 11 | #define SCRYPT_WORD_ENDIAN_SWAP U32_SWAP 12 | #define SCRYPT_BLOCK_BYTES 64 13 | #define SCRYPT_BLOCK_WORDS (SCRYPT_BLOCK_BYTES / sizeof(scrypt_mix_word_t)) 14 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) 15 | static void FASTCALL scrypt_ROMix_error(scrypt_mix_word_t *X/*[chunkWords]*/, scrypt_mix_word_t *Y/*[chunkWords]*/, scrypt_mix_word_t *V/*[chunkWords * N]*/, uint32_t N, uint32_t r) {} 16 | static scrypt_ROMixfn scrypt_getROMix() { return scrypt_ROMix_error; } 17 | #else 18 | static void FASTCALL scrypt_ROMix(scrypt_mix_word_t *X, scrypt_mix_word_t *Y, scrypt_mix_word_t *V, uint32_t N, uint32_t r) {} 19 | #endif 20 | static int scrypt_test_mix() { return 0; } 21 | #error must define a mix function! 22 | #endif 23 | 24 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) 25 | #undef SCRYPT_MIX 26 | #define SCRYPT_MIX SCRYPT_MIX_BASE 27 | #endif 28 | -------------------------------------------------------------------------------- /groestl.c: -------------------------------------------------------------------------------- 1 | #include "groestl.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_groestl.h" 8 | #include "sha256.h" 9 | 10 | void groestl_hash(const char* input, char* output, uint32_t len) 11 | { 12 | char hash1[64]; 13 | char hash2[64]; 14 | 15 | sph_groestl512_context ctx_groestl; 16 | sph_groestl512_init(&ctx_groestl); 17 | sph_groestl512(&ctx_groestl, input, len); 18 | sph_groestl512_close(&ctx_groestl, &hash1); 19 | 20 | sph_groestl512(&ctx_groestl, hash1, 64); 21 | sph_groestl512_close(&ctx_groestl, &hash2); 22 | 23 | memcpy(output, &hash2, 32); 24 | } 25 | 26 | void groestlmyriad_hash(const char* input, char* output, uint32_t len) 27 | { 28 | char temp[64]; 29 | 30 | sph_groestl512_context ctx_groestl; 31 | sph_groestl512_init(&ctx_groestl); 32 | sph_groestl512(&ctx_groestl, input, len); 33 | sph_groestl512_close(&ctx_groestl, &temp); 34 | 35 | SHA256_CTX ctx_sha256; 36 | SHA256_Init(&ctx_sha256); 37 | SHA256_Update(&ctx_sha256, &temp, 64); 38 | SHA256_Final((unsigned char*) output, &ctx_sha256); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /scryptjane.h: -------------------------------------------------------------------------------- 1 | #ifndef SCRYPT_JANE_H 2 | #define SCRYPT_JANE_H 3 | 4 | #include 5 | 6 | #define SCRYPT_KECCAK512 7 | #define SCRYPT_CHACHA 8 | #define SCRYPT_CHOOSE_COMPILETIME 9 | 10 | /* 11 | Nfactor: Increases CPU & Memory Hardness 12 | N = (1 << (Nfactor + 1)): How many times to mix a chunk and how many temporary chunks are used 13 | 14 | rfactor: Increases Memory Hardness 15 | r = (1 << rfactor): How large a chunk is 16 | 17 | pfactor: Increases CPU Hardness 18 | p = (1 << pfactor): Number of times to mix the main chunk 19 | 20 | A block is the basic mixing unit (salsa/chacha block = 64 bytes) 21 | A chunk is (2 * r) blocks 22 | 23 | ~Memory used = (N + 2) * ((2 * r) * block size) 24 | */ 25 | 26 | #include 27 | 28 | typedef void (*scrypt_fatal_errorfn)(const char *msg); 29 | void scrypt_set_fatal_error(scrypt_fatal_errorfn fn); 30 | 31 | void scrypt(const unsigned char *password, size_t password_len, const unsigned char *salt, size_t salt_len, unsigned char Nfactor, unsigned char rfactor, unsigned char pfactor, unsigned char *out, size_t bytes); 32 | 33 | unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax); 34 | void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor); 35 | 36 | #endif /* SCRYPT_JANE_H */ 37 | -------------------------------------------------------------------------------- /fresh.c: -------------------------------------------------------------------------------- 1 | #include "fresh.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_shavite.h" 8 | #include "sha3/sph_simd.h" 9 | #include "sha3/sph_echo.h" 10 | 11 | void fresh_hash(const char* input, char* output, uint32_t len) 12 | { 13 | sph_shavite512_context ctx_shavite1; 14 | sph_simd512_context ctx_simd1; 15 | sph_echo512_context ctx_echo1; 16 | 17 | //these uint512 in the c++ source of the client are backed by an array of uint32 18 | uint32_t hashA[16], hashB[16]; 19 | 20 | sph_shavite512_init (&ctx_shavite1); 21 | sph_shavite512 (&ctx_shavite1, input, len); 22 | sph_shavite512_close(&ctx_shavite1, hashA); 23 | 24 | sph_simd512_init (&ctx_simd1); 25 | sph_simd512 (&ctx_simd1, hashA, 64); 26 | sph_simd512_close(&ctx_simd1, hashB); 27 | 28 | sph_shavite512_init (&ctx_shavite1); 29 | sph_shavite512 (&ctx_shavite1, hashB, 64); 30 | sph_shavite512_close(&ctx_shavite1, hashA); 31 | 32 | sph_simd512_init (&ctx_simd1); 33 | sph_simd512 (&ctx_simd1, hashA, 64); 34 | sph_simd512_close(&ctx_simd1, hashB); 35 | 36 | sph_echo512_init (&ctx_echo1); 37 | sph_echo512 (&ctx_echo1, hashB, 64); 38 | sph_echo512_close(&ctx_echo1, hashA); 39 | 40 | memcpy(output, hashA, 32); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /crypto/c_blake256.h: -------------------------------------------------------------------------------- 1 | #ifndef _BLAKE256_H_ 2 | #define _BLAKE256_H_ 3 | 4 | #include 5 | 6 | typedef struct { 7 | uint32_t h[8], s[4], t[2]; 8 | int buflen, nullt; 9 | uint8_t buf[64]; 10 | } state; 11 | 12 | typedef struct { 13 | state inner; 14 | state outer; 15 | } hmac_state; 16 | 17 | void blake256_init(state *); 18 | void blake224_init(state *); 19 | 20 | void blake256_update(state *, const uint8_t *, uint64_t); 21 | void blake224_update(state *, const uint8_t *, uint64_t); 22 | 23 | void blake256_final(state *, uint8_t *); 24 | void blake224_final(state *, uint8_t *); 25 | 26 | void blake256_hash(uint8_t *, const uint8_t *, uint64_t); 27 | void blake224_hash(uint8_t *, const uint8_t *, uint64_t); 28 | 29 | /* HMAC functions: */ 30 | 31 | void hmac_blake256_init(hmac_state *, const uint8_t *, uint64_t); 32 | void hmac_blake224_init(hmac_state *, const uint8_t *, uint64_t); 33 | 34 | void hmac_blake256_update(hmac_state *, const uint8_t *, uint64_t); 35 | void hmac_blake224_update(hmac_state *, const uint8_t *, uint64_t); 36 | 37 | void hmac_blake256_final(hmac_state *, uint8_t *); 38 | void hmac_blake224_final(hmac_state *, uint8_t *); 39 | 40 | void hmac_blake256_hash(uint8_t *, const uint8_t *, uint64_t, const uint8_t *, uint64_t); 41 | void hmac_blake224_hash(uint8_t *, const uint8_t *, uint64_t, const uint8_t *, uint64_t); 42 | 43 | #endif /* _BLAKE256_H_ */ 44 | -------------------------------------------------------------------------------- /nist5.c: -------------------------------------------------------------------------------- 1 | #include "nist5.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_groestl.h" 9 | #include "sha3/sph_jh.h" 10 | #include "sha3/sph_keccak.h" 11 | #include "sha3/sph_skein.h" 12 | 13 | 14 | void nist5_hash(const char* input, char* output, uint32_t len) 15 | { 16 | sph_blake512_context ctx_blake; 17 | sph_groestl512_context ctx_groestl; 18 | sph_skein512_context ctx_skein; 19 | sph_jh512_context ctx_jh; 20 | sph_keccak512_context ctx_keccak; 21 | 22 | //these uint512 in the c++ source of the client are backed by an array of uint32 23 | uint32_t hash[16]; 24 | 25 | sph_blake512_init(&ctx_blake); 26 | sph_blake512 (&ctx_blake, input, len); 27 | sph_blake512_close (&ctx_blake, hash); 28 | 29 | sph_groestl512_init(&ctx_groestl); 30 | sph_groestl512 (&ctx_groestl, hash, 64); 31 | sph_groestl512_close(&ctx_groestl, hash); 32 | 33 | sph_jh512_init(&ctx_jh); 34 | sph_jh512 (&ctx_jh, hash, 64); 35 | sph_jh512_close(&ctx_jh, hash); 36 | 37 | sph_keccak512_init(&ctx_keccak); 38 | sph_keccak512 (&ctx_keccak, hash, 64); 39 | sph_keccak512_close(&ctx_keccak, hash); 40 | 41 | sph_skein512_init(&ctx_skein); 42 | sph_skein512 (&ctx_skein, hash, 64); 43 | sph_skein512_close (&ctx_skein, hash); 44 | 45 | memcpy(output, hash, 32); 46 | } -------------------------------------------------------------------------------- /x5.c: -------------------------------------------------------------------------------- 1 | #include "x5.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_groestl.h" 9 | #include "sha3/sph_jh.h" 10 | #include "sha3/sph_keccak.h" 11 | #include "sha3/sph_skein.h" 12 | 13 | void x5_hash(const char* input, char* output) 14 | { 15 | sph_blake512_context ctx_blake; 16 | sph_groestl512_context ctx_groestl; 17 | sph_jh512_context ctx_jh; 18 | sph_keccak512_context ctx_keccak; 19 | sph_skein512_context ctx_skein; 20 | 21 | unsigned char hash[64]; 22 | 23 | // BLAKE 24 | sph_blake512_init(&ctx_blake); 25 | sph_blake512(&ctx_blake, input, 80); 26 | sph_blake512_close(&ctx_blake, (void*) hash); 27 | // GROESTL 28 | sph_groestl512_init(&ctx_groestl); 29 | sph_groestl512(&ctx_groestl, (const void*) hash, 64); 30 | sph_groestl512_close(&ctx_groestl, (void*) hash); 31 | // JH 32 | sph_jh512_init(&ctx_jh); 33 | sph_jh512(&ctx_jh, (const void*) hash, 64); 34 | sph_jh512_close(&ctx_jh, (void*) hash); 35 | // KECCAK 36 | sph_keccak512_init(&ctx_keccak); 37 | sph_keccak512(&ctx_keccak, (const void*) hash, 64); 38 | sph_keccak512_close(&ctx_keccak, (void*) hash); 39 | // SKEIN 40 | sph_skein512_init(&ctx_skein); 41 | sph_skein512(&ctx_skein, (const void*) hash, 64); 42 | sph_skein512_close(&ctx_skein, (void*) hash); 43 | 44 | memcpy(state, hash, 32); 45 | } 46 | -------------------------------------------------------------------------------- /qubit.c: -------------------------------------------------------------------------------- 1 | #include "qubit.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "sha3/sph_cubehash.h" 7 | #include "sha3/sph_luffa.h" 8 | #include "sha3/sph_shavite.h" 9 | #include "sha3/sph_simd.h" 10 | #include "sha3/sph_echo.h" 11 | 12 | void qubit_hash(const char* input, char* output, uint32_t len) 13 | { 14 | sph_luffa512_context ctx_luffa; 15 | sph_cubehash512_context ctx_cubehash; 16 | sph_shavite512_context ctx_shavite; 17 | sph_simd512_context ctx_simd; 18 | sph_echo512_context ctx_echo; 19 | 20 | char hash1[64]; 21 | char hash2[64]; 22 | 23 | sph_luffa512_init(&ctx_luffa); 24 | sph_luffa512(&ctx_luffa, (const void*) input, len); 25 | sph_luffa512_close(&ctx_luffa, (void*) &hash1); // 1 26 | 27 | sph_cubehash512_init(&ctx_cubehash); 28 | sph_cubehash512(&ctx_cubehash, (const void*) &hash1, 64); // 1 29 | sph_cubehash512_close(&ctx_cubehash, (void*) &hash2); // 2 30 | 31 | sph_shavite512_init(&ctx_shavite); 32 | sph_shavite512(&ctx_shavite, (const void*) &hash2, 64); // 3 33 | sph_shavite512_close(&ctx_shavite, (void*) &hash1); // 4 34 | 35 | sph_simd512_init(&ctx_simd); 36 | sph_simd512(&ctx_simd, (const void*) &hash1, 64); // 4 37 | sph_simd512_close(&ctx_simd, (void*) &hash2); // 5 38 | 39 | sph_echo512_init(&ctx_echo); 40 | sph_echo512(&ctx_echo, (const void*) &hash2, 64); // 5 41 | sph_echo512_close(&ctx_echo, (void*) &hash1); // 6 42 | 43 | memcpy(output, &hash1, 32); 44 | } 45 | 46 | -------------------------------------------------------------------------------- /crypto/c_groestl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /* 3 | #include "crypto_uint8.h" 4 | #include "crypto_uint32.h" 5 | #include "crypto_uint64.h" 6 | #include "crypto_hash.h" 7 | 8 | typedef crypto_uint8 uint8_t; 9 | typedef crypto_uint32 uint32_t; 10 | typedef crypto_uint64 uint64_t; 11 | */ 12 | #include 13 | #include "hash.h" 14 | 15 | /* some sizes (number of bytes) */ 16 | #define ROWS 8 17 | #define LENGTHFIELDLEN ROWS 18 | #define COLS512 8 19 | 20 | #define SIZE512 (ROWS*COLS512) 21 | 22 | #define ROUNDS512 10 23 | #define HASH_BIT_LEN 256 24 | 25 | #define ROTL32(v, n) ((((v)<<(n))|((v)>>(32-(n))))&li_32(ffffffff)) 26 | 27 | 28 | #define li_32(h) 0x##h##u 29 | #define EXT_BYTE(var,n) ((uint8_t)((uint32_t)(var) >> (8*n))) 30 | #define u32BIG(a) \ 31 | ((ROTL32(a,8) & li_32(00FF00FF)) | \ 32 | (ROTL32(a,24) & li_32(FF00FF00))) 33 | 34 | 35 | /* NIST API begin */ 36 | typedef struct { 37 | uint32_t chaining[SIZE512/sizeof(uint32_t)]; /* actual state */ 38 | uint32_t block_counter1, 39 | block_counter2; /* message block counter(s) */ 40 | BitSequence buffer[SIZE512]; /* data buffer */ 41 | int buf_ptr; /* data buffer pointer */ 42 | int bits_in_last_byte; /* no. of message bits in last byte of 43 | data buffer */ 44 | } hashState; 45 | 46 | /*void Init(hashState*); 47 | void Update(hashState*, const BitSequence*, DataLength); 48 | void Final(hashState*, BitSequence*); */ 49 | void groestl(const BitSequence*, DataLength, BitSequence*); 50 | /* NIST API end */ 51 | 52 | /* 53 | int crypto_hash(unsigned char *out, 54 | const unsigned char *in, 55 | unsigned long long len); 56 | */ 57 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-mix_chacha.h: -------------------------------------------------------------------------------- 1 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_CHACHA_INCLUDED) 2 | 3 | #undef SCRYPT_MIX 4 | #define SCRYPT_MIX "ChaCha20/8 Ref" 5 | 6 | #undef SCRYPT_CHACHA_INCLUDED 7 | #define SCRYPT_CHACHA_INCLUDED 8 | #define SCRYPT_CHACHA_BASIC 9 | 10 | static void 11 | chacha_core_basic(uint32_t state[16]) { 12 | size_t rounds = 8; 13 | uint32_t x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,t; 14 | 15 | x0 = state[0]; 16 | x1 = state[1]; 17 | x2 = state[2]; 18 | x3 = state[3]; 19 | x4 = state[4]; 20 | x5 = state[5]; 21 | x6 = state[6]; 22 | x7 = state[7]; 23 | x8 = state[8]; 24 | x9 = state[9]; 25 | x10 = state[10]; 26 | x11 = state[11]; 27 | x12 = state[12]; 28 | x13 = state[13]; 29 | x14 = state[14]; 30 | x15 = state[15]; 31 | 32 | #define quarter(a,b,c,d) \ 33 | a += b; t = d^a; d = ROTL32(t,16); \ 34 | c += d; t = b^c; b = ROTL32(t,12); \ 35 | a += b; t = d^a; d = ROTL32(t, 8); \ 36 | c += d; t = b^c; b = ROTL32(t, 7); 37 | 38 | for (; rounds; rounds -= 2) { 39 | quarter( x0, x4, x8,x12) 40 | quarter( x1, x5, x9,x13) 41 | quarter( x2, x6,x10,x14) 42 | quarter( x3, x7,x11,x15) 43 | quarter( x0, x5,x10,x15) 44 | quarter( x1, x6,x11,x12) 45 | quarter( x2, x7, x8,x13) 46 | quarter( x3, x4, x9,x14) 47 | } 48 | 49 | state[0] += x0; 50 | state[1] += x1; 51 | state[2] += x2; 52 | state[3] += x3; 53 | state[4] += x4; 54 | state[5] += x5; 55 | state[6] += x6; 56 | state[7] += x7; 57 | state[8] += x8; 58 | state[9] += x9; 59 | state[10] += x10; 60 | state[11] += x11; 61 | state[12] += x12; 62 | state[13] += x13; 63 | state[14] += x14; 64 | state[15] += x15; 65 | 66 | #undef quarter 67 | } 68 | 69 | #endif -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-mix_salsa.h: -------------------------------------------------------------------------------- 1 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_SALSA_INCLUDED) 2 | 3 | #undef SCRYPT_MIX 4 | #define SCRYPT_MIX "Salsa20/8 Ref" 5 | 6 | #undef SCRYPT_SALSA_INCLUDED 7 | #define SCRYPT_SALSA_INCLUDED 8 | #define SCRYPT_SALSA_BASIC 9 | 10 | static void 11 | salsa_core_basic(uint32_t state[16]) { 12 | size_t rounds = 8; 13 | uint32_t x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,t; 14 | 15 | x0 = state[0]; 16 | x1 = state[1]; 17 | x2 = state[2]; 18 | x3 = state[3]; 19 | x4 = state[4]; 20 | x5 = state[5]; 21 | x6 = state[6]; 22 | x7 = state[7]; 23 | x8 = state[8]; 24 | x9 = state[9]; 25 | x10 = state[10]; 26 | x11 = state[11]; 27 | x12 = state[12]; 28 | x13 = state[13]; 29 | x14 = state[14]; 30 | x15 = state[15]; 31 | 32 | #define quarter(a,b,c,d) \ 33 | t = a+d; t = ROTL32(t, 7); b ^= t; \ 34 | t = b+a; t = ROTL32(t, 9); c ^= t; \ 35 | t = c+b; t = ROTL32(t, 13); d ^= t; \ 36 | t = d+c; t = ROTL32(t, 18); a ^= t; \ 37 | 38 | for (; rounds; rounds -= 2) { 39 | quarter( x0, x4, x8,x12) 40 | quarter( x5, x9,x13, x1) 41 | quarter(x10,x14, x2, x6) 42 | quarter(x15, x3, x7,x11) 43 | quarter( x0, x1, x2, x3) 44 | quarter( x5, x6, x7, x4) 45 | quarter(x10,x11, x8, x9) 46 | quarter(x15,x12,x13,x14) 47 | } 48 | 49 | state[0] += x0; 50 | state[1] += x1; 51 | state[2] += x2; 52 | state[3] += x3; 53 | state[4] += x4; 54 | state[5] += x5; 55 | state[6] += x6; 56 | state[7] += x7; 57 | state[8] += x8; 58 | state[9] += x9; 59 | state[10] += x10; 60 | state[11] += x11; 61 | state[12] += x12; 62 | state[13] += x13; 63 | state[14] += x14; 64 | state[15] += x15; 65 | 66 | #undef quarter 67 | } 68 | 69 | #endif 70 | 71 | -------------------------------------------------------------------------------- /crypto/hash-ops.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #pragma once 6 | 7 | #if !defined(__cplusplus) 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "int-util.h" 15 | 16 | static inline void *padd(void *p, size_t i) { 17 | return (char *) p + i; 18 | } 19 | 20 | static inline const void *cpadd(const void *p, size_t i) { 21 | return (const char *) p + i; 22 | } 23 | 24 | static inline void place_length(uint8_t *buffer, size_t bufsize, size_t length) { 25 | if (sizeof(size_t) == 4) { 26 | *(uint32_t *) padd(buffer, bufsize - 4) = swap32be(length); 27 | } else { 28 | *(uint64_t *) padd(buffer, bufsize - 8) = swap64be(length); 29 | } 30 | } 31 | 32 | #pragma pack(push, 1) 33 | union hash_state { 34 | uint8_t b[200]; 35 | uint64_t w[25]; 36 | }; 37 | #pragma pack(pop) 38 | 39 | void hash_permutation(union hash_state *state); 40 | void hash_process(union hash_state *state, const uint8_t *buf, size_t count); 41 | 42 | #endif 43 | 44 | enum { 45 | HASH_SIZE = 32, 46 | HASH_DATA_AREA = 136 47 | }; 48 | 49 | void cn_fast_hash(const void *data, size_t length, char *hash); 50 | void cn_slow_hash(const void *data, size_t length, char *hash); 51 | 52 | void hash_extra_blake(const void *data, size_t length, char *hash); 53 | void hash_extra_groestl(const void *data, size_t length, char *hash); 54 | void hash_extra_jh(const void *data, size_t length, char *hash); 55 | void hash_extra_skein(const void *data, size_t length, char *hash); 56 | 57 | void tree_hash(const char (*hashes)[HASH_SIZE], size_t count, char *root_hash); 58 | -------------------------------------------------------------------------------- /sha1.c: -------------------------------------------------------------------------------- 1 | #include "sha1.h" 2 | 3 | #include 4 | #include 5 | 6 | inline void encodeb64(const unsigned char* pch, char* buff) 7 | { 8 | const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 9 | int mode = 0, left = 0; 10 | const int len = 20; 11 | const unsigned char *pchEnd = pch + len; 12 | while (pch < pchEnd) { 13 | int enc = *(pch++); 14 | if (mode == 0) { 15 | *buff++ = pbase64[enc >> 2]; 16 | left = (enc & 3) << 4; 17 | mode = 1; 18 | } 19 | else if (mode == 1) { 20 | *buff++ = pbase64[left | (enc >> 4)]; 21 | left = (enc & 15) << 2; 22 | mode = 2; 23 | } 24 | else { 25 | *buff++ = pbase64[left | (enc >> 6)]; 26 | *buff++ = pbase64[enc & 63]; 27 | mode = 0; 28 | } 29 | } 30 | *buff = pbase64[left]; 31 | *(buff + 1) = 0; 32 | } 33 | 34 | void sha1_hash(const char* input, char* output, uint32_t len) 35 | { 36 | char str[38] __attribute__((aligned(32))); // 26 + 11 + 1 37 | uint32_t prehash[5] __attribute__((aligned(32))); 38 | uint32_t hash[5] __attribute__((aligned(32))) = { 0 }; 39 | int i = 0; 40 | SHA_CTX ctx; 41 | SHA1_Init(&ctx); 42 | SHA1_Update(&ctx, (void *)input, len); 43 | SHA1_Final((void *)prehash, &ctx); 44 | encodeb64((const unsigned char *)prehash, str); 45 | memcpy(&str[26], str, 11); 46 | str[37] = 0; 47 | for (i = 0; i < 26; i++) { 48 | SHA1_Init(&ctx); 49 | SHA1_Update(&ctx, (void *)&str[i], 12); 50 | SHA1_Final((void *)prehash, &ctx); 51 | hash[0] ^= prehash[0]; 52 | hash[1] ^= prehash[1]; 53 | hash[2] ^= prehash[2]; 54 | hash[3] ^= prehash[3]; 55 | hash[4] ^= prehash[4]; 56 | } 57 | memset(output, 0, 32 - 20); 58 | memcpy(&output[32 - 20], hash, 20); 59 | } 60 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-hash.h: -------------------------------------------------------------------------------- 1 | #if defined(SCRYPT_BLAKE512) 2 | #include "scrypt-jane-hash_blake512.h" 3 | #elif defined(SCRYPT_BLAKE256) 4 | #include "scrypt-jane-hash_blake256.h" 5 | #elif defined(SCRYPT_SHA512) 6 | #include "scrypt-jane-hash_sha512.h" 7 | #elif defined(SCRYPT_SHA256) 8 | #include "scrypt-jane-hash_sha256.h" 9 | #elif defined(SCRYPT_SKEIN512) 10 | #include "scrypt-jane-hash_skein512.h" 11 | #elif defined(SCRYPT_KECCAK512) || defined(SCRYPT_KECCAK256) 12 | #include "scrypt-jane-hash_keccak.h" 13 | #else 14 | #define SCRYPT_HASH "ERROR" 15 | #define SCRYPT_HASH_BLOCK_SIZE 64 16 | #define SCRYPT_HASH_DIGEST_SIZE 64 17 | typedef struct scrypt_hash_state_t { size_t dummy; } scrypt_hash_state; 18 | typedef uint8_t scrypt_hash_digest[SCRYPT_HASH_DIGEST_SIZE]; 19 | static void scrypt_hash_init(scrypt_hash_state *S) {} 20 | static void scrypt_hash_update(scrypt_hash_state *S, const uint8_t *in, size_t inlen) {} 21 | static void scrypt_hash_finish(scrypt_hash_state *S, uint8_t *hash) {} 22 | static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = {0}; 23 | #error must define a hash function! 24 | #endif 25 | 26 | #include "scrypt-jane-pbkdf2.h" 27 | 28 | #define SCRYPT_TEST_HASH_LEN 257 /* (2 * largest block size) + 1 */ 29 | 30 | static int 31 | scrypt_test_hash() { 32 | scrypt_hash_state st; 33 | scrypt_hash_digest hash, final; 34 | uint8_t msg[SCRYPT_TEST_HASH_LEN]; 35 | size_t i; 36 | 37 | for (i = 0; i < SCRYPT_TEST_HASH_LEN; i++) 38 | msg[i] = (uint8_t)i; 39 | 40 | scrypt_hash_init(&st); 41 | for (i = 0; i < SCRYPT_TEST_HASH_LEN + 1; i++) { 42 | scrypt_hash(hash, msg, i); 43 | scrypt_hash_update(&st, hash, sizeof(hash)); 44 | } 45 | scrypt_hash_finish(&st, final); 46 | return scrypt_verify(final, scrypt_test_hash_expected, SCRYPT_HASH_DIGEST_SIZE); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /crypto/cryptonote_core/account.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "include_base_utils.h" 10 | #include "account.h" 11 | #include "warnings.h" 12 | #include "crypto/crypto.h" 13 | #include "cryptonote_core/cryptonote_basic_impl.h" 14 | #include "cryptonote_core/cryptonote_format_utils.h" 15 | using namespace std; 16 | 17 | DISABLE_VS_WARNINGS(4244 4345) 18 | 19 | namespace cryptonote 20 | { 21 | //----------------------------------------------------------------- 22 | account_base::account_base() 23 | { 24 | set_null(); 25 | } 26 | //----------------------------------------------------------------- 27 | void account_base::set_null() 28 | { 29 | m_keys = account_keys(); 30 | } 31 | //----------------------------------------------------------------- 32 | void account_base::generate() 33 | { 34 | generate_keys(m_keys.m_account_address.m_spend_public_key, m_keys.m_spend_secret_key); 35 | generate_keys(m_keys.m_account_address.m_view_public_key, m_keys.m_view_secret_key); 36 | m_creation_timestamp = time(NULL); 37 | } 38 | //----------------------------------------------------------------- 39 | const account_keys& account_base::get_keys() const 40 | { 41 | return m_keys; 42 | } 43 | //----------------------------------------------------------------- 44 | std::string account_base::get_public_address_str() 45 | { 46 | //TODO: change this code into base 58 47 | return get_account_address_as_str(m_keys.m_account_address); 48 | } 49 | //----------------------------------------------------------------- 50 | } 51 | -------------------------------------------------------------------------------- /crypto/c_skein.h: -------------------------------------------------------------------------------- 1 | #ifndef _SKEIN_H_ 2 | #define _SKEIN_H_ 1 3 | /************************************************************************** 4 | ** 5 | ** Interface declarations and internal definitions for Skein hashing. 6 | ** 7 | ** Source code author: Doug Whiting, 2008. 8 | ** 9 | ** This algorithm and source code is released to the public domain. 10 | ** 11 | *************************************************************************** 12 | ** 13 | ** The following compile-time switches may be defined to control some 14 | ** tradeoffs between speed, code size, error checking, and security. 15 | ** 16 | ** The "default" note explains what happens when the switch is not defined. 17 | ** 18 | ** SKEIN_DEBUG -- make callouts from inside Skein code 19 | ** to examine/display intermediate values. 20 | ** [default: no callouts (no overhead)] 21 | ** 22 | ** SKEIN_ERR_CHECK -- how error checking is handled inside Skein 23 | ** code. If not defined, most error checking 24 | ** is disabled (for performance). Otherwise, 25 | ** the switch value is interpreted as: 26 | ** 0: use assert() to flag errors 27 | ** 1: return SKEIN_FAIL to flag errors 28 | ** 29 | ***************************************************************************/ 30 | #include "skein_port.h" /* get platform-specific definitions */ 31 | #include "hash.h" 32 | 33 | typedef enum 34 | { 35 | SKEIN_SUCCESS = 0, /* return codes from Skein calls */ 36 | SKEIN_FAIL = 1, 37 | SKEIN_BAD_HASHLEN = 2 38 | } 39 | SkeinHashReturn; 40 | 41 | /* "all-in-one" call */ 42 | SkeinHashReturn c_skein_hash(int hashbitlen, const BitSequence *data, 43 | DataLength databitlen, BitSequence *hashval); 44 | 45 | #endif /* ifndef _SKEIN_H_ */ 46 | -------------------------------------------------------------------------------- /sha3/sph_fugue.h: -------------------------------------------------------------------------------- 1 | #ifndef SPH_FUGUE_H__ 2 | #define SPH_FUGUE_H__ 3 | 4 | #include 5 | #include "sph_types.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C"{ 9 | #endif 10 | 11 | #define SPH_SIZE_fugue224 224 12 | 13 | #define SPH_SIZE_fugue256 256 14 | 15 | #define SPH_SIZE_fugue384 384 16 | 17 | #define SPH_SIZE_fugue512 512 18 | 19 | typedef struct { 20 | #ifndef DOXYGEN_IGNORE 21 | sph_u32 partial; 22 | unsigned partial_len; 23 | unsigned round_shift; 24 | sph_u32 S[36]; 25 | #if SPH_64 26 | sph_u64 bit_count; 27 | #else 28 | sph_u32 bit_count_high, bit_count_low; 29 | #endif 30 | #endif 31 | } sph_fugue_context; 32 | 33 | typedef sph_fugue_context sph_fugue224_context; 34 | 35 | typedef sph_fugue_context sph_fugue256_context; 36 | 37 | typedef sph_fugue_context sph_fugue384_context; 38 | 39 | typedef sph_fugue_context sph_fugue512_context; 40 | 41 | void sph_fugue224_init(void *cc); 42 | 43 | void sph_fugue224(void *cc, const void *data, size_t len); 44 | 45 | void sph_fugue224_close(void *cc, void *dst); 46 | 47 | void sph_fugue224_addbits_and_close( 48 | void *cc, unsigned ub, unsigned n, void *dst); 49 | 50 | void sph_fugue256_init(void *cc); 51 | 52 | void sph_fugue256(void *cc, const void *data, size_t len); 53 | 54 | void sph_fugue256_close(void *cc, void *dst); 55 | 56 | void sph_fugue256_addbits_and_close( 57 | void *cc, unsigned ub, unsigned n, void *dst); 58 | 59 | void sph_fugue384_init(void *cc); 60 | 61 | void sph_fugue384(void *cc, const void *data, size_t len); 62 | 63 | void sph_fugue384_close(void *cc, void *dst); 64 | 65 | void sph_fugue384_addbits_and_close( 66 | void *cc, unsigned ub, unsigned n, void *dst); 67 | 68 | void sph_fugue512_init(void *cc); 69 | 70 | void sph_fugue512(void *cc, const void *data, size_t len); 71 | 72 | void sph_fugue512_close(void *cc, void *dst); 73 | 74 | void sph_fugue512_addbits_and_close( 75 | void *cc, unsigned ub, unsigned n, void *dst); 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif -------------------------------------------------------------------------------- /crypto/cryptonote_core/account.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #pragma once 6 | 7 | #include "cryptonote_core/cryptonote_basic.h" 8 | #include "crypto/crypto.h" 9 | #include "serialization/keyvalue_serialization.h" 10 | 11 | namespace cryptonote 12 | { 13 | 14 | struct account_keys 15 | { 16 | account_public_address m_account_address; 17 | crypto::secret_key m_spend_secret_key; 18 | crypto::secret_key m_view_secret_key; 19 | 20 | BEGIN_KV_SERIALIZE_MAP() 21 | KV_SERIALIZE(m_account_address) 22 | KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_spend_secret_key) 23 | KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(m_view_secret_key) 24 | END_KV_SERIALIZE_MAP() 25 | }; 26 | 27 | /************************************************************************/ 28 | /* */ 29 | /************************************************************************/ 30 | class account_base 31 | { 32 | public: 33 | account_base(); 34 | void generate(); 35 | const account_keys& get_keys() const; 36 | std::string get_public_address_str(); 37 | 38 | uint64_t get_createtime() const { return m_creation_timestamp; } 39 | void set_createtime(uint64_t val) { m_creation_timestamp = val; } 40 | 41 | bool load(const std::string& file_path); 42 | bool store(const std::string& file_path); 43 | 44 | template 45 | inline void serialize(t_archive &a, const unsigned int /*ver*/) 46 | { 47 | a & m_keys; 48 | a & m_creation_timestamp; 49 | } 50 | 51 | BEGIN_KV_SERIALIZE_MAP() 52 | KV_SERIALIZE(m_keys) 53 | KV_SERIALIZE(m_creation_timestamp) 54 | END_KV_SERIALIZE_MAP() 55 | 56 | private: 57 | void set_null(); 58 | account_keys m_keys; 59 | uint64_t m_creation_timestamp; 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /crypto/oaes_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * --------------------------------------------------------------------------- 3 | * OpenAES License 4 | * --------------------------------------------------------------------------- 5 | * Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * - Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * - Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | * --------------------------------------------------------------------------- 29 | */ 30 | 31 | #ifndef _OAES_CONFIG_H 32 | #define _OAES_CONFIG_H 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | //#ifndef OAES_HAVE_ISAAC 39 | //#define OAES_HAVE_ISAAC 1 40 | //#endif // OAES_HAVE_ISAAC 41 | 42 | //#ifndef OAES_DEBUG 43 | //#define OAES_DEBUG 0 44 | //#endif // OAES_DEBUG 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif // _OAES_CONFIG_H 51 | -------------------------------------------------------------------------------- /hefty1.c: -------------------------------------------------------------------------------- 1 | #include "hefty1.h" 2 | 3 | #include "sha3/sph_hefty1.h" 4 | #include "sha3/sph_keccak.h" 5 | #include "sha3/sph_groestl.h" 6 | #include "sha3/sph_blake.h" 7 | #include "sha256.h" 8 | 9 | void hefty1_hash(const char* input, char* output, uint32_t len) 10 | { 11 | HEFTY1_CTX ctx_hefty1; 12 | SHA256_CTX ctx_sha256; 13 | sph_keccak512_context ctx_keccak; 14 | sph_groestl512_context ctx_groestl; 15 | sph_blake512_context ctx_blake; 16 | 17 | char hash32_1[32]; 18 | char hash32_2[32]; 19 | char hash64_3[64]; 20 | char hash64_4[64]; 21 | char hash64_5[64]; 22 | 23 | HEFTY1_Init(&ctx_hefty1); 24 | HEFTY1_Update(&ctx_hefty1, (const void*) input, len); 25 | HEFTY1_Final((unsigned char*) &hash32_1, &ctx_hefty1); // 1 26 | 27 | SHA256_Init(&ctx_sha256); 28 | SHA256_Update(&ctx_sha256, (const void*) input, len); 29 | SHA256_Update(&ctx_sha256, (unsigned char*) &hash32_1, 32); // 1 30 | SHA256_Final((unsigned char*) &hash32_2, &ctx_sha256); // 2 31 | 32 | sph_keccak512_init(&ctx_keccak); 33 | sph_keccak512(&ctx_keccak, (const void*) input, len); 34 | sph_keccak512(&ctx_keccak, (unsigned char*) &hash32_1, 32); //1 35 | sph_keccak512_close(&ctx_keccak, (void*) &hash64_3); // 3 36 | 37 | sph_groestl512_init(&ctx_groestl); 38 | sph_groestl512(&ctx_groestl, (const void*) input, len); 39 | sph_groestl512(&ctx_groestl, (unsigned char*) &hash32_1, 32); // 1 40 | sph_groestl512_close(&ctx_groestl, (void*) &hash64_4); // 4 41 | 42 | sph_blake512_init(&ctx_blake); 43 | sph_blake512(&ctx_blake, (const void*) input, len); 44 | sph_blake512(&ctx_blake, (unsigned char*) &hash32_1, 32); // 1 45 | sph_blake512_close(&ctx_blake, (void*) &hash64_5); // 5 46 | 47 | memset(output, 0, 32); 48 | 49 | char* hash[4] = { hash32_2, hash64_3, hash64_4, hash64_5 }; 50 | 51 | uint32_t i; 52 | uint32_t j; 53 | 54 | #define OUTPUT_BIT (i * 4 + j) 55 | 56 | for(i = 0; i < 64; i++) { 57 | for(j = 0; j < 4; j++) { 58 | if((*(hash[j] + (i / 8)) & (0x80 >> (i % 8))) != 0) 59 | *(output + (OUTPUT_BIT / 8)) |= 0x80 >> (OUTPUT_BIT % 8); 60 | } 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-romix-basic.h: -------------------------------------------------------------------------------- 1 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) 2 | /* function type returned by scrypt_getROMix, used with cpu detection */ 3 | typedef void (FASTCALL *scrypt_ROMixfn)(scrypt_mix_word_t *X/*[chunkWords]*/, scrypt_mix_word_t *Y/*[chunkWords]*/, scrypt_mix_word_t *V/*[chunkWords * N]*/, uint32_t N, uint32_t r); 4 | #endif 5 | 6 | /* romix pre/post nop function */ 7 | static void STDCALL 8 | scrypt_romix_nop(scrypt_mix_word_t *blocks, size_t nblocks) { 9 | } 10 | 11 | /* romix pre/post endian conversion function */ 12 | static void STDCALL 13 | scrypt_romix_convert_endian(scrypt_mix_word_t *blocks, size_t nblocks) { 14 | #if !defined(CPU_LE) 15 | static const union { uint8_t b[2]; uint16_t w; } endian_test = {{1,0}}; 16 | size_t i; 17 | if (endian_test.w == 0x100) { 18 | nblocks *= SCRYPT_BLOCK_WORDS; 19 | for (i = 0; i < nblocks; i++) { 20 | SCRYPT_WORD_ENDIAN_SWAP(blocks[i]); 21 | } 22 | } 23 | #endif 24 | } 25 | 26 | /* chunkmix test function */ 27 | typedef void (STDCALL *chunkmixfn)(scrypt_mix_word_t *Bout/*[chunkWords]*/, scrypt_mix_word_t *Bin/*[chunkWords]*/, scrypt_mix_word_t *Bxor/*[chunkWords]*/, uint32_t r); 28 | typedef void (STDCALL *blockfixfn)(scrypt_mix_word_t *blocks, size_t nblocks); 29 | 30 | static int 31 | scrypt_test_mix_instance(chunkmixfn mixfn, blockfixfn prefn, blockfixfn postfn, const uint8_t expected[16]) { 32 | /* r = 2, (2 * r) = 4 blocks in a chunk, 4 * SCRYPT_BLOCK_WORDS total */ 33 | const uint32_t r = 2, blocks = 2 * r, words = blocks * SCRYPT_BLOCK_WORDS; 34 | scrypt_mix_word_t MM16 chunk[2][4 * SCRYPT_BLOCK_WORDS], v; 35 | uint8_t final[16]; 36 | size_t i; 37 | 38 | for (i = 0; i < words; i++) { 39 | v = (scrypt_mix_word_t)i; 40 | v = (v << 8) | v; 41 | v = (v << 16) | v; 42 | chunk[0][i] = v; 43 | } 44 | 45 | prefn(chunk[0], blocks); 46 | mixfn(chunk[1], chunk[0], NULL, r); 47 | postfn(chunk[1], blocks); 48 | 49 | /* grab the last 16 bytes of the final block */ 50 | for (i = 0; i < 16; i += sizeof(scrypt_mix_word_t)) { 51 | SCRYPT_WORDTO8_LE(final + i, chunk[1][words - (16 / sizeof(scrypt_mix_word_t)) + (i / sizeof(scrypt_mix_word_t))]); 52 | } 53 | 54 | return scrypt_verify(expected, final, 16); 55 | } 56 | 57 | /* returns a pointer to item i, where item is len scrypt_mix_word_t's long */ 58 | static scrypt_mix_word_t * 59 | scrypt_item(scrypt_mix_word_t *base, scrypt_mix_word_t i, scrypt_mix_word_t len) { 60 | return base + (i * len); 61 | } 62 | 63 | /* returns a pointer to block i */ 64 | static scrypt_mix_word_t * 65 | scrypt_block(scrypt_mix_word_t *base, scrypt_mix_word_t i) { 66 | return base + (i * SCRYPT_BLOCK_WORDS); 67 | } 68 | -------------------------------------------------------------------------------- /sha3/sph_hefty1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * HEFTY1 cryptographic hash function 3 | * 4 | * Copyright (c) 2014, dbcc14 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * 1. Redistributions of source code must retain the above copyright notice, this 11 | * list of conditions and the following disclaimer. 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, 13 | * this list of conditions and the following disclaimer in the documentation 14 | * and/or other materials provided with the distribution. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * The views and conclusions contained in the software and documentation are those 28 | * of the authors and should not be interpreted as representing official policies, 29 | * either expressed or implied, of the FreeBSD Project. 30 | */ 31 | 32 | #ifndef __HEFTY1_H__ 33 | #define __HEFTY1_H__ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | #ifndef WIN32 40 | #include 41 | #endif 42 | 43 | #include 44 | 45 | #define HEFTY1_DIGEST_BYTES 32 46 | #define HEFTY1_BLOCK_BYTES 64 47 | #define HEFTY1_STATE_WORDS 8 48 | #define HEFTY1_SPONGE_WORDS 4 49 | 50 | typedef struct HEFTY1_CTX { 51 | uint32_t h[HEFTY1_STATE_WORDS]; 52 | uint8_t block[HEFTY1_BLOCK_BYTES]; 53 | uint64_t written; 54 | uint32_t sponge[HEFTY1_SPONGE_WORDS]; 55 | } HEFTY1_CTX; 56 | 57 | void HEFTY1_Init(HEFTY1_CTX *cxt); 58 | void HEFTY1_Update(HEFTY1_CTX *cxt, const void *data, size_t len); 59 | void HEFTY1_Final(unsigned char *digest, HEFTY1_CTX *cxt); 60 | unsigned char* HEFTY1(const unsigned char *data, size_t len, unsigned char *digest); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* __HEFTY1_H__ */ -------------------------------------------------------------------------------- /yescrypt/sha256_Y.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 2005,2007,2009 Colin Percival 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | * 26 | * $FreeBSD: src/lib/libmd/sha256_Y.h,v 1.2 2006/01/17 15:35:56 phk Exp $ 27 | */ 28 | 29 | #ifndef _SHA256_H_ 30 | #define _SHA256_H_ 31 | 32 | #include 33 | 34 | #include 35 | 36 | typedef struct SHA256Context { 37 | uint32_t state[8]; 38 | uint32_t count[2]; 39 | unsigned char buf[64]; 40 | } SHA256_CTX_Y; 41 | 42 | typedef struct HMAC_SHA256Context { 43 | SHA256_CTX_Y ictx; 44 | SHA256_CTX_Y octx; 45 | } HMAC_SHA256_CTX_Y; 46 | 47 | void SHA256_Init_Y(SHA256_CTX_Y *); 48 | void SHA256_Update_Y(SHA256_CTX_Y *, const void *, size_t); 49 | void SHA256_Final_Y(unsigned char [32], SHA256_CTX_Y *); 50 | void HMAC_SHA256_Init_Y(HMAC_SHA256_CTX_Y *, const void *, size_t); 51 | void HMAC_SHA256_Update_Y(HMAC_SHA256_CTX_Y *, const void *, size_t); 52 | void HMAC_SHA256_Final_Y(unsigned char [32], HMAC_SHA256_CTX_Y *); 53 | 54 | /** 55 | * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): 56 | * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and 57 | * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). 58 | */ 59 | void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t, 60 | uint64_t, uint8_t *, size_t); 61 | 62 | #endif /* !_SHA256_H_ */ 63 | -------------------------------------------------------------------------------- /c11.c: -------------------------------------------------------------------------------- 1 | #include "c11.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_bmw.h" 9 | #include "sha3/sph_groestl.h" 10 | #include "sha3/sph_jh.h" 11 | #include "sha3/sph_keccak.h" 12 | #include "sha3/sph_skein.h" 13 | #include "sha3/sph_luffa.h" 14 | #include "sha3/sph_cubehash.h" 15 | #include "sha3/sph_shavite.h" 16 | #include "sha3/sph_simd.h" 17 | #include "sha3/sph_echo.h" 18 | 19 | void c11_hash(const char* input, char* output) 20 | { 21 | sph_blake512_context ctx_blake; 22 | sph_bmw512_context ctx_bmw; 23 | sph_groestl512_context ctx_groestl; 24 | sph_skein512_context ctx_skein; 25 | sph_jh512_context ctx_jh; 26 | sph_keccak512_context ctx_keccak; 27 | sph_luffa512_context ctx_luffa1; 28 | sph_cubehash512_context ctx_cubehash1; 29 | sph_shavite512_context ctx_shavite1; 30 | sph_simd512_context ctx_simd1; 31 | sph_echo512_context ctx_echo1; 32 | 33 | uint32_t hashA[16], hashB[16]; 34 | 35 | //blake-bmw-groestl-sken-jh-meccak-luffa-cubehash-shivite-simd-echo 36 | 37 | sph_blake512_init(&ctx_blake); 38 | sph_blake512 (&ctx_blake, input, 80); 39 | sph_blake512_close (&ctx_blake, hashA); 40 | 41 | sph_bmw512_init(&ctx_bmw); 42 | sph_bmw512 (&ctx_bmw, hashA, 64); 43 | sph_bmw512_close(&ctx_bmw, hashB); 44 | 45 | sph_groestl512_init(&ctx_groestl); 46 | sph_groestl512 (&ctx_groestl, hashB, 64); 47 | sph_groestl512_close(&ctx_groestl, hashA); 48 | 49 | sph_jh512_init(&ctx_jh); 50 | sph_jh512 (&ctx_jh, hashA, 64); 51 | sph_jh512_close(&ctx_jh, hashB); 52 | 53 | sph_keccak512_init(&ctx_keccak); 54 | sph_keccak512 (&ctx_keccak, hashB, 64); 55 | sph_keccak512_close(&ctx_keccak, hashA); 56 | 57 | sph_skein512_init(&ctx_skein); 58 | sph_skein512 (&ctx_skein, hashA, 64); 59 | sph_skein512_close(&ctx_skein, hashB); 60 | 61 | sph_luffa512_init (&ctx_luffa1); 62 | sph_luffa512 (&ctx_luffa1, hashB, 64); 63 | sph_luffa512_close (&ctx_luffa1, hashA); 64 | 65 | sph_cubehash512_init (&ctx_cubehash1); 66 | sph_cubehash512 (&ctx_cubehash1, hashA, 64); 67 | sph_cubehash512_close(&ctx_cubehash1, hashB); 68 | 69 | sph_shavite512_init (&ctx_shavite1); 70 | sph_shavite512 (&ctx_shavite1, hashB, 64); 71 | sph_shavite512_close(&ctx_shavite1, hashA); 72 | 73 | sph_simd512_init (&ctx_simd1); 74 | sph_simd512 (&ctx_simd1, hashA, 64); 75 | sph_simd512_close(&ctx_simd1, hashB); 76 | 77 | sph_echo512_init (&ctx_echo1); 78 | sph_echo512 (&ctx_echo1, hashB, 64); 79 | sph_echo512_close(&ctx_echo1, hashA); 80 | 81 | memcpy(output, hashA, 32); 82 | 83 | } 84 | -------------------------------------------------------------------------------- /x11.c: -------------------------------------------------------------------------------- 1 | #include "x11.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_bmw.h" 9 | #include "sha3/sph_groestl.h" 10 | #include "sha3/sph_jh.h" 11 | #include "sha3/sph_keccak.h" 12 | #include "sha3/sph_skein.h" 13 | #include "sha3/sph_luffa.h" 14 | #include "sha3/sph_cubehash.h" 15 | #include "sha3/sph_shavite.h" 16 | #include "sha3/sph_simd.h" 17 | #include "sha3/sph_echo.h" 18 | 19 | 20 | void x11_hash(const char* input, char* output, uint32_t len) 21 | { 22 | sph_blake512_context ctx_blake; 23 | sph_bmw512_context ctx_bmw; 24 | sph_groestl512_context ctx_groestl; 25 | sph_skein512_context ctx_skein; 26 | sph_jh512_context ctx_jh; 27 | sph_keccak512_context ctx_keccak; 28 | 29 | sph_luffa512_context ctx_luffa1; 30 | sph_cubehash512_context ctx_cubehash1; 31 | sph_shavite512_context ctx_shavite1; 32 | sph_simd512_context ctx_simd1; 33 | sph_echo512_context ctx_echo1; 34 | 35 | //these uint512 in the c++ source of the client are backed by an array of uint32 36 | uint32_t hashA[16], hashB[16]; 37 | 38 | sph_blake512_init(&ctx_blake); 39 | sph_blake512 (&ctx_blake, input, len); 40 | sph_blake512_close (&ctx_blake, hashA); 41 | 42 | sph_bmw512_init(&ctx_bmw); 43 | sph_bmw512 (&ctx_bmw, hashA, 64); 44 | sph_bmw512_close(&ctx_bmw, hashB); 45 | 46 | sph_groestl512_init(&ctx_groestl); 47 | sph_groestl512 (&ctx_groestl, hashB, 64); 48 | sph_groestl512_close(&ctx_groestl, hashA); 49 | 50 | sph_skein512_init(&ctx_skein); 51 | sph_skein512 (&ctx_skein, hashA, 64); 52 | sph_skein512_close (&ctx_skein, hashB); 53 | 54 | sph_jh512_init(&ctx_jh); 55 | sph_jh512 (&ctx_jh, hashB, 64); 56 | sph_jh512_close(&ctx_jh, hashA); 57 | 58 | sph_keccak512_init(&ctx_keccak); 59 | sph_keccak512 (&ctx_keccak, hashA, 64); 60 | sph_keccak512_close(&ctx_keccak, hashB); 61 | 62 | sph_luffa512_init (&ctx_luffa1); 63 | sph_luffa512 (&ctx_luffa1, hashB, 64); 64 | sph_luffa512_close (&ctx_luffa1, hashA); 65 | 66 | sph_cubehash512_init (&ctx_cubehash1); 67 | sph_cubehash512 (&ctx_cubehash1, hashA, 64); 68 | sph_cubehash512_close(&ctx_cubehash1, hashB); 69 | 70 | sph_shavite512_init (&ctx_shavite1); 71 | sph_shavite512 (&ctx_shavite1, hashB, 64); 72 | sph_shavite512_close(&ctx_shavite1, hashA); 73 | 74 | sph_simd512_init (&ctx_simd1); 75 | sph_simd512 (&ctx_simd1, hashA, 64); 76 | sph_simd512_close(&ctx_simd1, hashB); 77 | 78 | sph_echo512_init (&ctx_echo1); 79 | sph_echo512 (&ctx_echo1, hashB, 64); 80 | sph_echo512_close(&ctx_echo1, hashA); 81 | 82 | memcpy(output, hashA, 32); 83 | 84 | } 85 | 86 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "multihashing", 5 | "sources": [ 6 | "multihashing.cc", 7 | "scryptn.c", 8 | "yescrypt/sha256_Y.c", 9 | "yescrypt/yescrypt-best.c", 10 | "yescrypt/yescryptcommon.c", 11 | "keccak.c", 12 | "skein.c", 13 | "x11.c", 14 | "quark.c", 15 | "bcrypt.c", 16 | "groestl.c", 17 | "blake.c", 18 | "fugue.c", 19 | "qubit.c", 20 | "hefty1.c", 21 | "shavite3.c", 22 | "cryptonight.c", 23 | "x13.c", 24 | "x14.c", 25 | "boolberry.cc", 26 | "nist5.c", 27 | "sha1.c", 28 | "x15.c", 29 | "fresh.c", 30 | "s3.c", 31 | "neoscrypt.c", 32 | "dcrypt.c", 33 | "jh.c", 34 | "c11.c", 35 | "timetravel.c", 36 | "sha3/sph_hefty1.c", 37 | "sha3/sph_fugue.c", 38 | "sha3/aes_helper.c", 39 | "sha3/sph_blake.c", 40 | "sha3/sph_bmw.c", 41 | "sha3/sph_cubehash.c", 42 | "sha3/sph_echo.c", 43 | "sha3/sph_groestl.c", 44 | "sha3/sph_jh.c", 45 | "sha3/sph_keccak.c", 46 | "sha3/sph_luffa.c", 47 | "sha3/sph_shavite.c", 48 | "sha3/sph_simd.c", 49 | "sha3/sph_skein.c", 50 | "sha3/sph_whirlpool.c", 51 | "sha3/sph_shabal.c", 52 | "sha3/hamsi.c", 53 | "crypto/oaes_lib.c", 54 | "crypto/c_keccak.c", 55 | "crypto/c_groestl.c", 56 | "crypto/c_blake256.c", 57 | "crypto/c_jh.c", 58 | "crypto/c_skein.c", 59 | "crypto/hash.c", 60 | "crypto/aesb.c", 61 | "crypto/wild_keccak.cpp", 62 | ], 63 | "include_dirs": [ 64 | "crypto", 65 | ], 66 | "cflags": [ 67 | "-D_GNU_SOURCE -maes -fPIC -Ofast -flto -fuse-linker-plugin -funroll-loops -funswitch-loops -fpeel-loops" 68 | ], 69 | "cflags!": [ 70 | "-O2", "-fno-strict-aliasing", "-fno-tree-vrp", "-fno-omit-frame-pointer" 71 | ], 72 | "ldflags": [ 73 | "-fPIC -Ofast -flto -fuse-linker-plugin" 74 | ], 75 | "cflags_cc": [ 76 | "-std=c++0x -maes -march=native" 77 | ] 78 | } 79 | ] 80 | } 81 | -------------------------------------------------------------------------------- /crypto/cryptonote_core/cryptonote_basic_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #pragma once 6 | 7 | #include "cryptonote_basic.h" 8 | #include "crypto/crypto.h" 9 | #include "crypto/hash.h" 10 | 11 | 12 | namespace cryptonote { 13 | /************************************************************************/ 14 | /* */ 15 | /************************************************************************/ 16 | template 17 | struct array_hasher: std::unary_function 18 | { 19 | std::size_t operator()(const t_array& val) const 20 | { 21 | return boost::hash_range(&val.data[0], &val.data[sizeof(val.data)]); 22 | } 23 | }; 24 | 25 | 26 | #pragma pack(push, 1) 27 | struct public_address_outer_blob 28 | { 29 | uint8_t m_ver; 30 | account_public_address m_address; 31 | uint8_t check_sum; 32 | }; 33 | #pragma pack (pop) 34 | 35 | 36 | /************************************************************************/ 37 | /* Cryptonote helper functions */ 38 | /************************************************************************/ 39 | size_t get_max_block_size(); 40 | size_t get_max_tx_size(); 41 | bool get_block_reward(size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward); 42 | uint8_t get_account_address_checksum(const public_address_outer_blob& bl); 43 | std::string get_account_address_as_str(const account_public_address& adr); 44 | bool get_account_address_from_str(account_public_address& adr, const std::string& str); 45 | bool is_coinbase(const transaction& tx); 46 | 47 | bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b); 48 | bool operator ==(const cryptonote::block& a, const cryptonote::block& b); 49 | } 50 | 51 | template 52 | std::ostream &print256(std::ostream &o, const T &v) { 53 | return o << "<" << epee::string_tools::pod_to_hex(v) << ">"; 54 | } 55 | 56 | bool parse_hash256(const std::string str_hash, crypto::hash& hash); 57 | 58 | namespace crypto { 59 | inline std::ostream &operator <<(std::ostream &o, const crypto::public_key &v) { return print256(o, v); } 60 | inline std::ostream &operator <<(std::ostream &o, const crypto::secret_key &v) { return print256(o, v); } 61 | inline std::ostream &operator <<(std::ostream &o, const crypto::key_derivation &v) { return print256(o, v); } 62 | inline std::ostream &operator <<(std::ostream &o, const crypto::key_image &v) { return print256(o, v); } 63 | inline std::ostream &operator <<(std::ostream &o, const crypto::signature &v) { return print256(o, v); } 64 | inline std::ostream &operator <<(std::ostream &o, const crypto::hash &v) { return print256(o, v); } 65 | } 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-multi-hashing 2 | =============== 3 | 4 | [![Build Status](https://travis-ci.org/zone117x/node-multi-hashing.png?branch=master)](https://travis-ci.org/zone117x/node-multi-hashing) 5 | 6 | [![NPM](https://nodei.co/npm/multi-hashing.png?downloads=true&stars=true)](https://nodei.co/npm/multi-hashing/) 7 | 8 | Cryptocurrency hashing functions for node.js. 9 | 10 | 11 | Algorithms 12 | ---------- 13 | * quark 14 | * x11 15 | * x13 16 | * nist5 17 | * scrypt 18 | * scryptn 19 | * scryptjane 20 | * keccak 21 | * bcrypt 22 | * skein 23 | * groestl 24 | * blake 25 | * fugue 26 | * qubit 27 | * hefty1 28 | * shavite3 29 | * cryptonight 30 | * boolberry 31 | 32 | Usage 33 | ----- 34 | 35 | Install 36 | 37 | ```bash 38 | npm install multi-hashing 39 | ``` 40 | 41 | So far this native Node.js addon can do the following hashing algos 42 | 43 | ```javascript 44 | var multiHashing = require('multi-hashing'); 45 | 46 | var algorithms = ['quark', 'x11', 'scrypt', 'scryptn', 'scryptjane', 'keccak', 'bcrypt', 'skein', 'blake']; 47 | 48 | var data = new Buffer("7000000001e980924e4e1109230383e66d62945ff8e749903bea4336755c00000000000051928aff1b4d72416173a8c3948159a09a73ac3bb556aa6bfbcad1a85da7f4c1d13350531e24031b939b9e2b", "hex"); 49 | 50 | var hashedData = algorithms.map(function(algo){ 51 | if (algo === 'scryptjane'){ 52 | //scryptjane needs block.nTime and nChainStartTime (found in coin source) 53 | var yaCoinChainStartTime = 1367991200; 54 | var nTime = Math.round(Date.now() / 1000); 55 | return multiHashing[algo](data, nTime, yaCoinChainStartTime); 56 | } 57 | else{ 58 | return multiHashing[algo](data); 59 | } 60 | }); 61 | 62 | 63 | console.log(hashedData); 64 | // 65 | 66 | 67 | ``` 68 | 69 | Credits 70 | ------- 71 | * [NSA](http://www.nsa.gov/) and [NIST](http://www.nist.gov/) for creation or sponsoring creation of SHA2 and SHA3 algos 72 | * [Keccak](http://en.wikipedia.org/wiki/Keccak) - Guido Bertoni, Joan Daemen, Michaël Peeters, and Gilles Van Assche 73 | * [Skein](http://en.wikipedia.org/wiki/Skein_(hash_function)) - Bruce Schneier, Stefan Lucks, Niels Ferguson, Doug Whiting, Mihir Bellare, Tadayoshi Kohno, Jon Callas and Jesse Walker. 74 | * [BLAKE](http://en.wikipedia.org/wiki/BLAKE_(hash_function)) - Jean-Philippe Aumasson, Luca Henzen, Willi Meier, and Raphael C.-W. Phan 75 | * [Grøstl](http://en.wikipedia.org/wiki/Gr%C3%B8stl) - Praveen Gauravaram, Lars Knudsen, Krystian Matusiewicz, Florian Mendel, Christian Rechberger, Martin Schläffer, and Søren S. Thomsen 76 | * [JH](http://en.wikipedia.org/wiki/JH_(hash_function)) - Hongjun Wu 77 | * [Fugue](http://en.wikipedia.org/wiki/Fugue_(hash_function)) - Shai Halevi, William E. Hall, and Charanjit S. Jutla 78 | * [scrypt](http://en.wikipedia.org/wiki/Scrypt) - Colin Percival 79 | * [bcrypt](http://en.wikipedia.org/wiki/Bcrypt) - Niels Provos and David Mazières 80 | * [X11](http://www.darkcoin.io/), [Hefty1](http://heavycoin.github.io/about.html), [Quark](http://www.qrk.cc/) creators (they just mixed together a bunch of the above algos) 81 | -------------------------------------------------------------------------------- /x13.c: -------------------------------------------------------------------------------- 1 | #include "x13.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_bmw.h" 9 | #include "sha3/sph_groestl.h" 10 | #include "sha3/sph_jh.h" 11 | #include "sha3/sph_keccak.h" 12 | #include "sha3/sph_skein.h" 13 | #include "sha3/sph_luffa.h" 14 | #include "sha3/sph_cubehash.h" 15 | #include "sha3/sph_shavite.h" 16 | #include "sha3/sph_simd.h" 17 | #include "sha3/sph_echo.h" 18 | #include "sha3/sph_hamsi.h" 19 | #include "sha3/sph_fugue.h" 20 | 21 | 22 | void x13_hash(const char* input, char* output, uint32_t len) 23 | { 24 | sph_blake512_context ctx_blake; 25 | sph_bmw512_context ctx_bmw; 26 | sph_groestl512_context ctx_groestl; 27 | sph_skein512_context ctx_skein; 28 | sph_jh512_context ctx_jh; 29 | sph_keccak512_context ctx_keccak; 30 | sph_luffa512_context ctx_luffa1; 31 | sph_cubehash512_context ctx_cubehash1; 32 | sph_shavite512_context ctx_shavite1; 33 | sph_simd512_context ctx_simd1; 34 | sph_echo512_context ctx_echo1; 35 | sph_hamsi512_context ctx_hamsi1; 36 | sph_fugue512_context ctx_fugue1; 37 | 38 | //these uint512 in the c++ source of the client are backed by an array of uint32 39 | uint32_t hashA[16], hashB[16]; 40 | 41 | sph_blake512_init(&ctx_blake); 42 | sph_blake512 (&ctx_blake, input, len); 43 | sph_blake512_close (&ctx_blake, hashA); 44 | 45 | sph_bmw512_init(&ctx_bmw); 46 | sph_bmw512 (&ctx_bmw, hashA, 64); 47 | sph_bmw512_close(&ctx_bmw, hashB); 48 | 49 | sph_groestl512_init(&ctx_groestl); 50 | sph_groestl512 (&ctx_groestl, hashB, 64); 51 | sph_groestl512_close(&ctx_groestl, hashA); 52 | 53 | sph_skein512_init(&ctx_skein); 54 | sph_skein512 (&ctx_skein, hashA, 64); 55 | sph_skein512_close (&ctx_skein, hashB); 56 | 57 | sph_jh512_init(&ctx_jh); 58 | sph_jh512 (&ctx_jh, hashB, 64); 59 | sph_jh512_close(&ctx_jh, hashA); 60 | 61 | sph_keccak512_init(&ctx_keccak); 62 | sph_keccak512 (&ctx_keccak, hashA, 64); 63 | sph_keccak512_close(&ctx_keccak, hashB); 64 | 65 | sph_luffa512_init (&ctx_luffa1); 66 | sph_luffa512 (&ctx_luffa1, hashB, 64); 67 | sph_luffa512_close (&ctx_luffa1, hashA); 68 | 69 | sph_cubehash512_init (&ctx_cubehash1); 70 | sph_cubehash512 (&ctx_cubehash1, hashA, 64); 71 | sph_cubehash512_close(&ctx_cubehash1, hashB); 72 | 73 | sph_shavite512_init (&ctx_shavite1); 74 | sph_shavite512 (&ctx_shavite1, hashB, 64); 75 | sph_shavite512_close(&ctx_shavite1, hashA); 76 | 77 | sph_simd512_init (&ctx_simd1); 78 | sph_simd512 (&ctx_simd1, hashA, 64); 79 | sph_simd512_close(&ctx_simd1, hashB); 80 | 81 | sph_echo512_init (&ctx_echo1); 82 | sph_echo512 (&ctx_echo1, hashB, 64); 83 | sph_echo512_close(&ctx_echo1, hashA); 84 | 85 | sph_hamsi512_init (&ctx_hamsi1); 86 | sph_hamsi512 (&ctx_hamsi1, hashA, 64); 87 | sph_hamsi512_close(&ctx_hamsi1, hashB); 88 | 89 | sph_fugue512_init (&ctx_fugue1); 90 | sph_fugue512 (&ctx_fugue1, hashB, 64); 91 | sph_fugue512_close(&ctx_fugue1, hashA); 92 | 93 | 94 | 95 | memcpy(output, hashA, 32); 96 | 97 | } 98 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-salsa.h: -------------------------------------------------------------------------------- 1 | #define SCRYPT_MIX_BASE "Salsa20/8" 2 | 3 | typedef uint32_t scrypt_mix_word_t; 4 | 5 | #define SCRYPT_WORDTO8_LE U32TO8_LE 6 | #define SCRYPT_WORD_ENDIAN_SWAP U32_SWAP 7 | 8 | #define SCRYPT_BLOCK_BYTES 64 9 | #define SCRYPT_BLOCK_WORDS (SCRYPT_BLOCK_BYTES / sizeof(scrypt_mix_word_t)) 10 | 11 | /* must have these here in case block bytes is ever != 64 */ 12 | #include "scrypt-jane-romix-basic.h" 13 | 14 | #include "scrypt-jane-mix_salsa-avx.h" 15 | #include "scrypt-jane-mix_salsa-sse2.h" 16 | #include "scrypt-jane-mix_salsa.h" 17 | 18 | #if defined(SCRYPT_SALSA_AVX) 19 | #define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_avx 20 | #define SCRYPT_ROMIX_FN scrypt_ROMix_avx 21 | #define SCRYPT_ROMIX_TANGLE_FN salsa_core_tangle_sse2 22 | #define SCRYPT_ROMIX_UNTANGLE_FN salsa_core_tangle_sse2 23 | #include "scrypt-jane-romix-template.h" 24 | #endif 25 | 26 | #if defined(SCRYPT_SALSA_SSE2) 27 | #define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_sse2 28 | #define SCRYPT_ROMIX_FN scrypt_ROMix_sse2 29 | #define SCRYPT_MIX_FN salsa_core_sse2 30 | #define SCRYPT_ROMIX_TANGLE_FN salsa_core_tangle_sse2 31 | #define SCRYPT_ROMIX_UNTANGLE_FN salsa_core_tangle_sse2 32 | #include "scrypt-jane-romix-template.h" 33 | #endif 34 | 35 | /* cpu agnostic */ 36 | #define SCRYPT_ROMIX_FN scrypt_ROMix_basic 37 | #define SCRYPT_MIX_FN salsa_core_basic 38 | #define SCRYPT_ROMIX_TANGLE_FN scrypt_romix_convert_endian 39 | #define SCRYPT_ROMIX_UNTANGLE_FN scrypt_romix_convert_endian 40 | #include "scrypt-jane-romix-template.h" 41 | 42 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) 43 | static scrypt_ROMixfn 44 | scrypt_getROMix() { 45 | size_t cpuflags = detect_cpu(); 46 | 47 | #if defined(SCRYPT_SALSA_AVX) 48 | if (cpuflags & cpu_avx) 49 | return scrypt_ROMix_avx; 50 | else 51 | #endif 52 | 53 | #if defined(SCRYPT_SALSA_SSE2) 54 | if (cpuflags & cpu_sse2) 55 | return scrypt_ROMix_sse2; 56 | else 57 | #endif 58 | 59 | return scrypt_ROMix_basic; 60 | } 61 | #endif 62 | 63 | 64 | #if defined(SCRYPT_TEST_SPEED) 65 | static size_t 66 | available_implementations() { 67 | size_t flags = 0; 68 | 69 | #if defined(SCRYPT_SALSA_AVX) 70 | flags |= cpu_avx; 71 | #endif 72 | 73 | #if defined(SCRYPT_SALSA_SSE2) 74 | flags |= cpu_sse2; 75 | #endif 76 | 77 | return flags; 78 | } 79 | #endif 80 | 81 | 82 | static int 83 | scrypt_test_mix() { 84 | static const uint8_t expected[16] = { 85 | 0x41,0x1f,0x2e,0xa3,0xab,0xa3,0x1a,0x34,0x87,0x1d,0x8a,0x1c,0x76,0xa0,0x27,0x66, 86 | }; 87 | 88 | int ret = 1; 89 | size_t cpuflags = detect_cpu(); 90 | 91 | #if defined(SCRYPT_SALSA_AVX) 92 | if (cpuflags & cpu_avx) 93 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_avx, salsa_core_tangle_sse2, salsa_core_tangle_sse2, expected); 94 | #endif 95 | 96 | #if defined(SCRYPT_SALSA_SSE2) 97 | if (cpuflags & cpu_sse2) 98 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_sse2, salsa_core_tangle_sse2, salsa_core_tangle_sse2, expected); 99 | #endif 100 | 101 | #if defined(SCRYPT_SALSA_BASIC) 102 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_basic, scrypt_romix_convert_endian, scrypt_romix_convert_endian, expected); 103 | #endif 104 | 105 | return ret; 106 | } 107 | -------------------------------------------------------------------------------- /crypto/c_keccak.c: -------------------------------------------------------------------------------- 1 | // keccak.c 2 | // 19-Nov-11 Markku-Juhani O. Saarinen 3 | // A baseline Keccak (3rd round) implementation. 4 | 5 | #include "hash-ops.h" 6 | #include "c_keccak.h" 7 | 8 | const uint64_t keccakf_rndc[24] = 9 | { 10 | 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 11 | 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 12 | 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 13 | 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, 14 | 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 15 | 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 16 | 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 17 | 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 18 | }; 19 | 20 | const int keccakf_rotc[24] = 21 | { 22 | 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 23 | 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 24 | }; 25 | 26 | const int keccakf_piln[24] = 27 | { 28 | 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 29 | 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 30 | }; 31 | 32 | // update the state with given number of rounds 33 | 34 | void keccakf(uint64_t st[25], int rounds) 35 | { 36 | int i, j, round; 37 | uint64_t t, bc[5]; 38 | 39 | for (round = 0; round < rounds; round++) { 40 | 41 | // Theta 42 | for (i = 0; i < 5; i++) 43 | bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20]; 44 | 45 | for (i = 0; i < 5; i++) { 46 | t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1); 47 | for (j = 0; j < 25; j += 5) 48 | st[j + i] ^= t; 49 | } 50 | 51 | // Rho Pi 52 | t = st[1]; 53 | for (i = 0; i < 24; i++) { 54 | j = keccakf_piln[i]; 55 | bc[0] = st[j]; 56 | st[j] = ROTL64(t, keccakf_rotc[i]); 57 | t = bc[0]; 58 | } 59 | 60 | // Chi 61 | for (j = 0; j < 25; j += 5) { 62 | for (i = 0; i < 5; i++) 63 | bc[i] = st[j + i]; 64 | for (i = 0; i < 5; i++) 65 | st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; 66 | } 67 | 68 | // Iota 69 | st[0] ^= keccakf_rndc[round]; 70 | } 71 | } 72 | 73 | // compute a keccak hash (md) of given byte length from "in" 74 | typedef uint64_t state_t[25]; 75 | 76 | int keccak(const uint8_t *in, int inlen, uint8_t *md, int mdlen) 77 | { 78 | state_t st; 79 | uint8_t temp[144]; 80 | int i, rsiz, rsizw; 81 | 82 | rsiz = sizeof(state_t) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen; 83 | rsizw = rsiz / 8; 84 | 85 | memset(st, 0, sizeof(st)); 86 | 87 | for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) { 88 | for (i = 0; i < rsizw; i++) 89 | st[i] ^= ((uint64_t *) in)[i]; 90 | keccakf(st, KECCAK_ROUNDS); 91 | } 92 | 93 | // last block and padding 94 | memcpy(temp, in, inlen); 95 | temp[inlen++] = 1; 96 | memset(temp + inlen, 0, rsiz - inlen); 97 | temp[rsiz - 1] |= 0x80; 98 | 99 | for (i = 0; i < rsizw; i++) 100 | st[i] ^= ((uint64_t *) temp)[i]; 101 | 102 | keccakf(st, KECCAK_ROUNDS); 103 | 104 | memcpy(md, st, mdlen); 105 | 106 | return 0; 107 | } 108 | 109 | void keccak1600(const uint8_t *in, int inlen, uint8_t *md) 110 | { 111 | keccak(in, inlen, md, sizeof(state_t)); 112 | } 113 | -------------------------------------------------------------------------------- /x14.c: -------------------------------------------------------------------------------- 1 | #include "x14.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_bmw.h" 9 | #include "sha3/sph_groestl.h" 10 | #include "sha3/sph_jh.h" 11 | #include "sha3/sph_keccak.h" 12 | #include "sha3/sph_skein.h" 13 | #include "sha3/sph_luffa.h" 14 | #include "sha3/sph_cubehash.h" 15 | #include "sha3/sph_shavite.h" 16 | #include "sha3/sph_simd.h" 17 | #include "sha3/sph_echo.h" 18 | #include "sha3/sph_hamsi.h" 19 | #include "sha3/sph_fugue.h" 20 | #include "sha3/sph_shabal.h" 21 | 22 | void x14_hash(const char* input, char* output, uint32_t len) 23 | { 24 | sph_blake512_context ctx_blake; 25 | sph_bmw512_context ctx_bmw; 26 | sph_groestl512_context ctx_groestl; 27 | sph_skein512_context ctx_skein; 28 | sph_jh512_context ctx_jh; 29 | sph_keccak512_context ctx_keccak; 30 | sph_luffa512_context ctx_luffa1; 31 | sph_cubehash512_context ctx_cubehash1; 32 | sph_shavite512_context ctx_shavite1; 33 | sph_simd512_context ctx_simd1; 34 | sph_echo512_context ctx_echo1; 35 | sph_hamsi512_context ctx_hamsi1; 36 | sph_fugue512_context ctx_fugue1; 37 | sph_shabal512_context ctx_shabal1; 38 | 39 | //these uint512 in the c++ source of the client are backed by an array of uint32 40 | uint32_t hashA[16], hashB[16]; 41 | 42 | sph_blake512_init(&ctx_blake); 43 | sph_blake512 (&ctx_blake, input, 80); 44 | sph_blake512_close (&ctx_blake, hashA); 45 | 46 | sph_bmw512_init(&ctx_bmw); 47 | sph_bmw512 (&ctx_bmw, hashA, 64); 48 | sph_bmw512_close(&ctx_bmw, hashB); 49 | 50 | sph_groestl512_init(&ctx_groestl); 51 | sph_groestl512 (&ctx_groestl, hashB, 64); 52 | sph_groestl512_close(&ctx_groestl, hashA); 53 | 54 | sph_skein512_init(&ctx_skein); 55 | sph_skein512 (&ctx_skein, hashA, 64); 56 | sph_skein512_close (&ctx_skein, hashB); 57 | 58 | sph_jh512_init(&ctx_jh); 59 | sph_jh512 (&ctx_jh, hashB, 64); 60 | sph_jh512_close(&ctx_jh, hashA); 61 | 62 | sph_keccak512_init(&ctx_keccak); 63 | sph_keccak512 (&ctx_keccak, hashA, 64); 64 | sph_keccak512_close(&ctx_keccak, hashB); 65 | 66 | sph_luffa512_init (&ctx_luffa1); 67 | sph_luffa512 (&ctx_luffa1, hashB, 64); 68 | sph_luffa512_close (&ctx_luffa1, hashA); 69 | 70 | sph_cubehash512_init (&ctx_cubehash1); 71 | sph_cubehash512 (&ctx_cubehash1, hashA, 64); 72 | sph_cubehash512_close(&ctx_cubehash1, hashB); 73 | 74 | sph_shavite512_init (&ctx_shavite1); 75 | sph_shavite512 (&ctx_shavite1, hashB, 64); 76 | sph_shavite512_close(&ctx_shavite1, hashA); 77 | 78 | sph_simd512_init (&ctx_simd1); 79 | sph_simd512 (&ctx_simd1, hashA, 64); 80 | sph_simd512_close(&ctx_simd1, hashB); 81 | 82 | sph_echo512_init (&ctx_echo1); 83 | sph_echo512 (&ctx_echo1, hashB, 64); 84 | sph_echo512_close(&ctx_echo1, hashA); 85 | 86 | sph_hamsi512_init (&ctx_hamsi1); 87 | sph_hamsi512 (&ctx_hamsi1, hashA, 64); 88 | sph_hamsi512_close(&ctx_hamsi1, hashB); 89 | 90 | sph_fugue512_init (&ctx_fugue1); 91 | sph_fugue512 (&ctx_fugue1, hashB, 64); 92 | sph_fugue512_close(&ctx_fugue1, hashA); 93 | 94 | sph_shabal512_init (&ctx_shabal1); 95 | sph_shabal512 (&ctx_shabal1, hashA, 64); 96 | sph_shabal512_close(&ctx_shabal1, hashB); 97 | 98 | 99 | 100 | memcpy(output, hashB, 32); 101 | } 102 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-romix-template.h: -------------------------------------------------------------------------------- 1 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_HAVE_ROMIX) 2 | 3 | #if defined(SCRYPT_CHOOSE_COMPILETIME) 4 | #undef SCRYPT_ROMIX_FN 5 | #define SCRYPT_ROMIX_FN scrypt_ROMix 6 | #endif 7 | 8 | #undef SCRYPT_HAVE_ROMIX 9 | #define SCRYPT_HAVE_ROMIX 10 | 11 | #if !defined(SCRYPT_CHUNKMIX_FN) 12 | 13 | #define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_basic 14 | 15 | /* 16 | Bout = ChunkMix(Bin) 17 | 18 | 2*r: number of blocks in the chunk 19 | */ 20 | static void STDCALL 21 | SCRYPT_CHUNKMIX_FN(scrypt_mix_word_t *Bout/*[chunkWords]*/, scrypt_mix_word_t *Bin/*[chunkWords]*/, scrypt_mix_word_t *Bxor/*[chunkWords]*/, uint32_t r) { 22 | scrypt_mix_word_t MM16 X[SCRYPT_BLOCK_WORDS], *block; 23 | uint32_t i, j, blocksPerChunk = r * 2, half = 0; 24 | 25 | /* 1: X = B_{2r - 1} */ 26 | block = scrypt_block(Bin, blocksPerChunk - 1); 27 | for (i = 0; i < SCRYPT_BLOCK_WORDS; i++) 28 | X[i] = block[i]; 29 | 30 | if (Bxor) { 31 | block = scrypt_block(Bxor, blocksPerChunk - 1); 32 | for (i = 0; i < SCRYPT_BLOCK_WORDS; i++) 33 | X[i] ^= block[i]; 34 | } 35 | 36 | /* 2: for i = 0 to 2r - 1 do */ 37 | for (i = 0; i < blocksPerChunk; i++, half ^= r) { 38 | /* 3: X = H(X ^ B_i) */ 39 | block = scrypt_block(Bin, i); 40 | for (j = 0; j < SCRYPT_BLOCK_WORDS; j++) 41 | X[j] ^= block[j]; 42 | 43 | if (Bxor) { 44 | block = scrypt_block(Bxor, i); 45 | for (j = 0; j < SCRYPT_BLOCK_WORDS; j++) 46 | X[j] ^= block[j]; 47 | } 48 | SCRYPT_MIX_FN(X); 49 | 50 | /* 4: Y_i = X */ 51 | /* 6: B'[0..r-1] = Y_even */ 52 | /* 6: B'[r..2r-1] = Y_odd */ 53 | block = scrypt_block(Bout, (i / 2) + half); 54 | for (j = 0; j < SCRYPT_BLOCK_WORDS; j++) 55 | block[j] = X[j]; 56 | } 57 | } 58 | #endif 59 | 60 | /* 61 | X = ROMix(X) 62 | 63 | X: chunk to mix 64 | Y: scratch chunk 65 | N: number of rounds 66 | V[N]: array of chunks to randomly index in to 67 | 2*r: number of blocks in a chunk 68 | */ 69 | 70 | static void NOINLINE FASTCALL 71 | SCRYPT_ROMIX_FN(scrypt_mix_word_t *X/*[chunkWords]*/, scrypt_mix_word_t *Y/*[chunkWords]*/, scrypt_mix_word_t *V/*[N * chunkWords]*/, uint32_t N, uint32_t r) { 72 | uint32_t i, j, chunkWords = SCRYPT_BLOCK_WORDS * r * 2; 73 | scrypt_mix_word_t *block = V; 74 | 75 | SCRYPT_ROMIX_TANGLE_FN(X, r * 2); 76 | 77 | /* 1: X = B */ 78 | /* implicit */ 79 | 80 | /* 2: for i = 0 to N - 1 do */ 81 | memcpy(block, X, chunkWords * sizeof(scrypt_mix_word_t)); 82 | for (i = 0; i < N - 1; i++, block += chunkWords) { 83 | /* 3: V_i = X */ 84 | /* 4: X = H(X) */ 85 | SCRYPT_CHUNKMIX_FN(block + chunkWords, block, NULL, r); 86 | } 87 | SCRYPT_CHUNKMIX_FN(X, block, NULL, r); 88 | 89 | /* 6: for i = 0 to N - 1 do */ 90 | for (i = 0; i < N; i += 2) { 91 | /* 7: j = Integerify(X) % N */ 92 | j = X[chunkWords - SCRYPT_BLOCK_WORDS] & (N - 1); 93 | 94 | /* 8: X = H(Y ^ V_j) */ 95 | SCRYPT_CHUNKMIX_FN(Y, X, scrypt_item(V, j, chunkWords), r); 96 | 97 | /* 7: j = Integerify(Y) % N */ 98 | j = Y[chunkWords - SCRYPT_BLOCK_WORDS] & (N - 1); 99 | 100 | /* 8: X = H(Y ^ V_j) */ 101 | SCRYPT_CHUNKMIX_FN(X, Y, scrypt_item(V, j, chunkWords), r); 102 | } 103 | 104 | /* 10: B' = X */ 105 | /* implicit */ 106 | 107 | SCRYPT_ROMIX_UNTANGLE_FN(X, r * 2); 108 | } 109 | 110 | #endif /* !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_HAVE_ROMIX) */ 111 | 112 | 113 | #undef SCRYPT_CHUNKMIX_FN 114 | #undef SCRYPT_ROMIX_FN 115 | #undef SCRYPT_MIX_FN 116 | #undef SCRYPT_ROMIX_TANGLE_FN 117 | #undef SCRYPT_ROMIX_UNTANGLE_FN 118 | 119 | -------------------------------------------------------------------------------- /crypto/wild_keccak.cpp: -------------------------------------------------------------------------------- 1 | // keccak.c 2 | // 19-Nov-11 Markku-Juhani O. Saarinen 3 | // A baseline Keccak (3rd round) implementation. 4 | 5 | // Memory-hard extension of keccak for PoW 6 | // Copyright (c) 2014 The Boolberry developers 7 | // Distributed under the MIT/X11 software license, see the accompanying 8 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 9 | 10 | 11 | #include "wild_keccak.h" 12 | namespace crypto 13 | { 14 | 15 | const uint64_t keccakf_rndc[24] = 16 | { 17 | 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 18 | 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 19 | 0x8000000080008081, 0x8000000000008009, 0x000000000000008a, 20 | 0x0000000000000088, 0x0000000080008009, 0x000000008000000a, 21 | 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 22 | 0x8000000000008003, 0x8000000000008002, 0x8000000000000080, 23 | 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 24 | 0x8000000000008080, 0x0000000080000001, 0x8000000080008008 25 | }; 26 | 27 | const int keccakf_rotc[24] = 28 | { 29 | 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 30 | 27, 41, 56, 8, 25, 43, 62, 18, 39, 61, 20, 44 31 | }; 32 | 33 | const int keccakf_piln[24] = 34 | { 35 | 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 36 | 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1 37 | }; 38 | 39 | // update the state with given number of rounds 40 | void regular_f::keccakf(uint64_t st[25], int rounds) 41 | { 42 | int i, j, round; 43 | uint64_t t, bc[5]; 44 | 45 | for (round = 0; round < rounds; round++) { 46 | 47 | // Theta 48 | for (i = 0; i < 5; i++) 49 | bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] ^ st[i + 15] ^ st[i + 20]; 50 | 51 | for (i = 0; i < 5; i++) { 52 | t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1); 53 | for (j = 0; j < 25; j += 5) 54 | st[j + i] ^= t; 55 | } 56 | 57 | // Rho Pi 58 | t = st[1]; 59 | for (i = 0; i < 24; i++) { 60 | j = keccakf_piln[i]; 61 | bc[0] = st[j]; 62 | st[j] = ROTL64(t, keccakf_rotc[i]); 63 | t = bc[0]; 64 | } 65 | 66 | // Chi 67 | for (j = 0; j < 25; j += 5) { 68 | for (i = 0; i < 5; i++) 69 | bc[i] = st[j + i]; 70 | for (i = 0; i < 5; i++) 71 | st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; 72 | } 73 | 74 | // Iota 75 | st[0] ^= keccakf_rndc[round]; 76 | } 77 | } 78 | 79 | void mul_f::keccakf(uint64_t st[25], int rounds) 80 | { 81 | int i, j, round; 82 | uint64_t t, bc[5]; 83 | 84 | for (round = 0; round < rounds; round++) { 85 | 86 | // Theta 87 | for (i = 0; i < 5; i++) 88 | { 89 | bc[i] = st[i] ^ st[i + 5] ^ st[i + 10] * st[i + 15] * st[i + 20];//surprise 90 | } 91 | 92 | for (i = 0; i < 5; i++) { 93 | t = bc[(i + 4) % 5] ^ ROTL64(bc[(i + 1) % 5], 1); 94 | for (j = 0; j < 25; j += 5) 95 | st[j + i] ^= t; 96 | } 97 | 98 | // Rho Pi 99 | t = st[1]; 100 | for (i = 0; i < 24; i++) { 101 | j = keccakf_piln[i]; 102 | bc[0] = st[j]; 103 | st[j] = ROTL64(t, keccakf_rotc[i]); 104 | t = bc[0]; 105 | } 106 | 107 | // Chi 108 | for (j = 0; j < 25; j += 5) { 109 | for (i = 0; i < 5; i++) 110 | bc[i] = st[j + i]; 111 | for (i = 0; i < 5; i++) 112 | st[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; 113 | } 114 | 115 | // Iota 116 | st[0] ^= keccakf_rndc[round]; 117 | } 118 | } 119 | } -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-pbkdf2.h: -------------------------------------------------------------------------------- 1 | typedef struct scrypt_hmac_state_t { 2 | scrypt_hash_state inner, outer; 3 | } scrypt_hmac_state; 4 | 5 | 6 | static void 7 | scrypt_hash(scrypt_hash_digest hash, const uint8_t *m, size_t mlen) { 8 | scrypt_hash_state st; 9 | scrypt_hash_init(&st); 10 | scrypt_hash_update(&st, m, mlen); 11 | scrypt_hash_finish(&st, hash); 12 | } 13 | 14 | /* hmac */ 15 | static void 16 | scrypt_hmac_init(scrypt_hmac_state *st, const uint8_t *key, size_t keylen) { 17 | uint8_t pad[SCRYPT_HASH_BLOCK_SIZE] = {0}; 18 | size_t i; 19 | 20 | scrypt_hash_init(&st->inner); 21 | scrypt_hash_init(&st->outer); 22 | 23 | if (keylen <= SCRYPT_HASH_BLOCK_SIZE) { 24 | /* use the key directly if it's <= blocksize bytes */ 25 | memcpy(pad, key, keylen); 26 | } else { 27 | /* if it's > blocksize bytes, hash it */ 28 | scrypt_hash(pad, key, keylen); 29 | } 30 | 31 | /* inner = (key ^ 0x36) */ 32 | /* h(inner || ...) */ 33 | for (i = 0; i < SCRYPT_HASH_BLOCK_SIZE; i++) 34 | pad[i] ^= 0x36; 35 | scrypt_hash_update(&st->inner, pad, SCRYPT_HASH_BLOCK_SIZE); 36 | 37 | /* outer = (key ^ 0x5c) */ 38 | /* h(outer || ...) */ 39 | for (i = 0; i < SCRYPT_HASH_BLOCK_SIZE; i++) 40 | pad[i] ^= (0x5c ^ 0x36); 41 | scrypt_hash_update(&st->outer, pad, SCRYPT_HASH_BLOCK_SIZE); 42 | 43 | scrypt_ensure_zero(pad, sizeof(pad)); 44 | } 45 | 46 | static void 47 | scrypt_hmac_update(scrypt_hmac_state *st, const uint8_t *m, size_t mlen) { 48 | /* h(inner || m...) */ 49 | scrypt_hash_update(&st->inner, m, mlen); 50 | } 51 | 52 | static void 53 | scrypt_hmac_finish(scrypt_hmac_state *st, scrypt_hash_digest mac) { 54 | /* h(inner || m) */ 55 | scrypt_hash_digest innerhash; 56 | scrypt_hash_finish(&st->inner, innerhash); 57 | 58 | /* h(outer || h(inner || m)) */ 59 | scrypt_hash_update(&st->outer, innerhash, sizeof(innerhash)); 60 | scrypt_hash_finish(&st->outer, mac); 61 | 62 | scrypt_ensure_zero(st, sizeof(*st)); 63 | } 64 | 65 | static void 66 | scrypt_pbkdf2(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint64_t N, uint8_t *out, size_t bytes) { 67 | scrypt_hmac_state hmac_pw, hmac_pw_salt, work; 68 | scrypt_hash_digest ti, u; 69 | uint8_t be[4]; 70 | uint32_t i, j, blocks; 71 | uint64_t c; 72 | 73 | /* bytes must be <= (0xffffffff - (SCRYPT_HASH_DIGEST_SIZE - 1)), which they will always be under scrypt */ 74 | 75 | /* hmac(password, ...) */ 76 | scrypt_hmac_init(&hmac_pw, password, password_len); 77 | 78 | /* hmac(password, salt...) */ 79 | hmac_pw_salt = hmac_pw; 80 | scrypt_hmac_update(&hmac_pw_salt, salt, salt_len); 81 | 82 | blocks = ((uint32_t)bytes + (SCRYPT_HASH_DIGEST_SIZE - 1)) / SCRYPT_HASH_DIGEST_SIZE; 83 | for (i = 1; i <= blocks; i++) { 84 | /* U1 = hmac(password, salt || be(i)) */ 85 | U32TO8_BE(be, i); 86 | work = hmac_pw_salt; 87 | scrypt_hmac_update(&work, be, 4); 88 | scrypt_hmac_finish(&work, ti); 89 | memcpy(u, ti, sizeof(u)); 90 | 91 | /* T[i] = U1 ^ U2 ^ U3... */ 92 | for (c = 0; c < N - 1; c++) { 93 | /* UX = hmac(password, U{X-1}) */ 94 | work = hmac_pw; 95 | scrypt_hmac_update(&work, u, SCRYPT_HASH_DIGEST_SIZE); 96 | scrypt_hmac_finish(&work, u); 97 | 98 | /* T[i] ^= UX */ 99 | for (j = 0; j < sizeof(u); j++) 100 | ti[j] ^= u[j]; 101 | } 102 | 103 | memcpy(out, ti, (bytes > SCRYPT_HASH_DIGEST_SIZE) ? SCRYPT_HASH_DIGEST_SIZE : bytes); 104 | out += SCRYPT_HASH_DIGEST_SIZE; 105 | bytes -= SCRYPT_HASH_DIGEST_SIZE; 106 | } 107 | 108 | scrypt_ensure_zero(ti, sizeof(ti)); 109 | scrypt_ensure_zero(u, sizeof(u)); 110 | scrypt_ensure_zero(&hmac_pw, sizeof(hmac_pw)); 111 | scrypt_ensure_zero(&hmac_pw_salt, sizeof(hmac_pw_salt)); 112 | } 113 | -------------------------------------------------------------------------------- /x15.c: -------------------------------------------------------------------------------- 1 | #include "x15.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sha3/sph_blake.h" 8 | #include "sha3/sph_bmw.h" 9 | #include "sha3/sph_groestl.h" 10 | #include "sha3/sph_jh.h" 11 | #include "sha3/sph_keccak.h" 12 | #include "sha3/sph_skein.h" 13 | #include "sha3/sph_luffa.h" 14 | #include "sha3/sph_cubehash.h" 15 | #include "sha3/sph_shavite.h" 16 | #include "sha3/sph_simd.h" 17 | #include "sha3/sph_echo.h" 18 | #include "sha3/sph_hamsi.h" 19 | #include "sha3/sph_fugue.h" 20 | #include "sha3/sph_shabal.h" 21 | #include "sha3/sph_whirlpool.h" 22 | 23 | void x15_hash(const char* input, char* output, uint32_t len) 24 | { 25 | sph_blake512_context ctx_blake; 26 | sph_bmw512_context ctx_bmw; 27 | sph_groestl512_context ctx_groestl; 28 | sph_skein512_context ctx_skein; 29 | sph_jh512_context ctx_jh; 30 | sph_keccak512_context ctx_keccak; 31 | sph_luffa512_context ctx_luffa1; 32 | sph_cubehash512_context ctx_cubehash1; 33 | sph_shavite512_context ctx_shavite1; 34 | sph_simd512_context ctx_simd1; 35 | sph_echo512_context ctx_echo1; 36 | sph_hamsi512_context ctx_hamsi1; 37 | sph_fugue512_context ctx_fugue1; 38 | sph_shabal512_context ctx_shabal1; 39 | sph_whirlpool_context ctx_whirlpool1; 40 | 41 | //these uint512 in the c++ source of the client are backed by an array of uint32 42 | uint32_t hashA[16], hashB[16]; 43 | 44 | sph_blake512_init(&ctx_blake); 45 | sph_blake512 (&ctx_blake, input, len); 46 | sph_blake512_close (&ctx_blake, hashA); 47 | 48 | sph_bmw512_init(&ctx_bmw); 49 | sph_bmw512 (&ctx_bmw, hashA, 64); 50 | sph_bmw512_close(&ctx_bmw, hashB); 51 | 52 | sph_groestl512_init(&ctx_groestl); 53 | sph_groestl512 (&ctx_groestl, hashB, 64); 54 | sph_groestl512_close(&ctx_groestl, hashA); 55 | 56 | sph_skein512_init(&ctx_skein); 57 | sph_skein512 (&ctx_skein, hashA, 64); 58 | sph_skein512_close (&ctx_skein, hashB); 59 | 60 | sph_jh512_init(&ctx_jh); 61 | sph_jh512 (&ctx_jh, hashB, 64); 62 | sph_jh512_close(&ctx_jh, hashA); 63 | 64 | sph_keccak512_init(&ctx_keccak); 65 | sph_keccak512 (&ctx_keccak, hashA, 64); 66 | sph_keccak512_close(&ctx_keccak, hashB); 67 | 68 | sph_luffa512_init (&ctx_luffa1); 69 | sph_luffa512 (&ctx_luffa1, hashB, 64); 70 | sph_luffa512_close (&ctx_luffa1, hashA); 71 | 72 | sph_cubehash512_init (&ctx_cubehash1); 73 | sph_cubehash512 (&ctx_cubehash1, hashA, 64); 74 | sph_cubehash512_close(&ctx_cubehash1, hashB); 75 | 76 | sph_shavite512_init (&ctx_shavite1); 77 | sph_shavite512 (&ctx_shavite1, hashB, 64); 78 | sph_shavite512_close(&ctx_shavite1, hashA); 79 | 80 | sph_simd512_init (&ctx_simd1); 81 | sph_simd512 (&ctx_simd1, hashA, 64); 82 | sph_simd512_close(&ctx_simd1, hashB); 83 | 84 | sph_echo512_init (&ctx_echo1); 85 | sph_echo512 (&ctx_echo1, hashB, 64); 86 | sph_echo512_close(&ctx_echo1, hashA); 87 | 88 | sph_hamsi512_init (&ctx_hamsi1); 89 | sph_hamsi512 (&ctx_hamsi1, hashA, 64); 90 | sph_hamsi512_close(&ctx_hamsi1, hashB); 91 | 92 | sph_fugue512_init (&ctx_fugue1); 93 | sph_fugue512 (&ctx_fugue1, hashB, 64); 94 | sph_fugue512_close(&ctx_fugue1, hashA); 95 | 96 | sph_shabal512_init (&ctx_shabal1); 97 | sph_shabal512 (&ctx_shabal1, hashA, 64); 98 | sph_shabal512_close(&ctx_shabal1, hashB); 99 | 100 | sph_whirlpool_init (&ctx_whirlpool1); 101 | sph_whirlpool (&ctx_whirlpool1, hashB, 64); 102 | sph_whirlpool_close(&ctx_whirlpool1, hashA); 103 | 104 | memcpy(output, hashA, 32); 105 | 106 | } 107 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-chacha.h: -------------------------------------------------------------------------------- 1 | #define SCRYPT_MIX_BASE "ChaCha20/8" 2 | 3 | typedef uint32_t scrypt_mix_word_t; 4 | 5 | #define SCRYPT_WORDTO8_LE U32TO8_LE 6 | #define SCRYPT_WORD_ENDIAN_SWAP U32_SWAP 7 | 8 | #define SCRYPT_BLOCK_BYTES 64 9 | #define SCRYPT_BLOCK_WORDS (SCRYPT_BLOCK_BYTES / sizeof(scrypt_mix_word_t)) 10 | 11 | /* must have these here in case block bytes is ever != 64 */ 12 | #include "scrypt-jane-romix-basic.h" 13 | 14 | #include "scrypt-jane-mix_chacha-avx.h" 15 | #include "scrypt-jane-mix_chacha-ssse3.h" 16 | #include "scrypt-jane-mix_chacha-sse2.h" 17 | #include "scrypt-jane-mix_chacha.h" 18 | 19 | #if defined(SCRYPT_CHACHA_AVX) 20 | #define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_avx 21 | #define SCRYPT_ROMIX_FN scrypt_ROMix_avx 22 | #define SCRYPT_MIX_FN chacha_core_avx 23 | #define SCRYPT_ROMIX_TANGLE_FN scrypt_romix_nop 24 | #define SCRYPT_ROMIX_UNTANGLE_FN scrypt_romix_nop 25 | #include "scrypt-jane-romix-template.h" 26 | #endif 27 | 28 | #if defined(SCRYPT_CHACHA_SSSE3) 29 | #define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_ssse3 30 | #define SCRYPT_ROMIX_FN scrypt_ROMix_ssse3 31 | #define SCRYPT_MIX_FN chacha_core_ssse3 32 | #define SCRYPT_ROMIX_TANGLE_FN scrypt_romix_nop 33 | #define SCRYPT_ROMIX_UNTANGLE_FN scrypt_romix_nop 34 | #include "scrypt-jane-romix-template.h" 35 | #endif 36 | 37 | #if defined(SCRYPT_CHACHA_SSE2) 38 | #define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_sse2 39 | #define SCRYPT_ROMIX_FN scrypt_ROMix_sse2 40 | #define SCRYPT_MIX_FN chacha_core_sse2 41 | #define SCRYPT_ROMIX_TANGLE_FN scrypt_romix_nop 42 | #define SCRYPT_ROMIX_UNTANGLE_FN scrypt_romix_nop 43 | #include "scrypt-jane-romix-template.h" 44 | #endif 45 | 46 | /* cpu agnostic */ 47 | #define SCRYPT_ROMIX_FN scrypt_ROMix_basic 48 | #define SCRYPT_MIX_FN chacha_core_basic 49 | #define SCRYPT_ROMIX_TANGLE_FN scrypt_romix_convert_endian 50 | #define SCRYPT_ROMIX_UNTANGLE_FN scrypt_romix_convert_endian 51 | #include "scrypt-jane-romix-template.h" 52 | 53 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) 54 | static scrypt_ROMixfn 55 | scrypt_getROMix() { 56 | size_t cpuflags = detect_cpu(); 57 | 58 | #if defined(SCRYPT_CHACHA_AVX) 59 | if (cpuflags & cpu_avx) 60 | return scrypt_ROMix_avx; 61 | else 62 | #endif 63 | 64 | #if defined(SCRYPT_CHACHA_SSSE3) 65 | if (cpuflags & cpu_ssse3) 66 | return scrypt_ROMix_ssse3; 67 | else 68 | #endif 69 | 70 | #if defined(SCRYPT_CHACHA_SSE2) 71 | if (cpuflags & cpu_sse2) 72 | return scrypt_ROMix_sse2; 73 | else 74 | #endif 75 | 76 | return scrypt_ROMix_basic; 77 | } 78 | #endif 79 | 80 | 81 | #if defined(SCRYPT_TEST_SPEED) 82 | static size_t 83 | available_implementations() { 84 | size_t flags = 0; 85 | 86 | #if defined(SCRYPT_CHACHA_AVX) 87 | flags |= cpu_avx; 88 | #endif 89 | 90 | #if defined(SCRYPT_CHACHA_SSSE3) 91 | flags |= cpu_ssse3; 92 | #endif 93 | 94 | #if defined(SCRYPT_CHACHA_SSE2) 95 | flags |= cpu_sse2; 96 | #endif 97 | 98 | return flags; 99 | } 100 | #endif 101 | 102 | static int 103 | scrypt_test_mix() { 104 | static const uint8_t expected[16] = { 105 | 0x48,0x2b,0x2d,0xb8,0xa1,0x33,0x22,0x73,0xcd,0x16,0xc4,0xb4,0xb0,0x7f,0xb1,0x8a, 106 | }; 107 | 108 | int ret = 1; 109 | size_t cpuflags = detect_cpu(); 110 | 111 | #if defined(SCRYPT_CHACHA_AVX) 112 | if (cpuflags & cpu_avx) 113 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_avx, scrypt_romix_nop, scrypt_romix_nop, expected); 114 | #endif 115 | 116 | #if defined(SCRYPT_CHACHA_SSSE3) 117 | if (cpuflags & cpu_ssse3) 118 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_ssse3, scrypt_romix_nop, scrypt_romix_nop, expected); 119 | #endif 120 | 121 | #if defined(SCRYPT_CHACHA_SSE2) 122 | if (cpuflags & cpu_sse2) 123 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_sse2, scrypt_romix_nop, scrypt_romix_nop, expected); 124 | #endif 125 | 126 | #if defined(SCRYPT_CHACHA_BASIC) 127 | ret &= scrypt_test_mix_instance(scrypt_ChunkMix_basic, scrypt_romix_convert_endian, scrypt_romix_convert_endian, expected); 128 | #endif 129 | 130 | return ret; 131 | } 132 | 133 | -------------------------------------------------------------------------------- /yescrypt/sysendian.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 2007-2009 Colin Percival 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | * 26 | * This file was originally written by Colin Percival as part of the Tarsnap 27 | * online backup system. 28 | */ 29 | #ifndef _SYSENDIAN_H_ 30 | #define _SYSENDIAN_H_ 31 | 32 | /* If we don't have be64enc, the we have isn't usable. */ 33 | #if !HAVE_DECL_BE64ENC 34 | #undef HAVE_SYS_ENDIAN_H 35 | #endif 36 | 37 | #ifdef HAVE_SYS_ENDIAN_H 38 | 39 | #include 40 | 41 | #else 42 | 43 | #include 44 | 45 | #if !HAVE_DECL_BE32DEC 46 | static inline uint32_t be32dec(const void *pp) 47 | { 48 | const uint8_t *p = (uint8_t const *)pp; 49 | return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + 50 | ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); 51 | } 52 | #endif 53 | 54 | #if !HAVE_DECL_LE32DEC 55 | static inline uint32_t le32dec(const void *pp) 56 | { 57 | const uint8_t *p = (uint8_t const *)pp; 58 | return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + 59 | ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); 60 | } 61 | #endif 62 | 63 | #if !HAVE_DECL_BE32ENC 64 | static inline void be32enc(void *pp, uint32_t x) 65 | { 66 | uint8_t *p = (uint8_t *)pp; 67 | p[3] = x & 0xff; 68 | p[2] = (x >> 8) & 0xff; 69 | p[1] = (x >> 16) & 0xff; 70 | p[0] = (x >> 24) & 0xff; 71 | } 72 | #endif 73 | 74 | #if !HAVE_DECL_LE32ENC 75 | static inline void le32enc(void *pp, uint32_t x) 76 | { 77 | uint8_t *p = (uint8_t *)pp; 78 | p[0] = x & 0xff; 79 | p[1] = (x >> 8) & 0xff; 80 | p[2] = (x >> 16) & 0xff; 81 | p[3] = (x >> 24) & 0xff; 82 | } 83 | #endif 84 | 85 | static inline uint64_t 86 | be64dec(const void *pp) 87 | { 88 | const uint8_t *p = (uint8_t const *)pp; 89 | 90 | return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) + 91 | ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) + 92 | ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) + 93 | ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56)); 94 | } 95 | 96 | static inline void 97 | be64enc(void *pp, uint64_t x) 98 | { 99 | uint8_t * p = (uint8_t *)pp; 100 | 101 | p[7] = x & 0xff; 102 | p[6] = (x >> 8) & 0xff; 103 | p[5] = (x >> 16) & 0xff; 104 | p[4] = (x >> 24) & 0xff; 105 | p[3] = (x >> 32) & 0xff; 106 | p[2] = (x >> 40) & 0xff; 107 | p[1] = (x >> 48) & 0xff; 108 | p[0] = (x >> 56) & 0xff; 109 | } 110 | 111 | 112 | 113 | static inline uint64_t 114 | le64dec(const void *pp) 115 | { 116 | const uint8_t *p = (uint8_t const *)pp; 117 | 118 | return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) + 119 | ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) + 120 | ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) + 121 | ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56)); 122 | } 123 | 124 | static inline void 125 | le64enc(void *pp, uint64_t x) 126 | { 127 | uint8_t * p = (uint8_t *)pp; 128 | 129 | p[0] = x & 0xff; 130 | p[1] = (x >> 8) & 0xff; 131 | p[2] = (x >> 16) & 0xff; 132 | p[3] = (x >> 24) & 0xff; 133 | p[4] = (x >> 32) & 0xff; 134 | p[5] = (x >> 40) & 0xff; 135 | p[6] = (x >> 48) & 0xff; 136 | p[7] = (x >> 56) & 0xff; 137 | } 138 | #endif /* !HAVE_SYS_ENDIAN_H */ 139 | 140 | #endif /* !_SYSENDIAN_H_ */ 141 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-hash_sha256.h: -------------------------------------------------------------------------------- 1 | #define SCRYPT_HASH "SHA-2-256" 2 | #define SCRYPT_HASH_BLOCK_SIZE 64 3 | #define SCRYPT_HASH_DIGEST_SIZE 32 4 | 5 | typedef uint8_t scrypt_hash_digest[SCRYPT_HASH_DIGEST_SIZE]; 6 | 7 | typedef struct scrypt_hash_state_t { 8 | uint32_t H[8]; 9 | uint64_t T; 10 | uint32_t leftover; 11 | uint8_t buffer[SCRYPT_HASH_BLOCK_SIZE]; 12 | } scrypt_hash_state; 13 | 14 | static const uint32_t sha256_constants[64] = { 15 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 16 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 17 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 18 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 19 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 20 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 21 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 22 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 23 | }; 24 | 25 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) 26 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) 27 | #define S0(x) (ROTR32(x, 2) ^ ROTR32(x, 13) ^ ROTR32(x, 22)) 28 | #define S1(x) (ROTR32(x, 6) ^ ROTR32(x, 11) ^ ROTR32(x, 25)) 29 | #define G0(x) (ROTR32(x, 7) ^ ROTR32(x, 18) ^ (x >> 3)) 30 | #define G1(x) (ROTR32(x, 17) ^ ROTR32(x, 19) ^ (x >> 10)) 31 | #define W0(in,i) (U8TO32_BE(&in[i * 4])) 32 | #define W1(i) (G1(w[i - 2]) + w[i - 7] + G0(w[i - 15]) + w[i - 16]) 33 | #define STEP(i) \ 34 | t1 = S0(r[0]) + Maj(r[0], r[1], r[2]); \ 35 | t0 = r[7] + S1(r[4]) + Ch(r[4], r[5], r[6]) + sha256_constants[i] + w[i]; \ 36 | r[7] = r[6]; \ 37 | r[6] = r[5]; \ 38 | r[5] = r[4]; \ 39 | r[4] = r[3] + t0; \ 40 | r[3] = r[2]; \ 41 | r[2] = r[1]; \ 42 | r[1] = r[0]; \ 43 | r[0] = t0 + t1; 44 | 45 | static void 46 | sha256_blocks(scrypt_hash_state *S, const uint8_t *in, size_t blocks) { 47 | uint32_t r[8], w[64], t0, t1; 48 | size_t i; 49 | 50 | for (i = 0; i < 8; i++) r[i] = S->H[i]; 51 | 52 | while (blocks--) { 53 | for (i = 0; i < 16; i++) { w[i] = W0(in, i); } 54 | for (i = 16; i < 64; i++) { w[i] = W1(i); } 55 | for (i = 0; i < 64; i++) { STEP(i); } 56 | for (i = 0; i < 8; i++) { r[i] += S->H[i]; S->H[i] = r[i]; } 57 | S->T += SCRYPT_HASH_BLOCK_SIZE * 8; 58 | in += SCRYPT_HASH_BLOCK_SIZE; 59 | } 60 | } 61 | 62 | static void 63 | scrypt_hash_init(scrypt_hash_state *S) { 64 | S->H[0] = 0x6a09e667; 65 | S->H[1] = 0xbb67ae85; 66 | S->H[2] = 0x3c6ef372; 67 | S->H[3] = 0xa54ff53a; 68 | S->H[4] = 0x510e527f; 69 | S->H[5] = 0x9b05688c; 70 | S->H[6] = 0x1f83d9ab; 71 | S->H[7] = 0x5be0cd19; 72 | S->T = 0; 73 | S->leftover = 0; 74 | } 75 | 76 | static void 77 | scrypt_hash_update(scrypt_hash_state *S, const uint8_t *in, size_t inlen) { 78 | size_t blocks, want; 79 | 80 | /* handle the previous data */ 81 | if (S->leftover) { 82 | want = (SCRYPT_HASH_BLOCK_SIZE - S->leftover); 83 | want = (want < inlen) ? want : inlen; 84 | memcpy(S->buffer + S->leftover, in, want); 85 | S->leftover += (uint32_t)want; 86 | if (S->leftover < SCRYPT_HASH_BLOCK_SIZE) 87 | return; 88 | in += want; 89 | inlen -= want; 90 | sha256_blocks(S, S->buffer, 1); 91 | } 92 | 93 | /* handle the current data */ 94 | blocks = (inlen & ~(SCRYPT_HASH_BLOCK_SIZE - 1)); 95 | S->leftover = (uint32_t)(inlen - blocks); 96 | if (blocks) { 97 | sha256_blocks(S, in, blocks / SCRYPT_HASH_BLOCK_SIZE); 98 | in += blocks; 99 | } 100 | 101 | /* handle leftover data */ 102 | if (S->leftover) 103 | memcpy(S->buffer, in, S->leftover); 104 | } 105 | 106 | static void 107 | scrypt_hash_finish(scrypt_hash_state *S, uint8_t *hash) { 108 | uint64_t t = S->T + (S->leftover * 8); 109 | 110 | S->buffer[S->leftover] = 0x80; 111 | if (S->leftover <= 55) { 112 | memset(S->buffer + S->leftover + 1, 0, 55 - S->leftover); 113 | } else { 114 | memset(S->buffer + S->leftover + 1, 0, 63 - S->leftover); 115 | sha256_blocks(S, S->buffer, 1); 116 | memset(S->buffer, 0, 56); 117 | } 118 | 119 | U64TO8_BE(S->buffer + 56, t); 120 | sha256_blocks(S, S->buffer, 1); 121 | 122 | U32TO8_BE(&hash[ 0], S->H[0]); 123 | U32TO8_BE(&hash[ 4], S->H[1]); 124 | U32TO8_BE(&hash[ 8], S->H[2]); 125 | U32TO8_BE(&hash[12], S->H[3]); 126 | U32TO8_BE(&hash[16], S->H[4]); 127 | U32TO8_BE(&hash[20], S->H[5]); 128 | U32TO8_BE(&hash[24], S->H[6]); 129 | U32TO8_BE(&hash[28], S->H[7]); 130 | } 131 | 132 | static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = { 133 | 0xee,0x36,0xae,0xa6,0x65,0xf0,0x28,0x7d,0xc9,0xde,0xd8,0xad,0x48,0x33,0x7d,0xbf, 134 | 0xcb,0xc0,0x48,0xfa,0x5f,0x92,0xfd,0x0a,0x95,0x6f,0x34,0x8e,0x8c,0x1e,0x73,0xad, 135 | }; 136 | -------------------------------------------------------------------------------- /crypto/cryptonote_protocol/cryptonote_protocol_defs.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #pragma once 6 | 7 | #include 8 | #include "serialization/keyvalue_serialization.h" 9 | #include "cryptonote_core/cryptonote_basic.h" 10 | #include "cryptonote_protocol/blobdatatype.h" 11 | namespace cryptonote 12 | { 13 | 14 | 15 | #define BC_COMMANDS_POOL_BASE 2000 16 | 17 | 18 | /************************************************************************/ 19 | /* */ 20 | /************************************************************************/ 21 | struct block_complete_entry 22 | { 23 | blobdata block; 24 | std::list txs; 25 | BEGIN_KV_SERIALIZE_MAP() 26 | KV_SERIALIZE(block) 27 | KV_SERIALIZE(txs) 28 | END_KV_SERIALIZE_MAP() 29 | }; 30 | 31 | 32 | /************************************************************************/ 33 | /* */ 34 | /************************************************************************/ 35 | struct NOTIFY_NEW_BLOCK 36 | { 37 | const static int ID = BC_COMMANDS_POOL_BASE + 1; 38 | 39 | struct request 40 | { 41 | block_complete_entry b; 42 | uint64_t current_blockchain_height; 43 | uint32_t hop; 44 | 45 | BEGIN_KV_SERIALIZE_MAP() 46 | KV_SERIALIZE(b) 47 | KV_SERIALIZE(current_blockchain_height) 48 | KV_SERIALIZE(hop) 49 | END_KV_SERIALIZE_MAP() 50 | }; 51 | }; 52 | 53 | /************************************************************************/ 54 | /* */ 55 | /************************************************************************/ 56 | struct NOTIFY_NEW_TRANSACTIONS 57 | { 58 | const static int ID = BC_COMMANDS_POOL_BASE + 2; 59 | 60 | struct request 61 | { 62 | std::list txs; 63 | 64 | BEGIN_KV_SERIALIZE_MAP() 65 | KV_SERIALIZE(txs) 66 | END_KV_SERIALIZE_MAP() 67 | }; 68 | }; 69 | /************************************************************************/ 70 | /* */ 71 | /************************************************************************/ 72 | struct NOTIFY_REQUEST_GET_OBJECTS 73 | { 74 | const static int ID = BC_COMMANDS_POOL_BASE + 3; 75 | 76 | struct request 77 | { 78 | std::list txs; 79 | std::list blocks; 80 | 81 | BEGIN_KV_SERIALIZE_MAP() 82 | KV_SERIALIZE_CONTAINER_POD_AS_BLOB(txs) 83 | KV_SERIALIZE_CONTAINER_POD_AS_BLOB(blocks) 84 | END_KV_SERIALIZE_MAP() 85 | }; 86 | }; 87 | 88 | struct NOTIFY_RESPONSE_GET_OBJECTS 89 | { 90 | const static int ID = BC_COMMANDS_POOL_BASE + 4; 91 | 92 | struct request 93 | { 94 | std::list txs; 95 | std::list blocks; 96 | std::list missed_ids; 97 | uint64_t current_blockchain_height; 98 | 99 | BEGIN_KV_SERIALIZE_MAP() 100 | KV_SERIALIZE(txs) 101 | KV_SERIALIZE(blocks) 102 | KV_SERIALIZE_CONTAINER_POD_AS_BLOB(missed_ids) 103 | KV_SERIALIZE(current_blockchain_height) 104 | END_KV_SERIALIZE_MAP() 105 | }; 106 | }; 107 | 108 | 109 | struct CORE_SYNC_DATA 110 | { 111 | uint64_t current_height; 112 | crypto::hash top_id; 113 | 114 | BEGIN_KV_SERIALIZE_MAP() 115 | KV_SERIALIZE(current_height) 116 | KV_SERIALIZE_VAL_POD_AS_BLOB(top_id) 117 | END_KV_SERIALIZE_MAP() 118 | }; 119 | 120 | struct NOTIFY_REQUEST_CHAIN 121 | { 122 | const static int ID = BC_COMMANDS_POOL_BASE + 6; 123 | 124 | struct request 125 | { 126 | std::list block_ids; /*IDs of the first 10 blocks are sequential, next goes with pow(2,n) offset, like 2, 4, 8, 16, 32, 64 and so on, and the last one is always genesis block */ 127 | 128 | BEGIN_KV_SERIALIZE_MAP() 129 | KV_SERIALIZE_CONTAINER_POD_AS_BLOB(block_ids) 130 | END_KV_SERIALIZE_MAP() 131 | }; 132 | }; 133 | 134 | struct NOTIFY_RESPONSE_CHAIN_ENTRY 135 | { 136 | const static int ID = BC_COMMANDS_POOL_BASE + 7; 137 | 138 | struct request 139 | { 140 | uint64_t start_height; 141 | uint64_t total_height; 142 | std::list m_block_ids; 143 | 144 | BEGIN_KV_SERIALIZE_MAP() 145 | KV_SERIALIZE(start_height) 146 | KV_SERIALIZE(total_height) 147 | KV_SERIALIZE_CONTAINER_POD_AS_BLOB(m_block_ids) 148 | END_KV_SERIALIZE_MAP() 149 | }; 150 | }; 151 | 152 | } 153 | -------------------------------------------------------------------------------- /crypto/wild_keccak.h: -------------------------------------------------------------------------------- 1 | // keccak.h 2 | // 19-Nov-11 Markku-Juhani O. Saarinen 3 | 4 | // Copyright (c) 2014 The Boolberry developers 5 | // Distributed under the MIT/X11 software license, see the accompanying 6 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 7 | 8 | 9 | #pragma once 10 | 11 | #include 12 | #include 13 | #include "hash.h" 14 | 15 | #ifndef KECCAK_ROUNDS 16 | #define KECCAK_ROUNDS 24 17 | #endif 18 | 19 | #ifndef ROTL64 20 | #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y)))) 21 | #endif 22 | 23 | // compute a keccak hash (md) of given byte length from "in" 24 | 25 | #define KK_MIXIN_SIZE 24 26 | 27 | namespace crypto 28 | { 29 | template 30 | pod_operand_a xor_pod(const pod_operand_a& a, const pod_operand_b& b) 31 | { 32 | static_assert(sizeof(pod_operand_a) == sizeof(pod_operand_b), "invalid xor_h usage: different sizes"); 33 | static_assert(sizeof(pod_operand_a)%8 == 0, "invalid xor_h usage: wrong size"); 34 | 35 | hash r; 36 | for(size_t i = 0; i != 4; i++) 37 | { 38 | ((uint64_t*)&r)[i] = ((const uint64_t*)&a)[i] ^ ((const uint64_t*)&b)[i]; 39 | } 40 | return r; 41 | } 42 | 43 | #define XOR_2(A, B) crypto::xor_pod(A, B) 44 | #define XOR_3(A, B, C) crypto::xor_pod(A, XOR_2(B, C)) 45 | #define XOR_4(A, B, C, D) crypto::xor_pod(A, XOR_3(B, C, D)) 46 | #define XOR_5(A, B, C, D, E) crypto::xor_pod(A, XOR_4(B, C, D, E)) 47 | #define XOR_8(A, B, C, D, F, G, H, I) crypto::xor_pod(XOR_4(A, B, C, D), XOR_4(F, G, H, I)) 48 | 49 | 50 | 51 | 52 | typedef uint64_t state_t_m[25]; 53 | typedef uint64_t mixin_t[KK_MIXIN_SIZE]; 54 | 55 | //with multiplication, for tests 56 | template 57 | int keccak_generic(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen) 58 | { 59 | state_t_m st; 60 | uint8_t temp[144]; 61 | size_t i, rsiz, rsizw; 62 | 63 | rsiz = sizeof(state_t_m) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen; 64 | rsizw = rsiz / 8; 65 | 66 | memset(st, 0, sizeof(st)); 67 | 68 | for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) { 69 | for (i = 0; i < rsizw; i++) 70 | st[i] ^= ((uint64_t *) in)[i]; 71 | f_traits::keccakf(st, KECCAK_ROUNDS); 72 | } 73 | 74 | 75 | // last block and padding 76 | memcpy(temp, in, inlen); 77 | temp[inlen++] = 1; 78 | memset(temp + inlen, 0, rsiz - inlen); 79 | temp[rsiz - 1] |= 0x80; 80 | 81 | for (i = 0; i < rsizw; i++) 82 | st[i] ^= ((uint64_t *) temp)[i]; 83 | 84 | f_traits::keccakf(st, KECCAK_ROUNDS); 85 | 86 | memcpy(md, st, mdlen); 87 | 88 | return 0; 89 | } 90 | 91 | template 92 | int wild_keccak(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen, callback_t cb) 93 | { 94 | state_t_m st; 95 | uint8_t temp[144]; 96 | uint64_t rsiz, rsizw; 97 | 98 | rsiz = sizeof(state_t_m) == mdlen ? HASH_DATA_AREA : 200 - 2 * mdlen; 99 | rsizw = rsiz / 8; 100 | memset(&st[0], 0, 25*sizeof(st[0])); 101 | 102 | 103 | for ( ; inlen >= rsiz; inlen -= rsiz, in += rsiz) 104 | { 105 | for (size_t i = 0; i < rsizw; i++) 106 | st[i] ^= ((uint64_t *) in)[i]; 107 | 108 | for(size_t ll = 0; ll != KECCAK_ROUNDS; ll++) 109 | { 110 | if(ll != 0) 111 | {//skip first round 112 | mixin_t mix_in; 113 | cb(st, mix_in); 114 | for (size_t k = 0; k < KK_MIXIN_SIZE; k++) 115 | st[k] ^= mix_in[k]; 116 | } 117 | f_traits::keccakf(st, 1); 118 | } 119 | } 120 | 121 | // last block and padding 122 | memcpy(temp, in, inlen); 123 | temp[inlen++] = 1; 124 | memset(temp + inlen, 0, rsiz - inlen); 125 | temp[rsiz - 1] |= 0x80; 126 | 127 | for (size_t i = 0; i < rsizw; i++) 128 | st[i] ^= ((uint64_t *) temp)[i]; 129 | 130 | for(size_t ll = 0; ll != KECCAK_ROUNDS; ll++) 131 | { 132 | if(ll != 0) 133 | {//skip first state with 134 | mixin_t mix_in; 135 | cb(st, mix_in); 136 | for (size_t k = 0; k < KK_MIXIN_SIZE; k++) 137 | st[k] ^= mix_in[k]; 138 | } 139 | f_traits::keccakf(st, 1); 140 | } 141 | 142 | memcpy(md, st, mdlen); 143 | 144 | return 0; 145 | } 146 | 147 | template 148 | int wild_keccak_dbl(const uint8_t *in, size_t inlen, uint8_t *md, size_t mdlen, callback_t cb) 149 | { 150 | //Satoshi's classic 151 | wild_keccak(in, inlen, md, mdlen, cb); 152 | wild_keccak(md, mdlen, md, mdlen, cb); 153 | return 0; 154 | } 155 | 156 | class regular_f 157 | { 158 | public: 159 | static void keccakf(uint64_t st[25], int rounds); 160 | }; 161 | 162 | class mul_f 163 | { 164 | public: 165 | static void keccakf(uint64_t st[25], int rounds); 166 | }; 167 | } 168 | 169 | -------------------------------------------------------------------------------- /timetravel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define HASH_FUNC_BASE_TIMESTAMP 1389040865 // Machinecoin: Genesis Timestamp 6 | #define HASH_FUNC_COUNT 8 // Machinecoin: HASH_FUNC_COUNT of 11 7 | #define HASH_FUNC_COUNT_PERMUTATIONS 40320 // Machinecoin: HASH_FUNC_COUNT! 8 | 9 | #include "sha3/sph_blake.h" 10 | #include "sha3/sph_bmw.h" 11 | #include "sha3/sph_groestl.h" 12 | #include "sha3/sph_jh.h" 13 | #include "sha3/sph_keccak.h" 14 | #include "sha3/sph_skein.h" 15 | #include "sha3/sph_luffa.h" 16 | #include "sha3/sph_cubehash.h" 17 | #if HASH_FUNC_COUNT > 8 18 | #include "sha3/sph_shavite.h" 19 | #include "sha3/sph_simd.h" 20 | #include "sha3/sph_echo.h" 21 | #endif 22 | 23 | #define _ALIGN(x) __attribute__ ((aligned(x))) 24 | 25 | // helpers 26 | inline void swap(int *a, int *b) { 27 | int c = *a; 28 | *a = *b; 29 | *b = c; 30 | } 31 | 32 | static void reverse(int *pbegin, int *pend) { 33 | while ( (pbegin != pend) && (pbegin != --pend) ) 34 | swap(pbegin++, pend); 35 | } 36 | 37 | static void next_permutation(int *pbegin, int *pend) { 38 | if (pbegin == pend) 39 | return; 40 | 41 | int *i = pbegin; 42 | ++i; 43 | if (i == pend) 44 | return; 45 | 46 | i = pend; 47 | --i; 48 | 49 | while (1) { 50 | int *j = i; 51 | --i; 52 | 53 | if (*i < *j) { 54 | int *k = pend; 55 | 56 | while (!(*i < *--k)) 57 | /* pass */; 58 | 59 | swap(i, k); 60 | reverse(j, pend); 61 | return; // true 62 | } 63 | 64 | if (i == pbegin) { 65 | reverse(pbegin, pend); 66 | return; // false 67 | } 68 | } 69 | } 70 | // helpers 71 | 72 | void timetravel_hash(const char* input, char* output, uint32_t len) 73 | { 74 | uint32_t _ALIGN(64) hash[16 * HASH_FUNC_COUNT]; 75 | uint32_t *hashA, *hashB; 76 | uint32_t dataLen = 64; 77 | uint32_t *work_data = (uint32_t *)input; 78 | const uint32_t timestamp = work_data[17]; 79 | 80 | 81 | sph_blake512_context ctx_blake; 82 | sph_bmw512_context ctx_bmw; 83 | sph_groestl512_context ctx_groestl; 84 | sph_skein512_context ctx_skein; 85 | sph_jh512_context ctx_jh; 86 | sph_keccak512_context ctx_keccak; 87 | sph_luffa512_context ctx_luffa; 88 | sph_cubehash512_context ctx_cubehash; 89 | #if HASH_FUNC_COUNT > 8 90 | sph_shavite512_context ctx_shavite; 91 | sph_simd512_context ctx_simd; 92 | sph_echo512_context ctx_echo; 93 | #endif 94 | // We want to permute algorithms. To get started we 95 | // initialize an array with a sorted sequence of unique 96 | // integers where every integer represents its own algorithm. 97 | uint32_t permutation[HASH_FUNC_COUNT]; 98 | for (uint32_t i = 0; i < HASH_FUNC_COUNT; i++) { 99 | permutation[i]=i; 100 | } 101 | 102 | // Compute the next permuation 103 | uint32_t steps = (timestamp - HASH_FUNC_BASE_TIMESTAMP) % HASH_FUNC_COUNT_PERMUTATIONS; 104 | for (uint32_t i = 0; i < steps; i++) { 105 | next_permutation(permutation, permutation + HASH_FUNC_COUNT); 106 | } 107 | 108 | for (uint32_t i = 0; i < HASH_FUNC_COUNT; i++) { 109 | if (i == 0) { 110 | dataLen = len; 111 | hashA = work_data; 112 | } else { 113 | dataLen = 64; 114 | hashA = &hash[16 * (i - 1)]; 115 | } 116 | hashB = &hash[16 * i]; 117 | 118 | switch(permutation[i]) { 119 | case 0: 120 | sph_blake512_init(&ctx_blake); 121 | sph_blake512(&ctx_blake, hashA, dataLen); 122 | sph_blake512_close(&ctx_blake, hashB); 123 | break; 124 | case 1: 125 | sph_bmw512_init(&ctx_bmw); 126 | sph_bmw512 (&ctx_bmw, hashA, dataLen); 127 | sph_bmw512_close(&ctx_bmw, hashB); 128 | break; 129 | case 2: 130 | sph_groestl512_init(&ctx_groestl); 131 | sph_groestl512 (&ctx_groestl, hashA, dataLen); 132 | sph_groestl512_close(&ctx_groestl, hashB); 133 | break; 134 | case 3: 135 | sph_skein512_init(&ctx_skein); 136 | sph_skein512 (&ctx_skein, hashA, dataLen); 137 | sph_skein512_close(&ctx_skein, hashB); 138 | break; 139 | case 4: 140 | sph_jh512_init(&ctx_jh); 141 | sph_jh512 (&ctx_jh, hashA, dataLen); 142 | sph_jh512_close(&ctx_jh, hashB); 143 | break; 144 | case 5: 145 | sph_keccak512_init(&ctx_keccak); 146 | sph_keccak512 (&ctx_keccak, hashA, dataLen); 147 | sph_keccak512_close(&ctx_keccak, hashB); 148 | break; 149 | case 6: 150 | sph_luffa512_init(&ctx_luffa); 151 | sph_luffa512 (&ctx_luffa, hashA, dataLen); 152 | sph_luffa512_close(&ctx_luffa, hashB); 153 | break; 154 | case 7: 155 | sph_cubehash512_init(&ctx_cubehash); 156 | sph_cubehash512 (&ctx_cubehash, hashA, dataLen); 157 | sph_cubehash512_close(&ctx_cubehash, hashB); 158 | break; 159 | #if HASH_FUNC_COUNT > 8 160 | case 8: 161 | sph_shavite512_init(&ctx_shavite); 162 | sph_shavite512(&ctx_shavite, hashA, dataLen); 163 | sph_shavite512_close(&ctx_shavite, hashB); 164 | break; 165 | case 9: 166 | sph_simd512_init(&ctx_simd); 167 | sph_simd512 (&ctx_simd, hashA, dataLen); 168 | sph_simd512_close(&ctx_simd, hashB); 169 | break; 170 | case 10: 171 | sph_echo512_init(&ctx_echo); 172 | sph_echo512 (&ctx_echo, hashA, dataLen); 173 | sph_echo512_close(&ctx_echo, hashB); 174 | break; 175 | #endif 176 | default: 177 | break; 178 | } 179 | } 180 | 181 | memcpy(output, &hash[16 * (HASH_FUNC_COUNT - 1)], 32); 182 | } -------------------------------------------------------------------------------- /yescrypt/yescrypt-platform.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 2013,2014 Alexander Peslyak 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted. 7 | * 8 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 9 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 11 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 12 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 13 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 14 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 15 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 16 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 17 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 18 | * SUCH DAMAGE. 19 | */ 20 | 21 | #include 22 | #include "yescrypt.h" 23 | #define HUGEPAGE_THRESHOLD (12 * 1024 * 1024) 24 | 25 | #ifdef __x86_64__ 26 | #define HUGEPAGE_SIZE (2 * 1024 * 1024) 27 | #else 28 | #undef HUGEPAGE_SIZE 29 | #endif 30 | 31 | static void * 32 | alloc_region(yescrypt_region_t * region, size_t size) 33 | { 34 | size_t base_size = size; 35 | uint8_t * base, * aligned; 36 | #ifdef MAP_ANON 37 | int flags = 38 | #ifdef MAP_NOCORE 39 | MAP_NOCORE | 40 | #endif 41 | MAP_ANON | MAP_PRIVATE; 42 | #if defined(MAP_HUGETLB) && defined(HUGEPAGE_SIZE) 43 | size_t new_size = size; 44 | const size_t hugepage_mask = (size_t)HUGEPAGE_SIZE - 1; 45 | if (size >= HUGEPAGE_THRESHOLD && size + hugepage_mask >= size) { 46 | flags |= MAP_HUGETLB; 47 | /* 48 | * Linux's munmap() fails on MAP_HUGETLB mappings if size is not a multiple of 49 | * huge page size, so let's round up to huge page size here. 50 | */ 51 | new_size = size + hugepage_mask; 52 | new_size &= ~hugepage_mask; 53 | } 54 | base = mmap(NULL, new_size, PROT_READ | PROT_WRITE, flags, -1, 0); 55 | if (base != MAP_FAILED) { 56 | base_size = new_size; 57 | } else 58 | if (flags & MAP_HUGETLB) { 59 | flags &= ~MAP_HUGETLB; 60 | base = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0); 61 | } 62 | 63 | #else 64 | base = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0); 65 | #endif 66 | if (base == MAP_FAILED) 67 | base = NULL; 68 | aligned = base; 69 | #elif defined(HAVE_POSIX_MEMALIGN) 70 | if ((errno = posix_memalign((void **)&base, 64, size)) != 0) 71 | base = NULL; 72 | aligned = base; 73 | #else 74 | base = aligned = NULL; 75 | if (size + 63 < size) { 76 | errno = ENOMEM; 77 | } else if ((base = malloc(size + 63)) != NULL) { 78 | aligned = base + 63; 79 | aligned -= (uintptr_t)aligned & 63; 80 | } 81 | #endif 82 | region->base = base; 83 | region->aligned = aligned; 84 | region->base_size = base ? base_size : 0; 85 | region->aligned_size = base ? size : 0; 86 | return aligned; 87 | } 88 | 89 | static inline void 90 | init_region(yescrypt_region_t * region) 91 | { 92 | region->base = region->aligned = NULL; 93 | region->base_size = region->aligned_size = 0; 94 | } 95 | 96 | static int 97 | free_region(yescrypt_region_t * region) 98 | { 99 | if (region->base) { 100 | #ifdef MAP_ANON 101 | if (munmap(region->base, region->base_size)) 102 | return -1; 103 | #else 104 | free(region->base); 105 | #endif 106 | } 107 | init_region(region); 108 | return 0; 109 | } 110 | 111 | int 112 | yescrypt_init_shared(yescrypt_shared_t * shared, 113 | const uint8_t * param, size_t paramlen, 114 | uint64_t N, uint32_t r, uint32_t p, 115 | yescrypt_init_shared_flags_t flags, uint32_t mask, 116 | uint8_t * buf, size_t buflen) 117 | { 118 | yescrypt_shared1_t * shared1 = &shared->shared1; 119 | yescrypt_shared_t dummy, half1, half2; 120 | uint8_t salt[32]; 121 | 122 | if (flags & YESCRYPT_SHARED_PREALLOCATED) { 123 | if (!shared1->aligned || !shared1->aligned_size) 124 | return -1; 125 | } else { 126 | init_region(shared1); 127 | } 128 | shared->mask1 = 1; 129 | if (!param && !paramlen && !N && !r && !p && !buf && !buflen) 130 | return 0; 131 | 132 | init_region(&dummy.shared1); 133 | dummy.mask1 = 1; 134 | if (yescrypt_kdf(&dummy, shared1, 135 | param, paramlen, NULL, 0, N, r, p, 0, 136 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_1, 137 | salt, sizeof(salt))) 138 | goto out; 139 | 140 | half1 = half2 = *shared; 141 | half1.shared1.aligned_size /= 2; 142 | half2.shared1.aligned += half1.shared1.aligned_size; 143 | half2.shared1.aligned_size = half1.shared1.aligned_size; 144 | N /= 2; 145 | 146 | if (p > 1 && yescrypt_kdf(&half1, &half2.shared1, 147 | param, paramlen, salt, sizeof(salt), N, r, p, 0, 148 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_2, 149 | salt, sizeof(salt))) 150 | goto out; 151 | 152 | if (yescrypt_kdf(&half2, &half1.shared1, 153 | param, paramlen, salt, sizeof(salt), N, r, p, 0, 154 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_1, 155 | salt, sizeof(salt))) 156 | goto out; 157 | 158 | if (yescrypt_kdf(&half1, &half2.shared1, 159 | param, paramlen, salt, sizeof(salt), N, r, p, 0, 160 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_1, 161 | buf, buflen)) 162 | goto out; 163 | 164 | shared->mask1 = mask; 165 | 166 | return 0; 167 | 168 | out: 169 | if (!(flags & YESCRYPT_SHARED_PREALLOCATED)) 170 | free_region(shared1); 171 | return -1; 172 | } 173 | 174 | int 175 | yescrypt_free_shared(yescrypt_shared_t * shared) 176 | { 177 | return free_region(&shared->shared1); 178 | } 179 | 180 | int 181 | yescrypt_init_local(yescrypt_local_t * local) 182 | { 183 | init_region(local); 184 | return 0; 185 | } 186 | 187 | int 188 | yescrypt_free_local(yescrypt_local_t * local) 189 | { 190 | return free_region(local); 191 | } 192 | -------------------------------------------------------------------------------- /crypto/groestl_tables.h: -------------------------------------------------------------------------------- 1 | #ifndef __tables_h 2 | #define __tables_h 3 | 4 | 5 | const uint32_t T[512] = {0xa5f432c6, 0xc6a597f4, 0x84976ff8, 0xf884eb97, 0x99b05eee, 0xee99c7b0, 0x8d8c7af6, 0xf68df78c, 0xd17e8ff, 0xff0de517, 0xbddc0ad6, 0xd6bdb7dc, 0xb1c816de, 0xdeb1a7c8, 0x54fc6d91, 0x915439fc 6 | , 0x50f09060, 0x6050c0f0, 0x3050702, 0x2030405, 0xa9e02ece, 0xcea987e0, 0x7d87d156, 0x567dac87, 0x192bcce7, 0xe719d52b, 0x62a613b5, 0xb56271a6, 0xe6317c4d, 0x4de69a31, 0x9ab559ec, 0xec9ac3b5 7 | , 0x45cf408f, 0x8f4505cf, 0x9dbca31f, 0x1f9d3ebc, 0x40c04989, 0x894009c0, 0x879268fa, 0xfa87ef92, 0x153fd0ef, 0xef15c53f, 0xeb2694b2, 0xb2eb7f26, 0xc940ce8e, 0x8ec90740, 0xb1de6fb, 0xfb0bed1d 8 | , 0xec2f6e41, 0x41ec822f, 0x67a91ab3, 0xb3677da9, 0xfd1c435f, 0x5ffdbe1c, 0xea256045, 0x45ea8a25, 0xbfdaf923, 0x23bf46da, 0xf7025153, 0x53f7a602, 0x96a145e4, 0xe496d3a1, 0x5bed769b, 0x9b5b2ded 9 | , 0xc25d2875, 0x75c2ea5d, 0x1c24c5e1, 0xe11cd924, 0xaee9d43d, 0x3dae7ae9, 0x6abef24c, 0x4c6a98be, 0x5aee826c, 0x6c5ad8ee, 0x41c3bd7e, 0x7e41fcc3, 0x206f3f5, 0xf502f106, 0x4fd15283, 0x834f1dd1 10 | , 0x5ce48c68, 0x685cd0e4, 0xf4075651, 0x51f4a207, 0x345c8dd1, 0xd134b95c, 0x818e1f9, 0xf908e918, 0x93ae4ce2, 0xe293dfae, 0x73953eab, 0xab734d95, 0x53f59762, 0x6253c4f5, 0x3f416b2a, 0x2a3f5441 11 | , 0xc141c08, 0x80c1014, 0x52f66395, 0x955231f6, 0x65afe946, 0x46658caf, 0x5ee27f9d, 0x9d5e21e2, 0x28784830, 0x30286078, 0xa1f8cf37, 0x37a16ef8, 0xf111b0a, 0xa0f1411, 0xb5c4eb2f, 0x2fb55ec4 12 | , 0x91b150e, 0xe091c1b, 0x365a7e24, 0x2436485a, 0x9bb6ad1b, 0x1b9b36b6, 0x3d4798df, 0xdf3da547, 0x266aa7cd, 0xcd26816a, 0x69bbf54e, 0x4e699cbb, 0xcd4c337f, 0x7fcdfe4c, 0x9fba50ea, 0xea9fcfba 13 | , 0x1b2d3f12, 0x121b242d, 0x9eb9a41d, 0x1d9e3ab9, 0x749cc458, 0x5874b09c, 0x2e724634, 0x342e6872, 0x2d774136, 0x362d6c77, 0xb2cd11dc, 0xdcb2a3cd, 0xee299db4, 0xb4ee7329, 0xfb164d5b, 0x5bfbb616 14 | , 0xf601a5a4, 0xa4f65301, 0x4dd7a176, 0x764decd7, 0x61a314b7, 0xb76175a3, 0xce49347d, 0x7dcefa49, 0x7b8ddf52, 0x527ba48d, 0x3e429fdd, 0xdd3ea142, 0x7193cd5e, 0x5e71bc93, 0x97a2b113, 0x139726a2 15 | , 0xf504a2a6, 0xa6f55704, 0x68b801b9, 0xb96869b8, 0x0, 0x0, 0x2c74b5c1, 0xc12c9974, 0x60a0e040, 0x406080a0, 0x1f21c2e3, 0xe31fdd21, 0xc8433a79, 0x79c8f243, 0xed2c9ab6, 0xb6ed772c 16 | , 0xbed90dd4, 0xd4beb3d9, 0x46ca478d, 0x8d4601ca, 0xd9701767, 0x67d9ce70, 0x4bddaf72, 0x724be4dd, 0xde79ed94, 0x94de3379, 0xd467ff98, 0x98d42b67, 0xe82393b0, 0xb0e87b23, 0x4ade5b85, 0x854a11de 17 | , 0x6bbd06bb, 0xbb6b6dbd, 0x2a7ebbc5, 0xc52a917e, 0xe5347b4f, 0x4fe59e34, 0x163ad7ed, 0xed16c13a, 0xc554d286, 0x86c51754, 0xd762f89a, 0x9ad72f62, 0x55ff9966, 0x6655ccff, 0x94a7b611, 0x119422a7 18 | , 0xcf4ac08a, 0x8acf0f4a, 0x1030d9e9, 0xe910c930, 0x60a0e04, 0x406080a, 0x819866fe, 0xfe81e798, 0xf00baba0, 0xa0f05b0b, 0x44ccb478, 0x7844f0cc, 0xbad5f025, 0x25ba4ad5, 0xe33e754b, 0x4be3963e 19 | , 0xf30eaca2, 0xa2f35f0e, 0xfe19445d, 0x5dfeba19, 0xc05bdb80, 0x80c01b5b, 0x8a858005, 0x58a0a85, 0xadecd33f, 0x3fad7eec, 0xbcdffe21, 0x21bc42df, 0x48d8a870, 0x7048e0d8, 0x40cfdf1, 0xf104f90c 20 | , 0xdf7a1963, 0x63dfc67a, 0xc1582f77, 0x77c1ee58, 0x759f30af, 0xaf75459f, 0x63a5e742, 0x426384a5, 0x30507020, 0x20304050, 0x1a2ecbe5, 0xe51ad12e, 0xe12effd, 0xfd0ee112, 0x6db708bf, 0xbf6d65b7 21 | , 0x4cd45581, 0x814c19d4, 0x143c2418, 0x1814303c, 0x355f7926, 0x26354c5f, 0x2f71b2c3, 0xc32f9d71, 0xe13886be, 0xbee16738, 0xa2fdc835, 0x35a26afd, 0xcc4fc788, 0x88cc0b4f, 0x394b652e, 0x2e395c4b 22 | , 0x57f96a93, 0x93573df9, 0xf20d5855, 0x55f2aa0d, 0x829d61fc, 0xfc82e39d, 0x47c9b37a, 0x7a47f4c9, 0xacef27c8, 0xc8ac8bef, 0xe73288ba, 0xbae76f32, 0x2b7d4f32, 0x322b647d, 0x95a442e6, 0xe695d7a4 23 | , 0xa0fb3bc0, 0xc0a09bfb, 0x98b3aa19, 0x199832b3, 0xd168f69e, 0x9ed12768, 0x7f8122a3, 0xa37f5d81, 0x66aaee44, 0x446688aa, 0x7e82d654, 0x547ea882, 0xabe6dd3b, 0x3bab76e6, 0x839e950b, 0xb83169e 24 | , 0xca45c98c, 0x8cca0345, 0x297bbcc7, 0xc729957b, 0xd36e056b, 0x6bd3d66e, 0x3c446c28, 0x283c5044, 0x798b2ca7, 0xa779558b, 0xe23d81bc, 0xbce2633d, 0x1d273116, 0x161d2c27, 0x769a37ad, 0xad76419a 25 | , 0x3b4d96db, 0xdb3bad4d, 0x56fa9e64, 0x6456c8fa, 0x4ed2a674, 0x744ee8d2, 0x1e223614, 0x141e2822, 0xdb76e492, 0x92db3f76, 0xa1e120c, 0xc0a181e, 0x6cb4fc48, 0x486c90b4, 0xe4378fb8, 0xb8e46b37 26 | , 0x5de7789f, 0x9f5d25e7, 0x6eb20fbd, 0xbd6e61b2, 0xef2a6943, 0x43ef862a, 0xa6f135c4, 0xc4a693f1, 0xa8e3da39, 0x39a872e3, 0xa4f7c631, 0x31a462f7, 0x37598ad3, 0xd337bd59, 0x8b8674f2, 0xf28bff86 27 | , 0x325683d5, 0xd532b156, 0x43c54e8b, 0x8b430dc5, 0x59eb856e, 0x6e59dceb, 0xb7c218da, 0xdab7afc2, 0x8c8f8e01, 0x18c028f, 0x64ac1db1, 0xb16479ac, 0xd26df19c, 0x9cd2236d, 0xe03b7249, 0x49e0923b 28 | , 0xb4c71fd8, 0xd8b4abc7, 0xfa15b9ac, 0xacfa4315, 0x709faf3, 0xf307fd09, 0x256fa0cf, 0xcf25856f, 0xafea20ca, 0xcaaf8fea, 0x8e897df4, 0xf48ef389, 0xe9206747, 0x47e98e20, 0x18283810, 0x10182028 29 | , 0xd5640b6f, 0x6fd5de64, 0x888373f0, 0xf088fb83, 0x6fb1fb4a, 0x4a6f94b1, 0x7296ca5c, 0x5c72b896, 0x246c5438, 0x3824706c, 0xf1085f57, 0x57f1ae08, 0xc7522173, 0x73c7e652, 0x51f36497, 0x975135f3 30 | , 0x2365aecb, 0xcb238d65, 0x7c8425a1, 0xa17c5984, 0x9cbf57e8, 0xe89ccbbf, 0x21635d3e, 0x3e217c63, 0xdd7cea96, 0x96dd377c, 0xdc7f1e61, 0x61dcc27f, 0x86919c0d, 0xd861a91, 0x85949b0f, 0xf851e94 31 | , 0x90ab4be0, 0xe090dbab, 0x42c6ba7c, 0x7c42f8c6, 0xc4572671, 0x71c4e257, 0xaae529cc, 0xccaa83e5, 0xd873e390, 0x90d83b73, 0x50f0906, 0x6050c0f, 0x103f4f7, 0xf701f503, 0x12362a1c, 0x1c123836 32 | , 0xa3fe3cc2, 0xc2a39ffe, 0x5fe18b6a, 0x6a5fd4e1, 0xf910beae, 0xaef94710, 0xd06b0269, 0x69d0d26b, 0x91a8bf17, 0x17912ea8, 0x58e87199, 0x995829e8, 0x2769533a, 0x3a277469, 0xb9d0f727, 0x27b94ed0 33 | , 0x384891d9, 0xd938a948, 0x1335deeb, 0xeb13cd35, 0xb3cee52b, 0x2bb356ce, 0x33557722, 0x22334455, 0xbbd604d2, 0xd2bbbfd6, 0x709039a9, 0xa9704990, 0x89808707, 0x7890e80, 0xa7f2c133, 0x33a766f2 34 | , 0xb6c1ec2d, 0x2db65ac1, 0x22665a3c, 0x3c227866, 0x92adb815, 0x15922aad, 0x2060a9c9, 0xc9208960, 0x49db5c87, 0x874915db, 0xff1ab0aa, 0xaaff4f1a, 0x7888d850, 0x5078a088, 0x7a8e2ba5, 0xa57a518e 35 | , 0x8f8a8903, 0x38f068a, 0xf8134a59, 0x59f8b213, 0x809b9209, 0x980129b, 0x1739231a, 0x1a173439, 0xda751065, 0x65daca75, 0x315384d7, 0xd731b553, 0xc651d584, 0x84c61351, 0xb8d303d0, 0xd0b8bbd3 36 | , 0xc35edc82, 0x82c31f5e, 0xb0cbe229, 0x29b052cb, 0x7799c35a, 0x5a77b499, 0x11332d1e, 0x1e113c33, 0xcb463d7b, 0x7bcbf646, 0xfc1fb7a8, 0xa8fc4b1f, 0xd6610c6d, 0x6dd6da61, 0x3a4e622c, 0x2c3a584e}; 37 | 38 | #endif /* __tables_h */ 39 | -------------------------------------------------------------------------------- /quark.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 2009 Colin Percival, 2011 ArtForz, 2013 Neisklar, 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | * 26 | * This file was originally written by Colin Percival as part of the Tarsnap 27 | * online backup system. 28 | */ 29 | 30 | #include "quark.h" 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include "sha3/sph_blake.h" 36 | #include "sha3/sph_bmw.h" 37 | #include "sha3/sph_groestl.h" 38 | #include "sha3/sph_jh.h" 39 | #include "sha3/sph_keccak.h" 40 | #include "sha3/sph_skein.h" 41 | 42 | 43 | static __inline uint32_t 44 | be32dec(const void *pp) 45 | { 46 | const uint8_t *p = (uint8_t const *)pp; 47 | 48 | return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + 49 | ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); 50 | } 51 | 52 | static __inline void 53 | be32enc(void *pp, uint32_t x) 54 | { 55 | uint8_t * p = (uint8_t *)pp; 56 | 57 | p[3] = x & 0xff; 58 | p[2] = (x >> 8) & 0xff; 59 | p[1] = (x >> 16) & 0xff; 60 | p[0] = (x >> 24) & 0xff; 61 | } 62 | 63 | static __inline uint32_t 64 | le32dec(const void *pp) 65 | { 66 | const uint8_t *p = (uint8_t const *)pp; 67 | 68 | return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + 69 | ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); 70 | } 71 | 72 | static __inline void 73 | le32enc(void *pp, uint32_t x) 74 | { 75 | uint8_t * p = (uint8_t *)pp; 76 | 77 | p[0] = x & 0xff; 78 | p[1] = (x >> 8) & 0xff; 79 | p[2] = (x >> 16) & 0xff; 80 | p[3] = (x >> 24) & 0xff; 81 | } 82 | 83 | /* 84 | * Encode a length len/4 vector of (uint32_t) into a length len vector of 85 | * (unsigned char) in big-endian form. Assumes len is a multiple of 4. 86 | */ 87 | static void 88 | be32enc_vect(unsigned char *dst, const uint32_t *src, uint32_t len) 89 | { 90 | size_t i; 91 | 92 | for (i = 0; i < len / 4; i++) 93 | be32enc(dst + i * 4, src[i]); 94 | } 95 | 96 | /* 97 | * Decode a big-endian length len vector of (unsigned char) into a length 98 | * len/4 vector of (uint32_t). Assumes len is a multiple of 4. 99 | */ 100 | static void 101 | be32dec_vect(uint32_t *dst, const unsigned char *src, uint32_t len) 102 | { 103 | size_t i; 104 | 105 | for (i = 0; i < len / 4; i++) 106 | dst[i] = be32dec(src + i * 4); 107 | } 108 | 109 | void quark_hash(const char* input, char* output, uint32_t len) 110 | { 111 | sph_blake512_context ctx_blake; 112 | sph_bmw512_context ctx_bmw; 113 | sph_groestl512_context ctx_groestl; 114 | sph_jh512_context ctx_jh; 115 | sph_keccak512_context ctx_keccak; 116 | sph_skein512_context ctx_skein; 117 | 118 | uint32_t mask = 8; 119 | uint32_t zero = 0; 120 | 121 | uint32_t hashA[16], hashB[16]; 122 | 123 | 124 | 125 | sph_blake512_init(&ctx_blake); 126 | sph_blake512 (&ctx_blake, input, len); 127 | sph_blake512_close (&ctx_blake, hashA); //0 128 | 129 | 130 | sph_bmw512_init(&ctx_bmw); 131 | sph_bmw512 (&ctx_bmw, hashA, 64); //0 132 | sph_bmw512_close(&ctx_bmw, hashB); //1 133 | 134 | 135 | if ((hashB[0] & mask) != zero) //1 136 | { 137 | sph_groestl512_init(&ctx_groestl); 138 | sph_groestl512 (&ctx_groestl, hashB, 64); //1 139 | sph_groestl512_close(&ctx_groestl, hashA); //2 140 | } 141 | else 142 | { 143 | sph_skein512_init(&ctx_skein); 144 | sph_skein512 (&ctx_skein, hashB, 64); //1 145 | sph_skein512_close(&ctx_skein, hashA); //2 146 | } 147 | 148 | 149 | sph_groestl512_init(&ctx_groestl); 150 | sph_groestl512 (&ctx_groestl, hashA, 64); //2 151 | sph_groestl512_close(&ctx_groestl, hashB); //3 152 | 153 | sph_jh512_init(&ctx_jh); 154 | sph_jh512 (&ctx_jh, hashB, 64); //3 155 | sph_jh512_close(&ctx_jh, hashA); //4 156 | 157 | if ((hashA[0] & mask) != zero) //4 158 | { 159 | sph_blake512_init(&ctx_blake); 160 | sph_blake512 (&ctx_blake, hashA, 64); // 161 | sph_blake512_close(&ctx_blake, hashB); //5 162 | } 163 | else 164 | { 165 | sph_bmw512_init(&ctx_bmw); 166 | sph_bmw512 (&ctx_bmw, hashA, 64); //4 167 | sph_bmw512_close(&ctx_bmw, hashB); //5 168 | } 169 | 170 | sph_keccak512_init(&ctx_keccak); 171 | sph_keccak512 (&ctx_keccak,hashB, 64); //5 172 | sph_keccak512_close(&ctx_keccak, hashA); //6 173 | 174 | sph_skein512_init(&ctx_skein); 175 | sph_skein512 (&ctx_skein, hashA, 64); //6 176 | sph_skein512_close(&ctx_skein, hashB); //7 177 | 178 | if ((hashB[0] & mask) != zero) //7 179 | { 180 | sph_keccak512_init(&ctx_keccak); 181 | sph_keccak512 (&ctx_keccak, hashB, 64); // 182 | sph_keccak512_close(&ctx_keccak, hashA); //8 183 | } 184 | else 185 | { 186 | sph_jh512_init(&ctx_jh); 187 | sph_jh512 (&ctx_jh, hashB, 64); //7 188 | sph_jh512_close(&ctx_jh, hashA); //8 189 | } 190 | 191 | 192 | 193 | memcpy(output, hashA, 32); 194 | 195 | 196 | /* 197 | printf("result: "); 198 | for (ii=0; ii < 32; ii++) 199 | { 200 | printf ("%.2x",((uint8_t*)output)[ii]); 201 | } 202 | printf ("\n"); 203 | */ 204 | 205 | 206 | 207 | 208 | } 209 | 210 | 211 | -------------------------------------------------------------------------------- /scryptjane/scrypt-jane-hash_keccak.h: -------------------------------------------------------------------------------- 1 | #if defined(SCRYPT_KECCAK256) 2 | #define SCRYPT_HASH "Keccak-256" 3 | #define SCRYPT_HASH_DIGEST_SIZE 32 4 | #else 5 | #define SCRYPT_HASH "Keccak-512" 6 | #define SCRYPT_HASH_DIGEST_SIZE 64 7 | #endif 8 | #define SCRYPT_KECCAK_F 1600 9 | #define SCRYPT_KECCAK_C (SCRYPT_HASH_DIGEST_SIZE * 8 * 2) /* 256=512, 512=1024 */ 10 | #define SCRYPT_KECCAK_R (SCRYPT_KECCAK_F - SCRYPT_KECCAK_C) /* 256=1088, 512=576 */ 11 | #define SCRYPT_HASH_BLOCK_SIZE (SCRYPT_KECCAK_R / 8) 12 | 13 | typedef uint8_t scrypt_hash_digest[SCRYPT_HASH_DIGEST_SIZE]; 14 | 15 | typedef struct scrypt_hash_state_t { 16 | uint64_t state[SCRYPT_KECCAK_F / 64]; 17 | uint32_t leftover; 18 | uint8_t buffer[SCRYPT_HASH_BLOCK_SIZE]; 19 | } scrypt_hash_state; 20 | 21 | static const uint64_t keccak_round_constants[24] = { 22 | 0x0000000000000001ull, 0x0000000000008082ull, 23 | 0x800000000000808aull, 0x8000000080008000ull, 24 | 0x000000000000808bull, 0x0000000080000001ull, 25 | 0x8000000080008081ull, 0x8000000000008009ull, 26 | 0x000000000000008aull, 0x0000000000000088ull, 27 | 0x0000000080008009ull, 0x000000008000000aull, 28 | 0x000000008000808bull, 0x800000000000008bull, 29 | 0x8000000000008089ull, 0x8000000000008003ull, 30 | 0x8000000000008002ull, 0x8000000000000080ull, 31 | 0x000000000000800aull, 0x800000008000000aull, 32 | 0x8000000080008081ull, 0x8000000000008080ull, 33 | 0x0000000080000001ull, 0x8000000080008008ull 34 | }; 35 | 36 | static void 37 | keccak_block(scrypt_hash_state *S, const uint8_t *in) { 38 | size_t i; 39 | uint64_t *s = S->state, t[5], u[5], v, w; 40 | 41 | /* absorb input */ 42 | for (i = 0; i < SCRYPT_HASH_BLOCK_SIZE / 8; i++, in += 8) 43 | s[i] ^= U8TO64_LE(in); 44 | 45 | for (i = 0; i < 24; i++) { 46 | /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */ 47 | t[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20]; 48 | t[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21]; 49 | t[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22]; 50 | t[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23]; 51 | t[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24]; 52 | 53 | /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */ 54 | u[0] = t[4] ^ ROTL64(t[1], 1); 55 | u[1] = t[0] ^ ROTL64(t[2], 1); 56 | u[2] = t[1] ^ ROTL64(t[3], 1); 57 | u[3] = t[2] ^ ROTL64(t[4], 1); 58 | u[4] = t[3] ^ ROTL64(t[0], 1); 59 | 60 | /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */ 61 | s[0] ^= u[0]; s[5] ^= u[0]; s[10] ^= u[0]; s[15] ^= u[0]; s[20] ^= u[0]; 62 | s[1] ^= u[1]; s[6] ^= u[1]; s[11] ^= u[1]; s[16] ^= u[1]; s[21] ^= u[1]; 63 | s[2] ^= u[2]; s[7] ^= u[2]; s[12] ^= u[2]; s[17] ^= u[2]; s[22] ^= u[2]; 64 | s[3] ^= u[3]; s[8] ^= u[3]; s[13] ^= u[3]; s[18] ^= u[3]; s[23] ^= u[3]; 65 | s[4] ^= u[4]; s[9] ^= u[4]; s[14] ^= u[4]; s[19] ^= u[4]; s[24] ^= u[4]; 66 | 67 | /* rho pi: b[..] = rotl(a[..], ..) */ 68 | v = s[ 1]; 69 | s[ 1] = ROTL64(s[ 6], 44); 70 | s[ 6] = ROTL64(s[ 9], 20); 71 | s[ 9] = ROTL64(s[22], 61); 72 | s[22] = ROTL64(s[14], 39); 73 | s[14] = ROTL64(s[20], 18); 74 | s[20] = ROTL64(s[ 2], 62); 75 | s[ 2] = ROTL64(s[12], 43); 76 | s[12] = ROTL64(s[13], 25); 77 | s[13] = ROTL64(s[19], 8); 78 | s[19] = ROTL64(s[23], 56); 79 | s[23] = ROTL64(s[15], 41); 80 | s[15] = ROTL64(s[ 4], 27); 81 | s[ 4] = ROTL64(s[24], 14); 82 | s[24] = ROTL64(s[21], 2); 83 | s[21] = ROTL64(s[ 8], 55); 84 | s[ 8] = ROTL64(s[16], 45); 85 | s[16] = ROTL64(s[ 5], 36); 86 | s[ 5] = ROTL64(s[ 3], 28); 87 | s[ 3] = ROTL64(s[18], 21); 88 | s[18] = ROTL64(s[17], 15); 89 | s[17] = ROTL64(s[11], 10); 90 | s[11] = ROTL64(s[ 7], 6); 91 | s[ 7] = ROTL64(s[10], 3); 92 | s[10] = ROTL64( v, 1); 93 | 94 | /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */ 95 | v = s[ 0]; w = s[ 1]; s[ 0] ^= (~w) & s[ 2]; s[ 1] ^= (~s[ 2]) & s[ 3]; s[ 2] ^= (~s[ 3]) & s[ 4]; s[ 3] ^= (~s[ 4]) & v; s[ 4] ^= (~v) & w; 96 | v = s[ 5]; w = s[ 6]; s[ 5] ^= (~w) & s[ 7]; s[ 6] ^= (~s[ 7]) & s[ 8]; s[ 7] ^= (~s[ 8]) & s[ 9]; s[ 8] ^= (~s[ 9]) & v; s[ 9] ^= (~v) & w; 97 | v = s[10]; w = s[11]; s[10] ^= (~w) & s[12]; s[11] ^= (~s[12]) & s[13]; s[12] ^= (~s[13]) & s[14]; s[13] ^= (~s[14]) & v; s[14] ^= (~v) & w; 98 | v = s[15]; w = s[16]; s[15] ^= (~w) & s[17]; s[16] ^= (~s[17]) & s[18]; s[17] ^= (~s[18]) & s[19]; s[18] ^= (~s[19]) & v; s[19] ^= (~v) & w; 99 | v = s[20]; w = s[21]; s[20] ^= (~w) & s[22]; s[21] ^= (~s[22]) & s[23]; s[22] ^= (~s[23]) & s[24]; s[23] ^= (~s[24]) & v; s[24] ^= (~v) & w; 100 | 101 | /* iota: a[0,0] ^= round constant */ 102 | s[0] ^= keccak_round_constants[i]; 103 | } 104 | } 105 | 106 | static void 107 | scrypt_hash_init(scrypt_hash_state *S) { 108 | memset(S, 0, sizeof(*S)); 109 | } 110 | 111 | static void 112 | scrypt_hash_update(scrypt_hash_state *S, const uint8_t *in, size_t inlen) { 113 | size_t want; 114 | 115 | /* handle the previous data */ 116 | if (S->leftover) { 117 | want = (SCRYPT_HASH_BLOCK_SIZE - S->leftover); 118 | want = (want < inlen) ? want : inlen; 119 | memcpy(S->buffer + S->leftover, in, want); 120 | S->leftover += (uint32_t)want; 121 | if (S->leftover < SCRYPT_HASH_BLOCK_SIZE) 122 | return; 123 | in += want; 124 | inlen -= want; 125 | keccak_block(S, S->buffer); 126 | } 127 | 128 | /* handle the current data */ 129 | while (inlen >= SCRYPT_HASH_BLOCK_SIZE) { 130 | keccak_block(S, in); 131 | in += SCRYPT_HASH_BLOCK_SIZE; 132 | inlen -= SCRYPT_HASH_BLOCK_SIZE; 133 | } 134 | 135 | /* handle leftover data */ 136 | S->leftover = (uint32_t)inlen; 137 | if (S->leftover) 138 | memcpy(S->buffer, in, S->leftover); 139 | } 140 | 141 | static void 142 | scrypt_hash_finish(scrypt_hash_state *S, uint8_t *hash) { 143 | size_t i; 144 | 145 | S->buffer[S->leftover] = 0x01; 146 | memset(S->buffer + (S->leftover + 1), 0, SCRYPT_HASH_BLOCK_SIZE - (S->leftover + 1)); 147 | S->buffer[SCRYPT_HASH_BLOCK_SIZE - 1] |= 0x80; 148 | keccak_block(S, S->buffer); 149 | 150 | for (i = 0; i < SCRYPT_HASH_DIGEST_SIZE; i += 8) { 151 | U64TO8_LE(&hash[i], S->state[i / 8]); 152 | } 153 | } 154 | 155 | #if defined(SCRYPT_KECCAK256) 156 | static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = { 157 | 0x26,0xb7,0x10,0xb3,0x66,0xb1,0xd1,0xb1,0x25,0xfc,0x3e,0xe3,0x1e,0x33,0x1d,0x19, 158 | 0x94,0xaa,0x63,0x7a,0xd5,0x77,0x29,0xb4,0x27,0xe9,0xe0,0xf4,0x19,0xba,0x68,0xea, 159 | }; 160 | #else 161 | static const uint8_t scrypt_test_hash_expected[SCRYPT_HASH_DIGEST_SIZE] = { 162 | 0x17,0xc7,0x8c,0xa0,0xd9,0x08,0x1d,0xba,0x8a,0xc8,0x3e,0x07,0x90,0xda,0x91,0x88, 163 | 0x25,0xbd,0xd3,0xf8,0x78,0x4a,0x8d,0x5e,0xe4,0x96,0x9c,0x01,0xf3,0xeb,0xdc,0x12, 164 | 0xea,0x35,0x57,0xba,0x94,0xb8,0xe9,0xb9,0x27,0x45,0x0a,0x48,0x5c,0x3d,0x69,0xf0, 165 | 0xdb,0x22,0x38,0xb5,0x52,0x22,0x29,0xea,0x7a,0xb2,0xe6,0x07,0xaa,0x37,0x4d,0xe6, 166 | }; 167 | #endif 168 | 169 | -------------------------------------------------------------------------------- /crypto/oaes_lib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * --------------------------------------------------------------------------- 3 | * OpenAES License 4 | * --------------------------------------------------------------------------- 5 | * Copyright (c) 2012, Nabil S. Al Ramli, www.nalramli.com 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * - Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * - Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | * --------------------------------------------------------------------------- 29 | */ 30 | 31 | #ifndef _OAES_LIB_H 32 | #define _OAES_LIB_H 33 | 34 | #include 35 | #include 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | #ifdef _WIN32 42 | # ifdef OAES_SHARED 43 | # ifdef oaes_lib_EXPORTS 44 | # define OAES_API __declspec(dllexport) 45 | # else 46 | # define OAES_API __declspec(dllimport) 47 | # endif 48 | # else 49 | # define OAES_API 50 | # endif 51 | #else 52 | # define OAES_API 53 | #endif // WIN32 54 | 55 | #define OAES_VERSION "0.8.1" 56 | #define OAES_BLOCK_SIZE 16 57 | 58 | typedef void OAES_CTX; 59 | 60 | typedef enum 61 | { 62 | OAES_RET_FIRST = 0, 63 | OAES_RET_SUCCESS = 0, 64 | OAES_RET_UNKNOWN, 65 | OAES_RET_ARG1, 66 | OAES_RET_ARG2, 67 | OAES_RET_ARG3, 68 | OAES_RET_ARG4, 69 | OAES_RET_ARG5, 70 | OAES_RET_NOKEY, 71 | OAES_RET_MEM, 72 | OAES_RET_BUF, 73 | OAES_RET_HEADER, 74 | OAES_RET_COUNT 75 | } OAES_RET; 76 | 77 | /* 78 | * oaes_set_option() takes one of these values for its [option] parameter 79 | * some options accept either an optional or a required [value] parameter 80 | */ 81 | // no option 82 | #define OAES_OPTION_NONE 0 83 | // enable ECB mode, disable CBC mode 84 | #define OAES_OPTION_ECB 1 85 | // enable CBC mode, disable ECB mode 86 | // value is optional, may pass uint8_t iv[OAES_BLOCK_SIZE] to specify 87 | // the value of the initialization vector, iv 88 | #define OAES_OPTION_CBC 2 89 | 90 | #ifdef OAES_DEBUG 91 | typedef int ( * oaes_step_cb ) ( 92 | const uint8_t state[OAES_BLOCK_SIZE], 93 | const char * step_name, 94 | int step_count, 95 | void * user_data ); 96 | // enable state stepping mode 97 | // value is required, must pass oaes_step_cb to receive the state at each step 98 | #define OAES_OPTION_STEP_ON 4 99 | // disable state stepping mode 100 | #define OAES_OPTION_STEP_OFF 8 101 | #endif // OAES_DEBUG 102 | 103 | typedef uint16_t OAES_OPTION; 104 | 105 | typedef struct _oaes_key 106 | { 107 | size_t data_len; 108 | uint8_t *data; 109 | size_t exp_data_len; 110 | uint8_t *exp_data; 111 | size_t num_keys; 112 | size_t key_base; 113 | } oaes_key; 114 | 115 | typedef struct _oaes_ctx 116 | { 117 | #ifdef OAES_HAVE_ISAAC 118 | randctx * rctx; 119 | #endif // OAES_HAVE_ISAAC 120 | 121 | #ifdef OAES_DEBUG 122 | oaes_step_cb step_cb; 123 | #endif // OAES_DEBUG 124 | 125 | oaes_key * key; 126 | OAES_OPTION options; 127 | uint8_t iv[OAES_BLOCK_SIZE]; 128 | } oaes_ctx; 129 | /* 130 | * // usage: 131 | * 132 | * OAES_CTX * ctx = oaes_alloc(); 133 | * . 134 | * . 135 | * . 136 | * { 137 | * oaes_gen_key_xxx( ctx ); 138 | * { 139 | * oaes_key_export( ctx, _buf, &_buf_len ); 140 | * // or 141 | * oaes_key_export_data( ctx, _buf, &_buf_len );\ 142 | * } 143 | * } 144 | * // or 145 | * { 146 | * oaes_key_import( ctx, _buf, _buf_len ); 147 | * // or 148 | * oaes_key_import_data( ctx, _buf, _buf_len ); 149 | * } 150 | * . 151 | * . 152 | * . 153 | * oaes_encrypt( ctx, m, m_len, c, &c_len ); 154 | * . 155 | * . 156 | * . 157 | * oaes_decrypt( ctx, c, c_len, m, &m_len ); 158 | * . 159 | * . 160 | * . 161 | * oaes_free( &ctx ); 162 | */ 163 | 164 | OAES_API OAES_CTX * oaes_alloc(void); 165 | 166 | OAES_API OAES_RET oaes_free( OAES_CTX ** ctx ); 167 | 168 | OAES_API OAES_RET oaes_set_option( OAES_CTX * ctx, 169 | OAES_OPTION option, const void * value ); 170 | 171 | OAES_API OAES_RET oaes_key_gen_128( OAES_CTX * ctx ); 172 | 173 | OAES_API OAES_RET oaes_key_gen_192( OAES_CTX * ctx ); 174 | 175 | OAES_API OAES_RET oaes_key_gen_256( OAES_CTX * ctx ); 176 | 177 | // export key with header information 178 | // set data == NULL to get the required data_len 179 | OAES_API OAES_RET oaes_key_export( OAES_CTX * ctx, 180 | uint8_t * data, size_t * data_len ); 181 | 182 | // directly export the data from key 183 | // set data == NULL to get the required data_len 184 | OAES_API OAES_RET oaes_key_export_data( OAES_CTX * ctx, 185 | uint8_t * data, size_t * data_len ); 186 | 187 | // import key with header information 188 | OAES_API OAES_RET oaes_key_import( OAES_CTX * ctx, 189 | const uint8_t * data, size_t data_len ); 190 | 191 | // directly import data into key 192 | OAES_API OAES_RET oaes_key_import_data( OAES_CTX * ctx, 193 | const uint8_t * data, size_t data_len ); 194 | 195 | // set c == NULL to get the required c_len 196 | OAES_API OAES_RET oaes_encrypt( OAES_CTX * ctx, 197 | const uint8_t * m, size_t m_len, uint8_t * c, size_t * c_len ); 198 | 199 | // set m == NULL to get the required m_len 200 | OAES_API OAES_RET oaes_decrypt( OAES_CTX * ctx, 201 | const uint8_t * c, size_t c_len, uint8_t * m, size_t * m_len ); 202 | 203 | // set buf == NULL to get the required buf_len 204 | OAES_API OAES_RET oaes_sprintf( 205 | char * buf, size_t * buf_len, const uint8_t * data, size_t data_len ); 206 | 207 | OAES_API OAES_RET oaes_encryption_round( const uint8_t * key, uint8_t * c ); 208 | 209 | OAES_API OAES_RET oaes_pseudo_encrypt_ecb( OAES_CTX * ctx, uint8_t * c ); 210 | 211 | #ifdef __cplusplus 212 | } 213 | #endif 214 | 215 | #endif // _OAES_LIB_H 216 | -------------------------------------------------------------------------------- /crypto/cryptonote_core/cryptonote_basic_impl.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | 6 | #include "include_base_utils.h" 7 | using namespace epee; 8 | 9 | #include "cryptonote_basic_impl.h" 10 | #include "string_tools.h" 11 | #include "serialization/binary_utils.h" 12 | #include "serialization/vector.h" 13 | #include "cryptonote_format_utils.h" 14 | #include "cryptonote_config.h" 15 | #include "misc_language.h" 16 | #include "common/base58.h" 17 | #include "crypto/hash.h" 18 | #include "common/int-util.h" 19 | 20 | namespace cryptonote { 21 | 22 | /************************************************************************/ 23 | /* Cryptonote helper functions */ 24 | /************************************************************************/ 25 | //----------------------------------------------------------------------------------------------- 26 | size_t get_max_block_size() 27 | { 28 | return CRYPTONOTE_MAX_BLOCK_SIZE; 29 | } 30 | //----------------------------------------------------------------------------------------------- 31 | size_t get_max_tx_size() 32 | { 33 | return CRYPTONOTE_MAX_TX_SIZE; 34 | } 35 | //----------------------------------------------------------------------------------------------- 36 | bool get_block_reward(size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward) { 37 | uint64_t base_reward = (MONEY_SUPPLY - already_generated_coins) >> EMISSION_SPEED_FACTOR; 38 | 39 | //make it soft 40 | if (median_size < CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE) { 41 | median_size = CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE; 42 | } 43 | 44 | if (current_block_size <= median_size) { 45 | reward = base_reward; 46 | return true; 47 | } 48 | 49 | if(current_block_size > 2 * median_size) { 50 | LOG_PRINT_L4("Block cumulative size is too big: " << current_block_size << ", expected less than " << 2 * median_size); 51 | return false; 52 | } 53 | 54 | assert(median_size < std::numeric_limits::max()); 55 | assert(current_block_size < std::numeric_limits::max()); 56 | 57 | uint64_t product_hi; 58 | uint64_t product_lo = mul128(base_reward, current_block_size * (2 * median_size - current_block_size), &product_hi); 59 | 60 | uint64_t reward_hi; 61 | uint64_t reward_lo; 62 | div128_32(product_hi, product_lo, static_cast(median_size), &reward_hi, &reward_lo); 63 | div128_32(reward_hi, reward_lo, static_cast(median_size), &reward_hi, &reward_lo); 64 | assert(0 == reward_hi); 65 | assert(reward_lo < base_reward); 66 | 67 | reward = reward_lo; 68 | return true; 69 | } 70 | //------------------------------------------------------------------------------------ 71 | uint8_t get_account_address_checksum(const public_address_outer_blob& bl) 72 | { 73 | const unsigned char* pbuf = reinterpret_cast(&bl); 74 | uint8_t summ = 0; 75 | for(size_t i = 0; i!= sizeof(public_address_outer_blob)-1; i++) 76 | summ += pbuf[i]; 77 | 78 | return summ; 79 | } 80 | //----------------------------------------------------------------------- 81 | std::string get_account_address_as_str(const account_public_address& adr) 82 | { 83 | return tools::base58::encode_addr(CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX, t_serializable_object_to_blob(adr)); 84 | } 85 | //----------------------------------------------------------------------- 86 | bool is_coinbase(const transaction& tx) 87 | { 88 | if(tx.vin.size() != 1) 89 | return false; 90 | 91 | if(tx.vin[0].type() != typeid(txin_gen)) 92 | return false; 93 | 94 | return true; 95 | } 96 | //----------------------------------------------------------------------- 97 | bool get_account_address_from_str(account_public_address& adr, const std::string& str) 98 | { 99 | if (2 * sizeof(public_address_outer_blob) != str.size()) 100 | { 101 | blobdata data; 102 | uint64_t prefix; 103 | if (!tools::base58::decode_addr(str, prefix, data)) 104 | { 105 | LOG_PRINT_L1("Invalid address format"); 106 | return false; 107 | } 108 | 109 | if (CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX != prefix) 110 | { 111 | LOG_PRINT_L1("Wrong address prefix: " << prefix << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_BASE58_PREFIX); 112 | return false; 113 | } 114 | 115 | if (!::serialization::parse_binary(data, adr)) 116 | { 117 | LOG_PRINT_L1("Account public address keys can't be parsed"); 118 | return false; 119 | } 120 | 121 | if (!crypto::check_key(adr.m_spend_public_key) || !crypto::check_key(adr.m_view_public_key)) 122 | { 123 | LOG_PRINT_L1("Failed to validate address keys"); 124 | return false; 125 | } 126 | } 127 | else 128 | { 129 | // Old address format 130 | std::string buff; 131 | if(!string_tools::parse_hexstr_to_binbuff(str, buff)) 132 | return false; 133 | 134 | if(buff.size()!=sizeof(public_address_outer_blob)) 135 | { 136 | LOG_PRINT_L1("Wrong public address size: " << buff.size() << ", expected size: " << sizeof(public_address_outer_blob)); 137 | return false; 138 | } 139 | 140 | public_address_outer_blob blob = *reinterpret_cast(buff.data()); 141 | 142 | 143 | if(blob.m_ver > CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER) 144 | { 145 | LOG_PRINT_L1("Unknown version of public address: " << blob.m_ver << ", expected " << CRYPTONOTE_PUBLIC_ADDRESS_TEXTBLOB_VER); 146 | return false; 147 | } 148 | 149 | if(blob.check_sum != get_account_address_checksum(blob)) 150 | { 151 | LOG_PRINT_L1("Wrong public address checksum"); 152 | return false; 153 | } 154 | 155 | //we success 156 | adr = blob.m_address; 157 | } 158 | 159 | return true; 160 | } 161 | 162 | bool operator ==(const cryptonote::transaction& a, const cryptonote::transaction& b) { 163 | return cryptonote::get_transaction_hash(a) == cryptonote::get_transaction_hash(b); 164 | } 165 | 166 | bool operator ==(const cryptonote::block& a, const cryptonote::block& b) { 167 | return cryptonote::get_block_hash(a) == cryptonote::get_block_hash(b); 168 | } 169 | } 170 | 171 | //-------------------------------------------------------------------------------- 172 | bool parse_hash256(const std::string str_hash, crypto::hash& hash) 173 | { 174 | std::string buf; 175 | bool res = epee::string_tools::parse_hexstr_to_binbuff(str_hash, buf); 176 | if (!res || buf.size() != sizeof(crypto::hash)) 177 | { 178 | std::cout << "invalid hash format: <" << str_hash << '>' << std::endl; 179 | return false; 180 | } 181 | else 182 | { 183 | buf.copy(reinterpret_cast(&hash), sizeof(crypto::hash)); 184 | return true; 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /scryptjane.c: -------------------------------------------------------------------------------- 1 | /* 2 | scrypt-jane by Andrew M, https://github.com/floodyberry/scrypt-jane 3 | 4 | Public Domain or MIT License, whichever is easier 5 | */ 6 | 7 | #include 8 | 9 | #include "scryptjane.h" 10 | #include "scryptjane/scrypt-jane-portable.h" 11 | #include "scryptjane/scrypt-jane-hash.h" 12 | #include "scryptjane/scrypt-jane-romix.h" 13 | #include "scryptjane/scrypt-jane-test-vectors.h" 14 | 15 | 16 | #define scrypt_maxN 30 /* (1 << (30 + 1)) = ~2 billion */ 17 | #if (SCRYPT_BLOCK_BYTES == 64) 18 | #define scrypt_r_32kb 8 /* (1 << 8) = 256 * 2 blocks in a chunk * 64 bytes = Max of 32kb in a chunk */ 19 | #elif (SCRYPT_BLOCK_BYTES == 128) 20 | #define scrypt_r_32kb 7 /* (1 << 7) = 128 * 2 blocks in a chunk * 128 bytes = Max of 32kb in a chunk */ 21 | #elif (SCRYPT_BLOCK_BYTES == 256) 22 | #define scrypt_r_32kb 6 /* (1 << 6) = 64 * 2 blocks in a chunk * 256 bytes = Max of 32kb in a chunk */ 23 | #elif (SCRYPT_BLOCK_BYTES == 512) 24 | #define scrypt_r_32kb 5 /* (1 << 5) = 32 * 2 blocks in a chunk * 512 bytes = Max of 32kb in a chunk */ 25 | #endif 26 | #define scrypt_maxr scrypt_r_32kb /* 32kb */ 27 | #define scrypt_maxp 25 /* (1 << 25) = ~33 million */ 28 | 29 | #include 30 | #include 31 | 32 | static void 33 | scrypt_fatal_error_default(const char *msg) { 34 | fprintf(stderr, "%s\n", msg); 35 | exit(1); 36 | } 37 | 38 | static scrypt_fatal_errorfn scrypt_fatal_error = scrypt_fatal_error_default; 39 | 40 | void 41 | scrypt_set_fatal_error_default(scrypt_fatal_errorfn fn) { 42 | scrypt_fatal_error = fn; 43 | } 44 | 45 | static int 46 | scrypt_power_on_self_test() { 47 | const scrypt_test_setting *t; 48 | uint8_t test_digest[64]; 49 | uint32_t i; 50 | int res = 7, scrypt_valid; 51 | 52 | if (!scrypt_test_mix()) { 53 | #if !defined(SCRYPT_TEST) 54 | scrypt_fatal_error("scrypt: mix function power-on-self-test failed"); 55 | #endif 56 | res &= ~1; 57 | } 58 | 59 | if (!scrypt_test_hash()) { 60 | #if !defined(SCRYPT_TEST) 61 | scrypt_fatal_error("scrypt: hash function power-on-self-test failed"); 62 | #endif 63 | res &= ~2; 64 | } 65 | 66 | for (i = 0, scrypt_valid = 1; post_settings[i].pw; i++) { 67 | t = post_settings + i; 68 | scrypt((uint8_t *)t->pw, strlen(t->pw), (uint8_t *)t->salt, strlen(t->salt), t->Nfactor, t->rfactor, t->pfactor, test_digest, sizeof(test_digest)); 69 | scrypt_valid &= scrypt_verify(post_vectors[i], test_digest, sizeof(test_digest)); 70 | } 71 | 72 | if (!scrypt_valid) { 73 | #if !defined(SCRYPT_TEST) 74 | scrypt_fatal_error("scrypt: scrypt power-on-self-test failed"); 75 | #endif 76 | res &= ~4; 77 | } 78 | 79 | return res; 80 | } 81 | 82 | typedef struct scrypt_aligned_alloc_t { 83 | uint8_t *mem, *ptr; 84 | } scrypt_aligned_alloc; 85 | 86 | #if defined(SCRYPT_TEST_SPEED) 87 | static uint8_t *mem_base = (uint8_t *)0; 88 | static size_t mem_bump = 0; 89 | 90 | /* allocations are assumed to be multiples of 64 bytes and total allocations not to exceed ~1.01gb */ 91 | static scrypt_aligned_alloc 92 | scrypt_alloc(uint64_t size) { 93 | scrypt_aligned_alloc aa; 94 | if (!mem_base) { 95 | mem_base = (uint8_t *)malloc((1024 * 1024 * 1024) + (1024 * 1024) + (SCRYPT_BLOCK_BYTES - 1)); 96 | if (!mem_base) 97 | scrypt_fatal_error("scrypt: out of memory"); 98 | mem_base = (uint8_t *)(((size_t)mem_base + (SCRYPT_BLOCK_BYTES - 1)) & ~(SCRYPT_BLOCK_BYTES - 1)); 99 | } 100 | aa.mem = mem_base + mem_bump; 101 | aa.ptr = aa.mem; 102 | mem_bump += (size_t)size; 103 | return aa; 104 | } 105 | 106 | static void 107 | scrypt_free(scrypt_aligned_alloc *aa) { 108 | mem_bump = 0; 109 | } 110 | #else 111 | static scrypt_aligned_alloc 112 | scrypt_alloc(uint64_t size) { 113 | static const size_t max_alloc = (size_t)-1; 114 | scrypt_aligned_alloc aa; 115 | size += (SCRYPT_BLOCK_BYTES - 1); 116 | if (size > max_alloc) 117 | scrypt_fatal_error("scrypt: not enough address space on this CPU to allocate required memory"); 118 | aa.mem = (uint8_t *)malloc((size_t)size); 119 | aa.ptr = (uint8_t *)(((size_t)aa.mem + (SCRYPT_BLOCK_BYTES - 1)) & ~(SCRYPT_BLOCK_BYTES - 1)); 120 | if (!aa.mem) 121 | scrypt_fatal_error("scrypt: out of memory"); 122 | return aa; 123 | } 124 | 125 | static void 126 | scrypt_free(scrypt_aligned_alloc *aa) { 127 | free(aa->mem); 128 | } 129 | #endif 130 | 131 | 132 | void 133 | scrypt(const uint8_t *password, size_t password_len, const uint8_t *salt, size_t salt_len, uint8_t Nfactor, uint8_t rfactor, uint8_t pfactor, uint8_t *out, size_t bytes) { 134 | scrypt_aligned_alloc YX, V; 135 | uint8_t *X, *Y; 136 | uint32_t N, r, p, chunk_bytes, i; 137 | 138 | #if !defined(SCRYPT_CHOOSE_COMPILETIME) 139 | scrypt_ROMixfn scrypt_ROMix = scrypt_getROMix(); 140 | #endif 141 | 142 | #if !defined(SCRYPT_TEST) 143 | static int power_on_self_test = 0; 144 | if (!power_on_self_test) { 145 | power_on_self_test = 1; 146 | if (!scrypt_power_on_self_test()) 147 | scrypt_fatal_error("scrypt: power on self test failed"); 148 | } 149 | #endif 150 | 151 | if (Nfactor > scrypt_maxN) 152 | scrypt_fatal_error("scrypt: N out of range"); 153 | if (rfactor > scrypt_maxr) 154 | scrypt_fatal_error("scrypt: r out of range"); 155 | if (pfactor > scrypt_maxp) 156 | scrypt_fatal_error("scrypt: p out of range"); 157 | 158 | N = (1 << (Nfactor + 1)); 159 | r = (1 << rfactor); 160 | p = (1 << pfactor); 161 | 162 | chunk_bytes = SCRYPT_BLOCK_BYTES * r * 2; 163 | V = scrypt_alloc((uint64_t)N * chunk_bytes); 164 | YX = scrypt_alloc((p + 1) * chunk_bytes); 165 | 166 | /* 1: X = PBKDF2(password, salt) */ 167 | Y = YX.ptr; 168 | X = Y + chunk_bytes; 169 | scrypt_pbkdf2(password, password_len, salt, salt_len, 1, X, chunk_bytes * p); 170 | 171 | /* 2: X = ROMix(X) */ 172 | for (i = 0; i < p; i++) 173 | scrypt_ROMix((scrypt_mix_word_t *)(X + (chunk_bytes * i)), (scrypt_mix_word_t *)Y, (scrypt_mix_word_t *)V.ptr, N, r); 174 | 175 | /* 3: Out = PBKDF2(password, X) */ 176 | scrypt_pbkdf2(password, password_len, X, chunk_bytes * p, 1, out, bytes); 177 | 178 | scrypt_ensure_zero(YX.ptr, (p + 1) * chunk_bytes); 179 | 180 | scrypt_free(&V); 181 | scrypt_free(&YX); 182 | } 183 | 184 | #define max(a,b) (((a) > (b)) ? (a) : (b)) 185 | #define min(a,b) (((a) < (b)) ? (a) : (b)) 186 | unsigned char GetNfactorJane(int nTimestamp, int nChainStartTime, int nMin, int nMax) { 187 | 188 | const unsigned char minNfactor = nMin;//4; 189 | const unsigned char maxNfactor = nMax;//30; 190 | 191 | int l = 0, s, n; 192 | unsigned char N; 193 | 194 | if (nTimestamp <= nChainStartTime) 195 | return 4; 196 | 197 | s = nTimestamp - nChainStartTime; 198 | while ((s >> 1) > 3) { 199 | l += 1; 200 | s >>= 1; 201 | } 202 | 203 | s &= 3; 204 | 205 | n = (l * 170 + s * 25 - 2320) / 100; 206 | 207 | if (n < 0) n = 0; 208 | 209 | if (n > 255) 210 | printf("GetNfactor(%d) - something wrong(n == %d)\n", nTimestamp, n); 211 | 212 | N = (unsigned char)n; 213 | //printf("GetNfactor: %d -> %d %d : %d / %d\n", nTimestamp - nChainStartTime, l, s, n, min(max(N, minNfactor), maxNfactor)); 214 | 215 | return min(max(N, minNfactor), maxNfactor); 216 | } 217 | 218 | void scryptjane_hash(const void* input, size_t inputlen, uint32_t *res, unsigned char Nfactor) 219 | { 220 | return scrypt((const unsigned char*)input, inputlen, 221 | (const unsigned char*)input, inputlen, 222 | Nfactor, 0, 0, (unsigned char*)res, 32); 223 | } 224 | -------------------------------------------------------------------------------- /sha3/sph_whirlpool.h: -------------------------------------------------------------------------------- 1 | /* $Id: sph_whirlpool.h 216 2010-06-08 09:46:57Z tp $ */ 2 | /** 3 | * WHIRLPOOL interface. 4 | * 5 | * WHIRLPOOL knows three variants, dubbed "WHIRLPOOL-0" (original 6 | * version, published in 2000, studied by NESSIE), "WHIRLPOOL-1" 7 | * (first revision, 2001, with a new S-box) and "WHIRLPOOL" (current 8 | * version, 2003, with a new diffusion matrix, also described as "plain 9 | * WHIRLPOOL"). All three variants are implemented here. 10 | * 11 | * The original WHIRLPOOL (i.e. WHIRLPOOL-0) was published in: P. S. L. 12 | * M. Barreto, V. Rijmen, "The Whirlpool Hashing Function", First open 13 | * NESSIE Workshop, Leuven, Belgium, November 13--14, 2000. 14 | * 15 | * The current WHIRLPOOL specification and a reference implementation 16 | * can be found on the WHIRLPOOL web page: 17 | * http://paginas.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html 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_whirlpool.h 45 | * @author Thomas Pornin 46 | */ 47 | 48 | #ifndef SPH_WHIRLPOOL_H__ 49 | #define SPH_WHIRLPOOL_H__ 50 | 51 | #include 52 | #include "sph_types.h" 53 | 54 | #if SPH_64 55 | 56 | /** 57 | * Output size (in bits) for WHIRLPOOL. 58 | */ 59 | #define SPH_SIZE_whirlpool 512 60 | 61 | /** 62 | * Output size (in bits) for WHIRLPOOL-0. 63 | */ 64 | #define SPH_SIZE_whirlpool0 512 65 | 66 | /** 67 | * Output size (in bits) for WHIRLPOOL-1. 68 | */ 69 | #define SPH_SIZE_whirlpool1 512 70 | 71 | /** 72 | * This structure is a context for WHIRLPOOL computations: it contains the 73 | * intermediate values and some data from the last entered block. Once 74 | * a WHIRLPOOL computation has been performed, the context can be reused for 75 | * another computation. 76 | * 77 | * The contents of this structure are private. A running WHIRLPOOL computation 78 | * can be cloned by copying the context (e.g. with a simple 79 | * memcpy()). 80 | */ 81 | typedef struct { 82 | #ifndef DOXYGEN_IGNORE 83 | unsigned char buf[64]; /* first field, for alignment */ 84 | sph_u64 state[8]; 85 | #if SPH_64 86 | sph_u64 count; 87 | #else 88 | sph_u32 count_high, count_low; 89 | #endif 90 | #endif 91 | } sph_whirlpool_context; 92 | 93 | /** 94 | * Initialize a WHIRLPOOL context. This process performs no memory allocation. 95 | * 96 | * @param cc the WHIRLPOOL context (pointer to a 97 | * sph_whirlpool_context) 98 | */ 99 | void sph_whirlpool_init(void *cc); 100 | 101 | /** 102 | * Process some data bytes. It is acceptable that len is zero 103 | * (in which case this function does nothing). This function applies the 104 | * plain WHIRLPOOL algorithm. 105 | * 106 | * @param cc the WHIRLPOOL context 107 | * @param data the input data 108 | * @param len the input data length (in bytes) 109 | */ 110 | void sph_whirlpool(void *cc, const void *data, size_t len); 111 | 112 | /** 113 | * Terminate the current WHIRLPOOL computation and output the result into the 114 | * provided buffer. The destination buffer must be wide enough to 115 | * accomodate the result (64 bytes). The context is automatically 116 | * reinitialized. 117 | * 118 | * @param cc the WHIRLPOOL context 119 | * @param dst the destination buffer 120 | */ 121 | void sph_whirlpool_close(void *cc, void *dst); 122 | 123 | /** 124 | * WHIRLPOOL-0 uses the same structure than plain WHIRLPOOL. 125 | */ 126 | typedef sph_whirlpool_context sph_whirlpool0_context; 127 | 128 | #ifdef DOXYGEN_IGNORE 129 | /** 130 | * Initialize a WHIRLPOOL-0 context. This function is identical to 131 | * sph_whirlpool_init(). 132 | * 133 | * @param cc the WHIRLPOOL context (pointer to a 134 | * sph_whirlpool0_context) 135 | */ 136 | void sph_whirlpool0_init(void *cc); 137 | #endif 138 | 139 | #ifndef DOXYGEN_IGNORE 140 | #define sph_whirlpool0_init sph_whirlpool_init 141 | #endif 142 | 143 | /** 144 | * Process some data bytes. It is acceptable that len is zero 145 | * (in which case this function does nothing). This function applies the 146 | * WHIRLPOOL-0 algorithm. 147 | * 148 | * @param cc the WHIRLPOOL context 149 | * @param data the input data 150 | * @param len the input data length (in bytes) 151 | */ 152 | void sph_whirlpool0(void *cc, const void *data, size_t len); 153 | 154 | /** 155 | * Terminate the current WHIRLPOOL-0 computation and output the result into the 156 | * provided buffer. The destination buffer must be wide enough to 157 | * accomodate the result (64 bytes). The context is automatically 158 | * reinitialized. 159 | * 160 | * @param cc the WHIRLPOOL-0 context 161 | * @param dst the destination buffer 162 | */ 163 | void sph_whirlpool0_close(void *cc, void *dst); 164 | 165 | /** 166 | * WHIRLPOOL-1 uses the same structure than plain WHIRLPOOL. 167 | */ 168 | typedef sph_whirlpool_context sph_whirlpool1_context; 169 | 170 | #ifdef DOXYGEN_IGNORE 171 | /** 172 | * Initialize a WHIRLPOOL-1 context. This function is identical to 173 | * sph_whirlpool_init(). 174 | * 175 | * @param cc the WHIRLPOOL context (pointer to a 176 | * sph_whirlpool1_context) 177 | */ 178 | void sph_whirlpool1_init(void *cc); 179 | #endif 180 | 181 | #ifndef DOXYGEN_IGNORE 182 | #define sph_whirlpool1_init sph_whirlpool_init 183 | #endif 184 | 185 | /** 186 | * Process some data bytes. It is acceptable that len is zero 187 | * (in which case this function does nothing). This function applies the 188 | * WHIRLPOOL-1 algorithm. 189 | * 190 | * @param cc the WHIRLPOOL context 191 | * @param data the input data 192 | * @param len the input data length (in bytes) 193 | */ 194 | void sph_whirlpool1(void *cc, const void *data, size_t len); 195 | 196 | /** 197 | * Terminate the current WHIRLPOOL-1 computation and output the result into the 198 | * provided buffer. The destination buffer must be wide enough to 199 | * accomodate the result (64 bytes). The context is automatically 200 | * reinitialized. 201 | * 202 | * @param cc the WHIRLPOOL-1 context 203 | * @param dst the destination buffer 204 | */ 205 | void sph_whirlpool1_close(void *cc, void *dst); 206 | 207 | #endif 208 | 209 | #endif -------------------------------------------------------------------------------- /crypto/skein_port.h: -------------------------------------------------------------------------------- 1 | #ifndef _SKEIN_PORT_H_ 2 | #define _SKEIN_PORT_H_ 3 | 4 | #include 5 | #include 6 | 7 | #ifndef RETURN_VALUES 8 | # define RETURN_VALUES 9 | # if defined( DLL_EXPORT ) 10 | # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) 11 | # define VOID_RETURN __declspec( dllexport ) void __stdcall 12 | # define INT_RETURN __declspec( dllexport ) int __stdcall 13 | # elif defined( __GNUC__ ) 14 | # define VOID_RETURN __declspec( __dllexport__ ) void 15 | # define INT_RETURN __declspec( __dllexport__ ) int 16 | # else 17 | # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 18 | # endif 19 | # elif defined( DLL_IMPORT ) 20 | # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER ) 21 | # define VOID_RETURN __declspec( dllimport ) void __stdcall 22 | # define INT_RETURN __declspec( dllimport ) int __stdcall 23 | # elif defined( __GNUC__ ) 24 | # define VOID_RETURN __declspec( __dllimport__ ) void 25 | # define INT_RETURN __declspec( __dllimport__ ) int 26 | # else 27 | # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers 28 | # endif 29 | # elif defined( __WATCOMC__ ) 30 | # define VOID_RETURN void __cdecl 31 | # define INT_RETURN int __cdecl 32 | # else 33 | # define VOID_RETURN void 34 | # define INT_RETURN int 35 | # endif 36 | #endif 37 | 38 | /* These defines are used to declare buffers in a way that allows 39 | faster operations on longer variables to be used. In all these 40 | defines 'size' must be a power of 2 and >= 8 41 | 42 | dec_unit_type(size,x) declares a variable 'x' of length 43 | 'size' bits 44 | 45 | dec_bufr_type(size,bsize,x) declares a buffer 'x' of length 'bsize' 46 | bytes defined as an array of variables 47 | each of 'size' bits (bsize must be a 48 | multiple of size / 8) 49 | 50 | ptr_cast(x,size) casts a pointer to a pointer to a 51 | varaiable of length 'size' bits 52 | */ 53 | 54 | #define ui_type(size) uint##size##_t 55 | #define dec_unit_type(size,x) typedef ui_type(size) x 56 | #define dec_bufr_type(size,bsize,x) typedef ui_type(size) x[bsize / (size >> 3)] 57 | #define ptr_cast(x,size) ((ui_type(size)*)(x)) 58 | 59 | typedef unsigned int uint_t; /* native unsigned integer */ 60 | typedef uint8_t u08b_t; /* 8-bit unsigned integer */ 61 | typedef uint64_t u64b_t; /* 64-bit unsigned integer */ 62 | 63 | #ifndef RotL_64 64 | #define RotL_64(x,N) (((x) << (N)) | ((x) >> (64-(N)))) 65 | #endif 66 | 67 | /* 68 | * Skein is "natively" little-endian (unlike SHA-xxx), for optimal 69 | * performance on x86 CPUs. The Skein code requires the following 70 | * definitions for dealing with endianness: 71 | * 72 | * SKEIN_NEED_SWAP: 0 for little-endian, 1 for big-endian 73 | * Skein_Put64_LSB_First 74 | * Skein_Get64_LSB_First 75 | * Skein_Swap64 76 | * 77 | * If SKEIN_NEED_SWAP is defined at compile time, it is used here 78 | * along with the portable versions of Put64/Get64/Swap64, which 79 | * are slow in general. 80 | * 81 | * Otherwise, an "auto-detect" of endianness is attempted below. 82 | * If the default handling doesn't work well, the user may insert 83 | * platform-specific code instead (e.g., for big-endian CPUs). 84 | * 85 | */ 86 | #ifndef SKEIN_NEED_SWAP /* compile-time "override" for endianness? */ 87 | 88 | 89 | #include "int-util.h" 90 | 91 | #define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ 92 | #define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ 93 | 94 | #if BYTE_ORDER == LITTLE_ENDIAN 95 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN 96 | #endif 97 | 98 | #if BYTE_ORDER == BIG_ENDIAN 99 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN 100 | #endif 101 | 102 | /* special handler for IA64, which may be either endianness (?) */ 103 | /* here we assume little-endian, but this may need to be changed */ 104 | #if defined(__ia64) || defined(__ia64__) || defined(_M_IA64) 105 | # define PLATFORM_MUST_ALIGN (1) 106 | #ifndef PLATFORM_BYTE_ORDER 107 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN 108 | #endif 109 | #endif 110 | 111 | #ifndef PLATFORM_MUST_ALIGN 112 | # define PLATFORM_MUST_ALIGN (0) 113 | #endif 114 | 115 | 116 | #if PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN 117 | /* here for big-endian CPUs */ 118 | #define SKEIN_NEED_SWAP (1) 119 | #elif PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN 120 | /* here for x86 and x86-64 CPUs (and other detected little-endian CPUs) */ 121 | #define SKEIN_NEED_SWAP (0) 122 | #if PLATFORM_MUST_ALIGN == 0 /* ok to use "fast" versions? */ 123 | #define Skein_Put64_LSB_First(dst08,src64,bCnt) memcpy(dst08,src64,bCnt) 124 | #define Skein_Get64_LSB_First(dst64,src08,wCnt) memcpy(dst64,src08,8*(wCnt)) 125 | #endif 126 | #else 127 | #error "Skein needs endianness setting!" 128 | #endif 129 | 130 | #endif /* ifndef SKEIN_NEED_SWAP */ 131 | 132 | /* 133 | ****************************************************************** 134 | * Provide any definitions still needed. 135 | ****************************************************************** 136 | */ 137 | #ifndef Skein_Swap64 /* swap for big-endian, nop for little-endian */ 138 | #if SKEIN_NEED_SWAP 139 | #define Skein_Swap64(w64) \ 140 | ( (( ((u64b_t)(w64)) & 0xFF) << 56) | \ 141 | (((((u64b_t)(w64)) >> 8) & 0xFF) << 48) | \ 142 | (((((u64b_t)(w64)) >>16) & 0xFF) << 40) | \ 143 | (((((u64b_t)(w64)) >>24) & 0xFF) << 32) | \ 144 | (((((u64b_t)(w64)) >>32) & 0xFF) << 24) | \ 145 | (((((u64b_t)(w64)) >>40) & 0xFF) << 16) | \ 146 | (((((u64b_t)(w64)) >>48) & 0xFF) << 8) | \ 147 | (((((u64b_t)(w64)) >>56) & 0xFF) ) ) 148 | #else 149 | #define Skein_Swap64(w64) (w64) 150 | #endif 151 | #endif /* ifndef Skein_Swap64 */ 152 | 153 | 154 | #ifndef Skein_Put64_LSB_First 155 | void Skein_Put64_LSB_First(u08b_t *dst,const u64b_t *src,size_t bCnt) 156 | #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */ 157 | { /* this version is fully portable (big-endian or little-endian), but slow */ 158 | size_t n; 159 | 160 | for (n=0;n>3] >> (8*(n&7))); 162 | } 163 | #else 164 | ; /* output only the function prototype */ 165 | #endif 166 | #endif /* ifndef Skein_Put64_LSB_First */ 167 | 168 | 169 | #ifndef Skein_Get64_LSB_First 170 | void Skein_Get64_LSB_First(u64b_t *dst,const u08b_t *src,size_t wCnt) 171 | #ifdef SKEIN_PORT_CODE /* instantiate the function code here? */ 172 | { /* this version is fully portable (big-endian or little-endian), but slow */ 173 | size_t n; 174 | 175 | for (n=0;n<8*wCnt;n+=8) 176 | dst[n/8] = (((u64b_t) src[n ]) ) + 177 | (((u64b_t) src[n+1]) << 8) + 178 | (((u64b_t) src[n+2]) << 16) + 179 | (((u64b_t) src[n+3]) << 24) + 180 | (((u64b_t) src[n+4]) << 32) + 181 | (((u64b_t) src[n+5]) << 40) + 182 | (((u64b_t) src[n+6]) << 48) + 183 | (((u64b_t) src[n+7]) << 56) ; 184 | } 185 | #else 186 | ; /* output only the function prototype */ 187 | #endif 188 | #endif /* ifndef Skein_Get64_LSB_First */ 189 | 190 | #endif /* ifndef _SKEIN_PORT_H_ */ 191 | -------------------------------------------------------------------------------- /dcrypt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define false 0 6 | #define true 1 7 | #define uint32_t unsigned int 8 | 9 | #define Ch(x, y, z) ((x & (y ^ z)) ^ z) 10 | #define Maj(x, y, z) ((x & (y | z)) | (y & z)) 11 | #define ROTR(x, n) ((x >> n) | (x << (32 - n))) 12 | #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) 13 | #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) 14 | #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3)) 15 | #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10)) 16 | 17 | const char hexmap[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; 18 | const uint32_t K[64] = { 19 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 20 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 21 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 22 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 23 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 24 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 25 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 26 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 27 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 28 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 29 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 30 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 31 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 32 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 33 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 34 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 35 | }; 36 | 37 | void sha256(unsigned char * instr, unsigned char * hash, unsigned int len) 38 | { 39 | 40 | uint32_t H[8] = { 41 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 42 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 43 | }; 44 | uint32_t W[64] = {0}; 45 | unsigned int T1, T2, t; 46 | unsigned int a, b, c, d, e, f, g, h, i; 47 | unsigned int curBlock = 0; 48 | 49 | unsigned int l = (len + 1) / 4 + 2; 50 | unsigned int N = (l + 15) / 16; 51 | instr[len] = 128; // Hash Terminator 52 | for (t = len + 1; t < N * 64; t++) instr[t] = 0; 53 | 54 | for (curBlock = 0; curBlock < N; curBlock ++) { 55 | for (t = 0; t < 16; t++) W[t] = (instr[curBlock * 64 + t * 4 + 0] << 24) + (instr[curBlock * 64 + t * 4 + 1] << 16) + (instr[curBlock * 64 + t * 4 + 2] << 8) + instr[curBlock * 64 + t * 4 + 3]; 56 | if (curBlock == N - 1) W[15] = len * 8; 57 | for (t = 16; t < 64; t++) W[t] = s1(W[t-2]) + W[t-7] + s0(W[t-15]) + W[t-16]; 58 | 59 | a = H[0]; b = H[1]; c = H[2]; d = H[3]; e = H[4]; f = H[5]; g = H[6]; h = H[7]; 60 | for (t = 0; t < 64 ; t++) { 61 | T1 = h + S1(e) + Ch(e,f,g) + K[t] + W[t]; 62 | T2 = S0(a) + Maj(a,b,c); 63 | h = g; 64 | g = f; 65 | f = e; 66 | e = d + T1; 67 | d = c; 68 | c = b; 69 | b = a; 70 | a = T1 + T2; 71 | } 72 | 73 | H[0] += a; H[1] += b; H[2] += c; H[3] += d; H[4] += e; H[5] += f; H[6] += g; H[7] += h; 74 | } 75 | 76 | 77 | t = 0; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 78 | t = 1; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 79 | t = 2; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 80 | t = 3; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 81 | t = 4; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 82 | t = 5; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 83 | t = 6; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 84 | t = 7; i = H[t]; hash[t * 8 + 7] = i & 0xf; hash[t * 8 + 6] = (i >> 4) & 0xf; hash[t * 8 + 5] = (i >> 8) & 0xf; hash[t * 8 + 4] = (i >> 12) & 0xf; hash[t * 8 + 3] = (i >> 16) & 0xf; hash[t * 8 + 2] = (i >> 20) & 0xf; hash[t * 8 + 1] = (i >> 24) & 0xf; hash[t * 8 + 0] = (i >> 28) & 0xf; 85 | } 86 | 87 | void hexToAsc(unsigned char * tmp_list, unsigned int len) { 88 | unsigned int i; 89 | for (i = 0; i < len; i++) tmp_list[i] = hexmap[ tmp_list[i] ]; 90 | } 91 | 92 | 93 | unsigned char mix_hashed_num(unsigned char * hashedData, unsigned char * ret_list, unsigned int * ret_list_len) { 94 | unsigned char * tmp_list = malloc(128); 95 | unsigned int i, index = 0, tmp_val; 96 | for (i = 0; i < 64; i++) tmp_list[i] = 255; 97 | *ret_list_len = 0; 98 | unsigned int counter = 0; 99 | 100 | unsigned char hashed_end = false; 101 | while (hashed_end == false) { 102 | counter += 1; 103 | index += hashedData[index] + 1; 104 | 105 | if (index >= 64) { 106 | index %= 64; 107 | hexToAsc(hashedData, 64); 108 | sha256(hashedData, hashedData, 64); 109 | } 110 | 111 | tmp_val = hexmap[ hashedData[index] ]; 112 | tmp_list[64] = tmp_val; 113 | sha256(tmp_list, tmp_list, 65); 114 | hexToAsc(tmp_list, 64); 115 | 116 | if (index == 63) 117 | if (tmp_val == tmp_list[63]) 118 | hashed_end = true; 119 | 120 | for(i = 0; i < 64;i++) ret_list[*ret_list_len + i] = tmp_list[i]; 121 | *ret_list_len += 64; 122 | 123 | if (*ret_list_len > 1048576) { 124 | free(tmp_list); 125 | return false; 126 | } 127 | } 128 | free(tmp_list); return true; 129 | } 130 | 131 | 132 | void dcrypt_hash(const char * input, char * hash, uint32_t len) { 133 | unsigned char * instr = malloc(len); 134 | memcpy( instr, input, len ); 135 | unsigned char * hashed = malloc(128); 136 | unsigned char * mixedHash = malloc(1048576 + 1024); // This assumes a max length of work of 1024 bytes?; 137 | unsigned char * finalToHash; 138 | unsigned int lenMixedHash = 0; 139 | sha256(instr, hashed, len); 140 | if (mix_hashed_num(hashed, mixedHash, &lenMixedHash) == true) { 141 | finalToHash = malloc( lenMixedHash + len + 64 ); 142 | memcpy( finalToHash, mixedHash, lenMixedHash); 143 | memcpy( &(finalToHash[lenMixedHash]), instr, len); 144 | sha256(finalToHash, hash, len + lenMixedHash); 145 | free(finalToHash); 146 | } else { 147 | printf("Buffer limit exceeded.\n"); 148 | } 149 | free(mixedHash); 150 | } 151 | 152 | /* :int main(int argc, char *argv[]) 153 | { 154 | char * hash = malloc(64); 155 | int i; 156 | 157 | memset(hash, 0, 32); 158 | char instr[128] = "The quick brown fox jumps over the lazy dog"; 159 | 160 | dcrypt_hash(instr, hash, 43); 161 | 162 | printf("\nHash result\n"); 163 | for (i = 0 ; i < 64; i++) printf("%01x", hash[i]); printf("\n"); 164 | 165 | free(hash); 166 | return 0; 167 | } */ 168 | -------------------------------------------------------------------------------- /crypto/int-util.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012-2013 The Cryptonote developers 2 | // Distributed under the MIT/X11 software license, see the accompanying 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 | 5 | #pragma once 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | /* 14 | * Create GNU compatible endian macros. We use the values for __LITTLE_ENDIAN 15 | * and __BIG_ENDIAN based on endian.h. 16 | */ 17 | #ifdef __sun 18 | #include 19 | #define LITTLE_ENDIAN 1234 20 | #define BIG_ENDIAN 4321 21 | #ifdef _LITTLE_ENDIAN 22 | #define BYTE_ORDER LITTLE_ENDIAN 23 | #else 24 | #define BYTE_ORDER BIG_ENDIAN 25 | #endif /* _LITTLE_ENDIAN */ 26 | #endif /* __sun */ 27 | 28 | #if defined(_MSC_VER) 29 | #include 30 | 31 | static inline uint32_t rol32(uint32_t x, int r) { 32 | static_assert(sizeof(uint32_t) == sizeof(unsigned int), "this code assumes 32-bit integers"); 33 | return _rotl(x, r); 34 | } 35 | 36 | static inline uint64_t rol64(uint64_t x, int r) { 37 | return _rotl64(x, r); 38 | } 39 | 40 | #else 41 | 42 | static inline uint32_t rol32(uint32_t x, int r) { 43 | return (x << (r & 31)) | (x >> (-r & 31)); 44 | } 45 | 46 | static inline uint64_t rol64(uint64_t x, int r) { 47 | return (x << (r & 63)) | (x >> (-r & 63)); 48 | } 49 | 50 | #endif 51 | 52 | static inline uint64_t hi_dword(uint64_t val) { 53 | return val >> 32; 54 | } 55 | 56 | static inline uint64_t lo_dword(uint64_t val) { 57 | return val & 0xFFFFFFFF; 58 | } 59 | 60 | static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) { 61 | // multiplier = ab = a * 2^32 + b 62 | // multiplicand = cd = c * 2^32 + d 63 | // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d 64 | uint64_t a = hi_dword(multiplier); 65 | uint64_t b = lo_dword(multiplier); 66 | uint64_t c = hi_dword(multiplicand); 67 | uint64_t d = lo_dword(multiplicand); 68 | 69 | uint64_t ac = a * c; 70 | uint64_t ad = a * d; 71 | uint64_t bc = b * c; 72 | uint64_t bd = b * d; 73 | 74 | uint64_t adbc = ad + bc; 75 | uint64_t adbc_carry = adbc < ad ? 1 : 0; 76 | 77 | // multiplier * multiplicand = product_hi * 2^64 + product_lo 78 | uint64_t product_lo = bd + (adbc << 32); 79 | uint64_t product_lo_carry = product_lo < bd ? 1 : 0; 80 | *product_hi = ac + (adbc >> 32) + (adbc_carry << 32) + product_lo_carry; 81 | assert(ac <= *product_hi); 82 | 83 | return product_lo; 84 | } 85 | 86 | static inline uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) { 87 | dividend |= ((uint64_t)*remainder) << 32; 88 | *remainder = dividend % divisor; 89 | return dividend / divisor; 90 | } 91 | 92 | // Long division with 2^32 base 93 | static inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) { 94 | uint64_t dividend_dwords[4]; 95 | uint32_t remainder = 0; 96 | 97 | dividend_dwords[3] = hi_dword(dividend_hi); 98 | dividend_dwords[2] = lo_dword(dividend_hi); 99 | dividend_dwords[1] = hi_dword(dividend_lo); 100 | dividend_dwords[0] = lo_dword(dividend_lo); 101 | 102 | *quotient_hi = div_with_reminder(dividend_dwords[3], divisor, &remainder) << 32; 103 | *quotient_hi |= div_with_reminder(dividend_dwords[2], divisor, &remainder); 104 | *quotient_lo = div_with_reminder(dividend_dwords[1], divisor, &remainder) << 32; 105 | *quotient_lo |= div_with_reminder(dividend_dwords[0], divisor, &remainder); 106 | 107 | return remainder; 108 | } 109 | 110 | #define IDENT32(x) ((uint32_t) (x)) 111 | #define IDENT64(x) ((uint64_t) (x)) 112 | 113 | #define SWAP32(x) ((((uint32_t) (x) & 0x000000ff) << 24) | \ 114 | (((uint32_t) (x) & 0x0000ff00) << 8) | \ 115 | (((uint32_t) (x) & 0x00ff0000) >> 8) | \ 116 | (((uint32_t) (x) & 0xff000000) >> 24)) 117 | #define SWAP64(x) ((((uint64_t) (x) & 0x00000000000000ff) << 56) | \ 118 | (((uint64_t) (x) & 0x000000000000ff00) << 40) | \ 119 | (((uint64_t) (x) & 0x0000000000ff0000) << 24) | \ 120 | (((uint64_t) (x) & 0x00000000ff000000) << 8) | \ 121 | (((uint64_t) (x) & 0x000000ff00000000) >> 8) | \ 122 | (((uint64_t) (x) & 0x0000ff0000000000) >> 24) | \ 123 | (((uint64_t) (x) & 0x00ff000000000000) >> 40) | \ 124 | (((uint64_t) (x) & 0xff00000000000000) >> 56)) 125 | 126 | static inline uint32_t ident32(uint32_t x) { return x; } 127 | static inline uint64_t ident64(uint64_t x) { return x; } 128 | 129 | static inline uint32_t swap32(uint32_t x) { 130 | x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8); 131 | return (x << 16) | (x >> 16); 132 | } 133 | static inline uint64_t swap64(uint64_t x) { 134 | x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8); 135 | x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16); 136 | return (x << 32) | (x >> 32); 137 | } 138 | 139 | #if defined(__GNUC__) 140 | #define UNUSED __attribute__((unused)) 141 | #else 142 | #define UNUSED 143 | #endif 144 | static inline void mem_inplace_ident(void *mem UNUSED, size_t n UNUSED) { } 145 | #undef UNUSED 146 | 147 | static inline void mem_inplace_swap32(void *mem, size_t n) { 148 | size_t i; 149 | for (i = 0; i < n; i++) { 150 | ((uint32_t *) mem)[i] = swap32(((const uint32_t *) mem)[i]); 151 | } 152 | } 153 | static inline void mem_inplace_swap64(void *mem, size_t n) { 154 | size_t i; 155 | for (i = 0; i < n; i++) { 156 | ((uint64_t *) mem)[i] = swap64(((const uint64_t *) mem)[i]); 157 | } 158 | } 159 | 160 | static inline void memcpy_ident32(void *dst, const void *src, size_t n) { 161 | memcpy(dst, src, 4 * n); 162 | } 163 | static inline void memcpy_ident64(void *dst, const void *src, size_t n) { 164 | memcpy(dst, src, 8 * n); 165 | } 166 | 167 | static inline void memcpy_swap32(void *dst, const void *src, size_t n) { 168 | size_t i; 169 | for (i = 0; i < n; i++) { 170 | ((uint32_t *) dst)[i] = swap32(((const uint32_t *) src)[i]); 171 | } 172 | } 173 | static inline void memcpy_swap64(void *dst, const void *src, size_t n) { 174 | size_t i; 175 | for (i = 0; i < n; i++) { 176 | ((uint64_t *) dst)[i] = swap64(((const uint64_t *) src)[i]); 177 | } 178 | } 179 | 180 | #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) 181 | #if __STDC_VERSION__ - 0 >= 201112L 182 | static_assert(false, "BYTE_ORDER is undefined. Perhaps, GNU extensions are not enabled"); 183 | #else 184 | #error "BYTE_ORDER is undefined. Perhaps, GNU extensions are not enabled" 185 | #endif 186 | #endif 187 | 188 | #if BYTE_ORDER == LITTLE_ENDIAN 189 | #define SWAP32LE IDENT32 190 | #define SWAP32BE SWAP32 191 | #define swap32le ident32 192 | #define swap32be swap32 193 | #define mem_inplace_swap32le mem_inplace_ident 194 | #define mem_inplace_swap32be mem_inplace_swap32 195 | #define memcpy_swap32le memcpy_ident32 196 | #define memcpy_swap32be memcpy_swap32 197 | #define SWAP64LE IDENT64 198 | #define SWAP64BE SWAP64 199 | #define swap64le ident64 200 | #define swap64be swap64 201 | #define mem_inplace_swap64le mem_inplace_ident 202 | #define mem_inplace_swap64be mem_inplace_swap64 203 | #define memcpy_swap64le memcpy_ident64 204 | #define memcpy_swap64be memcpy_swap64 205 | #endif 206 | 207 | #if BYTE_ORDER == BIG_ENDIAN 208 | #define SWAP32BE IDENT32 209 | #define SWAP32LE SWAP32 210 | #define swap32be ident32 211 | #define swap32le swap32 212 | #define mem_inplace_swap32be mem_inplace_ident 213 | #define mem_inplace_swap32le mem_inplace_swap32 214 | #define memcpy_swap32be memcpy_ident32 215 | #define memcpy_swap32le memcpy_swap32 216 | #define SWAP64BE IDENT64 217 | #define SWAP64LE SWAP64 218 | #define swap64be ident64 219 | #define swap64le swap64 220 | #define mem_inplace_swap64be mem_inplace_ident 221 | #define mem_inplace_swap64le mem_inplace_swap64 222 | #define memcpy_swap64be memcpy_ident64 223 | #define memcpy_swap64le memcpy_swap64 224 | #endif 225 | -------------------------------------------------------------------------------- /crypto/aesb.c: -------------------------------------------------------------------------------- 1 | /* 2 | --------------------------------------------------------------------------- 3 | Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved. 4 | 5 | The redistribution and use of this software (with or without changes) 6 | is allowed without the payment of fees or royalties provided that: 7 | 8 | source code distributions include the above copyright notice, this 9 | list of conditions and the following disclaimer; 10 | 11 | binary distributions include the above copyright notice, this list 12 | of conditions and the following disclaimer in their documentation. 13 | 14 | This software is provided 'as is' with no explicit or implied warranties 15 | in respect of its operation, including, but not limited to, correctness 16 | and fitness for purpose. 17 | --------------------------------------------------------------------------- 18 | Issue Date: 20/12/2007 19 | */ 20 | 21 | #include 22 | 23 | #if defined(__cplusplus) 24 | extern "C" 25 | { 26 | #endif 27 | 28 | #define TABLE_ALIGN 32 29 | #define WPOLY 0x011b 30 | #define N_COLS 4 31 | #define AES_BLOCK_SIZE 16 32 | #define RC_LENGTH (5 * (AES_BLOCK_SIZE / 4 - 2)) 33 | 34 | #if defined(_MSC_VER) 35 | #define ALIGN __declspec(align(TABLE_ALIGN)) 36 | #elif defined(__GNUC__) 37 | #define ALIGN __attribute__ ((aligned(16))) 38 | #else 39 | #define ALIGN 40 | #endif 41 | 42 | #define rf1(r,c) (r) 43 | #define word_in(x,c) (*((uint32_t*)(x)+(c))) 44 | #define word_out(x,c,v) (*((uint32_t*)(x)+(c)) = (v)) 45 | 46 | #define s(x,c) x[c] 47 | #define si(y,x,c) (s(y,c) = word_in(x, c)) 48 | #define so(y,x,c) word_out(y, c, s(x,c)) 49 | #define state_in(y,x) si(y,x,0); si(y,x,1); si(y,x,2); si(y,x,3) 50 | #define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3) 51 | #define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3) 52 | #define to_byte(x) ((x) & 0xff) 53 | #define bval(x,n) to_byte((x) >> (8 * (n))) 54 | 55 | #define fwd_var(x,r,c)\ 56 | ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\ 57 | : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\ 58 | : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\ 59 | : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))) 60 | 61 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c)) 62 | 63 | #define sb_data(w) {\ 64 | w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\ 65 | w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\ 66 | w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\ 67 | w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\ 68 | w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\ 69 | w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\ 70 | w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\ 71 | w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\ 72 | w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\ 73 | w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\ 74 | w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\ 75 | w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\ 76 | w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\ 77 | w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\ 78 | w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\ 79 | w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\ 80 | w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\ 81 | w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\ 82 | w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\ 83 | w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\ 84 | w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\ 85 | w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\ 86 | w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\ 87 | w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\ 88 | w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\ 89 | w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\ 90 | w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\ 91 | w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\ 92 | w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\ 93 | w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\ 94 | w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\ 95 | w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) } 96 | 97 | #define rc_data(w) {\ 98 | w(0x01), w(0x02), w(0x04), w(0x08), w(0x10),w(0x20), w(0x40), w(0x80),\ 99 | w(0x1b), w(0x36) } 100 | 101 | #define bytes2word(b0, b1, b2, b3) (((uint32_t)(b3) << 24) | \ 102 | ((uint32_t)(b2) << 16) | ((uint32_t)(b1) << 8) | (b0)) 103 | 104 | #define h0(x) (x) 105 | #define w0(p) bytes2word(p, 0, 0, 0) 106 | #define w1(p) bytes2word(0, p, 0, 0) 107 | #define w2(p) bytes2word(0, 0, p, 0) 108 | #define w3(p) bytes2word(0, 0, 0, p) 109 | 110 | #define u0(p) bytes2word(f2(p), p, p, f3(p)) 111 | #define u1(p) bytes2word(f3(p), f2(p), p, p) 112 | #define u2(p) bytes2word(p, f3(p), f2(p), p) 113 | #define u3(p) bytes2word(p, p, f3(p), f2(p)) 114 | 115 | #define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p)) 116 | #define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p)) 117 | #define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p)) 118 | #define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p)) 119 | 120 | #define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY)) 121 | #define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY)) 122 | #define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) ^ (((x>>5) & 4) * WPOLY)) 123 | #define f3(x) (f2(x) ^ x) 124 | #define f9(x) (f8(x) ^ x) 125 | #define fb(x) (f8(x) ^ f2(x) ^ x) 126 | #define fd(x) (f8(x) ^ f4(x) ^ x) 127 | #define fe(x) (f8(x) ^ f4(x) ^ f2(x)) 128 | 129 | #define t_dec(m,n) t_##m##n 130 | #define t_set(m,n) t_##m##n 131 | #define t_use(m,n) t_##m##n 132 | 133 | #define d_4(t,n,b,e,f,g,h) ALIGN const t n[4][256] = { b(e), b(f), b(g), b(h) } 134 | 135 | #define four_tables(x,tab,vf,rf,c) \ 136 | (tab[0][bval(vf(x,0,c),rf(0,c))] \ 137 | ^ tab[1][bval(vf(x,1,c),rf(1,c))] \ 138 | ^ tab[2][bval(vf(x,2,c),rf(2,c))] \ 139 | ^ tab[3][bval(vf(x,3,c),rf(3,c))]) 140 | 141 | d_4(uint32_t, t_dec(f,n), sb_data, u0, u1, u2, u3); 142 | 143 | void aesb_single_round(const uint8_t *in, uint8_t *out, uint8_t *expandedKey) 144 | { 145 | uint32_t b0[4], b1[4]; 146 | const uint32_t *kp = (uint32_t *) expandedKey; 147 | state_in(b0, in); 148 | 149 | round(fwd_rnd, b1, b0, kp); 150 | 151 | state_out(out, b1); 152 | } 153 | 154 | void aesb_pseudo_round(const uint8_t *in, uint8_t *out, uint8_t *expandedKey) 155 | { 156 | uint32_t b0[4], b1[4]; 157 | const uint32_t *kp = (uint32_t *) expandedKey; 158 | state_in(b0, in); 159 | 160 | round(fwd_rnd, b1, b0, kp); 161 | round(fwd_rnd, b0, b1, kp + 1 * N_COLS); 162 | round(fwd_rnd, b1, b0, kp + 2 * N_COLS); 163 | round(fwd_rnd, b0, b1, kp + 3 * N_COLS); 164 | round(fwd_rnd, b1, b0, kp + 4 * N_COLS); 165 | round(fwd_rnd, b0, b1, kp + 5 * N_COLS); 166 | round(fwd_rnd, b1, b0, kp + 6 * N_COLS); 167 | round(fwd_rnd, b0, b1, kp + 7 * N_COLS); 168 | round(fwd_rnd, b1, b0, kp + 8 * N_COLS); 169 | round(fwd_rnd, b0, b1, kp + 9 * N_COLS); 170 | 171 | state_out(out, b0); 172 | } 173 | 174 | 175 | #if defined(__cplusplus) 176 | } 177 | #endif 178 | --------------------------------------------------------------------------------