├── compat ├── jansson │ ├── .gitignore │ ├── util.h │ ├── Makefile.am │ ├── utf.h │ ├── strbuffer.h │ ├── LICENSE │ ├── jansson_private.h │ ├── config.h │ ├── strbuffer.c │ ├── utf.c │ ├── jansson.h │ ├── hashtable.h │ ├── hashtable.c │ └── dump.c └── Makefile.am ├── ChangeLog ├── AUTHORS ├── yescrypt-best.c ├── LICENSE ├── autogen.sh ├── example-cfg.json ├── .gitignore ├── compat.h ├── depend └── depend.sh ├── Dockerfile ├── Makefile.am ├── nomacro.pl ├── sha256.h ├── README ├── yescrypt.c ├── configure.ac ├── sysendian.h ├── yescrypt-platform.c ├── elist.h ├── miner.h ├── minerd.1 ├── NEWS ├── sha256.c ├── yescrypt.h ├── sha2.c ├── COPYING ├── scrypt-x86.S.orig └── yescrypt-ref.c /compat/jansson/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | libjansson.a 3 | 4 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | See git repository ('git log') for full changelog. 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Jeff Garzik 2 | 3 | ArtForz 4 | 5 | pooler 6 | -------------------------------------------------------------------------------- /compat/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | if WANT_JANSSON 3 | SUBDIRS = jansson 4 | else 5 | SUBDIRS = 6 | endif 7 | 8 | -------------------------------------------------------------------------------- /yescrypt-best.c: -------------------------------------------------------------------------------- 1 | #ifdef __SSE2__ 2 | #include "yescrypt-simd.c" 3 | #else 4 | #include "yescrypt-opt.c" 5 | #endif 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | cpuminer is available under the terms of the GNU Public License version 2. 2 | 3 | See COPYING for details. 4 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # You need autoconf 2.5x, preferably 2.57 or later 4 | # You need automake 1.7 or later. 1.6 might work. 5 | 6 | set -e 7 | 8 | aclocal 9 | autoheader 10 | automake --gnu --add-missing --copy 11 | autoconf 12 | 13 | -------------------------------------------------------------------------------- /example-cfg.json: -------------------------------------------------------------------------------- 1 | { 2 | "_comment1" : "Any long-format command line argument ", 3 | "_comment2" : "may be used in this JSON configuration file", 4 | 5 | "url" : "http://127.0.0.1:9332/", 6 | "user" : "rpcuser", 7 | "pass" : "rpcpass", 8 | 9 | "algo" : "scrypt", 10 | "threads" : "4", 11 | 12 | "quiet" : true 13 | } 14 | -------------------------------------------------------------------------------- /compat/jansson/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef UTIL_H 9 | #define UTIL_H 10 | 11 | #define max(a, b) ((a) > (b) ? (a) : (b)) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /compat/jansson/Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | noinst_LIBRARIES = libjansson.a 3 | 4 | libjansson_a_SOURCES = \ 5 | config.h \ 6 | dump.c \ 7 | hashtable.c \ 8 | hashtable.h \ 9 | jansson.h \ 10 | jansson_private.h \ 11 | load.c \ 12 | strbuffer.c \ 13 | strbuffer.h \ 14 | utf.c \ 15 | utf.h \ 16 | util.h \ 17 | value.c 18 | 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | minerd 3 | minerd.exe 4 | *.o 5 | 6 | autom4te.cache 7 | .deps 8 | 9 | Makefile 10 | Makefile.in 11 | INSTALL 12 | aclocal.m4 13 | configure 14 | configure.lineno 15 | depcomp 16 | missing 17 | install-sh 18 | stamp-h1 19 | cpuminer-config.h* 20 | compile 21 | config.log 22 | config.status 23 | config.status.lineno 24 | config.guess 25 | config.sub 26 | 27 | mingw32-config.cache 28 | 29 | -------------------------------------------------------------------------------- /compat.h: -------------------------------------------------------------------------------- 1 | #ifndef __COMPAT_H__ 2 | #define __COMPAT_H__ 3 | 4 | #ifdef WIN32 5 | 6 | #include 7 | 8 | #define sleep(secs) Sleep((secs) * 1000) 9 | 10 | enum { 11 | PRIO_PROCESS = 0, 12 | }; 13 | 14 | static inline int setpriority(int which, int who, int prio) 15 | { 16 | return -!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE); 17 | } 18 | 19 | #endif /* WIN32 */ 20 | 21 | #endif /* __COMPAT_H__ */ 22 | -------------------------------------------------------------------------------- /depend/depend.sh: -------------------------------------------------------------------------------- 1 | echo 'download libcurl64' 2 | wget http://curl.haxx.se/gknw.net/rm/7.38.0/dist-w64/curl-7.38.0-devel-mingw64.zip 3 | unzip curl-7.38.0-devel-mingw64.zip 4 | echo '#/bin/sh' > curl-7.38.0-devel-mingw64/bin/curl-config 5 | echo 'echo "libcurl 7.38.0"' >> curl-7.38.0-devel-mingw64/bin/curl-config 6 | chmod 755 curl-7.38.0-devel-mingw64/bin/curl-config 7 | 8 | echo 'download libcurl32' 9 | wget http://curl.haxx.se/gknw.net/rm/7.38.0/dist-w32/curl-7.38.0-devel-mingw32.zip 10 | unzip curl-7.38.0-devel-mingw32.zip 11 | echo '#/bin/sh' > curl-7.38.0-devel-mingw32/bin/curl-config 12 | echo 'echo "libcurl 7.38.0"' >> curl-7.38.0-devel-mingw32/bin/curl-config 13 | chmod 755 curl-7.38.0-devel-mingw32/bin/curl-config 14 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Dockerfile for cpuminer 3 | # usage: docker run creack/cpuminer --url xxxx --user xxxx --pass xxxx 4 | # ex: docker run creack/cpuminer --url stratum+tcp://ltc.pool.com:80 --user creack.worker1 --pass abcdef 5 | # 6 | # 7 | 8 | FROM ubuntu:14.04 9 | MAINTAINER Guillaume J. Charmes 10 | 11 | RUN apt-get update -qq && \ 12 | apt-get install -qqy automake libcurl4-openssl-dev git make 13 | 14 | RUN git clone https://github.com/pooler/cpuminer 15 | 16 | RUN cd cpuminer && \ 17 | ./autogen.sh && \ 18 | ./configure CFLAGS="-O3" && \ 19 | make 20 | 21 | WORKDIR /cpuminer 22 | ENTRYPOINT ["./minerd"] 23 | -------------------------------------------------------------------------------- /compat/jansson/utf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef UTF_H 9 | #define UTF_H 10 | 11 | #include 12 | 13 | #ifdef HAVE_INTTYPES_H 14 | /* inttypes.h includes stdint.h in a standard environment, so there's 15 | no need to include stdint.h separately. If inttypes.h doesn't define 16 | int32_t, it's defined in config.h. */ 17 | #include 18 | #endif 19 | 20 | int utf8_encode(int codepoint, char *buffer, int *size); 21 | 22 | int utf8_check_first(char byte); 23 | int utf8_check_full(const char *buffer, int size, int32_t *codepoint); 24 | const char *utf8_iterate(const char *buffer, int32_t *codepoint); 25 | 26 | int utf8_check_string(const char *string, int length); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | 2 | if WANT_JANSSON 3 | JANSSON_INCLUDES= -I$(top_srcdir)/compat/jansson 4 | else 5 | JANSSON_INCLUDES= 6 | endif 7 | 8 | EXTRA_DIST = example-cfg.json nomacro.pl 9 | 10 | SUBDIRS = compat 11 | 12 | bin_PROGRAMS = minerd 13 | 14 | dist_man_MANS = minerd.1 15 | 16 | minerd_SOURCES = elist.h miner.h compat.h \ 17 | cpu-miner.c util.c \ 18 | sha2.c scrypt.c yescrypt.c 19 | if USE_ASM 20 | if ARCH_x86 21 | minerd_SOURCES += sha2-x86.S scrypt-x86.S 22 | endif 23 | if ARCH_x86_64 24 | minerd_SOURCES += sha2-x64.S scrypt-x64.S 25 | endif 26 | if ARCH_ARM 27 | minerd_SOURCES += sha2-arm.S scrypt-arm.S 28 | endif 29 | if ARCH_PPC 30 | minerd_SOURCES += sha2-ppc.S scrypt-ppc.S 31 | endif 32 | endif 33 | minerd_LDFLAGS = $(PTHREAD_FLAGS) 34 | minerd_LDADD = @LIBCURL@ @JANSSON_LIBS@ @PTHREAD_LIBS@ @WS2_LIBS@ 35 | minerd_CFLAGS = -fno-strict-aliasing 36 | minerd_CPPFLAGS = @LIBCURL_CPPFLAGS@ $(JANSSON_INCLUDES) $(PTHREAD_FLAGS) 37 | 38 | -------------------------------------------------------------------------------- /compat/jansson/strbuffer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef STRBUFFER_H 9 | #define STRBUFFER_H 10 | 11 | typedef struct { 12 | char *value; 13 | int length; /* bytes used */ 14 | int size; /* bytes allocated */ 15 | } strbuffer_t; 16 | 17 | int strbuffer_init(strbuffer_t *strbuff); 18 | void strbuffer_close(strbuffer_t *strbuff); 19 | 20 | void strbuffer_clear(strbuffer_t *strbuff); 21 | 22 | const char *strbuffer_value(const strbuffer_t *strbuff); 23 | char *strbuffer_steal_value(strbuffer_t *strbuff); 24 | 25 | int strbuffer_append(strbuffer_t *strbuff, const char *string); 26 | int strbuffer_append_byte(strbuffer_t *strbuff, char byte); 27 | int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size); 28 | 29 | char strbuffer_pop(strbuffer_t *strbuff); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /compat/jansson/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009, 2010 Petri Lehtinen 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /nomacro.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | # Copyright 2012, 2015 pooler@litecoinpool.org 3 | # 4 | # This program is free software; you can redistribute it and/or modify it 5 | # under the terms of the GNU General Public License as published by the Free 6 | # Software Foundation; either version 2 of the License, or (at your option) 7 | # any later version. See COPYING for more details. 8 | # 9 | # nomacro.pl - expand assembler macros. 10 | 11 | use strict; 12 | 13 | foreach my $f (<*.S>) { 14 | rename $f, "$f.orig" unless -e "$f.orig"; 15 | open FIN, "$f.orig"; 16 | open FOUT, ">$f"; 17 | my %macros = (); 18 | my %m = (); 19 | while () { 20 | if (m/^\.macro\s+(\w+)\s*(.*)$/) { 21 | $m{name} = $1; 22 | @m{args} = [split /\s*,\s*/, $2]; 23 | $m{body} = ""; 24 | next; 25 | } 26 | if (m/^\.endm/) { 27 | $macros{$m{name}} = {%m}; 28 | %m = (); 29 | next; 30 | } 31 | for my $n (keys %macros) { 32 | if (m/^\s*$n\b\s*(.*)$/) { 33 | my @a = split /\s*,\s*/, $1; 34 | $_ = $macros{$n}{body}; 35 | for my $i (0 .. $#{$macros{$n}{args}}) { 36 | s/\\$macros{$n}{args}[$i]\b/$a[$i]/g; 37 | } 38 | last; 39 | } 40 | } 41 | if (%m) { 42 | $m{body} .= $_; 43 | next; 44 | } 45 | print FOUT; 46 | } 47 | close FOUT; 48 | close FIN; 49 | } 50 | -------------------------------------------------------------------------------- /compat/jansson/jansson_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef JANSSON_PRIVATE_H 9 | #define JANSSON_PRIVATE_H 10 | 11 | #include "jansson.h" 12 | #include "hashtable.h" 13 | 14 | #define container_of(ptr_, type_, member_) \ 15 | ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_)) 16 | 17 | typedef struct { 18 | json_t json; 19 | hashtable_t hashtable; 20 | unsigned long serial; 21 | int visited; 22 | } json_object_t; 23 | 24 | typedef struct { 25 | json_t json; 26 | unsigned int size; 27 | unsigned int entries; 28 | json_t **table; 29 | int visited; 30 | } json_array_t; 31 | 32 | typedef struct { 33 | json_t json; 34 | char *value; 35 | } json_string_t; 36 | 37 | typedef struct { 38 | json_t json; 39 | double value; 40 | } json_real_t; 41 | 42 | typedef struct { 43 | json_t json; 44 | int value; 45 | } json_integer_t; 46 | 47 | #define json_to_object(json_) container_of(json_, json_object_t, json) 48 | #define json_to_array(json_) container_of(json_, json_array_t, json) 49 | #define json_to_string(json_) container_of(json_, json_string_t, json) 50 | #define json_to_real(json_) container_of(json_, json_real_t, json) 51 | #define json_to_integer(json_) container_of(json_, json_integer_t, json) 52 | 53 | typedef struct { 54 | unsigned long serial; 55 | char key[]; 56 | } object_key_t; 57 | 58 | const object_key_t *jsonp_object_iter_fullkey(void *iter); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /compat/jansson/config.h: -------------------------------------------------------------------------------- 1 | /* config.h. Generated from config.h.in by configure. */ 2 | /* config.h.in. Generated from configure.ac by autoheader. */ 3 | 4 | /* Define to 1 if you have the header file. */ 5 | #define HAVE_DLFCN_H 1 6 | 7 | /* Define to 1 if you have the header file. */ 8 | #define HAVE_INTTYPES_H 1 9 | 10 | /* Define to 1 if you have the header file. */ 11 | #define HAVE_MEMORY_H 1 12 | 13 | /* Define to 1 if you have the header file. */ 14 | #define HAVE_STDINT_H 1 15 | 16 | /* Define to 1 if you have the header file. */ 17 | #define HAVE_STDLIB_H 1 18 | 19 | /* Define to 1 if you have the header file. */ 20 | #define HAVE_STRINGS_H 1 21 | 22 | /* Define to 1 if you have the header file. */ 23 | #define HAVE_STRING_H 1 24 | 25 | /* Define to 1 if you have the header file. */ 26 | #define HAVE_SYS_STAT_H 1 27 | 28 | /* Define to 1 if you have the header file. */ 29 | #define HAVE_SYS_TYPES_H 1 30 | 31 | /* Define to 1 if you have the header file. */ 32 | #define HAVE_UNISTD_H 1 33 | 34 | /* Define to the sub-directory in which libtool stores uninstalled libraries. 35 | */ 36 | #define LT_OBJDIR ".libs/" 37 | 38 | /* Name of package */ 39 | #define PACKAGE "jansson" 40 | 41 | /* Define to the address where bug reports for this package should be sent. */ 42 | #define PACKAGE_BUGREPORT "petri@digip.org" 43 | 44 | /* Define to the full name of this package. */ 45 | #define PACKAGE_NAME "jansson" 46 | 47 | /* Define to the full name and version of this package. */ 48 | #define PACKAGE_STRING "jansson 1.3" 49 | 50 | /* Define to the one symbol short name of this package. */ 51 | #define PACKAGE_TARNAME "jansson" 52 | 53 | /* Define to the home page for this package. */ 54 | #define PACKAGE_URL "" 55 | 56 | /* Define to the version of this package. */ 57 | #define PACKAGE_VERSION "1.3" 58 | 59 | /* Define to 1 if you have the ANSI C header files. */ 60 | #define STDC_HEADERS 1 61 | 62 | /* Version number of package */ 63 | #define VERSION "1.3" 64 | 65 | /* Define to `__inline__' or `__inline' if that's what the C compiler 66 | calls it, or to nothing if 'inline' is not supported under any name. */ 67 | #ifndef __cplusplus 68 | /* #undef inline */ 69 | #endif 70 | 71 | /* Define to the type of a signed integer type of width exactly 32 bits if 72 | such a type exists and the standard includes do not define it. */ 73 | /* #undef int32_t */ 74 | -------------------------------------------------------------------------------- /compat/jansson/strbuffer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #define _GNU_SOURCE 9 | #include 10 | #include 11 | #include "strbuffer.h" 12 | #include "util.h" 13 | 14 | #define STRBUFFER_MIN_SIZE 16 15 | #define STRBUFFER_FACTOR 2 16 | 17 | int strbuffer_init(strbuffer_t *strbuff) 18 | { 19 | strbuff->size = STRBUFFER_MIN_SIZE; 20 | strbuff->length = 0; 21 | 22 | strbuff->value = malloc(strbuff->size); 23 | if(!strbuff->value) 24 | return -1; 25 | 26 | /* initialize to empty */ 27 | strbuff->value[0] = '\0'; 28 | return 0; 29 | } 30 | 31 | void strbuffer_close(strbuffer_t *strbuff) 32 | { 33 | free(strbuff->value); 34 | strbuff->size = 0; 35 | strbuff->length = 0; 36 | strbuff->value = NULL; 37 | } 38 | 39 | void strbuffer_clear(strbuffer_t *strbuff) 40 | { 41 | strbuff->length = 0; 42 | strbuff->value[0] = '\0'; 43 | } 44 | 45 | const char *strbuffer_value(const strbuffer_t *strbuff) 46 | { 47 | return strbuff->value; 48 | } 49 | 50 | char *strbuffer_steal_value(strbuffer_t *strbuff) 51 | { 52 | char *result = strbuff->value; 53 | strbuffer_init(strbuff); 54 | return result; 55 | } 56 | 57 | int strbuffer_append(strbuffer_t *strbuff, const char *string) 58 | { 59 | return strbuffer_append_bytes(strbuff, string, strlen(string)); 60 | } 61 | 62 | int strbuffer_append_byte(strbuffer_t *strbuff, char byte) 63 | { 64 | return strbuffer_append_bytes(strbuff, &byte, 1); 65 | } 66 | 67 | int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, int size) 68 | { 69 | if(strbuff->length + size >= strbuff->size) 70 | { 71 | strbuff->size = max(strbuff->size * STRBUFFER_FACTOR, 72 | strbuff->length + size + 1); 73 | 74 | strbuff->value = realloc(strbuff->value, strbuff->size); 75 | if(!strbuff->value) 76 | return -1; 77 | } 78 | 79 | memcpy(strbuff->value + strbuff->length, data, size); 80 | strbuff->length += size; 81 | strbuff->value[strbuff->length] = '\0'; 82 | 83 | return 0; 84 | } 85 | 86 | char strbuffer_pop(strbuffer_t *strbuff) 87 | { 88 | if(strbuff->length > 0) { 89 | char c = strbuff->value[--strbuff->length]; 90 | strbuff->value[strbuff->length] = '\0'; 91 | return c; 92 | } 93 | else 94 | return '\0'; 95 | } 96 | -------------------------------------------------------------------------------- /sha256.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.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; 41 | 42 | typedef struct HMAC_SHA256Context { 43 | SHA256_CTX ictx; 44 | SHA256_CTX octx; 45 | } HMAC_SHA256_CTX; 46 | 47 | static void SHA256_Init(SHA256_CTX *); 48 | static void SHA256_Update(SHA256_CTX *, const void *, size_t); 49 | static void SHA256_Final(unsigned char [32], SHA256_CTX *); 50 | static void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t); 51 | static void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t); 52 | static void HMAC_SHA256_Final(unsigned char [32], HMAC_SHA256_CTX *); 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 | static 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 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a multi-threaded CPU miner for Litecoin and Bitcoin, 2 | fork of Jeff Garzik's reference cpuminer. 3 | 4 | License: GPLv2. See COPYING for details. 5 | 6 | Downloads: https://sourceforge.net/projects/cpuminer/files/ 7 | Git tree: https://github.com/pooler/cpuminer 8 | 9 | Dependencies: 10 | libcurl http://curl.haxx.se/libcurl/ 11 | jansson http://www.digip.org/jansson/ 12 | (jansson is included in-tree) 13 | 14 | Basic *nix build instructions: 15 | ./autogen.sh # only needed if building from git repo 16 | ./nomacro.pl # in case the assembler doesn't support macros 17 | ./configure CFLAGS="-O3" # Make sure -O3 is an O and not a zero! 18 | make 19 | 20 | Notes for AIX users: 21 | * To build a 64-bit binary, export OBJECT_MODE=64 22 | * GNU-style long options are not supported, but are accessible 23 | via configuration file 24 | 25 | Basic Windows build instructions, using MinGW: 26 | Install MinGW and the MSYS Developer Tool Kit (http://www.mingw.org/) 27 | * Make sure you have mstcpip.h in MinGW\include 28 | If using MinGW-w64, install pthreads-w64 29 | Install libcurl devel (http://curl.haxx.se/download.html) 30 | * Make sure you have libcurl.m4 in MinGW\share\aclocal 31 | * Make sure you have curl-config in MinGW\bin 32 | In the MSYS shell, run: 33 | ./autogen.sh # only needed if building from git repo 34 | LIBCURL="-lcurldll" ./configure CFLAGS="-O3" 35 | make 36 | 37 | Architecture-specific notes: 38 | ARM: No runtime CPU detection. The miner can take advantage 39 | of some instructions specific to ARMv5E and later processors, 40 | but the decision whether to use them is made at compile time, 41 | based on compiler-defined macros. 42 | To use NEON instructions, add "-mfpu=neon" to CFLAGS. 43 | PowerPC: No runtime CPU detection. 44 | To use AltiVec instructions, add "-maltivec" to CFLAGS. 45 | x86: The miner checks for SSE2 instructions support at runtime, 46 | and uses them if they are available. 47 | x86-64: The miner can take advantage of AVX, AVX2 and XOP instructions, 48 | but only if both the CPU and the operating system support them. 49 | * Linux supports AVX starting from kernel version 2.6.30. 50 | * FreeBSD supports AVX starting with 9.1-RELEASE. 51 | * Mac OS X added AVX support in the 10.6.8 update. 52 | * Windows supports AVX starting from Windows 7 SP1 and 53 | Windows Server 2008 R2 SP1. 54 | The configure script outputs a warning if the assembler 55 | doesn't support some instruction sets. In that case, the miner 56 | can still be built, but unavailable optimizations are left off. 57 | The miner uses the VIA Padlock Hash Engine where available. 58 | 59 | Usage instructions: Run "minerd --help" to see options. 60 | 61 | Connecting through a proxy: Use the --proxy option. 62 | To use a SOCKS proxy, add a socks4:// or socks5:// prefix to the proxy host. 63 | Protocols socks4a and socks5h, allowing remote name resolving, are also 64 | available since libcurl 7.18.0. 65 | If no protocol is specified, the proxy is assumed to be a HTTP proxy. 66 | When the --proxy option is not used, the program honors the http_proxy 67 | and all_proxy environment variables. 68 | 69 | Also many issues and FAQs are covered in the forum thread 70 | dedicated to this program, 71 | https://bitcointalk.org/index.php?topic=55038.0 72 | -------------------------------------------------------------------------------- /yescrypt.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 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 | #include "yescrypt.h" 21 | #include "sha256.c" 22 | #include "yescrypt-best.c" 23 | 24 | #define YESCRYPT_N 2048 25 | #define YESCRYPT_R 8 26 | #define YESCRYPT_P 1 27 | #define YESCRYPT_T 0 28 | #define YESCRYPT_FLAGS (YESCRYPT_RW | YESCRYPT_PWXFORM) 29 | 30 | static int yescrypt_bitzeny(const uint8_t *passwd, size_t passwdlen, 31 | const uint8_t *salt, size_t saltlen, 32 | uint8_t *buf, size_t buflen) 33 | { 34 | static __thread int initialized = 0; 35 | static __thread yescrypt_shared_t shared; 36 | static __thread yescrypt_local_t local; 37 | int retval; 38 | if (!initialized) { 39 | /* "shared" could in fact be shared, but it's simpler to keep it private 40 | * along with "local". It's dummy and tiny anyway. */ 41 | if (yescrypt_init_shared(&shared, NULL, 0, 42 | 0, 0, 0, YESCRYPT_SHARED_DEFAULTS, 0, NULL, 0)) 43 | return -1; 44 | if (yescrypt_init_local(&local)) { 45 | yescrypt_free_shared(&shared); 46 | return -1; 47 | } 48 | initialized = 1; 49 | } 50 | retval = yescrypt_kdf(&shared, &local, passwd, passwdlen, salt, saltlen, 51 | YESCRYPT_N, YESCRYPT_R, YESCRYPT_P, YESCRYPT_T, 52 | YESCRYPT_FLAGS, buf, buflen); 53 | #if 0 54 | if (yescrypt_free_local(&local)) { 55 | yescrypt_free_shared(&shared); 56 | return -1; 57 | } 58 | if (yescrypt_free_shared(&shared)) 59 | return -1; 60 | initialized = 0; 61 | #endif 62 | if (retval < 0) { 63 | yescrypt_free_local(&local); 64 | yescrypt_free_shared(&shared); 65 | } 66 | return retval; 67 | } 68 | 69 | static void yescrypt_hash(const char *input, char *output) 70 | { 71 | yescrypt_bitzeny((const uint8_t *) input, 80, 72 | (const uint8_t *) input, 80, 73 | (uint8_t *) output, 32); 74 | } 75 | 76 | #include 77 | struct work_restart { 78 | volatile unsigned long restart; 79 | char padding[128 - sizeof(unsigned long)]; 80 | }; 81 | 82 | extern struct work_restart *work_restart; 83 | extern bool fulltest(const uint32_t *hash, const uint32_t *target); 84 | 85 | static int pretest(const uint32_t *hash, const uint32_t *target) 86 | { 87 | return hash[7] < target[7]; 88 | } 89 | 90 | int scanhash_yescrypt(int thr_id, uint32_t *pdata, const uint32_t *ptarget, 91 | uint32_t max_nonce, unsigned long *hashes_done) 92 | { 93 | uint32_t data[20] __attribute__((aligned(128))); 94 | uint32_t hash[8] __attribute__((aligned(32))); 95 | uint32_t n = pdata[19] - 1; 96 | const uint32_t first_nonce = pdata[19]; 97 | 98 | for (int i = 0; i < 20; i++) { 99 | be32enc(&data[i], pdata[i]); 100 | } 101 | do { 102 | be32enc(&data[19], ++n); 103 | yescrypt_hash((char *)data, (char *)hash); 104 | if (pretest(hash, ptarget) && fulltest(hash, ptarget)) { 105 | pdata[19] = n; 106 | *hashes_done = n - first_nonce + 1; 107 | return 1; 108 | } 109 | } while (n < max_nonce && !work_restart[thr_id].restart); 110 | 111 | *hashes_done = n - first_nonce + 1; 112 | pdata[19] = n; 113 | return 0; 114 | } 115 | 116 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | AC_INIT([cpuminer], [2.5.0]) 2 | 3 | AC_PREREQ([2.59c]) 4 | AC_CANONICAL_SYSTEM 5 | AC_CONFIG_SRCDIR([cpu-miner.c]) 6 | AM_INIT_AUTOMAKE([gnu]) 7 | AC_CONFIG_HEADERS([cpuminer-config.h]) 8 | 9 | dnl Make sure anyone changing configure.ac/Makefile.am has a clue 10 | AM_MAINTAINER_MODE 11 | 12 | dnl Checks for programs 13 | AC_PROG_CC_C99 14 | AC_PROG_GCC_TRADITIONAL 15 | AM_PROG_CC_C_O 16 | AM_PROG_AS 17 | AC_PROG_RANLIB 18 | 19 | dnl Checks for header files 20 | AC_HEADER_STDC 21 | AC_CHECK_HEADERS([sys/endian.h sys/param.h syslog.h]) 22 | # sys/sysctl.h requires sys/types.h on FreeBSD 23 | # sys/sysctl.h requires sys/param.h on OpenBSD 24 | AC_CHECK_HEADERS([sys/sysctl.h], [], [], 25 | [#include 26 | #ifdef HAVE_SYS_PARAM_H 27 | #include 28 | #endif 29 | ]) 30 | 31 | AC_CHECK_DECLS([be32dec, le32dec, be32enc, le32enc], [], [], 32 | [AC_INCLUDES_DEFAULT 33 | #ifdef HAVE_SYS_ENDIAN_H 34 | #include 35 | #endif 36 | ]) 37 | 38 | AC_FUNC_ALLOCA 39 | AC_CHECK_FUNCS([getopt_long]) 40 | 41 | case $target in 42 | i*86-*-*) 43 | have_x86=true 44 | ;; 45 | x86_64-*-*|amd64-*-*) 46 | have_x86_64=true 47 | ;; 48 | arm*-*-*) 49 | have_arm=true 50 | ;; 51 | powerpc*-*-*) 52 | have_ppc=true 53 | ;; 54 | esac 55 | 56 | PTHREAD_FLAGS="-pthread" 57 | WS2_LIBS="" 58 | 59 | case $target in 60 | *-*-mingw*) 61 | have_win32=true 62 | PTHREAD_FLAGS="" 63 | WS2_LIBS="-lws2_32" 64 | ;; 65 | esac 66 | 67 | AC_ARG_ENABLE([assembly], 68 | AS_HELP_STRING([--disable-assembly], [disable assembly-language routines])) 69 | if test x$enable_assembly != xno; then 70 | AC_DEFINE([USE_ASM], [1], [Define to 1 if assembly routines are wanted.]) 71 | fi 72 | 73 | if test x$enable_assembly != xno -a x$have_x86_64 = xtrue 74 | then 75 | AC_MSG_CHECKING(whether we can compile AVX code) 76 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,[asm ("vmovdqa %ymm0, %ymm1");])], 77 | AC_DEFINE(USE_AVX, 1, [Define to 1 if AVX assembly is available.]) 78 | AC_MSG_RESULT(yes) 79 | AC_MSG_CHECKING(whether we can compile XOP code) 80 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,[asm ("vprotd \$7, %xmm0, %xmm1");])], 81 | AC_DEFINE(USE_XOP, 1, [Define to 1 if XOP assembly is available.]) 82 | AC_MSG_RESULT(yes) 83 | , 84 | AC_MSG_RESULT(no) 85 | AC_MSG_WARN([The assembler does not support the XOP instruction set.]) 86 | ) 87 | AC_MSG_CHECKING(whether we can compile AVX2 code) 88 | AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,[asm ("vpaddd %ymm0, %ymm1, %ymm2");])], 89 | AC_DEFINE(USE_AVX2, 1, [Define to 1 if AVX2 assembly is available.]) 90 | AC_MSG_RESULT(yes) 91 | , 92 | AC_MSG_RESULT(no) 93 | AC_MSG_WARN([The assembler does not support the AVX2 instruction set.]) 94 | ) 95 | , 96 | AC_MSG_RESULT(no) 97 | AC_MSG_WARN([The assembler does not support the AVX instruction set.]) 98 | ) 99 | fi 100 | 101 | AC_CHECK_LIB(jansson, json_loads, request_jansson=false, request_jansson=true) 102 | AC_CHECK_LIB([pthread], [pthread_create], PTHREAD_LIBS="-lpthread", 103 | AC_CHECK_LIB([pthreadGC2], [pthread_create], PTHREAD_LIBS="-lpthreadGC2", 104 | AC_CHECK_LIB([pthreadGC1], [pthread_create], PTHREAD_LIBS="-lpthreadGC1", 105 | AC_CHECK_LIB([pthreadGC], [pthread_create], PTHREAD_LIBS="-lpthreadGC" 106 | )))) 107 | 108 | AM_CONDITIONAL([WANT_JANSSON], [test x$request_jansson = xtrue]) 109 | AM_CONDITIONAL([HAVE_WINDOWS], [test x$have_win32 = xtrue]) 110 | AM_CONDITIONAL([USE_ASM], [test x$enable_assembly != xno]) 111 | AM_CONDITIONAL([ARCH_x86], [test x$have_x86 = xtrue]) 112 | AM_CONDITIONAL([ARCH_x86_64], [test x$have_x86_64 = xtrue]) 113 | AM_CONDITIONAL([ARCH_ARM], [test x$have_arm = xtrue]) 114 | AM_CONDITIONAL([ARCH_PPC], [test x$have_ppc = xtrue]) 115 | 116 | if test x$request_jansson = xtrue 117 | then 118 | JANSSON_LIBS="compat/jansson/libjansson.a" 119 | else 120 | JANSSON_LIBS=-ljansson 121 | fi 122 | 123 | LIBCURL_CHECK_CONFIG(, 7.15.2, , 124 | [AC_MSG_ERROR([Missing required libcurl >= 7.15.2])]) 125 | 126 | AC_SUBST(JANSSON_LIBS) 127 | AC_SUBST(PTHREAD_FLAGS) 128 | AC_SUBST(PTHREAD_LIBS) 129 | AC_SUBST(WS2_LIBS) 130 | 131 | AC_CONFIG_FILES([ 132 | Makefile 133 | compat/Makefile 134 | compat/jansson/Makefile 135 | ]) 136 | AC_OUTPUT 137 | -------------------------------------------------------------------------------- /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 | static inline uint32_t 46 | be32dec(const void *pp) 47 | { 48 | const uint8_t *p = (uint8_t const *)pp; 49 | 50 | return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + 51 | ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); 52 | } 53 | 54 | static inline void 55 | be32enc(void *pp, uint32_t x) 56 | { 57 | uint8_t * p = (uint8_t *)pp; 58 | 59 | p[3] = x & 0xff; 60 | p[2] = (x >> 8) & 0xff; 61 | p[1] = (x >> 16) & 0xff; 62 | p[0] = (x >> 24) & 0xff; 63 | } 64 | 65 | static inline uint64_t 66 | be64dec(const void *pp) 67 | { 68 | const uint8_t *p = (uint8_t const *)pp; 69 | 70 | return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) + 71 | ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) + 72 | ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) + 73 | ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56)); 74 | } 75 | 76 | static inline void 77 | be64enc(void *pp, uint64_t x) 78 | { 79 | uint8_t * p = (uint8_t *)pp; 80 | 81 | p[7] = x & 0xff; 82 | p[6] = (x >> 8) & 0xff; 83 | p[5] = (x >> 16) & 0xff; 84 | p[4] = (x >> 24) & 0xff; 85 | p[3] = (x >> 32) & 0xff; 86 | p[2] = (x >> 40) & 0xff; 87 | p[1] = (x >> 48) & 0xff; 88 | p[0] = (x >> 56) & 0xff; 89 | } 90 | 91 | static inline uint32_t 92 | le32dec(const void *pp) 93 | { 94 | const uint8_t *p = (uint8_t const *)pp; 95 | 96 | return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + 97 | ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); 98 | } 99 | 100 | static inline void 101 | le32enc(void *pp, uint32_t x) 102 | { 103 | uint8_t * p = (uint8_t *)pp; 104 | 105 | p[0] = x & 0xff; 106 | p[1] = (x >> 8) & 0xff; 107 | p[2] = (x >> 16) & 0xff; 108 | p[3] = (x >> 24) & 0xff; 109 | } 110 | 111 | static inline uint64_t 112 | le64dec(const void *pp) 113 | { 114 | const uint8_t *p = (uint8_t const *)pp; 115 | 116 | return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) + 117 | ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) + 118 | ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) + 119 | ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56)); 120 | } 121 | 122 | static inline void 123 | le64enc(void *pp, uint64_t x) 124 | { 125 | uint8_t * p = (uint8_t *)pp; 126 | 127 | p[0] = x & 0xff; 128 | p[1] = (x >> 8) & 0xff; 129 | p[2] = (x >> 16) & 0xff; 130 | p[3] = (x >> 24) & 0xff; 131 | p[4] = (x >> 32) & 0xff; 132 | p[5] = (x >> 40) & 0xff; 133 | p[6] = (x >> 48) & 0xff; 134 | p[7] = (x >> 56) & 0xff; 135 | } 136 | #endif /* !HAVE_SYS_ENDIAN_H */ 137 | 138 | #endif /* !_SYSENDIAN_H_ */ 139 | -------------------------------------------------------------------------------- /compat/jansson/utf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #include 9 | #include "utf.h" 10 | 11 | int utf8_encode(int32_t codepoint, char *buffer, int *size) 12 | { 13 | if(codepoint < 0) 14 | return -1; 15 | else if(codepoint < 0x80) 16 | { 17 | buffer[0] = (char)codepoint; 18 | *size = 1; 19 | } 20 | else if(codepoint < 0x800) 21 | { 22 | buffer[0] = 0xC0 + ((codepoint & 0x7C0) >> 6); 23 | buffer[1] = 0x80 + ((codepoint & 0x03F)); 24 | *size = 2; 25 | } 26 | else if(codepoint < 0x10000) 27 | { 28 | buffer[0] = 0xE0 + ((codepoint & 0xF000) >> 12); 29 | buffer[1] = 0x80 + ((codepoint & 0x0FC0) >> 6); 30 | buffer[2] = 0x80 + ((codepoint & 0x003F)); 31 | *size = 3; 32 | } 33 | else if(codepoint <= 0x10FFFF) 34 | { 35 | buffer[0] = 0xF0 + ((codepoint & 0x1C0000) >> 18); 36 | buffer[1] = 0x80 + ((codepoint & 0x03F000) >> 12); 37 | buffer[2] = 0x80 + ((codepoint & 0x000FC0) >> 6); 38 | buffer[3] = 0x80 + ((codepoint & 0x00003F)); 39 | *size = 4; 40 | } 41 | else 42 | return -1; 43 | 44 | return 0; 45 | } 46 | 47 | int utf8_check_first(char byte) 48 | { 49 | unsigned char u = (unsigned char)byte; 50 | 51 | if(u < 0x80) 52 | return 1; 53 | 54 | if(0x80 <= u && u <= 0xBF) { 55 | /* second, third or fourth byte of a multi-byte 56 | sequence, i.e. a "continuation byte" */ 57 | return 0; 58 | } 59 | else if(u == 0xC0 || u == 0xC1) { 60 | /* overlong encoding of an ASCII byte */ 61 | return 0; 62 | } 63 | else if(0xC2 <= u && u <= 0xDF) { 64 | /* 2-byte sequence */ 65 | return 2; 66 | } 67 | 68 | else if(0xE0 <= u && u <= 0xEF) { 69 | /* 3-byte sequence */ 70 | return 3; 71 | } 72 | else if(0xF0 <= u && u <= 0xF4) { 73 | /* 4-byte sequence */ 74 | return 4; 75 | } 76 | else { /* u >= 0xF5 */ 77 | /* Restricted (start of 4-, 5- or 6-byte sequence) or invalid 78 | UTF-8 */ 79 | return 0; 80 | } 81 | } 82 | 83 | int utf8_check_full(const char *buffer, int size, int32_t *codepoint) 84 | { 85 | int i; 86 | int32_t value = 0; 87 | unsigned char u = (unsigned char)buffer[0]; 88 | 89 | if(size == 2) 90 | { 91 | value = u & 0x1F; 92 | } 93 | else if(size == 3) 94 | { 95 | value = u & 0xF; 96 | } 97 | else if(size == 4) 98 | { 99 | value = u & 0x7; 100 | } 101 | else 102 | return 0; 103 | 104 | for(i = 1; i < size; i++) 105 | { 106 | u = (unsigned char)buffer[i]; 107 | 108 | if(u < 0x80 || u > 0xBF) { 109 | /* not a continuation byte */ 110 | return 0; 111 | } 112 | 113 | value = (value << 6) + (u & 0x3F); 114 | } 115 | 116 | if(value > 0x10FFFF) { 117 | /* not in Unicode range */ 118 | return 0; 119 | } 120 | 121 | else if(0xD800 <= value && value <= 0xDFFF) { 122 | /* invalid code point (UTF-16 surrogate halves) */ 123 | return 0; 124 | } 125 | 126 | else if((size == 2 && value < 0x80) || 127 | (size == 3 && value < 0x800) || 128 | (size == 4 && value < 0x10000)) { 129 | /* overlong encoding */ 130 | return 0; 131 | } 132 | 133 | if(codepoint) 134 | *codepoint = value; 135 | 136 | return 1; 137 | } 138 | 139 | const char *utf8_iterate(const char *buffer, int32_t *codepoint) 140 | { 141 | int count; 142 | int32_t value; 143 | 144 | if(!*buffer) 145 | return buffer; 146 | 147 | count = utf8_check_first(buffer[0]); 148 | if(count <= 0) 149 | return NULL; 150 | 151 | if(count == 1) 152 | value = (unsigned char)buffer[0]; 153 | else 154 | { 155 | if(!utf8_check_full(buffer, count, &value)) 156 | return NULL; 157 | } 158 | 159 | if(codepoint) 160 | *codepoint = value; 161 | 162 | return buffer + count; 163 | } 164 | 165 | int utf8_check_string(const char *string, int length) 166 | { 167 | int i; 168 | 169 | if(length == -1) 170 | length = strlen(string); 171 | 172 | for(i = 0; i < length; i++) 173 | { 174 | int count = utf8_check_first(string[i]); 175 | if(count == 0) 176 | return 0; 177 | else if(count > 1) 178 | { 179 | if(i + count > length) 180 | return 0; 181 | 182 | if(!utf8_check_full(&string[i], count, NULL)) 183 | return 0; 184 | 185 | i += count - 1; 186 | } 187 | } 188 | 189 | return 1; 190 | } 191 | -------------------------------------------------------------------------------- /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 | 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 | static 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 | static int 175 | yescrypt_free_shared(yescrypt_shared_t * shared) 176 | { 177 | return free_region(&shared->shared1); 178 | } 179 | 180 | static int 181 | yescrypt_init_local(yescrypt_local_t * local) 182 | { 183 | init_region(local); 184 | return 0; 185 | } 186 | 187 | static int 188 | yescrypt_free_local(yescrypt_local_t * local) 189 | { 190 | return free_region(local); 191 | } 192 | -------------------------------------------------------------------------------- /compat/jansson/jansson.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef JANSSON_H 9 | #define JANSSON_H 10 | 11 | #include 12 | 13 | #ifndef __cplusplus 14 | #define JSON_INLINE inline 15 | #else 16 | #define JSON_INLINE inline 17 | extern "C" { 18 | #endif 19 | 20 | /* types */ 21 | 22 | typedef enum { 23 | JSON_OBJECT, 24 | JSON_ARRAY, 25 | JSON_STRING, 26 | JSON_INTEGER, 27 | JSON_REAL, 28 | JSON_TRUE, 29 | JSON_FALSE, 30 | JSON_NULL 31 | } json_type; 32 | 33 | typedef struct { 34 | json_type type; 35 | unsigned long refcount; 36 | } json_t; 37 | 38 | #define json_typeof(json) ((json)->type) 39 | #define json_is_object(json) (json && json_typeof(json) == JSON_OBJECT) 40 | #define json_is_array(json) (json && json_typeof(json) == JSON_ARRAY) 41 | #define json_is_string(json) (json && json_typeof(json) == JSON_STRING) 42 | #define json_is_integer(json) (json && json_typeof(json) == JSON_INTEGER) 43 | #define json_is_real(json) (json && json_typeof(json) == JSON_REAL) 44 | #define json_is_number(json) (json_is_integer(json) || json_is_real(json)) 45 | #define json_is_true(json) (json && json_typeof(json) == JSON_TRUE) 46 | #define json_is_false(json) (json && json_typeof(json) == JSON_FALSE) 47 | #define json_is_boolean(json) (json_is_true(json) || json_is_false(json)) 48 | #define json_is_null(json) (json && json_typeof(json) == JSON_NULL) 49 | 50 | /* construction, destruction, reference counting */ 51 | 52 | json_t *json_object(void); 53 | json_t *json_array(void); 54 | json_t *json_string(const char *value); 55 | json_t *json_string_nocheck(const char *value); 56 | json_t *json_integer(int value); 57 | json_t *json_real(double value); 58 | json_t *json_true(void); 59 | json_t *json_false(void); 60 | json_t *json_null(void); 61 | 62 | static JSON_INLINE 63 | json_t *json_incref(json_t *json) 64 | { 65 | if(json && json->refcount != (unsigned int)-1) 66 | ++json->refcount; 67 | return json; 68 | } 69 | 70 | /* do not call json_delete directly */ 71 | void json_delete(json_t *json); 72 | 73 | static JSON_INLINE 74 | void json_decref(json_t *json) 75 | { 76 | if(json && json->refcount != (unsigned int)-1 && --json->refcount == 0) 77 | json_delete(json); 78 | } 79 | 80 | 81 | /* getters, setters, manipulation */ 82 | 83 | unsigned int json_object_size(const json_t *object); 84 | json_t *json_object_get(const json_t *object, const char *key); 85 | int json_object_set_new(json_t *object, const char *key, json_t *value); 86 | int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); 87 | int json_object_del(json_t *object, const char *key); 88 | int json_object_clear(json_t *object); 89 | int json_object_update(json_t *object, json_t *other); 90 | void *json_object_iter(json_t *object); 91 | void *json_object_iter_at(json_t *object, const char *key); 92 | void *json_object_iter_next(json_t *object, void *iter); 93 | const char *json_object_iter_key(void *iter); 94 | json_t *json_object_iter_value(void *iter); 95 | int json_object_iter_set_new(json_t *object, void *iter, json_t *value); 96 | 97 | static JSON_INLINE 98 | int json_object_set(json_t *object, const char *key, json_t *value) 99 | { 100 | return json_object_set_new(object, key, json_incref(value)); 101 | } 102 | 103 | static JSON_INLINE 104 | int json_object_set_nocheck(json_t *object, const char *key, json_t *value) 105 | { 106 | return json_object_set_new_nocheck(object, key, json_incref(value)); 107 | } 108 | 109 | static inline 110 | int json_object_iter_set(json_t *object, void *iter, json_t *value) 111 | { 112 | return json_object_iter_set_new(object, iter, json_incref(value)); 113 | } 114 | 115 | unsigned int json_array_size(const json_t *array); 116 | json_t *json_array_get(const json_t *array, unsigned int index); 117 | int json_array_set_new(json_t *array, unsigned int index, json_t *value); 118 | int json_array_append_new(json_t *array, json_t *value); 119 | int json_array_insert_new(json_t *array, unsigned int index, json_t *value); 120 | int json_array_remove(json_t *array, unsigned int index); 121 | int json_array_clear(json_t *array); 122 | int json_array_extend(json_t *array, json_t *other); 123 | 124 | static JSON_INLINE 125 | int json_array_set(json_t *array, unsigned int index, json_t *value) 126 | { 127 | return json_array_set_new(array, index, json_incref(value)); 128 | } 129 | 130 | static JSON_INLINE 131 | int json_array_append(json_t *array, json_t *value) 132 | { 133 | return json_array_append_new(array, json_incref(value)); 134 | } 135 | 136 | static JSON_INLINE 137 | int json_array_insert(json_t *array, unsigned int index, json_t *value) 138 | { 139 | return json_array_insert_new(array, index, json_incref(value)); 140 | } 141 | 142 | const char *json_string_value(const json_t *string); 143 | int json_integer_value(const json_t *integer); 144 | double json_real_value(const json_t *real); 145 | double json_number_value(const json_t *json); 146 | 147 | int json_string_set(json_t *string, const char *value); 148 | int json_string_set_nocheck(json_t *string, const char *value); 149 | int json_integer_set(json_t *integer, int value); 150 | int json_real_set(json_t *real, double value); 151 | 152 | 153 | /* equality */ 154 | 155 | int json_equal(json_t *value1, json_t *value2); 156 | 157 | 158 | /* copying */ 159 | 160 | json_t *json_copy(json_t *value); 161 | json_t *json_deep_copy(json_t *value); 162 | 163 | 164 | /* loading, printing */ 165 | 166 | #define JSON_ERROR_TEXT_LENGTH 160 167 | 168 | typedef struct { 169 | char text[JSON_ERROR_TEXT_LENGTH]; 170 | int line; 171 | } json_error_t; 172 | 173 | json_t *json_loads(const char *input, json_error_t *error); 174 | json_t *json_loadf(FILE *input, json_error_t *error); 175 | json_t *json_load_file(const char *path, json_error_t *error); 176 | 177 | #define JSON_INDENT(n) (n & 0xFF) 178 | #define JSON_COMPACT 0x100 179 | #define JSON_ENSURE_ASCII 0x200 180 | #define JSON_SORT_KEYS 0x400 181 | #define JSON_PRESERVE_ORDER 0x800 182 | 183 | char *json_dumps(const json_t *json, unsigned long flags); 184 | int json_dumpf(const json_t *json, FILE *output, unsigned long flags); 185 | int json_dump_file(const json_t *json, const char *path, unsigned long flags); 186 | 187 | #ifdef __cplusplus 188 | } 189 | #endif 190 | 191 | #endif 192 | -------------------------------------------------------------------------------- /compat/jansson/hashtable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * This library is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #ifndef HASHTABLE_H 9 | #define HASHTABLE_H 10 | 11 | typedef unsigned int (*key_hash_fn)(const void *key); 12 | typedef int (*key_cmp_fn)(const void *key1, const void *key2); 13 | typedef void (*free_fn)(void *key); 14 | 15 | struct hashtable_list { 16 | struct hashtable_list *prev; 17 | struct hashtable_list *next; 18 | }; 19 | 20 | struct hashtable_pair { 21 | void *key; 22 | void *value; 23 | unsigned int hash; 24 | struct hashtable_list list; 25 | }; 26 | 27 | struct hashtable_bucket { 28 | struct hashtable_list *first; 29 | struct hashtable_list *last; 30 | }; 31 | 32 | typedef struct hashtable { 33 | unsigned int size; 34 | struct hashtable_bucket *buckets; 35 | unsigned int num_buckets; /* index to primes[] */ 36 | struct hashtable_list list; 37 | 38 | key_hash_fn hash_key; 39 | key_cmp_fn cmp_keys; /* returns non-zero for equal keys */ 40 | free_fn free_key; 41 | free_fn free_value; 42 | } hashtable_t; 43 | 44 | /** 45 | * hashtable_create - Create a hashtable object 46 | * 47 | * @hash_key: The key hashing function 48 | * @cmp_keys: The key compare function. Returns non-zero for equal and 49 | * zero for unequal unequal keys 50 | * @free_key: If non-NULL, called for a key that is no longer referenced. 51 | * @free_value: If non-NULL, called for a value that is no longer referenced. 52 | * 53 | * Returns a new hashtable object that should be freed with 54 | * hashtable_destroy when it's no longer used, or NULL on failure (out 55 | * of memory). 56 | */ 57 | hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys, 58 | free_fn free_key, free_fn free_value); 59 | 60 | /** 61 | * hashtable_destroy - Destroy a hashtable object 62 | * 63 | * @hashtable: The hashtable 64 | * 65 | * Destroys a hashtable created with hashtable_create(). 66 | */ 67 | void hashtable_destroy(hashtable_t *hashtable); 68 | 69 | /** 70 | * hashtable_init - Initialize a hashtable object 71 | * 72 | * @hashtable: The (statically allocated) hashtable object 73 | * @hash_key: The key hashing function 74 | * @cmp_keys: The key compare function. Returns non-zero for equal and 75 | * zero for unequal unequal keys 76 | * @free_key: If non-NULL, called for a key that is no longer referenced. 77 | * @free_value: If non-NULL, called for a value that is no longer referenced. 78 | * 79 | * Initializes a statically allocated hashtable object. The object 80 | * should be cleared with hashtable_close when it's no longer used. 81 | * 82 | * Returns 0 on success, -1 on error (out of memory). 83 | */ 84 | int hashtable_init(hashtable_t *hashtable, 85 | key_hash_fn hash_key, key_cmp_fn cmp_keys, 86 | free_fn free_key, free_fn free_value); 87 | 88 | /** 89 | * hashtable_close - Release all resources used by a hashtable object 90 | * 91 | * @hashtable: The hashtable 92 | * 93 | * Destroys a statically allocated hashtable object. 94 | */ 95 | void hashtable_close(hashtable_t *hashtable); 96 | 97 | /** 98 | * hashtable_set - Add/modify value in hashtable 99 | * 100 | * @hashtable: The hashtable object 101 | * @key: The key 102 | * @value: The value 103 | * 104 | * If a value with the given key already exists, its value is replaced 105 | * with the new value. 106 | * 107 | * Key and value are "stealed" in the sense that hashtable frees them 108 | * automatically when they are no longer used. The freeing is 109 | * accomplished by calling free_key and free_value functions that were 110 | * supplied to hashtable_new. In case one or both of the free 111 | * functions is NULL, the corresponding item is not "stealed". 112 | * 113 | * Returns 0 on success, -1 on failure (out of memory). 114 | */ 115 | int hashtable_set(hashtable_t *hashtable, void *key, void *value); 116 | 117 | /** 118 | * hashtable_get - Get a value associated with a key 119 | * 120 | * @hashtable: The hashtable object 121 | * @key: The key 122 | * 123 | * Returns value if it is found, or NULL otherwise. 124 | */ 125 | void *hashtable_get(hashtable_t *hashtable, const void *key); 126 | 127 | /** 128 | * hashtable_del - Remove a value from the hashtable 129 | * 130 | * @hashtable: The hashtable object 131 | * @key: The key 132 | * 133 | * Returns 0 on success, or -1 if the key was not found. 134 | */ 135 | int hashtable_del(hashtable_t *hashtable, const void *key); 136 | 137 | /** 138 | * hashtable_clear - Clear hashtable 139 | * 140 | * @hashtable: The hashtable object 141 | * 142 | * Removes all items from the hashtable. 143 | */ 144 | void hashtable_clear(hashtable_t *hashtable); 145 | 146 | /** 147 | * hashtable_iter - Iterate over hashtable 148 | * 149 | * @hashtable: The hashtable object 150 | * 151 | * Returns an opaque iterator to the first element in the hashtable. 152 | * The iterator should be passed to hashtable_iter_* functions. 153 | * The hashtable items are not iterated over in any particular order. 154 | * 155 | * There's no need to free the iterator in any way. The iterator is 156 | * valid as long as the item that is referenced by the iterator is not 157 | * deleted. Other values may be added or deleted. In particular, 158 | * hashtable_iter_next() may be called on an iterator, and after that 159 | * the key/value pair pointed by the old iterator may be deleted. 160 | */ 161 | void *hashtable_iter(hashtable_t *hashtable); 162 | 163 | /** 164 | * hashtable_iter_at - Return an iterator at a specific key 165 | * 166 | * @hashtable: The hashtable object 167 | * @key: The key that the iterator should point to 168 | * 169 | * Like hashtable_iter() but returns an iterator pointing to a 170 | * specific key. 171 | */ 172 | void *hashtable_iter_at(hashtable_t *hashtable, const void *key); 173 | 174 | /** 175 | * hashtable_iter_next - Advance an iterator 176 | * 177 | * @hashtable: The hashtable object 178 | * @iter: The iterator 179 | * 180 | * Returns a new iterator pointing to the next element in the 181 | * hashtable or NULL if the whole hastable has been iterated over. 182 | */ 183 | void *hashtable_iter_next(hashtable_t *hashtable, void *iter); 184 | 185 | /** 186 | * hashtable_iter_key - Retrieve the key pointed by an iterator 187 | * 188 | * @iter: The iterator 189 | */ 190 | void *hashtable_iter_key(void *iter); 191 | 192 | /** 193 | * hashtable_iter_value - Retrieve the value pointed by an iterator 194 | * 195 | * @iter: The iterator 196 | */ 197 | void *hashtable_iter_value(void *iter); 198 | 199 | /** 200 | * hashtable_iter_set - Set the value pointed by an iterator 201 | * 202 | * @iter: The iterator 203 | * @value: The value to set 204 | */ 205 | void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value); 206 | 207 | #endif 208 | -------------------------------------------------------------------------------- /elist.h: -------------------------------------------------------------------------------- 1 | #ifndef _LINUX_LIST_H 2 | #define _LINUX_LIST_H 3 | 4 | /* 5 | * Simple doubly linked list implementation. 6 | * 7 | * Some of the internal functions ("__xxx") are useful when 8 | * manipulating whole lists rather than single entries, as 9 | * sometimes we already know the next/prev entries and we can 10 | * generate better code by using them directly rather than 11 | * using the generic single-entry routines. 12 | */ 13 | 14 | struct list_head { 15 | struct list_head *next, *prev; 16 | }; 17 | 18 | #define LIST_HEAD_INIT(name) { &(name), &(name) } 19 | 20 | #define LIST_HEAD(name) \ 21 | struct list_head name = LIST_HEAD_INIT(name) 22 | 23 | #define INIT_LIST_HEAD(ptr) do { \ 24 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ 25 | } while (0) 26 | 27 | /* 28 | * Insert a new entry between two known consecutive entries. 29 | * 30 | * This is only for internal list manipulation where we know 31 | * the prev/next entries already! 32 | */ 33 | static inline void __list_add(struct list_head *new, 34 | struct list_head *prev, 35 | struct list_head *next) 36 | { 37 | next->prev = new; 38 | new->next = next; 39 | new->prev = prev; 40 | prev->next = new; 41 | } 42 | 43 | /** 44 | * list_add - add a new entry 45 | * @new: new entry to be added 46 | * @head: list head to add it after 47 | * 48 | * Insert a new entry after the specified head. 49 | * This is good for implementing stacks. 50 | */ 51 | static inline void list_add(struct list_head *new, struct list_head *head) 52 | { 53 | __list_add(new, head, head->next); 54 | } 55 | 56 | /** 57 | * list_add_tail - add a new entry 58 | * @new: new entry to be added 59 | * @head: list head to add it before 60 | * 61 | * Insert a new entry before the specified head. 62 | * This is useful for implementing queues. 63 | */ 64 | static inline void list_add_tail(struct list_head *new, struct list_head *head) 65 | { 66 | __list_add(new, head->prev, head); 67 | } 68 | 69 | /* 70 | * Delete a list entry by making the prev/next entries 71 | * point to each other. 72 | * 73 | * This is only for internal list manipulation where we know 74 | * the prev/next entries already! 75 | */ 76 | static inline void __list_del(struct list_head *prev, struct list_head *next) 77 | { 78 | next->prev = prev; 79 | prev->next = next; 80 | } 81 | 82 | /** 83 | * list_del - deletes entry from list. 84 | * @entry: the element to delete from the list. 85 | * Note: list_empty on entry does not return true after this, the entry is in an undefined state. 86 | */ 87 | static inline void list_del(struct list_head *entry) 88 | { 89 | __list_del(entry->prev, entry->next); 90 | entry->next = (void *) 0; 91 | entry->prev = (void *) 0; 92 | } 93 | 94 | /** 95 | * list_del_init - deletes entry from list and reinitialize it. 96 | * @entry: the element to delete from the list. 97 | */ 98 | static inline void list_del_init(struct list_head *entry) 99 | { 100 | __list_del(entry->prev, entry->next); 101 | INIT_LIST_HEAD(entry); 102 | } 103 | 104 | /** 105 | * list_move - delete from one list and add as another's head 106 | * @list: the entry to move 107 | * @head: the head that will precede our entry 108 | */ 109 | static inline void list_move(struct list_head *list, struct list_head *head) 110 | { 111 | __list_del(list->prev, list->next); 112 | list_add(list, head); 113 | } 114 | 115 | /** 116 | * list_move_tail - delete from one list and add as another's tail 117 | * @list: the entry to move 118 | * @head: the head that will follow our entry 119 | */ 120 | static inline void list_move_tail(struct list_head *list, 121 | struct list_head *head) 122 | { 123 | __list_del(list->prev, list->next); 124 | list_add_tail(list, head); 125 | } 126 | 127 | /** 128 | * list_empty - tests whether a list is empty 129 | * @head: the list to test. 130 | */ 131 | static inline int list_empty(struct list_head *head) 132 | { 133 | return head->next == head; 134 | } 135 | 136 | static inline void __list_splice(struct list_head *list, 137 | struct list_head *head) 138 | { 139 | struct list_head *first = list->next; 140 | struct list_head *last = list->prev; 141 | struct list_head *at = head->next; 142 | 143 | first->prev = head; 144 | head->next = first; 145 | 146 | last->next = at; 147 | at->prev = last; 148 | } 149 | 150 | /** 151 | * list_splice - join two lists 152 | * @list: the new list to add. 153 | * @head: the place to add it in the first list. 154 | */ 155 | static inline void list_splice(struct list_head *list, struct list_head *head) 156 | { 157 | if (!list_empty(list)) 158 | __list_splice(list, head); 159 | } 160 | 161 | /** 162 | * list_splice_init - join two lists and reinitialise the emptied list. 163 | * @list: the new list to add. 164 | * @head: the place to add it in the first list. 165 | * 166 | * The list at @list is reinitialised 167 | */ 168 | static inline void list_splice_init(struct list_head *list, 169 | struct list_head *head) 170 | { 171 | if (!list_empty(list)) { 172 | __list_splice(list, head); 173 | INIT_LIST_HEAD(list); 174 | } 175 | } 176 | 177 | /** 178 | * list_entry - get the struct for this entry 179 | * @ptr: the &struct list_head pointer. 180 | * @type: the type of the struct this is embedded in. 181 | * @member: the name of the list_struct within the struct. 182 | */ 183 | #define list_entry(ptr, type, member) \ 184 | ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 185 | 186 | /** 187 | * list_for_each - iterate over a list 188 | * @pos: the &struct list_head to use as a loop counter. 189 | * @head: the head for your list. 190 | */ 191 | #define list_for_each(pos, head) \ 192 | for (pos = (head)->next; pos != (head); \ 193 | pos = pos->next) 194 | /** 195 | * list_for_each_prev - iterate over a list backwards 196 | * @pos: the &struct list_head to use as a loop counter. 197 | * @head: the head for your list. 198 | */ 199 | #define list_for_each_prev(pos, head) \ 200 | for (pos = (head)->prev; pos != (head); \ 201 | pos = pos->prev) 202 | 203 | /** 204 | * list_for_each_safe - iterate over a list safe against removal of list entry 205 | * @pos: the &struct list_head to use as a loop counter. 206 | * @n: another &struct list_head to use as temporary storage 207 | * @head: the head for your list. 208 | */ 209 | #define list_for_each_safe(pos, n, head) \ 210 | for (pos = (head)->next, n = pos->next; pos != (head); \ 211 | pos = n, n = pos->next) 212 | 213 | /** 214 | * list_for_each_entry - iterate over list of given type 215 | * @pos: the type * to use as a loop counter. 216 | * @head: the head for your list. 217 | * @member: the name of the list_struct within the struct. 218 | * @type: the type of the struct. 219 | */ 220 | #define list_for_each_entry(pos, head, member, type) \ 221 | for (pos = list_entry((head)->next, type, member); \ 222 | &pos->member != (head); \ 223 | pos = list_entry(pos->member.next, type, member)) 224 | 225 | /** 226 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 227 | * @pos: the type * to use as a loop counter. 228 | * @n: another type * to use as temporary storage 229 | * @head: the head for your list. 230 | * @member: the name of the list_struct within the struct. 231 | * @type: the type of the struct. 232 | */ 233 | #define list_for_each_entry_safe(pos, n, head, member, type) \ 234 | for (pos = list_entry((head)->next, type, member), \ 235 | n = list_entry(pos->member.next, type, member); \ 236 | &pos->member != (head); \ 237 | pos = n, n = list_entry(n->member.next, type, member)) 238 | 239 | /** 240 | * list_for_each_entry_continue - iterate over list of given type 241 | * continuing after existing point 242 | * @pos: the type * to use as a loop counter. 243 | * @head: the head for your list. 244 | * @member: the name of the list_struct within the struct. 245 | * @type: the type of the struct. 246 | */ 247 | #define list_for_each_entry_continue(pos, head, member, type) \ 248 | for (pos = list_entry(pos->member.next, type, member), \ 249 | prefetch(pos->member.next); \ 250 | &pos->member != (head); \ 251 | pos = list_entry(pos->member.next, type, member), \ 252 | prefetch(pos->member.next)) 253 | 254 | #endif 255 | -------------------------------------------------------------------------------- /miner.h: -------------------------------------------------------------------------------- 1 | #ifndef __MINER_H__ 2 | #define __MINER_H__ 3 | 4 | #include "cpuminer-config.h" 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #ifdef STDC_HEADERS 14 | # include 15 | # include 16 | #else 17 | # ifdef HAVE_STDLIB_H 18 | # include 19 | # endif 20 | #endif 21 | #ifdef HAVE_ALLOCA_H 22 | # include 23 | #elif !defined alloca 24 | # ifdef __GNUC__ 25 | # define alloca __builtin_alloca 26 | # elif defined _AIX 27 | # define alloca __alloca 28 | # elif defined _MSC_VER 29 | # include 30 | # define alloca _alloca 31 | # elif !defined HAVE_ALLOCA 32 | # ifdef __cplusplus 33 | extern "C" 34 | # endif 35 | void *alloca (size_t); 36 | # endif 37 | #endif 38 | 39 | #ifdef HAVE_SYSLOG_H 40 | #include 41 | #else 42 | enum { 43 | LOG_ERR, 44 | LOG_WARNING, 45 | LOG_NOTICE, 46 | LOG_INFO, 47 | LOG_DEBUG, 48 | }; 49 | #endif 50 | 51 | #undef unlikely 52 | #undef likely 53 | #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) 54 | #define unlikely(expr) (__builtin_expect(!!(expr), 0)) 55 | #define likely(expr) (__builtin_expect(!!(expr), 1)) 56 | #else 57 | #define unlikely(expr) (expr) 58 | #define likely(expr) (expr) 59 | #endif 60 | 61 | #ifndef ARRAY_SIZE 62 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 63 | #endif 64 | 65 | #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 66 | #define WANT_BUILTIN_BSWAP 67 | #else 68 | #define bswap_32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) \ 69 | | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu)) 70 | #endif 71 | 72 | static inline uint32_t swab32(uint32_t v) 73 | { 74 | #ifdef WANT_BUILTIN_BSWAP 75 | return __builtin_bswap32(v); 76 | #else 77 | return bswap_32(v); 78 | #endif 79 | } 80 | 81 | #ifdef HAVE_SYS_ENDIAN_H 82 | #include 83 | #endif 84 | 85 | #if !HAVE_DECL_BE32DEC 86 | static inline uint32_t be32dec(const void *pp) 87 | { 88 | const uint8_t *p = (uint8_t const *)pp; 89 | return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) + 90 | ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); 91 | } 92 | #endif 93 | 94 | #if !HAVE_DECL_LE32DEC 95 | static inline uint32_t le32dec(const void *pp) 96 | { 97 | const uint8_t *p = (uint8_t const *)pp; 98 | return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) + 99 | ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); 100 | } 101 | #endif 102 | 103 | #if !HAVE_DECL_BE32ENC 104 | static inline void be32enc(void *pp, uint32_t x) 105 | { 106 | uint8_t *p = (uint8_t *)pp; 107 | p[3] = x & 0xff; 108 | p[2] = (x >> 8) & 0xff; 109 | p[1] = (x >> 16) & 0xff; 110 | p[0] = (x >> 24) & 0xff; 111 | } 112 | #endif 113 | 114 | #if !HAVE_DECL_LE32ENC 115 | static inline void le32enc(void *pp, uint32_t x) 116 | { 117 | uint8_t *p = (uint8_t *)pp; 118 | p[0] = x & 0xff; 119 | p[1] = (x >> 8) & 0xff; 120 | p[2] = (x >> 16) & 0xff; 121 | p[3] = (x >> 24) & 0xff; 122 | } 123 | #endif 124 | 125 | #if JANSSON_MAJOR_VERSION >= 2 126 | #define JSON_LOADS(str, err_ptr) json_loads(str, 0, err_ptr) 127 | #define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, 0, err_ptr) 128 | #else 129 | #define JSON_LOADS(str, err_ptr) json_loads(str, err_ptr) 130 | #define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, err_ptr) 131 | #endif 132 | 133 | #define USER_AGENT PACKAGE_NAME "/" PACKAGE_VERSION 134 | 135 | void sha256_init(uint32_t *state); 136 | void sha256_transform(uint32_t *state, const uint32_t *block, int swap); 137 | void sha256d(unsigned char *hash, const unsigned char *data, int len); 138 | 139 | #ifdef USE_ASM 140 | #if defined(__ARM_NEON__) || defined(__ALTIVEC__) || defined(__i386__) || defined(__x86_64__) 141 | #define HAVE_SHA256_4WAY 1 142 | int sha256_use_4way(); 143 | void sha256_init_4way(uint32_t *state); 144 | void sha256_transform_4way(uint32_t *state, const uint32_t *block, int swap); 145 | #endif 146 | #if defined(__x86_64__) && defined(USE_AVX2) 147 | #define HAVE_SHA256_8WAY 1 148 | int sha256_use_8way(); 149 | void sha256_init_8way(uint32_t *state); 150 | void sha256_transform_8way(uint32_t *state, const uint32_t *block, int swap); 151 | #endif 152 | #endif 153 | 154 | extern int scanhash_yescrypt(int thr_id, uint32_t *pdata, 155 | const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done); 156 | 157 | extern int scanhash_sha256d(int thr_id, uint32_t *pdata, 158 | const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done); 159 | 160 | extern unsigned char *scrypt_buffer_alloc(int N); 161 | extern int scanhash_scrypt(int thr_id, uint32_t *pdata, 162 | unsigned char *scratchbuf, const uint32_t *ptarget, 163 | uint32_t max_nonce, unsigned long *hashes_done, int N); 164 | 165 | struct thr_info { 166 | int id; 167 | pthread_t pth; 168 | struct thread_q *q; 169 | }; 170 | 171 | struct work_restart { 172 | volatile unsigned long restart; 173 | char padding[128 - sizeof(unsigned long)]; 174 | }; 175 | 176 | extern bool opt_debug; 177 | extern bool opt_protocol; 178 | extern bool opt_redirect; 179 | extern int opt_timeout; 180 | extern bool want_longpoll; 181 | extern bool have_longpoll; 182 | extern bool have_gbt; 183 | extern bool allow_getwork; 184 | extern bool want_stratum; 185 | extern bool have_stratum; 186 | extern char *opt_cert; 187 | extern char *opt_proxy; 188 | extern long opt_proxy_type; 189 | extern bool use_syslog; 190 | extern pthread_mutex_t applog_lock; 191 | extern struct thr_info *thr_info; 192 | extern int longpoll_thr_id; 193 | extern int stratum_thr_id; 194 | extern struct work_restart *work_restart; 195 | 196 | #define JSON_RPC_LONGPOLL (1 << 0) 197 | #define JSON_RPC_QUIET_404 (1 << 1) 198 | 199 | extern void applog(int prio, const char *fmt, ...); 200 | extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, 201 | const char *rpc_req, int *curl_err, int flags); 202 | void memrev(unsigned char *p, size_t len); 203 | extern void bin2hex(char *s, const unsigned char *p, size_t len); 204 | extern char *abin2hex(const unsigned char *p, size_t len); 205 | extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len); 206 | extern int varint_encode(unsigned char *p, uint64_t n); 207 | extern size_t address_to_script(unsigned char *out, size_t outsz, const char *addr); 208 | extern int timeval_subtract(struct timeval *result, struct timeval *x, 209 | struct timeval *y); 210 | extern bool fulltest(const uint32_t *hash, const uint32_t *target); 211 | extern void diff_to_target(uint32_t *target, double diff); 212 | 213 | struct stratum_job { 214 | char *job_id; 215 | unsigned char prevhash[32]; 216 | size_t coinbase_size; 217 | unsigned char *coinbase; 218 | unsigned char *xnonce2; 219 | int merkle_count; 220 | unsigned char **merkle; 221 | unsigned char version[4]; 222 | unsigned char nbits[4]; 223 | unsigned char ntime[4]; 224 | bool clean; 225 | double diff; 226 | }; 227 | 228 | struct stratum_ctx { 229 | char *url; 230 | 231 | CURL *curl; 232 | char *curl_url; 233 | char curl_err_str[CURL_ERROR_SIZE]; 234 | curl_socket_t sock; 235 | size_t sockbuf_size; 236 | char *sockbuf; 237 | pthread_mutex_t sock_lock; 238 | 239 | double next_diff; 240 | 241 | char *session_id; 242 | size_t xnonce1_size; 243 | unsigned char *xnonce1; 244 | size_t xnonce2_size; 245 | struct stratum_job job; 246 | pthread_mutex_t work_lock; 247 | }; 248 | 249 | bool stratum_socket_full(struct stratum_ctx *sctx, int timeout); 250 | bool stratum_send_line(struct stratum_ctx *sctx, char *s); 251 | char *stratum_recv_line(struct stratum_ctx *sctx); 252 | bool stratum_connect(struct stratum_ctx *sctx, const char *url); 253 | void stratum_disconnect(struct stratum_ctx *sctx); 254 | bool stratum_subscribe(struct stratum_ctx *sctx); 255 | bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass); 256 | bool stratum_handle_method(struct stratum_ctx *sctx, const char *s); 257 | 258 | struct thread_q; 259 | 260 | extern struct thread_q *tq_new(void); 261 | extern void tq_free(struct thread_q *tq); 262 | extern bool tq_push(struct thread_q *tq, void *data); 263 | extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime); 264 | extern void tq_freeze(struct thread_q *tq); 265 | extern void tq_thaw(struct thread_q *tq); 266 | 267 | #endif /* __MINER_H__ */ 268 | -------------------------------------------------------------------------------- /minerd.1: -------------------------------------------------------------------------------- 1 | .TH MINERD 1 "June 2017" "cpuminer 2.5.0" 2 | .SH NAME 3 | minerd \- CPU miner for Bitcoin and Litecoin 4 | .SH SYNOPSIS 5 | .B minerd 6 | [\fIOPTION\fR]... 7 | .SH DESCRIPTION 8 | .B minerd 9 | is a multi-threaded CPU miner for Bitcoin, Litecoin and other cryptocurrencies. 10 | It supports the getwork and getblocktemplate (BIP 22) methods, 11 | as well as the Stratum mining protocol. 12 | .PP 13 | In its normal mode of operation, \fBminerd\fR connects to a mining server 14 | (specified with the \fB\-o\fR option), receives work from it and starts hashing. 15 | As soon as a solution is found, it is submitted to the same mining server, 16 | which can accept or reject it. 17 | When using getwork or getblocktemplate, 18 | \fBminerd\fR can take advantage of long polling, if the server supports it; 19 | in any case, fresh work is fetched as needed. 20 | When using the Stratum protocol this is not possible, 21 | and the server is responsible for sending fresh work at least every minute; 22 | if it fails to do so, 23 | \fBminerd\fR may drop the connection and try reconnecting again. 24 | .PP 25 | By default, \fBminerd\fR writes all its messages to standard error. 26 | On systems that have a syslog, the \fB\-\-syslog\fR option can be used 27 | to write to it instead. 28 | .PP 29 | On start, the nice value of all miner threads is set to 19. 30 | On Linux, the scheduling policy is also changed to SCHED_IDLE, 31 | or to SCHED_BATCH if that fails. 32 | On multiprocessor systems, \fBminerd\fR 33 | automatically sets the CPU affinity of miner threads 34 | if the number of threads is a multiple of the number of processors. 35 | .SH EXAMPLES 36 | To connect to a Litecoin mining pool that provides a Stratum server 37 | at example.com on port 3333, authenticating as worker "foo" with password "bar": 38 | .PP 39 | .nf 40 | .RS 41 | minerd \-o stratum+tcp://example.com:3333 \-O foo:bar 42 | .RE 43 | .fi 44 | .PP 45 | To mine to a local Bitcoin testnet instance running on port 18332, 46 | authenticating with username "rpcuser" and password "rpcpass": 47 | .PP 48 | .nf 49 | .RS 50 | minerd \-a sha256d \-o http://localhost:18332 \-O rpcuser:rpcpass \\ 51 | \-\-coinbase\-addr=mpXwg4jMtRhuSpVq4xS3HFHmCmWp9NyGKt 52 | .RE 53 | .fi 54 | .PP 55 | To connect to a Litecoin P2Pool node running on my.server on port 9327, 56 | mining in the background and having output sent to the syslog facility, 57 | omitting the per-thread hashmeter output: 58 | .PP 59 | .nf 60 | .RS 61 | minerd \-BSq \-o http://my.server:9327 62 | .RE 63 | .fi 64 | .SH OPTIONS 65 | .TP 66 | \fB\-a\fR, \fB\-\-algo\fR=\fIALGORITHM\fR 67 | Set the hashing algorithm to use. 68 | Default is scrypt. 69 | Possible values are: 70 | .RS 11 71 | .TP 10 72 | .B scrypt 73 | scrypt(1024, 1, 1) (used by Litecoin) 74 | .TP 75 | .B scrypt:\fIN\fR 76 | scrypt(\fIN\fR, 1, 1) (\fIN\fR must be a power of 2 greater than 1) 77 | .TP 78 | .B sha256d 79 | SHA-256d (used by Bitcoin) 80 | .RE 81 | .TP 82 | \fB\-\-benchmark\fR 83 | Run in offline benchmark mode. 84 | .TP 85 | \fB\-B\fR, \fB\-\-background\fR 86 | Run in the background as a daemon. 87 | .TP 88 | \fB\-\-cert\fR=\fIFILE\fR 89 | Set an SSL certificate to use with the mining server. 90 | Only supported when using the HTTPS protocol. 91 | .TP 92 | \fB\-\-coinbase\-addr\fR=\fIADDRESS\fR 93 | Set a payout address for solo mining. 94 | This is only used in getblocktemplate mode, 95 | and only if the server does not provide a coinbase transaction. 96 | .TP 97 | \fB\-\-coinbase\-sig\fR=\fITEXT\fR 98 | Set a string to be included in the coinbase (if allowed by the server). 99 | This is only used in getblocktemplate mode. 100 | .TP 101 | \fB\-c\fR, \fB\-\-config\fR=\fIFILE\fR 102 | Load options from a configuration file. 103 | \fIFILE\fR must contain a JSON object 104 | mapping long options to their arguments (as strings), 105 | or to \fBtrue\fR if no argument is required. 106 | Sample configuration file: 107 | 108 | .nf 109 | { 110 | "url": "stratum+tcp://example.com:3333", 111 | "userpass": "foo:bar", 112 | "retry-pause": "10", 113 | "quiet": true 114 | } 115 | .fi 116 | .TP 117 | \fB\-D\fR, \fB\-\-debug\fR 118 | Enable debug output. 119 | .TP 120 | \fB\-h\fR, \fB\-\-help\fR 121 | Print a help message and exit. 122 | .TP 123 | \fB\-\-no\-gbt\fR 124 | Do not use the getblocktemplate RPC method. 125 | .TP 126 | \fB\-\-no\-getwork\fR 127 | Do not use the getwork RPC method. 128 | .TP 129 | \fB\-\-no\-longpoll\fR 130 | Do not use long polling. 131 | .TP 132 | \fB\-\-no\-redirect\fR 133 | Ignore requests from the server to switch to a different URL. 134 | .TP 135 | \fB\-\-no\-stratum\fR 136 | Do not switch to Stratum, even if the server advertises support for it. 137 | .TP 138 | \fB\-o\fR, \fB\-\-url\fR=[\fISCHEME\fR://][\fIUSERNAME\fR[:\fIPASSWORD\fR]@]\fIHOST\fR:\fIPORT\fR[/\fIPATH\fR] 139 | Set the URL of the mining server to connect to. 140 | Supported schemes are \fBhttp\fR, \fBhttps\fR, \fBstratum+tcp\fR 141 | and \fBstratum+tcps\fR. 142 | If no scheme is specified, http is assumed. 143 | Specifying a \fIPATH\fR is only supported for HTTP and HTTPS. 144 | Specifying credentials has the same effect as using the \fB\-O\fR option. 145 | 146 | By default, on HTTP and HTTPS, 147 | the miner tries to use the getblocktemplate RPC method, 148 | and falls back to using getwork if getblocktemplate is unavailable. 149 | This behavior can be modified by using the \fB\-\-no\-gbt\fR 150 | and \fB\-\-no\-getwork\fR options. 151 | .TP 152 | \fB\-O\fR, \fB\-\-userpass\fR=\fIUSERNAME\fR:\fIPASSWORD\fR 153 | Set the credentials to use for connecting to the mining server. 154 | Any value previously set with \fB\-u\fR or \fB\-p\fR is discarded. 155 | .TP 156 | \fB\-p\fR, \fB\-\-pass\fR=\fIPASSWORD\fR 157 | Set the password to use for connecting to the mining server. 158 | Any password previously set with \fB\-O\fR is discarded. 159 | .TP 160 | \fB\-P\fR, \fB\-\-protocol\-dump\fR 161 | Enable output of all protocol-level activities. 162 | .TP 163 | \fB\-q\fR, \fB\-\-quiet\fR 164 | Disable per-thread hashmeter output. 165 | .TP 166 | \fB\-r\fR, \fB\-\-retries\fR=\fIN\fR 167 | Set the maximum number of times to retry if a network call fails. 168 | If not specified, the miner will retry indefinitely. 169 | .TP 170 | \fB\-R\fR, \fB\-\-retry\-pause\fR=\fISECONDS\fR 171 | Set how long to wait between retries. Default is 30 seconds. 172 | .TP 173 | \fB\-s\fR, \fB\-\-scantime\fR=\fISECONDS\fR 174 | Set an upper bound on the time the miner can go without fetching fresh work. 175 | This setting has no effect in Stratum mode or when long polling is activated. 176 | Default is 5 seconds. 177 | .TP 178 | \fB\-S\fR, \fB\-\-syslog\fR 179 | Log to the syslog facility instead of standard error. 180 | .TP 181 | \fB\-t\fR, \fB\-\-threads\fR=\fIN\fR 182 | Set the number of miner threads. 183 | If not specified, the miner will try to detect the number of available processors 184 | and use that. 185 | .TP 186 | \fB\-T\fR, \fB\-\-timeout\fR=\fISECONDS\fR 187 | Set a timeout for long polling. 188 | .TP 189 | \fB\-u\fR, \fB\-\-user\fR=\fIUSERNAME\fR 190 | Set the username to use for connecting to the mining server. 191 | Any username previously set with \fB\-O\fR is discarded. 192 | .TP 193 | \fB\-V\fR, \fB\-\-version\fR 194 | Display version information and quit. 195 | .TP 196 | \fB\-x\fR, \fB\-\-proxy\fR=[\fISCHEME\fR://][\fIUSERNAME\fR:\fIPASSWORD\fR@]\fIHOST\fR:\fIPORT\fR 197 | Connect to the mining server through a proxy. 198 | Supported schemes are: \fBhttp\fR, \fBsocks4\fR, \fBsocks5\fR. 199 | Since libcurl 7.18.0, the following are also supported: 200 | \fBsocks4a\fR, \fBsocks5h\fR (SOCKS5 with remote name resolving). 201 | If no scheme is specified, the proxy is treated as an HTTP proxy. 202 | .SH ENVIRONMENT 203 | The following environment variables can be specified in lower case or upper case; 204 | the lower-case version has precedence. \fBhttp_proxy\fR is an exception 205 | as it is only available in lower case. 206 | .PP 207 | .RS 208 | .TP 209 | \fBhttp_proxy\fR [\fISCHEME\fR://]\fIHOST\fR:\fIPORT\fR 210 | Sets the proxy server to use for HTTP. 211 | .TP 212 | \fBHTTPS_PROXY\fR [\fISCHEME\fR://]\fIHOST\fR:\fIPORT\fR 213 | Sets the proxy server to use for HTTPS. 214 | .TP 215 | \fBALL_PROXY\fR [\fISCHEME\fR://]\fIHOST\fR:\fIPORT\fR 216 | Sets the proxy server to use if no protocol-specific proxy is set. 217 | .RE 218 | .PP 219 | Using an environment variable to set the proxy has the same effect as 220 | using the \fB\-x\fR option. 221 | .SH AUTHOR 222 | Most of the code in the current version of minerd was written by 223 | Pooler with contributions from others. 224 | 225 | The original minerd was written by Jeff Garzik . 226 | -------------------------------------------------------------------------------- /compat/jansson/hashtable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * This library is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #include 9 | 10 | #include 11 | #include "hashtable.h" 12 | 13 | typedef struct hashtable_list list_t; 14 | typedef struct hashtable_pair pair_t; 15 | typedef struct hashtable_bucket bucket_t; 16 | 17 | #define container_of(ptr_, type_, member_) \ 18 | ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_)) 19 | 20 | #define list_to_pair(list_) container_of(list_, pair_t, list) 21 | 22 | static inline void list_init(list_t *list) 23 | { 24 | list->next = list; 25 | list->prev = list; 26 | } 27 | 28 | static inline void list_insert(list_t *list, list_t *node) 29 | { 30 | node->next = list; 31 | node->prev = list->prev; 32 | list->prev->next = node; 33 | list->prev = node; 34 | } 35 | 36 | static inline void list_remove(list_t *list) 37 | { 38 | list->prev->next = list->next; 39 | list->next->prev = list->prev; 40 | } 41 | 42 | static inline int bucket_is_empty(hashtable_t *hashtable, bucket_t *bucket) 43 | { 44 | return bucket->first == &hashtable->list && bucket->first == bucket->last; 45 | } 46 | 47 | static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, 48 | list_t *list) 49 | { 50 | if(bucket_is_empty(hashtable, bucket)) 51 | { 52 | list_insert(&hashtable->list, list); 53 | bucket->first = bucket->last = list; 54 | } 55 | else 56 | { 57 | list_insert(bucket->first, list); 58 | bucket->first = list; 59 | } 60 | } 61 | 62 | static unsigned int primes[] = { 63 | 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 64 | 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 65 | 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 66 | 805306457, 1610612741 67 | }; 68 | static const unsigned int num_primes = sizeof(primes) / sizeof(unsigned int); 69 | 70 | static inline unsigned int num_buckets(hashtable_t *hashtable) 71 | { 72 | return primes[hashtable->num_buckets]; 73 | } 74 | 75 | 76 | static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, 77 | const void *key, unsigned int hash) 78 | { 79 | list_t *list; 80 | pair_t *pair; 81 | 82 | if(bucket_is_empty(hashtable, bucket)) 83 | return NULL; 84 | 85 | list = bucket->first; 86 | while(1) 87 | { 88 | pair = list_to_pair(list); 89 | if(pair->hash == hash && hashtable->cmp_keys(pair->key, key)) 90 | return pair; 91 | 92 | if(list == bucket->last) 93 | break; 94 | 95 | list = list->next; 96 | } 97 | 98 | return NULL; 99 | } 100 | 101 | /* returns 0 on success, -1 if key was not found */ 102 | static int hashtable_do_del(hashtable_t *hashtable, 103 | const void *key, unsigned int hash) 104 | { 105 | pair_t *pair; 106 | bucket_t *bucket; 107 | unsigned int index; 108 | 109 | index = hash % num_buckets(hashtable); 110 | bucket = &hashtable->buckets[index]; 111 | 112 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 113 | if(!pair) 114 | return -1; 115 | 116 | if(&pair->list == bucket->first && &pair->list == bucket->last) 117 | bucket->first = bucket->last = &hashtable->list; 118 | 119 | else if(&pair->list == bucket->first) 120 | bucket->first = pair->list.next; 121 | 122 | else if(&pair->list == bucket->last) 123 | bucket->last = pair->list.prev; 124 | 125 | list_remove(&pair->list); 126 | 127 | if(hashtable->free_key) 128 | hashtable->free_key(pair->key); 129 | if(hashtable->free_value) 130 | hashtable->free_value(pair->value); 131 | 132 | free(pair); 133 | hashtable->size--; 134 | 135 | return 0; 136 | } 137 | 138 | static void hashtable_do_clear(hashtable_t *hashtable) 139 | { 140 | list_t *list, *next; 141 | pair_t *pair; 142 | 143 | for(list = hashtable->list.next; list != &hashtable->list; list = next) 144 | { 145 | next = list->next; 146 | pair = list_to_pair(list); 147 | if(hashtable->free_key) 148 | hashtable->free_key(pair->key); 149 | if(hashtable->free_value) 150 | hashtable->free_value(pair->value); 151 | free(pair); 152 | } 153 | } 154 | 155 | static int hashtable_do_rehash(hashtable_t *hashtable) 156 | { 157 | list_t *list, *next; 158 | pair_t *pair; 159 | unsigned int i, index, new_size; 160 | 161 | free(hashtable->buckets); 162 | 163 | hashtable->num_buckets++; 164 | new_size = num_buckets(hashtable); 165 | 166 | hashtable->buckets = malloc(new_size * sizeof(bucket_t)); 167 | if(!hashtable->buckets) 168 | return -1; 169 | 170 | for(i = 0; i < num_buckets(hashtable); i++) 171 | { 172 | hashtable->buckets[i].first = hashtable->buckets[i].last = 173 | &hashtable->list; 174 | } 175 | 176 | list = hashtable->list.next; 177 | list_init(&hashtable->list); 178 | 179 | for(; list != &hashtable->list; list = next) { 180 | next = list->next; 181 | pair = list_to_pair(list); 182 | index = pair->hash % new_size; 183 | insert_to_bucket(hashtable, &hashtable->buckets[index], &pair->list); 184 | } 185 | 186 | return 0; 187 | } 188 | 189 | 190 | hashtable_t *hashtable_create(key_hash_fn hash_key, key_cmp_fn cmp_keys, 191 | free_fn free_key, free_fn free_value) 192 | { 193 | hashtable_t *hashtable = malloc(sizeof(hashtable_t)); 194 | if(!hashtable) 195 | return NULL; 196 | 197 | if(hashtable_init(hashtable, hash_key, cmp_keys, free_key, free_value)) 198 | { 199 | free(hashtable); 200 | return NULL; 201 | } 202 | 203 | return hashtable; 204 | } 205 | 206 | void hashtable_destroy(hashtable_t *hashtable) 207 | { 208 | hashtable_close(hashtable); 209 | free(hashtable); 210 | } 211 | 212 | int hashtable_init(hashtable_t *hashtable, 213 | key_hash_fn hash_key, key_cmp_fn cmp_keys, 214 | free_fn free_key, free_fn free_value) 215 | { 216 | unsigned int i; 217 | 218 | hashtable->size = 0; 219 | hashtable->num_buckets = 0; /* index to primes[] */ 220 | hashtable->buckets = malloc(num_buckets(hashtable) * sizeof(bucket_t)); 221 | if(!hashtable->buckets) 222 | return -1; 223 | 224 | list_init(&hashtable->list); 225 | 226 | hashtable->hash_key = hash_key; 227 | hashtable->cmp_keys = cmp_keys; 228 | hashtable->free_key = free_key; 229 | hashtable->free_value = free_value; 230 | 231 | for(i = 0; i < num_buckets(hashtable); i++) 232 | { 233 | hashtable->buckets[i].first = hashtable->buckets[i].last = 234 | &hashtable->list; 235 | } 236 | 237 | return 0; 238 | } 239 | 240 | void hashtable_close(hashtable_t *hashtable) 241 | { 242 | hashtable_do_clear(hashtable); 243 | free(hashtable->buckets); 244 | } 245 | 246 | int hashtable_set(hashtable_t *hashtable, void *key, void *value) 247 | { 248 | pair_t *pair; 249 | bucket_t *bucket; 250 | unsigned int hash, index; 251 | 252 | /* rehash if the load ratio exceeds 1 */ 253 | if(hashtable->size >= num_buckets(hashtable)) 254 | if(hashtable_do_rehash(hashtable)) 255 | return -1; 256 | 257 | hash = hashtable->hash_key(key); 258 | index = hash % num_buckets(hashtable); 259 | bucket = &hashtable->buckets[index]; 260 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 261 | 262 | if(pair) 263 | { 264 | if(hashtable->free_key) 265 | hashtable->free_key(key); 266 | if(hashtable->free_value) 267 | hashtable->free_value(pair->value); 268 | pair->value = value; 269 | } 270 | else 271 | { 272 | pair = malloc(sizeof(pair_t)); 273 | if(!pair) 274 | return -1; 275 | 276 | pair->key = key; 277 | pair->value = value; 278 | pair->hash = hash; 279 | list_init(&pair->list); 280 | 281 | insert_to_bucket(hashtable, bucket, &pair->list); 282 | 283 | hashtable->size++; 284 | } 285 | return 0; 286 | } 287 | 288 | void *hashtable_get(hashtable_t *hashtable, const void *key) 289 | { 290 | pair_t *pair; 291 | unsigned int hash; 292 | bucket_t *bucket; 293 | 294 | hash = hashtable->hash_key(key); 295 | bucket = &hashtable->buckets[hash % num_buckets(hashtable)]; 296 | 297 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 298 | if(!pair) 299 | return NULL; 300 | 301 | return pair->value; 302 | } 303 | 304 | int hashtable_del(hashtable_t *hashtable, const void *key) 305 | { 306 | unsigned int hash = hashtable->hash_key(key); 307 | return hashtable_do_del(hashtable, key, hash); 308 | } 309 | 310 | void hashtable_clear(hashtable_t *hashtable) 311 | { 312 | unsigned int i; 313 | 314 | hashtable_do_clear(hashtable); 315 | 316 | for(i = 0; i < num_buckets(hashtable); i++) 317 | { 318 | hashtable->buckets[i].first = hashtable->buckets[i].last = 319 | &hashtable->list; 320 | } 321 | 322 | list_init(&hashtable->list); 323 | hashtable->size = 0; 324 | } 325 | 326 | void *hashtable_iter(hashtable_t *hashtable) 327 | { 328 | return hashtable_iter_next(hashtable, &hashtable->list); 329 | } 330 | 331 | void *hashtable_iter_at(hashtable_t *hashtable, const void *key) 332 | { 333 | pair_t *pair; 334 | unsigned int hash; 335 | bucket_t *bucket; 336 | 337 | hash = hashtable->hash_key(key); 338 | bucket = &hashtable->buckets[hash % num_buckets(hashtable)]; 339 | 340 | pair = hashtable_find_pair(hashtable, bucket, key, hash); 341 | if(!pair) 342 | return NULL; 343 | 344 | return &pair->list; 345 | } 346 | 347 | void *hashtable_iter_next(hashtable_t *hashtable, void *iter) 348 | { 349 | list_t *list = (list_t *)iter; 350 | if(list->next == &hashtable->list) 351 | return NULL; 352 | return list->next; 353 | } 354 | 355 | void *hashtable_iter_key(void *iter) 356 | { 357 | pair_t *pair = list_to_pair((list_t *)iter); 358 | return pair->key; 359 | } 360 | 361 | void *hashtable_iter_value(void *iter) 362 | { 363 | pair_t *pair = list_to_pair((list_t *)iter); 364 | return pair->value; 365 | } 366 | 367 | void hashtable_iter_set(hashtable_t *hashtable, void *iter, void *value) 368 | { 369 | pair_t *pair = list_to_pair((list_t *)iter); 370 | 371 | if(hashtable->free_value) 372 | hashtable->free_value(pair->value); 373 | 374 | pair->value = value; 375 | } 376 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | Version 2.5.0 - Jun 22, 2017 2 | 3 | - Add Segwit support 4 | - Add support for 64-bit PowerPC 5 | 6 | Version 2.4.5 - Jun 10, 2016 7 | 8 | - Fix a memory leak affecting long polling 9 | 10 | Version 2.4.4 - Mar 24, 2016 11 | 12 | - Fix memory leaks affecting getblocktemplate 13 | 14 | Version 2.4.3 - Jan 24, 2016 15 | 16 | - Add support for the VIA PadLock Hash Engine on x86-64 17 | - Allow block version 4 when using getblocktemplate 18 | 19 | Version 2.4.2 - Apr 28, 2015 20 | 21 | - Add support for Stratum over TLS 22 | - Allow block version 3 when using getblocktemplate 23 | 24 | Version 2.4.1 - Feb 25, 2015 25 | 26 | - Add support for scrypt(N, 1, 1) 27 | - Add optimized PowerPC code for scrypt and SHA-256d 28 | - Allow use of getblocktemplate with merge-mined cryptocurrencies 29 | - Automatically switch to getwork if no payout address is provided 30 | - Fix CVE-2014-6251 31 | 32 | Version 2.4 - May 20, 2014 33 | 34 | - Add support for the getblocktemplate RPC method (BIP 22) 35 | - Allow tunnelling Stratum through HTTP proxies 36 | - Add a --no-redirect option to ignore redirection requests 37 | - Timeout for long polling is now disabled by default 38 | - Fix CPU affinity on Linux (kiyominer) 39 | - Add support for building under 64-bit Cygwin 40 | - Expand version information with build details 41 | 42 | Version 2.3.3 - Feb 27, 2014 43 | 44 | - The --url option is now mandatory 45 | - Do not switch to Stratum when using an HTTP proxy 46 | - Fix scheduling policy change on Linux (clbr) 47 | - Fix CPU affinity on FreeBSD (ache) 48 | - Compatibility fixes for various platforms, including Solaris 8 49 | and old versions of OS X 50 | - A man page for minerd is now available 51 | 52 | Version 2.3.2 - Jul 10, 2013 53 | 54 | - Add optimizations for AVX2-capable x86-64 processors 55 | - Ensure that the output stream is flushed after every log message 56 | - Fix an undefined-behavior bug in the Stratum code 57 | 58 | Version 2.3.1 - Jun 18, 2013 59 | 60 | - Add a --cert option for specifying an SSL certificate (martinwguy) 61 | - Fix a bug that only made SHA-256d mining work at difficulty 1 62 | - Fix a couple of compatibility issues with some Stratum servers 63 | 64 | Version 2.3 - Jun 12, 2013 65 | 66 | - Add support for the Stratum mining protocol 67 | - Automatically switch to Stratum if the mining server supports 68 | the X-Stratum extension, unless --no-stratum is used 69 | - Set CPU affinity on FreeBSD (lye) 70 | - Fix a bug in libcurl initialization (martinwguy) 71 | 72 | Version 2.2.3 - Aug 5, 2012 73 | 74 | - Add optimized ARM NEON code for scrypt and SHA-256d 75 | - Add a --benchmark option that allows offline testing 76 | - Support for the X-Reject-Reason extension 77 | 78 | Version 2.2.2 - Jun 7, 2012 79 | 80 | - Various performance improvements for x86 and x86-64 81 | - Optimize scrypt for ARMv5E and later processors 82 | - Set the priority of miner threads to idle on Windows 83 | - Add an option to start minerd as a daemon on POSIX systems 84 | 85 | Version 2.2.1 - May 2, 2012 86 | 87 | - Add optimized code for ARM processors 88 | - Support for building on NetBSD and OpenBSD 89 | - Various compatibility fixes for AIX (pontius) 90 | 91 | Version 2.2 - Apr 2, 2012 92 | 93 | - Add an optimized SHA-256d algorithm, with specialized code 94 | for x86 and x86-64 and support for AVX and XOP instructions 95 | - Slight performance increase for scrypt on x86 and x86-64 96 | - The default timeout is now 270 seconds 97 | 98 | Version 2.1.5 - Mar 7, 2012 99 | 100 | - Add optimizations for AVX-capable x86-64 processors 101 | - Assume HTTP if no protocol is specified for the mining server 102 | - Fix MinGW compatibility issues and update build instructions 103 | - Add support for building on Solaris using gcc (pontius) 104 | 105 | Version 2.1.4 - Feb 28, 2012 106 | 107 | - Implement 4-way SHA-256 on x86-64 108 | - Add TCP keepalive to long polling connections 109 | - Support HTTP and SOCKS proxies via the --proxy option 110 | - Username and password are no longer mandatory 111 | - Add a script that makes assembly code compatible with old versions 112 | of the GNU assembler that do not support macros 113 | 114 | Version 2.1.3 - Feb 12, 2012 115 | 116 | - Smart handling of long polling failures: switch to short scan time 117 | if long polling fails, and only try to reactivate it if the server 118 | continues to advertise the feature in HTTP headers 119 | - Add "X-Mining-Extensions: midstate" to HTTP headers (p2k) 120 | - Add support for the "submitold" extension, used by p2pool 121 | - It is now possible to specify username and password in the URL, 122 | like this: http://username:password@host:port/ 123 | - Add a --version option, and clean up --help output 124 | - Avoid division by zero when computing hash rates 125 | - Handle empty responses properly (TimothyA) 126 | - Eliminate the delay between starting threads 127 | 128 | Version 2.1.2 - Jan 26, 2012 129 | 130 | - Do not submit work that is known to be stale 131 | - Allow miner threads to ask for new work if the current one is at least 132 | 45 seconds old and long polling is enabled 133 | - Refresh work when long polling times out 134 | - Fix minor speed regression 135 | - Modify x86-64 code to make it compatible with older versions of binutils 136 | 137 | Version 2.1.1 - Jan 20, 2012 138 | 139 | - Handle network errors properly 140 | - Make scantime retargeting more accurate 141 | 142 | Version 2.1 - Jan 19, 2012 143 | 144 | - Share the same work among all threads 145 | - Do not ask for new work if the current one is not expired 146 | - Do not discard the work returned by long polling 147 | 148 | Version 2.0 - Jan 16, 2012 149 | 150 | - Change default port to 9332 for Litecoin and remove default credentials 151 | - Add 'scrypt' as the default algorithm and remove other algorithms (ArtForz) 152 | - Optimize scrypt for x86 and x86-64 153 | - Make scantime retargeting less granular (ArtForz) 154 | - Test the whole hash instead of just looking at the high 32 bits 155 | - Add configurable timeout, with a default of 180 seconds 156 | - Add share summary output (inlikeflynn) 157 | - Fix priority and CPU count detection on Windows 158 | - Fix parameters -u and -p, and add short options -o and -O 159 | 160 | Version 1.0.2 - Jun 13, 2011 161 | 162 | - Linux x86_64 optimisations - Con Kolivas 163 | - Optimise for x86_64 by default by using sse2_64 algo 164 | - Detects CPUs and sets number of threads accordingly 165 | - Uses CPU affinity for each thread where appropriate 166 | - Sets scheduling policy to lowest possible 167 | - Minor performance tweaks 168 | 169 | Version 1.0.1 - May 14, 2011 170 | 171 | - OSX support 172 | 173 | Version 1.0 - May 9, 2011 174 | 175 | - jansson 2.0 compatibility 176 | - correct off-by-one in date (month) display output 177 | - fix platform detection 178 | - improve yasm configure bits 179 | - support full URL, in X-Long-Polling header 180 | 181 | Version 0.8.1 - March 22, 2011 182 | 183 | - Make --user, --pass actually work 184 | 185 | - Add User-Agent HTTP header to requests, so that server operators may 186 | more easily identify the miner client. 187 | 188 | - Fix minor bug in example JSON config file 189 | 190 | Version 0.8 - March 21, 2011 191 | 192 | - Support long polling: http://deepbit.net/longpolling.php 193 | 194 | - Adjust max workload based on scantime (default 5 seconds, 195 | or 60 seconds for longpoll) 196 | 197 | - Standardize program output, and support syslog on Unix platforms 198 | 199 | - Suport --user/--pass options (and "user" and "pass" in config file), 200 | as an alternative to the current --userpass 201 | 202 | Version 0.7.2 - March 14, 2011 203 | 204 | - Add port of ufasoft's sse2 assembly implementation (Linux only) 205 | This is a substantial speed improvement on Intel CPUs. 206 | 207 | - Move all JSON-RPC I/O to separate thread. This reduces the 208 | number of HTTP connections from one-per-thread to one, reducing resource 209 | usage on upstream bitcoind / pool server. 210 | 211 | Version 0.7.1 - March 2, 2011 212 | 213 | - Add support for JSON-format configuration file. See example 214 | file example-cfg.json. Any long argument on the command line 215 | may be stored in the config file. 216 | - Timestamp each solution found 217 | - Improve sha256_4way performance. NOTE: This optimization makes 218 | the 'hash' debug-print output for sha256_way incorrect. 219 | - Use __builtin_expect() intrinsic as compiler micro-optimization 220 | - Build on Intel compiler 221 | - HTTP library now follows HTTP redirects 222 | 223 | Version 0.7 - February 12, 2011 224 | 225 | - Re-use CURL object, thereby reuseing DNS cache and HTTP connections 226 | - Use bswap_32, if compiler intrinsic is not available 227 | - Disable full target validation (as opposed to simply H==0) for now 228 | 229 | Version 0.6.1 - February 4, 2011 230 | 231 | - Fully validate "hash < target", rather than simply stopping our scan 232 | if the high 32 bits are 00000000. 233 | - Add --retry-pause, to set length of pause time between failure retries 234 | - Display proof-of-work hash and target, if -D (debug mode) enabled 235 | - Fix max-nonce auto-adjustment to actually work. This means if your 236 | scan takes longer than 5 seconds (--scantime), the miner will slowly 237 | reduce the number of hashes you work on, before fetching a new work unit. 238 | 239 | Version 0.6 - January 29, 2011 240 | 241 | - Fetch new work unit, if scanhash takes longer than 5 seconds (--scantime) 242 | - BeeCee1's sha256 4way optimizations 243 | - lfm's byte swap optimization (improves via, cryptopp) 244 | - Fix non-working short options -q, -r 245 | 246 | Version 0.5 - December 28, 2010 247 | 248 | - Exit program, when all threads have exited 249 | - Improve JSON-RPC failure diagnostics and resilience 250 | - Add --quiet option, to disable hashmeter output. 251 | 252 | Version 0.3.3 - December 27, 2010 253 | 254 | - Critical fix for sha256_cryptopp 'cryptopp_asm' algo 255 | 256 | Version 0.3.2 - December 23, 2010 257 | 258 | - Critical fix for sha256_via 259 | 260 | Version 0.3.1 - December 19, 2010 261 | 262 | - Critical fix for sha256_via 263 | - Retry JSON-RPC failures (see --retry, under "minerd --help" output) 264 | 265 | Version 0.3 - December 18, 2010 266 | 267 | - Add crypto++ 32bit assembly implementation 268 | - show version upon 'minerd --help' 269 | - work around gcc 4.5.x bug that killed 4way performance 270 | 271 | Version 0.2.2 - December 6, 2010 272 | 273 | - VIA padlock implementation works now 274 | - Minor build and runtime fixes 275 | 276 | Version 0.2.1 - November 29, 2010 277 | 278 | - avoid buffer overflow when submitting solutions 279 | - add Crypto++ sha256 implementation (C only, ASM elided for now) 280 | - minor internal optimizations and cleanups 281 | 282 | Version 0.2 - November 27, 2010 283 | 284 | - Add script for building a Windows installer 285 | - improve hash performance (hashmeter) statistics 286 | - add tcatm 4way sha256 implementation 287 | - Add experimental VIA Padlock sha256 implementation 288 | 289 | Version 0.1.2 - November 26, 2010 290 | 291 | - many small cleanups and micro-optimizations 292 | - build win32 exe using mingw 293 | - RPC URL, username/password become command line arguments 294 | - remove unused OpenSSL dependency 295 | 296 | Version 0.1.1 - November 24, 2010 297 | 298 | - Do not build sha256_generic module separately from cpuminer. 299 | 300 | Version 0.1 - November 24, 2010 301 | 302 | - Initial release. 303 | 304 | -------------------------------------------------------------------------------- /sha256.c: -------------------------------------------------------------------------------- 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 | 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | #include "sysendian.h" 33 | 34 | #include "sha256.h" 35 | 36 | /* 37 | * Encode a length len/4 vector of (uint32_t) into a length len vector of 38 | * (unsigned char) in big-endian form. Assumes len is a multiple of 4. 39 | */ 40 | static void 41 | be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len) 42 | { 43 | size_t i; 44 | 45 | for (i = 0; i < len / 4; i++) 46 | be32enc(dst + i * 4, src[i]); 47 | } 48 | 49 | /* 50 | * Decode a big-endian length len vector of (unsigned char) into a length 51 | * len/4 vector of (uint32_t). Assumes len is a multiple of 4. 52 | */ 53 | static void 54 | be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len) 55 | { 56 | size_t i; 57 | 58 | for (i = 0; i < len / 4; i++) 59 | dst[i] = be32dec(src + i * 4); 60 | } 61 | 62 | /* Elementary functions used by SHA256 */ 63 | #define Ch(x, y, z) ((x & (y ^ z)) ^ z) 64 | #define Maj(x, y, z) ((x & (y | z)) | (y & z)) 65 | #define SHR(x, n) (x >> n) 66 | #define ROTR(x, n) ((x >> n) | (x << (32 - n))) 67 | #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) 68 | #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) 69 | #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3)) 70 | #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10)) 71 | 72 | /* SHA256 round function */ 73 | #define RND(a, b, c, d, e, f, g, h, k) \ 74 | t0 = h + S1(e) + Ch(e, f, g) + k; \ 75 | t1 = S0(a) + Maj(a, b, c); \ 76 | d += t0; \ 77 | h = t0 + t1; 78 | 79 | /* Adjusted round function for rotating state */ 80 | #define RNDr(S, W, i, k) \ 81 | RND(S[(64 - i) % 8], S[(65 - i) % 8], \ 82 | S[(66 - i) % 8], S[(67 - i) % 8], \ 83 | S[(68 - i) % 8], S[(69 - i) % 8], \ 84 | S[(70 - i) % 8], S[(71 - i) % 8], \ 85 | W[i] + k) 86 | 87 | /* 88 | * SHA256 block compression function. The 256-bit state is transformed via 89 | * the 512-bit input block to produce a new state. 90 | */ 91 | static void 92 | SHA256_Transform(uint32_t * state, const unsigned char block[64]) 93 | { 94 | uint32_t W[64]; 95 | uint32_t S[8]; 96 | uint32_t t0, t1; 97 | int i; 98 | 99 | /* 1. Prepare message schedule W. */ 100 | be32dec_vect(W, block, 64); 101 | for (i = 16; i < 64; i++) 102 | W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16]; 103 | 104 | /* 2. Initialize working variables. */ 105 | memcpy(S, state, 32); 106 | 107 | /* 3. Mix. */ 108 | RNDr(S, W, 0, 0x428a2f98); 109 | RNDr(S, W, 1, 0x71374491); 110 | RNDr(S, W, 2, 0xb5c0fbcf); 111 | RNDr(S, W, 3, 0xe9b5dba5); 112 | RNDr(S, W, 4, 0x3956c25b); 113 | RNDr(S, W, 5, 0x59f111f1); 114 | RNDr(S, W, 6, 0x923f82a4); 115 | RNDr(S, W, 7, 0xab1c5ed5); 116 | RNDr(S, W, 8, 0xd807aa98); 117 | RNDr(S, W, 9, 0x12835b01); 118 | RNDr(S, W, 10, 0x243185be); 119 | RNDr(S, W, 11, 0x550c7dc3); 120 | RNDr(S, W, 12, 0x72be5d74); 121 | RNDr(S, W, 13, 0x80deb1fe); 122 | RNDr(S, W, 14, 0x9bdc06a7); 123 | RNDr(S, W, 15, 0xc19bf174); 124 | RNDr(S, W, 16, 0xe49b69c1); 125 | RNDr(S, W, 17, 0xefbe4786); 126 | RNDr(S, W, 18, 0x0fc19dc6); 127 | RNDr(S, W, 19, 0x240ca1cc); 128 | RNDr(S, W, 20, 0x2de92c6f); 129 | RNDr(S, W, 21, 0x4a7484aa); 130 | RNDr(S, W, 22, 0x5cb0a9dc); 131 | RNDr(S, W, 23, 0x76f988da); 132 | RNDr(S, W, 24, 0x983e5152); 133 | RNDr(S, W, 25, 0xa831c66d); 134 | RNDr(S, W, 26, 0xb00327c8); 135 | RNDr(S, W, 27, 0xbf597fc7); 136 | RNDr(S, W, 28, 0xc6e00bf3); 137 | RNDr(S, W, 29, 0xd5a79147); 138 | RNDr(S, W, 30, 0x06ca6351); 139 | RNDr(S, W, 31, 0x14292967); 140 | RNDr(S, W, 32, 0x27b70a85); 141 | RNDr(S, W, 33, 0x2e1b2138); 142 | RNDr(S, W, 34, 0x4d2c6dfc); 143 | RNDr(S, W, 35, 0x53380d13); 144 | RNDr(S, W, 36, 0x650a7354); 145 | RNDr(S, W, 37, 0x766a0abb); 146 | RNDr(S, W, 38, 0x81c2c92e); 147 | RNDr(S, W, 39, 0x92722c85); 148 | RNDr(S, W, 40, 0xa2bfe8a1); 149 | RNDr(S, W, 41, 0xa81a664b); 150 | RNDr(S, W, 42, 0xc24b8b70); 151 | RNDr(S, W, 43, 0xc76c51a3); 152 | RNDr(S, W, 44, 0xd192e819); 153 | RNDr(S, W, 45, 0xd6990624); 154 | RNDr(S, W, 46, 0xf40e3585); 155 | RNDr(S, W, 47, 0x106aa070); 156 | RNDr(S, W, 48, 0x19a4c116); 157 | RNDr(S, W, 49, 0x1e376c08); 158 | RNDr(S, W, 50, 0x2748774c); 159 | RNDr(S, W, 51, 0x34b0bcb5); 160 | RNDr(S, W, 52, 0x391c0cb3); 161 | RNDr(S, W, 53, 0x4ed8aa4a); 162 | RNDr(S, W, 54, 0x5b9cca4f); 163 | RNDr(S, W, 55, 0x682e6ff3); 164 | RNDr(S, W, 56, 0x748f82ee); 165 | RNDr(S, W, 57, 0x78a5636f); 166 | RNDr(S, W, 58, 0x84c87814); 167 | RNDr(S, W, 59, 0x8cc70208); 168 | RNDr(S, W, 60, 0x90befffa); 169 | RNDr(S, W, 61, 0xa4506ceb); 170 | RNDr(S, W, 62, 0xbef9a3f7); 171 | RNDr(S, W, 63, 0xc67178f2); 172 | 173 | /* 4. Mix local working variables into global state */ 174 | for (i = 0; i < 8; i++) 175 | state[i] += S[i]; 176 | 177 | /* Clean the stack. */ 178 | memset(W, 0, 256); 179 | memset(S, 0, 32); 180 | t0 = t1 = 0; 181 | } 182 | 183 | static unsigned char PAD[64] = { 184 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 186 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 187 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 188 | }; 189 | 190 | /* Add padding and terminating bit-count. */ 191 | static void 192 | SHA256_Pad(SHA256_CTX * ctx) 193 | { 194 | unsigned char len[8]; 195 | uint32_t r, plen; 196 | 197 | /* 198 | * Convert length to a vector of bytes -- we do this now rather 199 | * than later because the length will change after we pad. 200 | */ 201 | be32enc_vect(len, ctx->count, 8); 202 | 203 | /* Add 1--64 bytes so that the resulting length is 56 mod 64 */ 204 | r = (ctx->count[1] >> 3) & 0x3f; 205 | plen = (r < 56) ? (56 - r) : (120 - r); 206 | SHA256_Update(ctx, PAD, (size_t)plen); 207 | 208 | /* Add the terminating bit-count */ 209 | SHA256_Update(ctx, len, 8); 210 | } 211 | 212 | /* SHA-256 initialization. Begins a SHA-256 operation. */ 213 | static void 214 | SHA256_Init(SHA256_CTX * ctx) 215 | { 216 | 217 | /* Zero bits processed so far */ 218 | ctx->count[0] = ctx->count[1] = 0; 219 | 220 | /* Magic initialization constants */ 221 | ctx->state[0] = 0x6A09E667; 222 | ctx->state[1] = 0xBB67AE85; 223 | ctx->state[2] = 0x3C6EF372; 224 | ctx->state[3] = 0xA54FF53A; 225 | ctx->state[4] = 0x510E527F; 226 | ctx->state[5] = 0x9B05688C; 227 | ctx->state[6] = 0x1F83D9AB; 228 | ctx->state[7] = 0x5BE0CD19; 229 | } 230 | 231 | /* Add bytes into the hash */ 232 | static void 233 | SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) 234 | { 235 | uint32_t bitlen[2]; 236 | uint32_t r; 237 | const unsigned char *src = in; 238 | 239 | /* Number of bytes left in the buffer from previous updates */ 240 | r = (ctx->count[1] >> 3) & 0x3f; 241 | 242 | /* Convert the length into a number of bits */ 243 | bitlen[1] = ((uint32_t)len) << 3; 244 | bitlen[0] = (uint32_t)(len >> 29); 245 | 246 | /* Update number of bits */ 247 | if ((ctx->count[1] += bitlen[1]) < bitlen[1]) 248 | ctx->count[0]++; 249 | ctx->count[0] += bitlen[0]; 250 | 251 | /* Handle the case where we don't need to perform any transforms */ 252 | if (len < 64 - r) { 253 | memcpy(&ctx->buf[r], src, len); 254 | return; 255 | } 256 | 257 | /* Finish the current block */ 258 | memcpy(&ctx->buf[r], src, 64 - r); 259 | SHA256_Transform(ctx->state, ctx->buf); 260 | src += 64 - r; 261 | len -= 64 - r; 262 | 263 | /* Perform complete blocks */ 264 | while (len >= 64) { 265 | SHA256_Transform(ctx->state, src); 266 | src += 64; 267 | len -= 64; 268 | } 269 | 270 | /* Copy left over data into buffer */ 271 | memcpy(ctx->buf, src, len); 272 | } 273 | 274 | /* 275 | * SHA-256 finalization. Pads the input data, exports the hash value, 276 | * and clears the context state. 277 | */ 278 | static void 279 | SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx) 280 | { 281 | 282 | /* Add padding */ 283 | SHA256_Pad(ctx); 284 | 285 | /* Write the hash */ 286 | be32enc_vect(digest, ctx->state, 32); 287 | 288 | /* Clear the context state */ 289 | memset((void *)ctx, 0, sizeof(*ctx)); 290 | } 291 | 292 | /* Initialize an HMAC-SHA256 operation with the given key. */ 293 | static void 294 | HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen) 295 | { 296 | unsigned char pad[64]; 297 | unsigned char khash[32]; 298 | const unsigned char * K = _K; 299 | size_t i; 300 | 301 | /* If Klen > 64, the key is really SHA256(K). */ 302 | if (Klen > 64) { 303 | SHA256_Init(&ctx->ictx); 304 | SHA256_Update(&ctx->ictx, K, Klen); 305 | SHA256_Final(khash, &ctx->ictx); 306 | K = khash; 307 | Klen = 32; 308 | } 309 | 310 | /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */ 311 | SHA256_Init(&ctx->ictx); 312 | memset(pad, 0x36, 64); 313 | for (i = 0; i < Klen; i++) 314 | pad[i] ^= K[i]; 315 | SHA256_Update(&ctx->ictx, pad, 64); 316 | 317 | /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */ 318 | SHA256_Init(&ctx->octx); 319 | memset(pad, 0x5c, 64); 320 | for (i = 0; i < Klen; i++) 321 | pad[i] ^= K[i]; 322 | SHA256_Update(&ctx->octx, pad, 64); 323 | 324 | /* Clean the stack. */ 325 | memset(khash, 0, 32); 326 | } 327 | 328 | /* Add bytes to the HMAC-SHA256 operation. */ 329 | static void 330 | HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void *in, size_t len) 331 | { 332 | 333 | /* Feed data to the inner SHA256 operation. */ 334 | SHA256_Update(&ctx->ictx, in, len); 335 | } 336 | 337 | /* Finish an HMAC-SHA256 operation. */ 338 | static void 339 | HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx) 340 | { 341 | unsigned char ihash[32]; 342 | 343 | /* Finish the inner SHA256 operation. */ 344 | SHA256_Final(ihash, &ctx->ictx); 345 | 346 | /* Feed the inner hash to the outer SHA256 operation. */ 347 | SHA256_Update(&ctx->octx, ihash, 32); 348 | 349 | /* Finish the outer SHA256 operation. */ 350 | SHA256_Final(digest, &ctx->octx); 351 | 352 | /* Clean the stack. */ 353 | memset(ihash, 0, 32); 354 | } 355 | 356 | /** 357 | * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): 358 | * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and 359 | * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). 360 | */ 361 | static void 362 | PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, 363 | size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen) 364 | { 365 | HMAC_SHA256_CTX PShctx, hctx; 366 | size_t i; 367 | uint8_t ivec[4]; 368 | uint8_t U[32]; 369 | uint8_t T[32]; 370 | uint64_t j; 371 | int k; 372 | size_t clen; 373 | 374 | /* Compute HMAC state after processing P and S. */ 375 | HMAC_SHA256_Init(&PShctx, passwd, passwdlen); 376 | HMAC_SHA256_Update(&PShctx, salt, saltlen); 377 | 378 | /* Iterate through the blocks. */ 379 | for (i = 0; i * 32 < dkLen; i++) { 380 | /* Generate INT(i + 1). */ 381 | be32enc(ivec, (uint32_t)(i + 1)); 382 | 383 | /* Compute U_1 = PRF(P, S || INT(i)). */ 384 | memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX)); 385 | HMAC_SHA256_Update(&hctx, ivec, 4); 386 | HMAC_SHA256_Final(U, &hctx); 387 | 388 | /* T_i = U_1 ... */ 389 | memcpy(T, U, 32); 390 | 391 | for (j = 2; j <= c; j++) { 392 | /* Compute U_j. */ 393 | HMAC_SHA256_Init(&hctx, passwd, passwdlen); 394 | HMAC_SHA256_Update(&hctx, U, 32); 395 | HMAC_SHA256_Final(U, &hctx); 396 | 397 | /* ... xor U_j ... */ 398 | for (k = 0; k < 32; k++) 399 | T[k] ^= U[k]; 400 | } 401 | 402 | /* Copy as many bytes as necessary into buf. */ 403 | clen = dkLen - i * 32; 404 | if (clen > 32) 405 | clen = 32; 406 | memcpy(&buf[i * 32], T, clen); 407 | } 408 | 409 | /* Clean PShctx, since we never called _Final on it. */ 410 | memset(&PShctx, 0, sizeof(HMAC_SHA256_CTX)); 411 | } 412 | -------------------------------------------------------------------------------- /yescrypt.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 2009 Colin Percival 3 | * Copyright 2013,2014 Alexander Peslyak 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | * 27 | * This file was originally written by Colin Percival as part of the Tarsnap 28 | * online backup system. 29 | */ 30 | #ifndef _YESCRYPT_H_ 31 | #define _YESCRYPT_H_ 32 | 33 | #include 34 | #include /* for size_t */ 35 | 36 | /** 37 | * Internal type used by the memory allocator. Please do not use it directly. 38 | * Use yescrypt_shared_t and yescrypt_local_t as appropriate instead, since 39 | * they might differ from each other in a future version. 40 | */ 41 | typedef struct { 42 | void * base, * aligned; 43 | size_t base_size, aligned_size; 44 | } yescrypt_region_t; 45 | 46 | /** 47 | * Types for shared (ROM) and thread-local (RAM) data structures. 48 | */ 49 | typedef yescrypt_region_t yescrypt_shared1_t; 50 | typedef struct { 51 | yescrypt_shared1_t shared1; 52 | uint32_t mask1; 53 | } yescrypt_shared_t; 54 | typedef yescrypt_region_t yescrypt_local_t; 55 | 56 | /** 57 | * Possible values for yescrypt_init_shared()'s flags argument. 58 | */ 59 | typedef enum { 60 | YESCRYPT_SHARED_DEFAULTS = 0, 61 | YESCRYPT_SHARED_PREALLOCATED = 0x100 62 | } yescrypt_init_shared_flags_t; 63 | 64 | /** 65 | * Possible values for the flags argument of yescrypt_kdf(), 66 | * yescrypt_gensalt_r(), yescrypt_gensalt(). These may be OR'ed together, 67 | * except that YESCRYPT_WORM and YESCRYPT_RW are mutually exclusive. 68 | * Please refer to the description of yescrypt_kdf() below for the meaning of 69 | * these flags. 70 | */ 71 | typedef enum { 72 | /* public */ 73 | YESCRYPT_WORM = 0, 74 | YESCRYPT_RW = 1, 75 | YESCRYPT_PARALLEL_SMIX = 2, 76 | YESCRYPT_PWXFORM = 4, 77 | /* private */ 78 | __YESCRYPT_INIT_SHARED_1 = 0x10000, 79 | __YESCRYPT_INIT_SHARED_2 = 0x20000, 80 | __YESCRYPT_INIT_SHARED = 0x30000 81 | } yescrypt_flags_t; 82 | 83 | #define YESCRYPT_KNOWN_FLAGS \ 84 | (YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | YESCRYPT_PWXFORM | \ 85 | __YESCRYPT_INIT_SHARED) 86 | 87 | /** 88 | * yescrypt_init_shared(shared, param, paramlen, N, r, p, flags, mask, 89 | * buf, buflen): 90 | * Optionally allocate memory for and initialize the shared (ROM) data 91 | * structure. The parameters N, r, and p must satisfy the same conditions as 92 | * with crypto_scrypt(). param and paramlen specify a local parameter with 93 | * which the ROM is seeded. If buf is not NULL, then it is used to return 94 | * buflen bytes of message digest for the initialized ROM (the caller may use 95 | * this to verify that the ROM has been computed in the same way that it was on 96 | * a previous run). 97 | * 98 | * Return 0 on success; or -1 on error. 99 | * 100 | * If bit YESCRYPT_SHARED_PREALLOCATED in flags is set, then memory for the 101 | * ROM is assumed to have been preallocated by the caller, with 102 | * shared->shared1.aligned being the start address of the ROM and 103 | * shared->shared1.aligned_size being its size (which must be consistent with 104 | * N, r, and p). This may be used e.g. when the ROM is to be placed in a SysV 105 | * shared memory segment allocated by the caller. 106 | * 107 | * mask controls the frequency of ROM accesses by yescrypt_kdf(). Normally it 108 | * should be set to 1, to interleave RAM and ROM accesses, which works well 109 | * when both regions reside in the machine's RAM anyway. Other values may be 110 | * used e.g. when the ROM is memory-mapped from a disk file. Recommended mask 111 | * values are powers of 2 minus 1 or minus 2. Here's the effect of some mask 112 | * values: 113 | * mask value ROM accesses in SMix 1st loop ROM accesses in SMix 2nd loop 114 | * 0 0 1/2 115 | * 1 1/2 1/2 116 | * 2 0 1/4 117 | * 3 1/4 1/4 118 | * 6 0 1/8 119 | * 7 1/8 1/8 120 | * 14 0 1/16 121 | * 15 1/16 1/16 122 | * 1022 0 1/1024 123 | * 1023 1/1024 1/1024 124 | * 125 | * Actual computation of the ROM contents may be avoided, if you don't intend 126 | * to use a ROM but need a dummy shared structure, by calling this function 127 | * with NULL, 0, 0, 0, 0, YESCRYPT_SHARED_DEFAULTS, 0, NULL, 0 for the 128 | * arguments starting with param and on. 129 | * 130 | * MT-safe as long as shared is local to the thread. 131 | */ 132 | static int yescrypt_init_shared(yescrypt_shared_t * __shared, 133 | const uint8_t * __param, size_t __paramlen, 134 | uint64_t __N, uint32_t __r, uint32_t __p, 135 | yescrypt_init_shared_flags_t __flags, uint32_t __mask, 136 | uint8_t * __buf, size_t __buflen); 137 | 138 | /** 139 | * yescrypt_free_shared(shared): 140 | * Free memory that had been allocated with yescrypt_init_shared(). 141 | * 142 | * Return 0 on success; or -1 on error. 143 | * 144 | * MT-safe as long as shared is local to the thread. 145 | */ 146 | static int yescrypt_free_shared(yescrypt_shared_t * __shared); 147 | 148 | /** 149 | * yescrypt_init_local(local): 150 | * Initialize the thread-local (RAM) data structure. Actual memory allocation 151 | * is currently fully postponed until a call to yescrypt_kdf() or yescrypt_r(). 152 | * 153 | * Return 0 on success; or -1 on error. 154 | * 155 | * MT-safe as long as local is local to the thread. 156 | */ 157 | static int yescrypt_init_local(yescrypt_local_t * __local); 158 | 159 | /** 160 | * yescrypt_free_local(local): 161 | * Free memory that may have been allocated for an initialized thread-local 162 | * (RAM) data structure. 163 | * 164 | * Return 0 on success; or -1 on error. 165 | * 166 | * MT-safe as long as local is local to the thread. 167 | */ 168 | static int yescrypt_free_local(yescrypt_local_t * __local); 169 | 170 | /** 171 | * yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen, 172 | * N, r, p, t, flags, buf, buflen): 173 | * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, 174 | * p, buflen), or a revision of scrypt as requested by flags and shared, and 175 | * write the result into buf. The parameters N, r, p, and buflen must satisfy 176 | * the same conditions as with crypto_scrypt(). t controls computation time 177 | * while not affecting peak memory usage. shared and flags may request 178 | * special modes as described below. local is the thread-local data 179 | * structure, allowing to preserve and reuse a memory allocation across calls, 180 | * thereby reducing its overhead. 181 | * 182 | * Return 0 on success; or -1 on error. 183 | * 184 | * t controls computation time. t = 0 is optimal in terms of achieving the 185 | * highest area-time for ASIC attackers. Thus, higher computation time, if 186 | * affordable, is best achieved by increasing N rather than by increasing t. 187 | * However, if the higher memory usage (which goes along with higher N) is not 188 | * affordable, or if fine-tuning of the time is needed (recall that N must be a 189 | * power of 2), then t = 1 or above may be used to increase time while staying 190 | * at the same peak memory usage. t = 1 increases the time by 25% and 191 | * decreases the normalized area-time to 96% of optimal. (Of course, in 192 | * absolute terms the area-time increases with higher t. It's just that it 193 | * would increase slightly more with higher N*r rather than with higher t.) 194 | * t = 2 increases the time by another 20% and decreases the normalized 195 | * area-time to 89% of optimal. Thus, these two values are reasonable to use 196 | * for fine-tuning. Values of t higher than 2 result in further increase in 197 | * time while reducing the efficiency much further (e.g., down to around 50% of 198 | * optimal for t = 5, which runs 3 to 4 times slower than t = 0, with exact 199 | * numbers varying by the flags settings). 200 | * 201 | * Classic scrypt is available by setting t = 0 and flags to YESCRYPT_WORM and 202 | * passing a dummy shared structure (see the description of 203 | * yescrypt_init_shared() above for how to produce one). In this mode, the 204 | * thread-local memory region (RAM) is first sequentially written to and then 205 | * randomly read from. This algorithm is friendly towards time-memory 206 | * tradeoffs (TMTO), available both to defenders (albeit not in this 207 | * implementation) and to attackers. 208 | * 209 | * Setting YESCRYPT_RW adds extra random reads and writes to the thread-local 210 | * memory region (RAM), which makes TMTO a lot less efficient. This may be 211 | * used to slow down the kinds of attackers who would otherwise benefit from 212 | * classic scrypt's efficient TMTO. Since classic scrypt's TMTO allows not 213 | * only for the tradeoff, but also for a decrease of attacker's area-time (by 214 | * up to a constant factor), setting YESCRYPT_RW substantially increases the 215 | * cost of attacks in area-time terms as well. Yet another benefit of it is 216 | * that optimal area-time is reached at an earlier time than with classic 217 | * scrypt, and t = 0 actually corresponds to this earlier completion time, 218 | * resulting in quicker hash computations (and thus in higher request rate 219 | * capacity). Due to these properties, YESCRYPT_RW should almost always be 220 | * set, except when compatibility with classic scrypt or TMTO-friendliness are 221 | * desired. 222 | * 223 | * YESCRYPT_PARALLEL_SMIX moves parallelism that is present with p > 1 to a 224 | * lower level as compared to where it is in classic scrypt. This reduces 225 | * flexibility for efficient computation (for both attackers and defenders) by 226 | * requiring that, short of resorting to TMTO, the full amount of memory be 227 | * allocated as needed for the specified p, regardless of whether that 228 | * parallelism is actually being fully made use of or not. (For comparison, a 229 | * single instance of classic scrypt may be computed in less memory without any 230 | * CPU time overhead, but in more real time, by not making full use of the 231 | * parallelism.) This may be desirable when the defender has enough memory 232 | * with sufficiently low latency and high bandwidth for efficient full parallel 233 | * execution, yet the required memory size is high enough that some likely 234 | * attackers might end up being forced to choose between using higher latency 235 | * memory than they could use otherwise (waiting for data longer) or using TMTO 236 | * (waiting for data more times per one hash computation). The area-time cost 237 | * for other kinds of attackers (who would use the same memory type and TMTO 238 | * factor or no TMTO either way) remains roughly the same, given the same 239 | * running time for the defender. In the TMTO-friendly YESCRYPT_WORM mode, as 240 | * long as the defender has enough memory that is just as fast as the smaller 241 | * per-thread regions would be, doesn't expect to ever need greater 242 | * flexibility (except possibly via TMTO), and doesn't need backwards 243 | * compatibility with classic scrypt, there are no other serious drawbacks to 244 | * this setting. In the YESCRYPT_RW mode, which is meant to discourage TMTO, 245 | * this new approach to parallelization makes TMTO less inefficient. (This is 246 | * an unfortunate side-effect of avoiding some random writes, as we have to in 247 | * order to allow for parallel threads to access a common memory region without 248 | * synchronization overhead.) Thus, in this mode this setting poses an extra 249 | * tradeoff of its own (higher area-time cost for a subset of attackers vs. 250 | * better TMTO resistance). Setting YESCRYPT_PARALLEL_SMIX also changes the 251 | * way the running time is to be controlled from N*r*p (for classic scrypt) to 252 | * N*r (in this modification). All of this applies only when p > 1. For 253 | * p = 1, this setting is a no-op. 254 | * 255 | * Passing a real shared structure, with ROM contents previously computed by 256 | * yescrypt_init_shared(), enables the use of ROM and requires YESCRYPT_RW for 257 | * the thread-local RAM region. In order to allow for initialization of the 258 | * ROM to be split into a separate program, the shared->shared1.aligned and 259 | * shared->shared1.aligned_size fields may be set by the caller of 260 | * yescrypt_kdf() manually rather than with yescrypt_init_shared(). 261 | * 262 | * local must be initialized with yescrypt_init_local(). 263 | * 264 | * MT-safe as long as local and buf are local to the thread. 265 | */ 266 | static int yescrypt_kdf(const yescrypt_shared_t * __shared, 267 | yescrypt_local_t * __local, 268 | const uint8_t * __passwd, size_t __passwdlen, 269 | const uint8_t * __salt, size_t __saltlen, 270 | uint64_t __N, uint32_t __r, uint32_t __p, uint32_t __t, 271 | yescrypt_flags_t __flags, 272 | uint8_t * __buf, size_t __buflen); 273 | 274 | #endif /* !_YESCRYPT_H_ */ 275 | -------------------------------------------------------------------------------- /compat/jansson/dump.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2009, 2010 Petri Lehtinen 3 | * 4 | * Jansson is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See LICENSE for details. 6 | */ 7 | 8 | #define _GNU_SOURCE 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include "jansson_private.h" 16 | #include "strbuffer.h" 17 | #include "utf.h" 18 | 19 | #define MAX_INTEGER_STR_LENGTH 100 20 | #define MAX_REAL_STR_LENGTH 100 21 | 22 | typedef int (*dump_func)(const char *buffer, int size, void *data); 23 | 24 | struct string 25 | { 26 | char *buffer; 27 | int length; 28 | int size; 29 | }; 30 | 31 | static int dump_to_strbuffer(const char *buffer, int size, void *data) 32 | { 33 | return strbuffer_append_bytes((strbuffer_t *)data, buffer, size); 34 | } 35 | 36 | static int dump_to_file(const char *buffer, int size, void *data) 37 | { 38 | FILE *dest = (FILE *)data; 39 | if(fwrite(buffer, size, 1, dest) != 1) 40 | return -1; 41 | return 0; 42 | } 43 | 44 | /* 256 spaces (the maximum indentation size) */ 45 | static char whitespace[] = " "; 46 | 47 | static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data) 48 | { 49 | if(JSON_INDENT(flags) > 0) 50 | { 51 | int i, ws_count = JSON_INDENT(flags); 52 | 53 | if(dump("\n", 1, data)) 54 | return -1; 55 | 56 | for(i = 0; i < depth; i++) 57 | { 58 | if(dump(whitespace, ws_count, data)) 59 | return -1; 60 | } 61 | } 62 | else if(space && !(flags & JSON_COMPACT)) 63 | { 64 | return dump(" ", 1, data); 65 | } 66 | return 0; 67 | } 68 | 69 | static int dump_string(const char *str, int ascii, dump_func dump, void *data) 70 | { 71 | const char *pos, *end; 72 | int32_t codepoint; 73 | 74 | if(dump("\"", 1, data)) 75 | return -1; 76 | 77 | end = pos = str; 78 | while(1) 79 | { 80 | const char *text; 81 | char seq[13]; 82 | int length; 83 | 84 | while(*end) 85 | { 86 | end = utf8_iterate(pos, &codepoint); 87 | if(!end) 88 | return -1; 89 | 90 | /* mandatory escape or control char */ 91 | if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20) 92 | break; 93 | 94 | /* non-ASCII */ 95 | if(ascii && codepoint > 0x7F) 96 | break; 97 | 98 | pos = end; 99 | } 100 | 101 | if(pos != str) { 102 | if(dump(str, pos - str, data)) 103 | return -1; 104 | } 105 | 106 | if(end == pos) 107 | break; 108 | 109 | /* handle \, ", and control codes */ 110 | length = 2; 111 | switch(codepoint) 112 | { 113 | case '\\': text = "\\\\"; break; 114 | case '\"': text = "\\\""; break; 115 | case '\b': text = "\\b"; break; 116 | case '\f': text = "\\f"; break; 117 | case '\n': text = "\\n"; break; 118 | case '\r': text = "\\r"; break; 119 | case '\t': text = "\\t"; break; 120 | default: 121 | { 122 | /* codepoint is in BMP */ 123 | if(codepoint < 0x10000) 124 | { 125 | sprintf(seq, "\\u%04x", codepoint); 126 | length = 6; 127 | } 128 | 129 | /* not in BMP -> construct a UTF-16 surrogate pair */ 130 | else 131 | { 132 | int32_t first, last; 133 | 134 | codepoint -= 0x10000; 135 | first = 0xD800 | ((codepoint & 0xffc00) >> 10); 136 | last = 0xDC00 | (codepoint & 0x003ff); 137 | 138 | sprintf(seq, "\\u%04x\\u%04x", first, last); 139 | length = 12; 140 | } 141 | 142 | text = seq; 143 | break; 144 | } 145 | } 146 | 147 | if(dump(text, length, data)) 148 | return -1; 149 | 150 | str = pos = end; 151 | } 152 | 153 | return dump("\"", 1, data); 154 | } 155 | 156 | static int object_key_compare_keys(const void *key1, const void *key2) 157 | { 158 | return strcmp((*(const object_key_t **)key1)->key, 159 | (*(const object_key_t **)key2)->key); 160 | } 161 | 162 | static int object_key_compare_serials(const void *key1, const void *key2) 163 | { 164 | return (*(const object_key_t **)key1)->serial - 165 | (*(const object_key_t **)key2)->serial; 166 | } 167 | 168 | static int do_dump(const json_t *json, unsigned long flags, int depth, 169 | dump_func dump, void *data) 170 | { 171 | int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0; 172 | 173 | switch(json_typeof(json)) { 174 | case JSON_NULL: 175 | return dump("null", 4, data); 176 | 177 | case JSON_TRUE: 178 | return dump("true", 4, data); 179 | 180 | case JSON_FALSE: 181 | return dump("false", 5, data); 182 | 183 | case JSON_INTEGER: 184 | { 185 | char buffer[MAX_INTEGER_STR_LENGTH]; 186 | int size; 187 | 188 | size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json)); 189 | if(size >= MAX_INTEGER_STR_LENGTH) 190 | return -1; 191 | 192 | return dump(buffer, size, data); 193 | } 194 | 195 | case JSON_REAL: 196 | { 197 | char buffer[MAX_REAL_STR_LENGTH]; 198 | int size; 199 | 200 | size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g", 201 | json_real_value(json)); 202 | if(size >= MAX_REAL_STR_LENGTH) 203 | return -1; 204 | 205 | /* Make sure there's a dot or 'e' in the output. Otherwise 206 | a real is converted to an integer when decoding */ 207 | if(strchr(buffer, '.') == NULL && 208 | strchr(buffer, 'e') == NULL) 209 | { 210 | if(size + 2 >= MAX_REAL_STR_LENGTH) { 211 | /* No space to append ".0" */ 212 | return -1; 213 | } 214 | buffer[size] = '.'; 215 | buffer[size + 1] = '0'; 216 | size += 2; 217 | } 218 | 219 | return dump(buffer, size, data); 220 | } 221 | 222 | case JSON_STRING: 223 | return dump_string(json_string_value(json), ascii, dump, data); 224 | 225 | case JSON_ARRAY: 226 | { 227 | int i; 228 | int n; 229 | json_array_t *array; 230 | 231 | /* detect circular references */ 232 | array = json_to_array(json); 233 | if(array->visited) 234 | goto array_error; 235 | array->visited = 1; 236 | 237 | n = json_array_size(json); 238 | 239 | if(dump("[", 1, data)) 240 | goto array_error; 241 | if(n == 0) { 242 | array->visited = 0; 243 | return dump("]", 1, data); 244 | } 245 | if(dump_indent(flags, depth + 1, 0, dump, data)) 246 | goto array_error; 247 | 248 | for(i = 0; i < n; ++i) { 249 | if(do_dump(json_array_get(json, i), flags, depth + 1, 250 | dump, data)) 251 | goto array_error; 252 | 253 | if(i < n - 1) 254 | { 255 | if(dump(",", 1, data) || 256 | dump_indent(flags, depth + 1, 1, dump, data)) 257 | goto array_error; 258 | } 259 | else 260 | { 261 | if(dump_indent(flags, depth, 0, dump, data)) 262 | goto array_error; 263 | } 264 | } 265 | 266 | array->visited = 0; 267 | return dump("]", 1, data); 268 | 269 | array_error: 270 | array->visited = 0; 271 | return -1; 272 | } 273 | 274 | case JSON_OBJECT: 275 | { 276 | json_object_t *object; 277 | void *iter; 278 | const char *separator; 279 | int separator_length; 280 | 281 | if(flags & JSON_COMPACT) { 282 | separator = ":"; 283 | separator_length = 1; 284 | } 285 | else { 286 | separator = ": "; 287 | separator_length = 2; 288 | } 289 | 290 | /* detect circular references */ 291 | object = json_to_object(json); 292 | if(object->visited) 293 | goto object_error; 294 | object->visited = 1; 295 | 296 | iter = json_object_iter((json_t *)json); 297 | 298 | if(dump("{", 1, data)) 299 | goto object_error; 300 | if(!iter) { 301 | object->visited = 0; 302 | return dump("}", 1, data); 303 | } 304 | if(dump_indent(flags, depth + 1, 0, dump, data)) 305 | goto object_error; 306 | 307 | if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER) 308 | { 309 | const object_key_t **keys; 310 | unsigned int size; 311 | unsigned int i; 312 | int (*cmp_func)(const void *, const void *); 313 | 314 | size = json_object_size(json); 315 | keys = malloc(size * sizeof(object_key_t *)); 316 | if(!keys) 317 | goto object_error; 318 | 319 | i = 0; 320 | while(iter) 321 | { 322 | keys[i] = jsonp_object_iter_fullkey(iter); 323 | iter = json_object_iter_next((json_t *)json, iter); 324 | i++; 325 | } 326 | assert(i == size); 327 | 328 | if(flags & JSON_SORT_KEYS) 329 | cmp_func = object_key_compare_keys; 330 | else 331 | cmp_func = object_key_compare_serials; 332 | 333 | qsort(keys, size, sizeof(object_key_t *), cmp_func); 334 | 335 | for(i = 0; i < size; i++) 336 | { 337 | const char *key; 338 | json_t *value; 339 | 340 | key = keys[i]->key; 341 | value = json_object_get(json, key); 342 | assert(value); 343 | 344 | dump_string(key, ascii, dump, data); 345 | if(dump(separator, separator_length, data) || 346 | do_dump(value, flags, depth + 1, dump, data)) 347 | { 348 | free(keys); 349 | goto object_error; 350 | } 351 | 352 | if(i < size - 1) 353 | { 354 | if(dump(",", 1, data) || 355 | dump_indent(flags, depth + 1, 1, dump, data)) 356 | { 357 | free(keys); 358 | goto object_error; 359 | } 360 | } 361 | else 362 | { 363 | if(dump_indent(flags, depth, 0, dump, data)) 364 | { 365 | free(keys); 366 | goto object_error; 367 | } 368 | } 369 | } 370 | 371 | free(keys); 372 | } 373 | else 374 | { 375 | /* Don't sort keys */ 376 | 377 | while(iter) 378 | { 379 | void *next = json_object_iter_next((json_t *)json, iter); 380 | 381 | dump_string(json_object_iter_key(iter), ascii, dump, data); 382 | if(dump(separator, separator_length, data) || 383 | do_dump(json_object_iter_value(iter), flags, depth + 1, 384 | dump, data)) 385 | goto object_error; 386 | 387 | if(next) 388 | { 389 | if(dump(",", 1, data) || 390 | dump_indent(flags, depth + 1, 1, dump, data)) 391 | goto object_error; 392 | } 393 | else 394 | { 395 | if(dump_indent(flags, depth, 0, dump, data)) 396 | goto object_error; 397 | } 398 | 399 | iter = next; 400 | } 401 | } 402 | 403 | object->visited = 0; 404 | return dump("}", 1, data); 405 | 406 | object_error: 407 | object->visited = 0; 408 | return -1; 409 | } 410 | 411 | default: 412 | /* not reached */ 413 | return -1; 414 | } 415 | } 416 | 417 | 418 | char *json_dumps(const json_t *json, unsigned long flags) 419 | { 420 | strbuffer_t strbuff; 421 | char *result; 422 | 423 | if(!json_is_array(json) && !json_is_object(json)) 424 | return NULL; 425 | 426 | if(strbuffer_init(&strbuff)) 427 | return NULL; 428 | 429 | if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) { 430 | strbuffer_close(&strbuff); 431 | return NULL; 432 | } 433 | 434 | result = strdup(strbuffer_value(&strbuff)); 435 | strbuffer_close(&strbuff); 436 | 437 | return result; 438 | } 439 | 440 | int json_dumpf(const json_t *json, FILE *output, unsigned long flags) 441 | { 442 | if(!json_is_array(json) && !json_is_object(json)) 443 | return -1; 444 | 445 | return do_dump(json, flags, 0, dump_to_file, (void *)output); 446 | } 447 | 448 | int json_dump_file(const json_t *json, const char *path, unsigned long flags) 449 | { 450 | int result; 451 | 452 | FILE *output = fopen(path, "w"); 453 | if(!output) 454 | return -1; 455 | 456 | result = json_dumpf(json, output, flags); 457 | 458 | fclose(output); 459 | return result; 460 | } 461 | -------------------------------------------------------------------------------- /sha2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 ArtForz 3 | * Copyright 2011-2013 pooler 4 | * 5 | * This program is free software; you can redistribute it and/or modify it 6 | * under the terms of the GNU General Public License as published by the Free 7 | * Software Foundation; either version 2 of the License, or (at your option) 8 | * any later version. See COPYING for more details. 9 | */ 10 | 11 | #include "cpuminer-config.h" 12 | #include "miner.h" 13 | 14 | #include 15 | #include 16 | 17 | #if defined(USE_ASM) && \ 18 | (defined(__x86_64__) || \ 19 | (defined(__arm__) && defined(__APCS_32__)) || \ 20 | (defined(__powerpc__) || defined(__ppc__) || defined(__PPC__))) 21 | #define EXTERN_SHA256 22 | #endif 23 | 24 | static const uint32_t sha256_h[8] = { 25 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 26 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 27 | }; 28 | 29 | static const uint32_t sha256_k[64] = { 30 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 31 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 32 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 33 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 34 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 35 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 36 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 37 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 38 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 39 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 40 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 41 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 42 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 43 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 44 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 45 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 46 | }; 47 | 48 | void sha256_init(uint32_t *state) 49 | { 50 | memcpy(state, sha256_h, 32); 51 | } 52 | 53 | /* Elementary functions used by SHA256 */ 54 | #define Ch(x, y, z) ((x & (y ^ z)) ^ z) 55 | #define Maj(x, y, z) ((x & (y | z)) | (y & z)) 56 | #define ROTR(x, n) ((x >> n) | (x << (32 - n))) 57 | #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) 58 | #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) 59 | #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ (x >> 3)) 60 | #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ (x >> 10)) 61 | 62 | /* SHA256 round function */ 63 | #define RND(a, b, c, d, e, f, g, h, k) \ 64 | do { \ 65 | t0 = h + S1(e) + Ch(e, f, g) + k; \ 66 | t1 = S0(a) + Maj(a, b, c); \ 67 | d += t0; \ 68 | h = t0 + t1; \ 69 | } while (0) 70 | 71 | /* Adjusted round function for rotating state */ 72 | #define RNDr(S, W, i) \ 73 | RND(S[(64 - i) % 8], S[(65 - i) % 8], \ 74 | S[(66 - i) % 8], S[(67 - i) % 8], \ 75 | S[(68 - i) % 8], S[(69 - i) % 8], \ 76 | S[(70 - i) % 8], S[(71 - i) % 8], \ 77 | W[i] + sha256_k[i]) 78 | 79 | #ifndef EXTERN_SHA256 80 | 81 | /* 82 | * SHA256 block compression function. The 256-bit state is transformed via 83 | * the 512-bit input block to produce a new state. 84 | */ 85 | void sha256_transform(uint32_t *state, const uint32_t *block, int swap) 86 | { 87 | uint32_t W[64]; 88 | uint32_t S[8]; 89 | uint32_t t0, t1; 90 | int i; 91 | 92 | /* 1. Prepare message schedule W. */ 93 | if (swap) { 94 | for (i = 0; i < 16; i++) 95 | W[i] = swab32(block[i]); 96 | } else 97 | memcpy(W, block, 64); 98 | for (i = 16; i < 64; i += 2) { 99 | W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16]; 100 | W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15]; 101 | } 102 | 103 | /* 2. Initialize working variables. */ 104 | memcpy(S, state, 32); 105 | 106 | /* 3. Mix. */ 107 | RNDr(S, W, 0); 108 | RNDr(S, W, 1); 109 | RNDr(S, W, 2); 110 | RNDr(S, W, 3); 111 | RNDr(S, W, 4); 112 | RNDr(S, W, 5); 113 | RNDr(S, W, 6); 114 | RNDr(S, W, 7); 115 | RNDr(S, W, 8); 116 | RNDr(S, W, 9); 117 | RNDr(S, W, 10); 118 | RNDr(S, W, 11); 119 | RNDr(S, W, 12); 120 | RNDr(S, W, 13); 121 | RNDr(S, W, 14); 122 | RNDr(S, W, 15); 123 | RNDr(S, W, 16); 124 | RNDr(S, W, 17); 125 | RNDr(S, W, 18); 126 | RNDr(S, W, 19); 127 | RNDr(S, W, 20); 128 | RNDr(S, W, 21); 129 | RNDr(S, W, 22); 130 | RNDr(S, W, 23); 131 | RNDr(S, W, 24); 132 | RNDr(S, W, 25); 133 | RNDr(S, W, 26); 134 | RNDr(S, W, 27); 135 | RNDr(S, W, 28); 136 | RNDr(S, W, 29); 137 | RNDr(S, W, 30); 138 | RNDr(S, W, 31); 139 | RNDr(S, W, 32); 140 | RNDr(S, W, 33); 141 | RNDr(S, W, 34); 142 | RNDr(S, W, 35); 143 | RNDr(S, W, 36); 144 | RNDr(S, W, 37); 145 | RNDr(S, W, 38); 146 | RNDr(S, W, 39); 147 | RNDr(S, W, 40); 148 | RNDr(S, W, 41); 149 | RNDr(S, W, 42); 150 | RNDr(S, W, 43); 151 | RNDr(S, W, 44); 152 | RNDr(S, W, 45); 153 | RNDr(S, W, 46); 154 | RNDr(S, W, 47); 155 | RNDr(S, W, 48); 156 | RNDr(S, W, 49); 157 | RNDr(S, W, 50); 158 | RNDr(S, W, 51); 159 | RNDr(S, W, 52); 160 | RNDr(S, W, 53); 161 | RNDr(S, W, 54); 162 | RNDr(S, W, 55); 163 | RNDr(S, W, 56); 164 | RNDr(S, W, 57); 165 | RNDr(S, W, 58); 166 | RNDr(S, W, 59); 167 | RNDr(S, W, 60); 168 | RNDr(S, W, 61); 169 | RNDr(S, W, 62); 170 | RNDr(S, W, 63); 171 | 172 | /* 4. Mix local working variables into global state */ 173 | for (i = 0; i < 8; i++) 174 | state[i] += S[i]; 175 | } 176 | 177 | #endif /* EXTERN_SHA256 */ 178 | 179 | 180 | static const uint32_t sha256d_hash1[16] = { 181 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 182 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 183 | 0x80000000, 0x00000000, 0x00000000, 0x00000000, 184 | 0x00000000, 0x00000000, 0x00000000, 0x00000100 185 | }; 186 | 187 | static void sha256d_80_swap(uint32_t *hash, const uint32_t *data) 188 | { 189 | uint32_t S[16]; 190 | int i; 191 | 192 | sha256_init(S); 193 | sha256_transform(S, data, 0); 194 | sha256_transform(S, data + 16, 0); 195 | memcpy(S + 8, sha256d_hash1 + 8, 32); 196 | sha256_init(hash); 197 | sha256_transform(hash, S, 0); 198 | for (i = 0; i < 8; i++) 199 | hash[i] = swab32(hash[i]); 200 | } 201 | 202 | void sha256d(unsigned char *hash, const unsigned char *data, int len) 203 | { 204 | uint32_t S[16], T[16]; 205 | int i, r; 206 | 207 | sha256_init(S); 208 | for (r = len; r > -9; r -= 64) { 209 | if (r < 64) 210 | memset(T, 0, 64); 211 | memcpy(T, data + len - r, r > 64 ? 64 : (r < 0 ? 0 : r)); 212 | if (r >= 0 && r < 64) 213 | ((unsigned char *)T)[r] = 0x80; 214 | for (i = 0; i < 16; i++) 215 | T[i] = be32dec(T + i); 216 | if (r < 56) 217 | T[15] = 8 * len; 218 | sha256_transform(S, T, 0); 219 | } 220 | memcpy(S + 8, sha256d_hash1 + 8, 32); 221 | sha256_init(T); 222 | sha256_transform(T, S, 0); 223 | for (i = 0; i < 8; i++) 224 | be32enc((uint32_t *)hash + i, T[i]); 225 | } 226 | 227 | static inline void sha256d_preextend(uint32_t *W) 228 | { 229 | W[16] = s1(W[14]) + W[ 9] + s0(W[ 1]) + W[ 0]; 230 | W[17] = s1(W[15]) + W[10] + s0(W[ 2]) + W[ 1]; 231 | W[18] = s1(W[16]) + W[11] + W[ 2]; 232 | W[19] = s1(W[17]) + W[12] + s0(W[ 4]); 233 | W[20] = W[13] + s0(W[ 5]) + W[ 4]; 234 | W[21] = W[14] + s0(W[ 6]) + W[ 5]; 235 | W[22] = W[15] + s0(W[ 7]) + W[ 6]; 236 | W[23] = W[16] + s0(W[ 8]) + W[ 7]; 237 | W[24] = W[17] + s0(W[ 9]) + W[ 8]; 238 | W[25] = s0(W[10]) + W[ 9]; 239 | W[26] = s0(W[11]) + W[10]; 240 | W[27] = s0(W[12]) + W[11]; 241 | W[28] = s0(W[13]) + W[12]; 242 | W[29] = s0(W[14]) + W[13]; 243 | W[30] = s0(W[15]) + W[14]; 244 | W[31] = s0(W[16]) + W[15]; 245 | } 246 | 247 | static inline void sha256d_prehash(uint32_t *S, const uint32_t *W) 248 | { 249 | uint32_t t0, t1; 250 | RNDr(S, W, 0); 251 | RNDr(S, W, 1); 252 | RNDr(S, W, 2); 253 | } 254 | 255 | #ifdef EXTERN_SHA256 256 | 257 | void sha256d_ms(uint32_t *hash, uint32_t *W, 258 | const uint32_t *midstate, const uint32_t *prehash); 259 | 260 | #else 261 | 262 | static inline void sha256d_ms(uint32_t *hash, uint32_t *W, 263 | const uint32_t *midstate, const uint32_t *prehash) 264 | { 265 | uint32_t S[64]; 266 | uint32_t t0, t1; 267 | int i; 268 | 269 | S[18] = W[18]; 270 | S[19] = W[19]; 271 | S[20] = W[20]; 272 | S[22] = W[22]; 273 | S[23] = W[23]; 274 | S[24] = W[24]; 275 | S[30] = W[30]; 276 | S[31] = W[31]; 277 | 278 | W[18] += s0(W[3]); 279 | W[19] += W[3]; 280 | W[20] += s1(W[18]); 281 | W[21] = s1(W[19]); 282 | W[22] += s1(W[20]); 283 | W[23] += s1(W[21]); 284 | W[24] += s1(W[22]); 285 | W[25] = s1(W[23]) + W[18]; 286 | W[26] = s1(W[24]) + W[19]; 287 | W[27] = s1(W[25]) + W[20]; 288 | W[28] = s1(W[26]) + W[21]; 289 | W[29] = s1(W[27]) + W[22]; 290 | W[30] += s1(W[28]) + W[23]; 291 | W[31] += s1(W[29]) + W[24]; 292 | for (i = 32; i < 64; i += 2) { 293 | W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16]; 294 | W[i+1] = s1(W[i - 1]) + W[i - 6] + s0(W[i - 14]) + W[i - 15]; 295 | } 296 | 297 | memcpy(S, prehash, 32); 298 | 299 | RNDr(S, W, 3); 300 | RNDr(S, W, 4); 301 | RNDr(S, W, 5); 302 | RNDr(S, W, 6); 303 | RNDr(S, W, 7); 304 | RNDr(S, W, 8); 305 | RNDr(S, W, 9); 306 | RNDr(S, W, 10); 307 | RNDr(S, W, 11); 308 | RNDr(S, W, 12); 309 | RNDr(S, W, 13); 310 | RNDr(S, W, 14); 311 | RNDr(S, W, 15); 312 | RNDr(S, W, 16); 313 | RNDr(S, W, 17); 314 | RNDr(S, W, 18); 315 | RNDr(S, W, 19); 316 | RNDr(S, W, 20); 317 | RNDr(S, W, 21); 318 | RNDr(S, W, 22); 319 | RNDr(S, W, 23); 320 | RNDr(S, W, 24); 321 | RNDr(S, W, 25); 322 | RNDr(S, W, 26); 323 | RNDr(S, W, 27); 324 | RNDr(S, W, 28); 325 | RNDr(S, W, 29); 326 | RNDr(S, W, 30); 327 | RNDr(S, W, 31); 328 | RNDr(S, W, 32); 329 | RNDr(S, W, 33); 330 | RNDr(S, W, 34); 331 | RNDr(S, W, 35); 332 | RNDr(S, W, 36); 333 | RNDr(S, W, 37); 334 | RNDr(S, W, 38); 335 | RNDr(S, W, 39); 336 | RNDr(S, W, 40); 337 | RNDr(S, W, 41); 338 | RNDr(S, W, 42); 339 | RNDr(S, W, 43); 340 | RNDr(S, W, 44); 341 | RNDr(S, W, 45); 342 | RNDr(S, W, 46); 343 | RNDr(S, W, 47); 344 | RNDr(S, W, 48); 345 | RNDr(S, W, 49); 346 | RNDr(S, W, 50); 347 | RNDr(S, W, 51); 348 | RNDr(S, W, 52); 349 | RNDr(S, W, 53); 350 | RNDr(S, W, 54); 351 | RNDr(S, W, 55); 352 | RNDr(S, W, 56); 353 | RNDr(S, W, 57); 354 | RNDr(S, W, 58); 355 | RNDr(S, W, 59); 356 | RNDr(S, W, 60); 357 | RNDr(S, W, 61); 358 | RNDr(S, W, 62); 359 | RNDr(S, W, 63); 360 | 361 | for (i = 0; i < 8; i++) 362 | S[i] += midstate[i]; 363 | 364 | W[18] = S[18]; 365 | W[19] = S[19]; 366 | W[20] = S[20]; 367 | W[22] = S[22]; 368 | W[23] = S[23]; 369 | W[24] = S[24]; 370 | W[30] = S[30]; 371 | W[31] = S[31]; 372 | 373 | memcpy(S + 8, sha256d_hash1 + 8, 32); 374 | S[16] = s1(sha256d_hash1[14]) + sha256d_hash1[ 9] + s0(S[ 1]) + S[ 0]; 375 | S[17] = s1(sha256d_hash1[15]) + sha256d_hash1[10] + s0(S[ 2]) + S[ 1]; 376 | S[18] = s1(S[16]) + sha256d_hash1[11] + s0(S[ 3]) + S[ 2]; 377 | S[19] = s1(S[17]) + sha256d_hash1[12] + s0(S[ 4]) + S[ 3]; 378 | S[20] = s1(S[18]) + sha256d_hash1[13] + s0(S[ 5]) + S[ 4]; 379 | S[21] = s1(S[19]) + sha256d_hash1[14] + s0(S[ 6]) + S[ 5]; 380 | S[22] = s1(S[20]) + sha256d_hash1[15] + s0(S[ 7]) + S[ 6]; 381 | S[23] = s1(S[21]) + S[16] + s0(sha256d_hash1[ 8]) + S[ 7]; 382 | S[24] = s1(S[22]) + S[17] + s0(sha256d_hash1[ 9]) + sha256d_hash1[ 8]; 383 | S[25] = s1(S[23]) + S[18] + s0(sha256d_hash1[10]) + sha256d_hash1[ 9]; 384 | S[26] = s1(S[24]) + S[19] + s0(sha256d_hash1[11]) + sha256d_hash1[10]; 385 | S[27] = s1(S[25]) + S[20] + s0(sha256d_hash1[12]) + sha256d_hash1[11]; 386 | S[28] = s1(S[26]) + S[21] + s0(sha256d_hash1[13]) + sha256d_hash1[12]; 387 | S[29] = s1(S[27]) + S[22] + s0(sha256d_hash1[14]) + sha256d_hash1[13]; 388 | S[30] = s1(S[28]) + S[23] + s0(sha256d_hash1[15]) + sha256d_hash1[14]; 389 | S[31] = s1(S[29]) + S[24] + s0(S[16]) + sha256d_hash1[15]; 390 | for (i = 32; i < 60; i += 2) { 391 | S[i] = s1(S[i - 2]) + S[i - 7] + s0(S[i - 15]) + S[i - 16]; 392 | S[i+1] = s1(S[i - 1]) + S[i - 6] + s0(S[i - 14]) + S[i - 15]; 393 | } 394 | S[60] = s1(S[58]) + S[53] + s0(S[45]) + S[44]; 395 | 396 | sha256_init(hash); 397 | 398 | RNDr(hash, S, 0); 399 | RNDr(hash, S, 1); 400 | RNDr(hash, S, 2); 401 | RNDr(hash, S, 3); 402 | RNDr(hash, S, 4); 403 | RNDr(hash, S, 5); 404 | RNDr(hash, S, 6); 405 | RNDr(hash, S, 7); 406 | RNDr(hash, S, 8); 407 | RNDr(hash, S, 9); 408 | RNDr(hash, S, 10); 409 | RNDr(hash, S, 11); 410 | RNDr(hash, S, 12); 411 | RNDr(hash, S, 13); 412 | RNDr(hash, S, 14); 413 | RNDr(hash, S, 15); 414 | RNDr(hash, S, 16); 415 | RNDr(hash, S, 17); 416 | RNDr(hash, S, 18); 417 | RNDr(hash, S, 19); 418 | RNDr(hash, S, 20); 419 | RNDr(hash, S, 21); 420 | RNDr(hash, S, 22); 421 | RNDr(hash, S, 23); 422 | RNDr(hash, S, 24); 423 | RNDr(hash, S, 25); 424 | RNDr(hash, S, 26); 425 | RNDr(hash, S, 27); 426 | RNDr(hash, S, 28); 427 | RNDr(hash, S, 29); 428 | RNDr(hash, S, 30); 429 | RNDr(hash, S, 31); 430 | RNDr(hash, S, 32); 431 | RNDr(hash, S, 33); 432 | RNDr(hash, S, 34); 433 | RNDr(hash, S, 35); 434 | RNDr(hash, S, 36); 435 | RNDr(hash, S, 37); 436 | RNDr(hash, S, 38); 437 | RNDr(hash, S, 39); 438 | RNDr(hash, S, 40); 439 | RNDr(hash, S, 41); 440 | RNDr(hash, S, 42); 441 | RNDr(hash, S, 43); 442 | RNDr(hash, S, 44); 443 | RNDr(hash, S, 45); 444 | RNDr(hash, S, 46); 445 | RNDr(hash, S, 47); 446 | RNDr(hash, S, 48); 447 | RNDr(hash, S, 49); 448 | RNDr(hash, S, 50); 449 | RNDr(hash, S, 51); 450 | RNDr(hash, S, 52); 451 | RNDr(hash, S, 53); 452 | RNDr(hash, S, 54); 453 | RNDr(hash, S, 55); 454 | RNDr(hash, S, 56); 455 | 456 | hash[2] += hash[6] + S1(hash[3]) + Ch(hash[3], hash[4], hash[5]) 457 | + S[57] + sha256_k[57]; 458 | hash[1] += hash[5] + S1(hash[2]) + Ch(hash[2], hash[3], hash[4]) 459 | + S[58] + sha256_k[58]; 460 | hash[0] += hash[4] + S1(hash[1]) + Ch(hash[1], hash[2], hash[3]) 461 | + S[59] + sha256_k[59]; 462 | hash[7] += hash[3] + S1(hash[0]) + Ch(hash[0], hash[1], hash[2]) 463 | + S[60] + sha256_k[60] 464 | + sha256_h[7]; 465 | } 466 | 467 | #endif /* EXTERN_SHA256 */ 468 | 469 | #ifdef HAVE_SHA256_4WAY 470 | 471 | void sha256d_ms_4way(uint32_t *hash, uint32_t *data, 472 | const uint32_t *midstate, const uint32_t *prehash); 473 | 474 | static inline int scanhash_sha256d_4way(int thr_id, uint32_t *pdata, 475 | const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done) 476 | { 477 | uint32_t data[4 * 64] __attribute__((aligned(128))); 478 | uint32_t hash[4 * 8] __attribute__((aligned(32))); 479 | uint32_t midstate[4 * 8] __attribute__((aligned(32))); 480 | uint32_t prehash[4 * 8] __attribute__((aligned(32))); 481 | uint32_t n = pdata[19] - 1; 482 | const uint32_t first_nonce = pdata[19]; 483 | const uint32_t Htarg = ptarget[7]; 484 | int i, j; 485 | 486 | memcpy(data, pdata + 16, 64); 487 | sha256d_preextend(data); 488 | for (i = 31; i >= 0; i--) 489 | for (j = 0; j < 4; j++) 490 | data[i * 4 + j] = data[i]; 491 | 492 | sha256_init(midstate); 493 | sha256_transform(midstate, pdata, 0); 494 | memcpy(prehash, midstate, 32); 495 | sha256d_prehash(prehash, pdata + 16); 496 | for (i = 7; i >= 0; i--) { 497 | for (j = 0; j < 4; j++) { 498 | midstate[i * 4 + j] = midstate[i]; 499 | prehash[i * 4 + j] = prehash[i]; 500 | } 501 | } 502 | 503 | do { 504 | for (i = 0; i < 4; i++) 505 | data[4 * 3 + i] = ++n; 506 | 507 | sha256d_ms_4way(hash, data, midstate, prehash); 508 | 509 | for (i = 0; i < 4; i++) { 510 | if (swab32(hash[4 * 7 + i]) <= Htarg) { 511 | pdata[19] = data[4 * 3 + i]; 512 | sha256d_80_swap(hash, pdata); 513 | if (fulltest(hash, ptarget)) { 514 | *hashes_done = n - first_nonce + 1; 515 | return 1; 516 | } 517 | } 518 | } 519 | } while (n < max_nonce && !work_restart[thr_id].restart); 520 | 521 | *hashes_done = n - first_nonce + 1; 522 | pdata[19] = n; 523 | return 0; 524 | } 525 | 526 | #endif /* HAVE_SHA256_4WAY */ 527 | 528 | #ifdef HAVE_SHA256_8WAY 529 | 530 | void sha256d_ms_8way(uint32_t *hash, uint32_t *data, 531 | const uint32_t *midstate, const uint32_t *prehash); 532 | 533 | static inline int scanhash_sha256d_8way(int thr_id, uint32_t *pdata, 534 | const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done) 535 | { 536 | uint32_t data[8 * 64] __attribute__((aligned(128))); 537 | uint32_t hash[8 * 8] __attribute__((aligned(32))); 538 | uint32_t midstate[8 * 8] __attribute__((aligned(32))); 539 | uint32_t prehash[8 * 8] __attribute__((aligned(32))); 540 | uint32_t n = pdata[19] - 1; 541 | const uint32_t first_nonce = pdata[19]; 542 | const uint32_t Htarg = ptarget[7]; 543 | int i, j; 544 | 545 | memcpy(data, pdata + 16, 64); 546 | sha256d_preextend(data); 547 | for (i = 31; i >= 0; i--) 548 | for (j = 0; j < 8; j++) 549 | data[i * 8 + j] = data[i]; 550 | 551 | sha256_init(midstate); 552 | sha256_transform(midstate, pdata, 0); 553 | memcpy(prehash, midstate, 32); 554 | sha256d_prehash(prehash, pdata + 16); 555 | for (i = 7; i >= 0; i--) { 556 | for (j = 0; j < 8; j++) { 557 | midstate[i * 8 + j] = midstate[i]; 558 | prehash[i * 8 + j] = prehash[i]; 559 | } 560 | } 561 | 562 | do { 563 | for (i = 0; i < 8; i++) 564 | data[8 * 3 + i] = ++n; 565 | 566 | sha256d_ms_8way(hash, data, midstate, prehash); 567 | 568 | for (i = 0; i < 8; i++) { 569 | if (swab32(hash[8 * 7 + i]) <= Htarg) { 570 | pdata[19] = data[8 * 3 + i]; 571 | sha256d_80_swap(hash, pdata); 572 | if (fulltest(hash, ptarget)) { 573 | *hashes_done = n - first_nonce + 1; 574 | return 1; 575 | } 576 | } 577 | } 578 | } while (n < max_nonce && !work_restart[thr_id].restart); 579 | 580 | *hashes_done = n - first_nonce + 1; 581 | pdata[19] = n; 582 | return 0; 583 | } 584 | 585 | #endif /* HAVE_SHA256_8WAY */ 586 | 587 | int scanhash_sha256d(int thr_id, uint32_t *pdata, const uint32_t *ptarget, 588 | uint32_t max_nonce, unsigned long *hashes_done) 589 | { 590 | uint32_t data[64] __attribute__((aligned(128))); 591 | uint32_t hash[8] __attribute__((aligned(32))); 592 | uint32_t midstate[8] __attribute__((aligned(32))); 593 | uint32_t prehash[8] __attribute__((aligned(32))); 594 | uint32_t n = pdata[19] - 1; 595 | const uint32_t first_nonce = pdata[19]; 596 | const uint32_t Htarg = ptarget[7]; 597 | 598 | #ifdef HAVE_SHA256_8WAY 599 | if (sha256_use_8way()) 600 | return scanhash_sha256d_8way(thr_id, pdata, ptarget, 601 | max_nonce, hashes_done); 602 | #endif 603 | #ifdef HAVE_SHA256_4WAY 604 | if (sha256_use_4way()) 605 | return scanhash_sha256d_4way(thr_id, pdata, ptarget, 606 | max_nonce, hashes_done); 607 | #endif 608 | 609 | memcpy(data, pdata + 16, 64); 610 | sha256d_preextend(data); 611 | 612 | sha256_init(midstate); 613 | sha256_transform(midstate, pdata, 0); 614 | memcpy(prehash, midstate, 32); 615 | sha256d_prehash(prehash, pdata + 16); 616 | 617 | do { 618 | data[3] = ++n; 619 | sha256d_ms(hash, data, midstate, prehash); 620 | if (swab32(hash[7]) <= Htarg) { 621 | pdata[19] = data[3]; 622 | sha256d_80_swap(hash, pdata); 623 | if (fulltest(hash, ptarget)) { 624 | *hashes_done = n - first_nonce + 1; 625 | return 1; 626 | } 627 | } 628 | } while (n < max_nonce && !work_restart[thr_id].restart); 629 | 630 | *hashes_done = n - first_nonce + 1; 631 | pdata[19] = n; 632 | return 0; 633 | } 634 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /scrypt-x86.S.orig: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011-2012, 2014 pooler@litecoinpool.org 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 | 27 | #include "cpuminer-config.h" 28 | 29 | #if defined(__linux__) && defined(__ELF__) 30 | .section .note.GNU-stack,"",%progbits 31 | #endif 32 | 33 | #if defined(USE_ASM) && defined(__i386__) 34 | 35 | .macro scrypt_shuffle src, so, dest, do 36 | movl \so+60(\src), %eax 37 | movl \so+44(\src), %ebx 38 | movl \so+28(\src), %ecx 39 | movl \so+12(\src), %edx 40 | movl %eax, \do+12(\dest) 41 | movl %ebx, \do+28(\dest) 42 | movl %ecx, \do+44(\dest) 43 | movl %edx, \do+60(\dest) 44 | movl \so+40(\src), %eax 45 | movl \so+8(\src), %ebx 46 | movl \so+48(\src), %ecx 47 | movl \so+16(\src), %edx 48 | movl %eax, \do+8(\dest) 49 | movl %ebx, \do+40(\dest) 50 | movl %ecx, \do+16(\dest) 51 | movl %edx, \do+48(\dest) 52 | movl \so+20(\src), %eax 53 | movl \so+4(\src), %ebx 54 | movl \so+52(\src), %ecx 55 | movl \so+36(\src), %edx 56 | movl %eax, \do+4(\dest) 57 | movl %ebx, \do+20(\dest) 58 | movl %ecx, \do+36(\dest) 59 | movl %edx, \do+52(\dest) 60 | movl \so+0(\src), %eax 61 | movl \so+24(\src), %ebx 62 | movl \so+32(\src), %ecx 63 | movl \so+56(\src), %edx 64 | movl %eax, \do+0(\dest) 65 | movl %ebx, \do+24(\dest) 66 | movl %ecx, \do+32(\dest) 67 | movl %edx, \do+56(\dest) 68 | .endm 69 | 70 | .macro salsa8_core_gen_quadround 71 | movl 52(%esp), %ecx 72 | movl 4(%esp), %edx 73 | movl 20(%esp), %ebx 74 | movl 8(%esp), %esi 75 | leal (%ecx, %edx), %edi 76 | roll $7, %edi 77 | xorl %edi, %ebx 78 | movl %ebx, 4(%esp) 79 | movl 36(%esp), %edi 80 | leal (%edx, %ebx), %ebp 81 | roll $9, %ebp 82 | xorl %ebp, %edi 83 | movl 24(%esp), %ebp 84 | movl %edi, 8(%esp) 85 | addl %edi, %ebx 86 | roll $13, %ebx 87 | xorl %ebx, %ecx 88 | movl 40(%esp), %ebx 89 | movl %ecx, 20(%esp) 90 | addl %edi, %ecx 91 | roll $18, %ecx 92 | leal (%esi, %ebp), %edi 93 | roll $7, %edi 94 | xorl %edi, %ebx 95 | movl %ebx, 24(%esp) 96 | movl 56(%esp), %edi 97 | xorl %ecx, %edx 98 | leal (%ebp, %ebx), %ecx 99 | roll $9, %ecx 100 | xorl %ecx, %edi 101 | movl %edi, 36(%esp) 102 | movl 28(%esp), %ecx 103 | movl %edx, 28(%esp) 104 | movl 44(%esp), %edx 105 | addl %edi, %ebx 106 | roll $13, %ebx 107 | xorl %ebx, %esi 108 | movl 60(%esp), %ebx 109 | movl %esi, 40(%esp) 110 | addl %edi, %esi 111 | roll $18, %esi 112 | leal (%ecx, %edx), %edi 113 | roll $7, %edi 114 | xorl %edi, %ebx 115 | movl %ebx, 44(%esp) 116 | movl 12(%esp), %edi 117 | xorl %esi, %ebp 118 | leal (%edx, %ebx), %esi 119 | roll $9, %esi 120 | xorl %esi, %edi 121 | movl %edi, 12(%esp) 122 | movl 48(%esp), %esi 123 | movl %ebp, 48(%esp) 124 | movl 64(%esp), %ebp 125 | addl %edi, %ebx 126 | roll $13, %ebx 127 | xorl %ebx, %ecx 128 | movl 16(%esp), %ebx 129 | movl %ecx, 16(%esp) 130 | addl %edi, %ecx 131 | roll $18, %ecx 132 | leal (%esi, %ebp), %edi 133 | roll $7, %edi 134 | xorl %edi, %ebx 135 | movl 32(%esp), %edi 136 | xorl %ecx, %edx 137 | leal (%ebp, %ebx), %ecx 138 | roll $9, %ecx 139 | xorl %ecx, %edi 140 | movl %edi, 32(%esp) 141 | movl %ebx, %ecx 142 | movl %edx, 52(%esp) 143 | movl 28(%esp), %edx 144 | addl %edi, %ebx 145 | roll $13, %ebx 146 | xorl %ebx, %esi 147 | movl 40(%esp), %ebx 148 | movl %esi, 28(%esp) 149 | addl %edi, %esi 150 | roll $18, %esi 151 | leal (%ecx, %edx), %edi 152 | roll $7, %edi 153 | xorl %edi, %ebx 154 | movl %ebx, 40(%esp) 155 | movl 12(%esp), %edi 156 | xorl %esi, %ebp 157 | leal (%edx, %ebx), %esi 158 | roll $9, %esi 159 | xorl %esi, %edi 160 | movl %edi, 12(%esp) 161 | movl 4(%esp), %esi 162 | movl %ebp, 4(%esp) 163 | movl 48(%esp), %ebp 164 | addl %edi, %ebx 165 | roll $13, %ebx 166 | xorl %ebx, %ecx 167 | movl 16(%esp), %ebx 168 | movl %ecx, 16(%esp) 169 | addl %edi, %ecx 170 | roll $18, %ecx 171 | leal (%esi, %ebp), %edi 172 | roll $7, %edi 173 | xorl %edi, %ebx 174 | movl %ebx, 48(%esp) 175 | movl 32(%esp), %edi 176 | xorl %ecx, %edx 177 | leal (%ebp, %ebx), %ecx 178 | roll $9, %ecx 179 | xorl %ecx, %edi 180 | movl %edi, 32(%esp) 181 | movl 24(%esp), %ecx 182 | movl %edx, 24(%esp) 183 | movl 52(%esp), %edx 184 | addl %edi, %ebx 185 | roll $13, %ebx 186 | xorl %ebx, %esi 187 | movl 28(%esp), %ebx 188 | movl %esi, 28(%esp) 189 | addl %edi, %esi 190 | roll $18, %esi 191 | leal (%ecx, %edx), %edi 192 | roll $7, %edi 193 | xorl %edi, %ebx 194 | movl %ebx, 52(%esp) 195 | movl 8(%esp), %edi 196 | xorl %esi, %ebp 197 | leal (%edx, %ebx), %esi 198 | roll $9, %esi 199 | xorl %esi, %edi 200 | movl %edi, 8(%esp) 201 | movl 44(%esp), %esi 202 | movl %ebp, 44(%esp) 203 | movl 4(%esp), %ebp 204 | addl %edi, %ebx 205 | roll $13, %ebx 206 | xorl %ebx, %ecx 207 | movl 20(%esp), %ebx 208 | movl %ecx, 4(%esp) 209 | addl %edi, %ecx 210 | roll $18, %ecx 211 | leal (%esi, %ebp), %edi 212 | roll $7, %edi 213 | xorl %edi, %ebx 214 | movl 36(%esp), %edi 215 | xorl %ecx, %edx 216 | leal (%ebp, %ebx), %ecx 217 | roll $9, %ecx 218 | xorl %ecx, %edi 219 | movl %edi, 20(%esp) 220 | movl %ebx, %ecx 221 | movl %edx, 36(%esp) 222 | movl 24(%esp), %edx 223 | addl %edi, %ebx 224 | roll $13, %ebx 225 | xorl %ebx, %esi 226 | movl 28(%esp), %ebx 227 | movl %esi, 24(%esp) 228 | addl %edi, %esi 229 | roll $18, %esi 230 | leal (%ecx, %edx), %edi 231 | roll $7, %edi 232 | xorl %edi, %ebx 233 | movl %ebx, 28(%esp) 234 | xorl %esi, %ebp 235 | movl 8(%esp), %esi 236 | leal (%edx, %ebx), %edi 237 | roll $9, %edi 238 | xorl %edi, %esi 239 | movl 40(%esp), %edi 240 | movl %ebp, 8(%esp) 241 | movl 44(%esp), %ebp 242 | movl %esi, 40(%esp) 243 | addl %esi, %ebx 244 | roll $13, %ebx 245 | xorl %ebx, %ecx 246 | movl 4(%esp), %ebx 247 | movl %ecx, 44(%esp) 248 | addl %esi, %ecx 249 | roll $18, %ecx 250 | leal (%edi, %ebp), %esi 251 | roll $7, %esi 252 | xorl %esi, %ebx 253 | movl %ebx, 4(%esp) 254 | movl 20(%esp), %esi 255 | xorl %ecx, %edx 256 | leal (%ebp, %ebx), %ecx 257 | roll $9, %ecx 258 | xorl %ecx, %esi 259 | movl %esi, 56(%esp) 260 | movl 48(%esp), %ecx 261 | movl %edx, 20(%esp) 262 | movl 36(%esp), %edx 263 | addl %esi, %ebx 264 | roll $13, %ebx 265 | xorl %ebx, %edi 266 | movl 24(%esp), %ebx 267 | movl %edi, 24(%esp) 268 | addl %esi, %edi 269 | roll $18, %edi 270 | leal (%ecx, %edx), %esi 271 | roll $7, %esi 272 | xorl %esi, %ebx 273 | movl %ebx, 60(%esp) 274 | movl 12(%esp), %esi 275 | xorl %edi, %ebp 276 | leal (%edx, %ebx), %edi 277 | roll $9, %edi 278 | xorl %edi, %esi 279 | movl %esi, 12(%esp) 280 | movl 52(%esp), %edi 281 | movl %ebp, 36(%esp) 282 | movl 8(%esp), %ebp 283 | addl %esi, %ebx 284 | roll $13, %ebx 285 | xorl %ebx, %ecx 286 | movl 16(%esp), %ebx 287 | movl %ecx, 16(%esp) 288 | addl %esi, %ecx 289 | roll $18, %ecx 290 | leal (%edi, %ebp), %esi 291 | roll $7, %esi 292 | xorl %esi, %ebx 293 | movl 32(%esp), %esi 294 | xorl %ecx, %edx 295 | leal (%ebp, %ebx), %ecx 296 | roll $9, %ecx 297 | xorl %ecx, %esi 298 | movl %esi, 32(%esp) 299 | movl %ebx, %ecx 300 | movl %edx, 48(%esp) 301 | movl 20(%esp), %edx 302 | addl %esi, %ebx 303 | roll $13, %ebx 304 | xorl %ebx, %edi 305 | movl 24(%esp), %ebx 306 | movl %edi, 20(%esp) 307 | addl %esi, %edi 308 | roll $18, %edi 309 | leal (%ecx, %edx), %esi 310 | roll $7, %esi 311 | xorl %esi, %ebx 312 | movl %ebx, 8(%esp) 313 | movl 12(%esp), %esi 314 | xorl %edi, %ebp 315 | leal (%edx, %ebx), %edi 316 | roll $9, %edi 317 | xorl %edi, %esi 318 | movl %esi, 12(%esp) 319 | movl 28(%esp), %edi 320 | movl %ebp, 52(%esp) 321 | movl 36(%esp), %ebp 322 | addl %esi, %ebx 323 | roll $13, %ebx 324 | xorl %ebx, %ecx 325 | movl 16(%esp), %ebx 326 | movl %ecx, 16(%esp) 327 | addl %esi, %ecx 328 | roll $18, %ecx 329 | leal (%edi, %ebp), %esi 330 | roll $7, %esi 331 | xorl %esi, %ebx 332 | movl %ebx, 28(%esp) 333 | movl 32(%esp), %esi 334 | xorl %ecx, %edx 335 | leal (%ebp, %ebx), %ecx 336 | roll $9, %ecx 337 | xorl %ecx, %esi 338 | movl %esi, 32(%esp) 339 | movl 4(%esp), %ecx 340 | movl %edx, 4(%esp) 341 | movl 48(%esp), %edx 342 | addl %esi, %ebx 343 | roll $13, %ebx 344 | xorl %ebx, %edi 345 | movl 20(%esp), %ebx 346 | movl %edi, 20(%esp) 347 | addl %esi, %edi 348 | roll $18, %edi 349 | leal (%ecx, %edx), %esi 350 | roll $7, %esi 351 | xorl %esi, %ebx 352 | movl %ebx, 48(%esp) 353 | movl 40(%esp), %esi 354 | xorl %edi, %ebp 355 | leal (%edx, %ebx), %edi 356 | roll $9, %edi 357 | xorl %edi, %esi 358 | movl %esi, 36(%esp) 359 | movl 60(%esp), %edi 360 | movl %ebp, 24(%esp) 361 | movl 52(%esp), %ebp 362 | addl %esi, %ebx 363 | roll $13, %ebx 364 | xorl %ebx, %ecx 365 | movl 44(%esp), %ebx 366 | movl %ecx, 40(%esp) 367 | addl %esi, %ecx 368 | roll $18, %ecx 369 | leal (%edi, %ebp), %esi 370 | roll $7, %esi 371 | xorl %esi, %ebx 372 | movl %ebx, 52(%esp) 373 | movl 56(%esp), %esi 374 | xorl %ecx, %edx 375 | leal (%ebp, %ebx), %ecx 376 | roll $9, %ecx 377 | xorl %ecx, %esi 378 | movl %esi, 56(%esp) 379 | addl %esi, %ebx 380 | movl %edx, 44(%esp) 381 | roll $13, %ebx 382 | xorl %ebx, %edi 383 | movl %edi, 60(%esp) 384 | addl %esi, %edi 385 | roll $18, %edi 386 | xorl %edi, %ebp 387 | movl %ebp, 64(%esp) 388 | .endm 389 | 390 | .text 391 | .p2align 5 392 | salsa8_core_gen: 393 | salsa8_core_gen_quadround 394 | salsa8_core_gen_quadround 395 | ret 396 | 397 | 398 | .text 399 | .p2align 5 400 | .globl scrypt_core 401 | .globl _scrypt_core 402 | scrypt_core: 403 | _scrypt_core: 404 | pushl %ebx 405 | pushl %ebp 406 | pushl %edi 407 | pushl %esi 408 | 409 | /* Check for SSE2 availability */ 410 | movl $1, %eax 411 | cpuid 412 | andl $0x04000000, %edx 413 | jnz scrypt_core_sse2 414 | 415 | scrypt_core_gen: 416 | movl 20(%esp), %edi 417 | movl 24(%esp), %esi 418 | movl 28(%esp), %ecx 419 | subl $72, %esp 420 | 421 | .macro scrypt_core_macro1a p, q 422 | movl \p(%edi), %eax 423 | movl \q(%edi), %edx 424 | movl %eax, \p(%esi) 425 | movl %edx, \q(%esi) 426 | xorl %edx, %eax 427 | movl %eax, \p(%edi) 428 | movl %eax, \p(%esp) 429 | .endm 430 | 431 | .macro scrypt_core_macro1b p, q 432 | movl \p(%edi), %eax 433 | xorl \p(%esi, %edx), %eax 434 | movl \q(%edi), %ebx 435 | xorl \q(%esi, %edx), %ebx 436 | movl %ebx, \q(%edi) 437 | xorl %ebx, %eax 438 | movl %eax, \p(%edi) 439 | movl %eax, \p(%esp) 440 | .endm 441 | 442 | .macro scrypt_core_macro2 p, q 443 | movl \p(%esp), %eax 444 | addl \p(%edi), %eax 445 | movl %eax, \p(%edi) 446 | xorl \q(%edi), %eax 447 | movl %eax, \q(%edi) 448 | movl %eax, \p(%esp) 449 | .endm 450 | 451 | .macro scrypt_core_macro3 p, q 452 | movl \p(%esp), %eax 453 | addl \q(%edi), %eax 454 | movl %eax, \q(%edi) 455 | .endm 456 | 457 | shll $7, %ecx 458 | addl %esi, %ecx 459 | scrypt_core_gen_loop1: 460 | movl %esi, 64(%esp) 461 | movl %ecx, 68(%esp) 462 | 463 | scrypt_core_macro1a 0, 64 464 | scrypt_core_macro1a 4, 68 465 | scrypt_core_macro1a 8, 72 466 | scrypt_core_macro1a 12, 76 467 | scrypt_core_macro1a 16, 80 468 | scrypt_core_macro1a 20, 84 469 | scrypt_core_macro1a 24, 88 470 | scrypt_core_macro1a 28, 92 471 | scrypt_core_macro1a 32, 96 472 | scrypt_core_macro1a 36, 100 473 | scrypt_core_macro1a 40, 104 474 | scrypt_core_macro1a 44, 108 475 | scrypt_core_macro1a 48, 112 476 | scrypt_core_macro1a 52, 116 477 | scrypt_core_macro1a 56, 120 478 | scrypt_core_macro1a 60, 124 479 | 480 | call salsa8_core_gen 481 | 482 | movl 92(%esp), %edi 483 | scrypt_core_macro2 0, 64 484 | scrypt_core_macro2 4, 68 485 | scrypt_core_macro2 8, 72 486 | scrypt_core_macro2 12, 76 487 | scrypt_core_macro2 16, 80 488 | scrypt_core_macro2 20, 84 489 | scrypt_core_macro2 24, 88 490 | scrypt_core_macro2 28, 92 491 | scrypt_core_macro2 32, 96 492 | scrypt_core_macro2 36, 100 493 | scrypt_core_macro2 40, 104 494 | scrypt_core_macro2 44, 108 495 | scrypt_core_macro2 48, 112 496 | scrypt_core_macro2 52, 116 497 | scrypt_core_macro2 56, 120 498 | scrypt_core_macro2 60, 124 499 | 500 | call salsa8_core_gen 501 | 502 | movl 92(%esp), %edi 503 | scrypt_core_macro3 0, 64 504 | scrypt_core_macro3 4, 68 505 | scrypt_core_macro3 8, 72 506 | scrypt_core_macro3 12, 76 507 | scrypt_core_macro3 16, 80 508 | scrypt_core_macro3 20, 84 509 | scrypt_core_macro3 24, 88 510 | scrypt_core_macro3 28, 92 511 | scrypt_core_macro3 32, 96 512 | scrypt_core_macro3 36, 100 513 | scrypt_core_macro3 40, 104 514 | scrypt_core_macro3 44, 108 515 | scrypt_core_macro3 48, 112 516 | scrypt_core_macro3 52, 116 517 | scrypt_core_macro3 56, 120 518 | scrypt_core_macro3 60, 124 519 | 520 | movl 64(%esp), %esi 521 | movl 68(%esp), %ecx 522 | addl $128, %esi 523 | cmpl %ecx, %esi 524 | jne scrypt_core_gen_loop1 525 | 526 | movl 96(%esp), %esi 527 | movl 100(%esp), %ecx 528 | movl %ecx, %eax 529 | subl $1, %eax 530 | movl %eax, 100(%esp) 531 | scrypt_core_gen_loop2: 532 | movl %ecx, 68(%esp) 533 | 534 | movl 64(%edi), %edx 535 | andl 100(%esp), %edx 536 | shll $7, %edx 537 | 538 | scrypt_core_macro1b 0, 64 539 | scrypt_core_macro1b 4, 68 540 | scrypt_core_macro1b 8, 72 541 | scrypt_core_macro1b 12, 76 542 | scrypt_core_macro1b 16, 80 543 | scrypt_core_macro1b 20, 84 544 | scrypt_core_macro1b 24, 88 545 | scrypt_core_macro1b 28, 92 546 | scrypt_core_macro1b 32, 96 547 | scrypt_core_macro1b 36, 100 548 | scrypt_core_macro1b 40, 104 549 | scrypt_core_macro1b 44, 108 550 | scrypt_core_macro1b 48, 112 551 | scrypt_core_macro1b 52, 116 552 | scrypt_core_macro1b 56, 120 553 | scrypt_core_macro1b 60, 124 554 | 555 | call salsa8_core_gen 556 | 557 | movl 92(%esp), %edi 558 | scrypt_core_macro2 0, 64 559 | scrypt_core_macro2 4, 68 560 | scrypt_core_macro2 8, 72 561 | scrypt_core_macro2 12, 76 562 | scrypt_core_macro2 16, 80 563 | scrypt_core_macro2 20, 84 564 | scrypt_core_macro2 24, 88 565 | scrypt_core_macro2 28, 92 566 | scrypt_core_macro2 32, 96 567 | scrypt_core_macro2 36, 100 568 | scrypt_core_macro2 40, 104 569 | scrypt_core_macro2 44, 108 570 | scrypt_core_macro2 48, 112 571 | scrypt_core_macro2 52, 116 572 | scrypt_core_macro2 56, 120 573 | scrypt_core_macro2 60, 124 574 | 575 | call salsa8_core_gen 576 | 577 | movl 92(%esp), %edi 578 | movl 96(%esp), %esi 579 | scrypt_core_macro3 0, 64 580 | scrypt_core_macro3 4, 68 581 | scrypt_core_macro3 8, 72 582 | scrypt_core_macro3 12, 76 583 | scrypt_core_macro3 16, 80 584 | scrypt_core_macro3 20, 84 585 | scrypt_core_macro3 24, 88 586 | scrypt_core_macro3 28, 92 587 | scrypt_core_macro3 32, 96 588 | scrypt_core_macro3 36, 100 589 | scrypt_core_macro3 40, 104 590 | scrypt_core_macro3 44, 108 591 | scrypt_core_macro3 48, 112 592 | scrypt_core_macro3 52, 116 593 | scrypt_core_macro3 56, 120 594 | scrypt_core_macro3 60, 124 595 | 596 | movl 68(%esp), %ecx 597 | subl $1, %ecx 598 | ja scrypt_core_gen_loop2 599 | 600 | addl $72, %esp 601 | popl %esi 602 | popl %edi 603 | popl %ebp 604 | popl %ebx 605 | ret 606 | 607 | 608 | .macro salsa8_core_sse2_doubleround 609 | movdqa %xmm1, %xmm4 610 | paddd %xmm0, %xmm4 611 | movdqa %xmm4, %xmm5 612 | pslld $7, %xmm4 613 | psrld $25, %xmm5 614 | pxor %xmm4, %xmm3 615 | movdqa %xmm0, %xmm4 616 | pxor %xmm5, %xmm3 617 | 618 | paddd %xmm3, %xmm4 619 | movdqa %xmm4, %xmm5 620 | pslld $9, %xmm4 621 | psrld $23, %xmm5 622 | pxor %xmm4, %xmm2 623 | movdqa %xmm3, %xmm4 624 | pxor %xmm5, %xmm2 625 | pshufd $0x93, %xmm3, %xmm3 626 | 627 | paddd %xmm2, %xmm4 628 | movdqa %xmm4, %xmm5 629 | pslld $13, %xmm4 630 | psrld $19, %xmm5 631 | pxor %xmm4, %xmm1 632 | movdqa %xmm2, %xmm4 633 | pxor %xmm5, %xmm1 634 | pshufd $0x4e, %xmm2, %xmm2 635 | 636 | paddd %xmm1, %xmm4 637 | movdqa %xmm4, %xmm5 638 | pslld $18, %xmm4 639 | psrld $14, %xmm5 640 | pxor %xmm4, %xmm0 641 | movdqa %xmm3, %xmm4 642 | pxor %xmm5, %xmm0 643 | pshufd $0x39, %xmm1, %xmm1 644 | 645 | paddd %xmm0, %xmm4 646 | movdqa %xmm4, %xmm5 647 | pslld $7, %xmm4 648 | psrld $25, %xmm5 649 | pxor %xmm4, %xmm1 650 | movdqa %xmm0, %xmm4 651 | pxor %xmm5, %xmm1 652 | 653 | paddd %xmm1, %xmm4 654 | movdqa %xmm4, %xmm5 655 | pslld $9, %xmm4 656 | psrld $23, %xmm5 657 | pxor %xmm4, %xmm2 658 | movdqa %xmm1, %xmm4 659 | pxor %xmm5, %xmm2 660 | pshufd $0x93, %xmm1, %xmm1 661 | 662 | paddd %xmm2, %xmm4 663 | movdqa %xmm4, %xmm5 664 | pslld $13, %xmm4 665 | psrld $19, %xmm5 666 | pxor %xmm4, %xmm3 667 | movdqa %xmm2, %xmm4 668 | pxor %xmm5, %xmm3 669 | pshufd $0x4e, %xmm2, %xmm2 670 | 671 | paddd %xmm3, %xmm4 672 | movdqa %xmm4, %xmm5 673 | pslld $18, %xmm4 674 | psrld $14, %xmm5 675 | pxor %xmm4, %xmm0 676 | pshufd $0x39, %xmm3, %xmm3 677 | pxor %xmm5, %xmm0 678 | .endm 679 | 680 | .macro salsa8_core_sse2 681 | salsa8_core_sse2_doubleround 682 | salsa8_core_sse2_doubleround 683 | salsa8_core_sse2_doubleround 684 | salsa8_core_sse2_doubleround 685 | .endm 686 | 687 | .p2align 5 688 | scrypt_core_sse2: 689 | movl 20(%esp), %edi 690 | movl 24(%esp), %esi 691 | movl %esp, %ebp 692 | subl $128, %esp 693 | andl $-16, %esp 694 | 695 | scrypt_shuffle %edi, 0, %esp, 0 696 | scrypt_shuffle %edi, 64, %esp, 64 697 | 698 | movdqa 96(%esp), %xmm6 699 | movdqa 112(%esp), %xmm7 700 | 701 | movl %esi, %edx 702 | movl 28(%ebp), %ecx 703 | shll $7, %ecx 704 | addl %esi, %ecx 705 | scrypt_core_sse2_loop1: 706 | movdqa 0(%esp), %xmm0 707 | movdqa 16(%esp), %xmm1 708 | movdqa 32(%esp), %xmm2 709 | movdqa 48(%esp), %xmm3 710 | movdqa 64(%esp), %xmm4 711 | movdqa 80(%esp), %xmm5 712 | pxor %xmm4, %xmm0 713 | pxor %xmm5, %xmm1 714 | movdqa %xmm0, 0(%edx) 715 | movdqa %xmm1, 16(%edx) 716 | pxor %xmm6, %xmm2 717 | pxor %xmm7, %xmm3 718 | movdqa %xmm2, 32(%edx) 719 | movdqa %xmm3, 48(%edx) 720 | movdqa %xmm4, 64(%edx) 721 | movdqa %xmm5, 80(%edx) 722 | movdqa %xmm6, 96(%edx) 723 | movdqa %xmm7, 112(%edx) 724 | 725 | salsa8_core_sse2 726 | paddd 0(%edx), %xmm0 727 | paddd 16(%edx), %xmm1 728 | paddd 32(%edx), %xmm2 729 | paddd 48(%edx), %xmm3 730 | movdqa %xmm0, 0(%esp) 731 | movdqa %xmm1, 16(%esp) 732 | movdqa %xmm2, 32(%esp) 733 | movdqa %xmm3, 48(%esp) 734 | 735 | pxor 64(%esp), %xmm0 736 | pxor 80(%esp), %xmm1 737 | pxor %xmm6, %xmm2 738 | pxor %xmm7, %xmm3 739 | movdqa %xmm0, 64(%esp) 740 | movdqa %xmm1, 80(%esp) 741 | movdqa %xmm2, %xmm6 742 | movdqa %xmm3, %xmm7 743 | salsa8_core_sse2 744 | paddd 64(%esp), %xmm0 745 | paddd 80(%esp), %xmm1 746 | paddd %xmm2, %xmm6 747 | paddd %xmm3, %xmm7 748 | movdqa %xmm0, 64(%esp) 749 | movdqa %xmm1, 80(%esp) 750 | 751 | addl $128, %edx 752 | cmpl %ecx, %edx 753 | jne scrypt_core_sse2_loop1 754 | 755 | movdqa 64(%esp), %xmm4 756 | movdqa 80(%esp), %xmm5 757 | 758 | movl 28(%ebp), %ecx 759 | movl %ecx, %eax 760 | subl $1, %eax 761 | scrypt_core_sse2_loop2: 762 | movd %xmm4, %edx 763 | movdqa 0(%esp), %xmm0 764 | movdqa 16(%esp), %xmm1 765 | movdqa 32(%esp), %xmm2 766 | movdqa 48(%esp), %xmm3 767 | andl %eax, %edx 768 | shll $7, %edx 769 | pxor 0(%esi, %edx), %xmm0 770 | pxor 16(%esi, %edx), %xmm1 771 | pxor 32(%esi, %edx), %xmm2 772 | pxor 48(%esi, %edx), %xmm3 773 | 774 | pxor %xmm4, %xmm0 775 | pxor %xmm5, %xmm1 776 | movdqa %xmm0, 0(%esp) 777 | movdqa %xmm1, 16(%esp) 778 | pxor %xmm6, %xmm2 779 | pxor %xmm7, %xmm3 780 | movdqa %xmm2, 32(%esp) 781 | movdqa %xmm3, 48(%esp) 782 | salsa8_core_sse2 783 | paddd 0(%esp), %xmm0 784 | paddd 16(%esp), %xmm1 785 | paddd 32(%esp), %xmm2 786 | paddd 48(%esp), %xmm3 787 | movdqa %xmm0, 0(%esp) 788 | movdqa %xmm1, 16(%esp) 789 | movdqa %xmm2, 32(%esp) 790 | movdqa %xmm3, 48(%esp) 791 | 792 | pxor 64(%esi, %edx), %xmm0 793 | pxor 80(%esi, %edx), %xmm1 794 | pxor 96(%esi, %edx), %xmm2 795 | pxor 112(%esi, %edx), %xmm3 796 | pxor 64(%esp), %xmm0 797 | pxor 80(%esp), %xmm1 798 | pxor %xmm6, %xmm2 799 | pxor %xmm7, %xmm3 800 | movdqa %xmm0, 64(%esp) 801 | movdqa %xmm1, 80(%esp) 802 | movdqa %xmm2, %xmm6 803 | movdqa %xmm3, %xmm7 804 | salsa8_core_sse2 805 | paddd 64(%esp), %xmm0 806 | paddd 80(%esp), %xmm1 807 | paddd %xmm2, %xmm6 808 | paddd %xmm3, %xmm7 809 | movdqa %xmm0, %xmm4 810 | movdqa %xmm1, %xmm5 811 | movdqa %xmm0, 64(%esp) 812 | movdqa %xmm1, 80(%esp) 813 | 814 | subl $1, %ecx 815 | ja scrypt_core_sse2_loop2 816 | 817 | movdqa %xmm6, 96(%esp) 818 | movdqa %xmm7, 112(%esp) 819 | 820 | scrypt_shuffle %esp, 0, %edi, 0 821 | scrypt_shuffle %esp, 64, %edi, 64 822 | 823 | movl %ebp, %esp 824 | popl %esi 825 | popl %edi 826 | popl %ebp 827 | popl %ebx 828 | ret 829 | 830 | #endif 831 | -------------------------------------------------------------------------------- /yescrypt-ref.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright 2009 Colin Percival 3 | * Copyright 2013,2014 Alexander Peslyak 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | * 27 | * This file was originally written by Colin Percival as part of the Tarsnap 28 | * online backup system. 29 | * 30 | * This is the reference implementation. Its purpose is to provide a simple 31 | * human- and machine-readable specification that implementations intended 32 | * for actual use should be tested against. It is deliberately mostly not 33 | * optimized, and it is not meant to be used in production. Instead, use 34 | * yescrypt-best.c or one of the source files included from there. 35 | */ 36 | 37 | #warning "This reference implementation is deliberately mostly not optimized. Use yescrypt-best.c instead unless you're testing (against) the reference implementation on purpose." 38 | 39 | #include 40 | #include 41 | #include 42 | 43 | #include "sha256.h" 44 | #include "sysendian.h" 45 | 46 | #include "yescrypt.h" 47 | 48 | static void 49 | blkcpy(uint32_t * dest, const uint32_t * src, size_t count) 50 | { 51 | do { 52 | *dest++ = *src++; 53 | } while (--count); 54 | } 55 | 56 | static void 57 | blkxor(uint32_t * dest, const uint32_t * src, size_t count) 58 | { 59 | do { 60 | *dest++ ^= *src++; 61 | } while (--count); 62 | } 63 | 64 | /** 65 | * salsa20_8(B): 66 | * Apply the salsa20/8 core to the provided block. 67 | */ 68 | static void 69 | salsa20_8(uint32_t B[16]) 70 | { 71 | uint32_t x[16]; 72 | size_t i; 73 | 74 | /* Mimic SIMD shuffling */ 75 | for (i = 0; i < 16; i++) 76 | x[i * 5 % 16] = B[i]; 77 | 78 | for (i = 0; i < 8; i += 2) { 79 | #define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) 80 | /* Operate on columns */ 81 | x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9); 82 | x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18); 83 | 84 | x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9); 85 | x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18); 86 | 87 | x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9); 88 | x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18); 89 | 90 | x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9); 91 | x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18); 92 | 93 | /* Operate on rows */ 94 | x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9); 95 | x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18); 96 | 97 | x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9); 98 | x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18); 99 | 100 | x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9); 101 | x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18); 102 | 103 | x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9); 104 | x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18); 105 | #undef R 106 | } 107 | 108 | /* Mimic SIMD shuffling */ 109 | for (i = 0; i < 16; i++) 110 | B[i] += x[i * 5 % 16]; 111 | } 112 | 113 | /** 114 | * blockmix_salsa8(B, Y, r): 115 | * Compute B = BlockMix_{salsa20/8, r}(B). The input B must be 128r bytes in 116 | * length; the temporary space Y must also be the same size. 117 | */ 118 | static void 119 | blockmix_salsa8(uint32_t * B, uint32_t * Y, size_t r) 120 | { 121 | uint32_t X[16]; 122 | size_t i; 123 | 124 | /* 1: X <-- B_{2r - 1} */ 125 | blkcpy(X, &B[(2 * r - 1) * 16], 16); 126 | 127 | /* 2: for i = 0 to 2r - 1 do */ 128 | for (i = 0; i < 2 * r; i++) { 129 | /* 3: X <-- H(X \xor B_i) */ 130 | blkxor(X, &B[i * 16], 16); 131 | salsa20_8(X); 132 | 133 | /* 4: Y_i <-- X */ 134 | blkcpy(&Y[i * 16], X, 16); 135 | } 136 | 137 | /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */ 138 | for (i = 0; i < r; i++) 139 | blkcpy(&B[i * 16], &Y[(i * 2) * 16], 16); 140 | for (i = 0; i < r; i++) 141 | blkcpy(&B[(i + r) * 16], &Y[(i * 2 + 1) * 16], 16); 142 | } 143 | 144 | /* These are tunable */ 145 | #define S_BITS 8 146 | #define S_SIMD 2 147 | #define S_P 4 148 | #define S_ROUNDS 6 149 | 150 | /* Number of S-boxes. Not tunable, hard-coded in a few places. */ 151 | #define S_N 2 152 | 153 | /* Derived values. Not tunable on their own. */ 154 | #define S_SIZE1 (1 << S_BITS) 155 | #define S_MASK ((S_SIZE1 - 1) * S_SIMD * 8) 156 | #define S_SIZE_ALL (S_N * S_SIZE1 * S_SIMD * 2) 157 | #define S_P_SIZE (S_P * S_SIMD * 2) 158 | #define S_MIN_R ((S_P * S_SIMD + 15) / 16) 159 | 160 | /** 161 | * pwxform(B): 162 | * Transform the provided block using the provided S-boxes. 163 | */ 164 | static void 165 | block_pwxform(uint32_t * B, const uint32_t * S) 166 | { 167 | uint32_t (*X)[S_SIMD][2] = (uint32_t (*)[S_SIMD][2])B; 168 | const uint32_t (*S0)[2] = (const uint32_t (*)[2])S; 169 | const uint32_t (*S1)[2] = S0 + S_SIZE1 * S_SIMD; 170 | size_t i, j, k; 171 | 172 | for (i = 0; i < S_ROUNDS; i++) { 173 | for (j = 0; j < S_P; j++) { 174 | uint32_t xl = X[j][0][0]; 175 | uint32_t xh = X[j][0][1]; 176 | const uint32_t (*p0)[2], (*p1)[2]; 177 | 178 | p0 = S0 + (xl & S_MASK) / sizeof(*S0); 179 | p1 = S1 + (xh & S_MASK) / sizeof(*S1); 180 | 181 | for (k = 0; k < S_SIMD; k++) { 182 | uint64_t x, s0, s1; 183 | 184 | s0 = ((uint64_t)p0[k][1] << 32) + p0[k][0]; 185 | s1 = ((uint64_t)p1[k][1] << 32) + p1[k][0]; 186 | 187 | xl = X[j][k][0]; 188 | xh = X[j][k][1]; 189 | 190 | x = (uint64_t)xh * xl; 191 | x += s0; 192 | x ^= s1; 193 | 194 | X[j][k][0] = x; 195 | X[j][k][1] = x >> 32; 196 | } 197 | } 198 | } 199 | } 200 | 201 | /** 202 | * blockmix_pwxform(B, Y, S, r): 203 | * Compute B = BlockMix_pwxform{salsa20/8, S, r}(B). The input B must be 128r 204 | * bytes in length; the temporary space Y must be at least S_P_SIZE*4 bytes. 205 | */ 206 | static void 207 | blockmix_pwxform(uint32_t * B, uint32_t * Y, const uint32_t * S, size_t r) 208 | { 209 | size_t r1, r2, i; 210 | 211 | /* Convert 128-byte blocks to (S_P_SIZE * 32-bit) blocks */ 212 | r1 = r * 128 / (S_P_SIZE * 4); 213 | 214 | /* X <-- B_{r1 - 1} */ 215 | blkcpy(Y, &B[(r1 - 1) * S_P_SIZE], S_P_SIZE); 216 | 217 | /* for i = 0 to r1 - 1 do */ 218 | for (i = 0; i < r1; i++) { 219 | /* X <-- X \xor B_i */ 220 | blkxor(Y, &B[i * S_P_SIZE], S_P_SIZE); 221 | 222 | /* X <-- H'(X) */ 223 | block_pwxform(Y, S); 224 | 225 | /* B'_i <-- X */ 226 | blkcpy(&B[i * S_P_SIZE], Y, S_P_SIZE); 227 | } 228 | 229 | i = (r1 - 1) * S_P_SIZE / 16; 230 | /* Convert 128-byte blocks to 64-byte blocks */ 231 | r2 = r * 2; 232 | 233 | /* B_i <-- H(B_i) */ 234 | salsa20_8(&B[i * 16]); 235 | i++; 236 | 237 | for (; i < r2; i++) { 238 | /* B_i <-- H(B_i \xor B_{i-1}) */ 239 | blkxor(&B[i * 16], &B[(i - 1) * 16], 16); 240 | salsa20_8(&B[i * 16]); 241 | } 242 | } 243 | 244 | /** 245 | * integerify(B, r): 246 | * Return the result of parsing B_{2r-1} as a little-endian integer. 247 | */ 248 | static uint64_t 249 | integerify(const uint32_t * B, size_t r) 250 | { 251 | /* 252 | * Our 32-bit words are in host byte order, and word 13 is the second word of 253 | * B_{2r-1} due to SIMD shuffling. The 64-bit value we return is also in host 254 | * byte order, as it should be. 255 | */ 256 | const uint32_t * X = &B[(2 * r - 1) * 16]; 257 | return ((uint64_t)X[13] << 32) + X[0]; 258 | } 259 | 260 | /** 261 | * p2floor(x): 262 | * Largest power of 2 not greater than argument. 263 | */ 264 | static uint64_t 265 | p2floor(uint64_t x) 266 | { 267 | uint64_t y; 268 | while ((y = x & (x - 1))) 269 | x = y; 270 | return x; 271 | } 272 | 273 | /** 274 | * wrap(x, i): 275 | * Wrap x to the range 0 to i-1. 276 | */ 277 | static uint64_t 278 | wrap(uint64_t x, uint64_t i) 279 | { 280 | uint64_t n = p2floor(i); 281 | return (x & (n - 1)) + (i - n); 282 | } 283 | 284 | /** 285 | * smix1(B, r, N, flags, V, NROM, shared, XY, S): 286 | * Compute first loop of B = SMix_r(B, N). The input B must be 128r bytes in 287 | * length; the temporary storage V must be 128rN bytes in length; the temporary 288 | * storage XY must be 256r bytes in length. 289 | */ 290 | static void 291 | smix1(uint32_t * B, size_t r, uint64_t N, yescrypt_flags_t flags, 292 | uint32_t * V, uint64_t NROM, const yescrypt_shared_t * shared, 293 | uint32_t * XY, uint32_t * S) 294 | { 295 | const uint32_t * VROM = shared->shared1.aligned; 296 | uint32_t VROM_mask = NROM ? shared->mask1 : 0; 297 | size_t s = 32 * r; 298 | uint32_t * X = XY; 299 | uint32_t * Y = &XY[s]; 300 | uint64_t i, j; 301 | size_t k; 302 | 303 | /* 1: X <-- B */ 304 | for (k = 0; k < 2 * r; k++) 305 | for (i = 0; i < 16; i++) 306 | X[k * 16 + i] = le32dec(&B[k * 16 + (i * 5 % 16)]); 307 | 308 | /* 2: for i = 0 to N - 1 do */ 309 | for (i = 0; i < N; i++) { 310 | /* 3: V_i <-- X */ 311 | blkcpy(&V[i * s], X, s); 312 | 313 | if ((i & VROM_mask) == 1) { 314 | /* j <-- Integerify(X) mod NROM */ 315 | j = integerify(X, r) & (NROM - 1); 316 | 317 | /* X <-- H(X \xor VROM_j) */ 318 | blkxor(X, &VROM[j * s], s); 319 | } else if ((flags & YESCRYPT_RW) && i > 1) { 320 | /* j <-- Wrap(Integerify(X), i) */ 321 | j = wrap(integerify(X, r), i); 322 | 323 | /* X <-- X \xor V_j */ 324 | blkxor(X, &V[j * s], s); 325 | } 326 | 327 | /* 4: X <-- H(X) */ 328 | if (S) 329 | blockmix_pwxform(X, Y, S, r); 330 | else 331 | blockmix_salsa8(X, Y, r); 332 | } 333 | 334 | /* B' <-- X */ 335 | for (k = 0; k < 2 * r; k++) 336 | for (i = 0; i < 16; i++) 337 | le32enc(&B[k * 16 + (i * 5 % 16)], X[k * 16 + i]); 338 | } 339 | 340 | /** 341 | * smix2(B, r, N, Nloop, flags, V, NROM, shared, XY, S): 342 | * Compute second loop of B = SMix_r(B, N). The input B must be 128r bytes in 343 | * length; the temporary storage V must be 128rN bytes in length; the temporary 344 | * storage XY must be 256r bytes in length. The value N must be a power of 2 345 | * greater than 1. 346 | */ 347 | static void 348 | smix2(uint32_t * B, size_t r, uint64_t N, uint64_t Nloop, 349 | yescrypt_flags_t flags, uint32_t * V, uint64_t NROM, 350 | const yescrypt_shared_t * shared, uint32_t * XY, uint32_t * S) 351 | { 352 | const uint32_t * VROM = shared->shared1.aligned; 353 | uint32_t VROM_mask = NROM ? (shared->mask1 | 1) : 0; 354 | size_t s = 32 * r; 355 | uint32_t * X = XY; 356 | uint32_t * Y = &XY[s]; 357 | uint64_t i, j; 358 | size_t k; 359 | 360 | /* X <-- B */ 361 | for (k = 0; k < 2 * r; k++) 362 | for (i = 0; i < 16; i++) 363 | X[k * 16 + i] = le32dec(&B[k * 16 + (i * 5 % 16)]); 364 | 365 | /* 6: for i = 0 to N - 1 do */ 366 | for (i = 0; i < Nloop; i++) { 367 | if ((i & VROM_mask) == 1) { 368 | /* j <-- Integerify(X) mod NROM */ 369 | j = integerify(X, r) & (NROM - 1); 370 | 371 | /* X <-- H(X \xor VROM_j) */ 372 | blkxor(X, &VROM[j * s], s); 373 | } else { 374 | /* 7: j <-- Integerify(X) mod N */ 375 | j = integerify(X, r) & (N - 1); 376 | 377 | /* 8.1: X <-- X \xor V_j */ 378 | blkxor(X, &V[j * s], s); 379 | /* V_j <-- X */ 380 | if (flags & YESCRYPT_RW) 381 | blkcpy(&V[j * s], X, s); 382 | } 383 | 384 | /* 8.2: X <-- H(X) */ 385 | if (S) 386 | blockmix_pwxform(X, Y, S, r); 387 | else 388 | blockmix_salsa8(X, Y, r); 389 | } 390 | 391 | /* 10: B' <-- X */ 392 | for (k = 0; k < 2 * r; k++) 393 | for (i = 0; i < 16; i++) 394 | le32enc(&B[k * 16 + (i * 5 % 16)], X[k * 16 + i]); 395 | } 396 | 397 | /** 398 | * smix(B, r, N, p, t, flags, V, NROM, shared, XY, S): 399 | * Compute B = SMix_r(B, N). The input B must be 128rp bytes in length; the 400 | * temporary storage V must be 128rN bytes in length; the temporary storage 401 | * XY must be 256r bytes in length. The value N must be a power of 2 greater 402 | * than 1. 403 | */ 404 | static void 405 | smix(uint32_t * B, size_t r, uint64_t N, uint32_t p, uint32_t t, 406 | yescrypt_flags_t flags, 407 | uint32_t * V, uint64_t NROM, const yescrypt_shared_t * shared, 408 | uint32_t * XY, uint32_t * S) 409 | { 410 | size_t s = 32 * r; 411 | uint64_t Vchunk = 0, Nchunk = N / p, Nloop_all, Nloop_rw; 412 | uint32_t i; 413 | 414 | Nloop_all = Nchunk; 415 | if (flags & YESCRYPT_RW) { 416 | if (t <= 1) { 417 | if (t) 418 | Nloop_all *= 2; /* 2/3 */ 419 | Nloop_all = (Nloop_all + 2) / 3; /* 1/3, round up */ 420 | } else { 421 | Nloop_all *= t - 1; 422 | } 423 | } else if (t) { 424 | if (t == 1) 425 | Nloop_all += (Nloop_all + 1) / 2; /* 1.5, round up */ 426 | Nloop_all *= t; 427 | } 428 | 429 | Nloop_rw = 0; 430 | if (flags & __YESCRYPT_INIT_SHARED) 431 | Nloop_rw = Nloop_all; 432 | else if (flags & YESCRYPT_RW) 433 | Nloop_rw = Nloop_all / p; 434 | 435 | Nchunk &= ~(uint64_t)1; /* round down to even */ 436 | Nloop_all++; Nloop_all &= ~(uint64_t)1; /* round up to even */ 437 | Nloop_rw &= ~(uint64_t)1; /* round down to even */ 438 | 439 | for (i = 0; i < p; i++) { 440 | uint32_t * Bp = &B[i * s]; 441 | uint32_t * Vp = &V[Vchunk * s]; 442 | uint64_t Np = (i < p - 1) ? Nchunk : (N - Vchunk); 443 | uint32_t * Sp = S ? &S[i * S_SIZE_ALL] : S; 444 | if (Sp) 445 | smix1(Bp, 1, S_SIZE_ALL / 32, 446 | flags & ~YESCRYPT_PWXFORM, 447 | Sp, NROM, shared, XY, NULL); 448 | if (!(flags & __YESCRYPT_INIT_SHARED_2)) 449 | smix1(Bp, r, Np, flags, Vp, NROM, shared, XY, Sp); 450 | smix2(Bp, r, p2floor(Np), Nloop_rw, flags, Vp, 451 | NROM, shared, XY, Sp); 452 | Vchunk += Nchunk; 453 | } 454 | 455 | for (i = 0; i < p; i++) { 456 | uint32_t * Bp = &B[i * s]; 457 | uint32_t * Sp = S ? &S[i * S_SIZE_ALL] : NULL; 458 | smix2(Bp, r, N, Nloop_all - Nloop_rw, flags & ~YESCRYPT_RW, 459 | V, NROM, shared, XY, Sp); 460 | } 461 | } 462 | 463 | /** 464 | * yescrypt_kdf(shared, local, passwd, passwdlen, salt, saltlen, 465 | * N, r, p, t, flags, buf, buflen): 466 | * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, 467 | * p, buflen), or a revision of scrypt as requested by flags and shared, and 468 | * write the result into buf. The parameters r, p, and buflen must satisfy 469 | * r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N must be a power 470 | * of 2 greater than 1. 471 | * 472 | * t controls computation time while not affecting peak memory usage. shared 473 | * and flags may request special modes as described in yescrypt.h. local is 474 | * the thread-local data structure, allowing optimized implementations to 475 | * preserve and reuse a memory allocation across calls, thereby reducing its 476 | * overhead (this reference implementation does not make that optimization). 477 | * 478 | * Return 0 on success; or -1 on error. 479 | */ 480 | int 481 | yescrypt_kdf(const yescrypt_shared_t * shared, yescrypt_local_t * local, 482 | const uint8_t * passwd, size_t passwdlen, 483 | const uint8_t * salt, size_t saltlen, 484 | uint64_t N, uint32_t r, uint32_t p, uint32_t t, yescrypt_flags_t flags, 485 | uint8_t * buf, size_t buflen) 486 | { 487 | int retval = -1; 488 | uint64_t NROM; 489 | size_t B_size, V_size; 490 | uint32_t * B, * V, * XY, * S; 491 | uint32_t sha256[8]; 492 | 493 | /* 494 | * YESCRYPT_PARALLEL_SMIX is a no-op at p = 1 for its intended purpose, 495 | * so don't let it have side-effects. Without this adjustment, it'd 496 | * enable the SHA-256 password pre-hashing and output post-hashing, 497 | * because any deviation from classic scrypt implies those. 498 | */ 499 | if (p == 1) 500 | flags &= ~YESCRYPT_PARALLEL_SMIX; 501 | 502 | /* Sanity-check parameters */ 503 | if (flags & ~YESCRYPT_KNOWN_FLAGS) { 504 | errno = EINVAL; 505 | return -1; 506 | } 507 | #if SIZE_MAX > UINT32_MAX 508 | if (buflen > (((uint64_t)(1) << 32) - 1) * 32) { 509 | errno = EFBIG; 510 | return -1; 511 | } 512 | #endif 513 | if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) { 514 | errno = EFBIG; 515 | return -1; 516 | } 517 | if (((N & (N - 1)) != 0) || (N <= 1) || (r < 1) || (p < 1)) { 518 | errno = EINVAL; 519 | return -1; 520 | } 521 | if ((flags & YESCRYPT_PARALLEL_SMIX) && (N / p <= 1)) { 522 | errno = EINVAL; 523 | return -1; 524 | } 525 | #if S_MIN_R > 1 526 | if ((flags & YESCRYPT_PWXFORM) && (r < S_MIN_R)) { 527 | errno = EINVAL; 528 | return -1; 529 | } 530 | #endif 531 | if ((r > SIZE_MAX / 128 / p) || 532 | #if SIZE_MAX / 256 <= UINT32_MAX 533 | (r > SIZE_MAX / 256) || 534 | #endif 535 | (N > SIZE_MAX / 128 / r)) { 536 | errno = ENOMEM; 537 | return -1; 538 | } 539 | if (N > UINT64_MAX / ((uint64_t)t + 1)) { 540 | errno = EFBIG; 541 | return -1; 542 | } 543 | if (((flags & (YESCRYPT_PWXFORM | YESCRYPT_PARALLEL_SMIX)) == 544 | (YESCRYPT_PWXFORM | YESCRYPT_PARALLEL_SMIX)) && 545 | p > SIZE_MAX / (S_SIZE_ALL * sizeof(*S))) { 546 | errno = ENOMEM; 547 | return -1; 548 | } 549 | 550 | NROM = 0; 551 | if (shared->shared1.aligned) { 552 | NROM = shared->shared1.aligned_size / ((size_t)128 * r); 553 | /* 554 | * This implementation could support NROM without YESCRYPT_RW as well, but we 555 | * currently don't want to make such support available so that it can be safely 556 | * excluded from optimized implementations (where it'd require extra code). 557 | */ 558 | if (((NROM & (NROM - 1)) != 0) || (NROM <= 1) || 559 | !(flags & YESCRYPT_RW)) { 560 | errno = EINVAL; 561 | return -1; 562 | } 563 | } 564 | 565 | /* Allocate memory */ 566 | V_size = (size_t)128 * r * N; 567 | if (flags & __YESCRYPT_INIT_SHARED) { 568 | V = (uint32_t *)local->aligned; 569 | if (local->aligned_size < V_size) { 570 | if (local->base || local->aligned || 571 | local->base_size || local->aligned_size) { 572 | errno = EINVAL; 573 | return -1; 574 | } 575 | if ((V = malloc(V_size)) == NULL) 576 | return -1; 577 | local->base = local->aligned = V; 578 | local->base_size = local->aligned_size = V_size; 579 | } 580 | } else { 581 | if ((V = malloc(V_size)) == NULL) 582 | return -1; 583 | } 584 | B_size = (size_t)128 * r * p; 585 | if ((B = malloc(B_size)) == NULL) 586 | goto free_V; 587 | if ((XY = malloc((size_t)256 * r)) == NULL) 588 | goto free_B; 589 | S = NULL; 590 | if (flags & YESCRYPT_PWXFORM) { 591 | size_t S_size = S_SIZE_ALL * sizeof(*S); 592 | if (flags & YESCRYPT_PARALLEL_SMIX) 593 | S_size *= p; 594 | if ((S = malloc(S_size)) == NULL) 595 | goto free_XY; 596 | } 597 | 598 | if (t || flags) { 599 | SHA256_CTX ctx; 600 | SHA256_Init(&ctx); 601 | SHA256_Update(&ctx, passwd, passwdlen); 602 | SHA256_Final((uint8_t *)sha256, &ctx); 603 | passwd = (uint8_t *)sha256; 604 | passwdlen = sizeof(sha256); 605 | } 606 | 607 | /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */ 608 | PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, 609 | (uint8_t *)B, B_size); 610 | 611 | if (t || flags) 612 | blkcpy(sha256, B, sizeof(sha256) / sizeof(sha256[0])); 613 | 614 | if (flags & YESCRYPT_PARALLEL_SMIX) { 615 | smix(B, r, N, p, t, flags, V, NROM, shared, XY, S); 616 | } else { 617 | uint32_t i; 618 | 619 | /* 2: for i = 0 to p - 1 do */ 620 | for (i = 0; i < p; i++) { 621 | /* 3: B_i <-- MF(B_i, N) */ 622 | smix(&B[(size_t)32 * r * i], r, N, 1, t, flags, V, 623 | NROM, shared, XY, S); 624 | } 625 | } 626 | 627 | /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */ 628 | PBKDF2_SHA256(passwd, passwdlen, (uint8_t *)B, B_size, 1, buf, buflen); 629 | 630 | /* 631 | * Except when computing classic scrypt, allow all computation so far 632 | * to be performed on the client. The final steps below match those of 633 | * SCRAM (RFC 5802), so that an extension of SCRAM (with the steps so 634 | * far in place of SCRAM's use of PBKDF2 and with SHA-256 in place of 635 | * SCRAM's use of SHA-1) would be usable with yescrypt hashes. 636 | */ 637 | if ((t || flags) && buflen == sizeof(sha256)) { 638 | /* Compute ClientKey */ 639 | { 640 | HMAC_SHA256_CTX ctx; 641 | HMAC_SHA256_Init(&ctx, buf, buflen); 642 | HMAC_SHA256_Update(&ctx, "Client Key", 10); 643 | HMAC_SHA256_Final((uint8_t *)sha256, &ctx); 644 | } 645 | /* Compute StoredKey */ 646 | { 647 | SHA256_CTX ctx; 648 | SHA256_Init(&ctx); 649 | SHA256_Update(&ctx, (uint8_t *)sha256, sizeof(sha256)); 650 | SHA256_Final(buf, &ctx); 651 | } 652 | } 653 | 654 | /* Success! */ 655 | retval = 0; 656 | 657 | /* Free memory */ 658 | free(S); 659 | free_XY: 660 | free(XY); 661 | free_B: 662 | free(B); 663 | free_V: 664 | if (!(flags & __YESCRYPT_INIT_SHARED)) 665 | free(V); 666 | 667 | return retval; 668 | } 669 | 670 | int 671 | yescrypt_init_shared(yescrypt_shared_t * shared, 672 | const uint8_t * param, size_t paramlen, 673 | uint64_t N, uint32_t r, uint32_t p, 674 | yescrypt_init_shared_flags_t flags, uint32_t mask, 675 | uint8_t * buf, size_t buflen) 676 | { 677 | yescrypt_shared1_t * shared1 = &shared->shared1; 678 | yescrypt_shared_t dummy, half1, half2; 679 | uint8_t salt[32]; 680 | 681 | if (flags & YESCRYPT_SHARED_PREALLOCATED) { 682 | if (!shared1->aligned || !shared1->aligned_size) 683 | return -1; 684 | } else { 685 | shared1->base = shared1->aligned = NULL; 686 | shared1->base_size = shared1->aligned_size = 0; 687 | } 688 | shared->mask1 = 1; 689 | if (!param && !paramlen && !N && !r && !p && !buf && !buflen) 690 | return 0; 691 | 692 | dummy.shared1.base = dummy.shared1.aligned = NULL; 693 | dummy.shared1.base_size = dummy.shared1.aligned_size = 0; 694 | dummy.mask1 = 1; 695 | if (yescrypt_kdf(&dummy, shared1, 696 | param, paramlen, NULL, 0, N, r, p, 0, 697 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_1, 698 | salt, sizeof(salt))) 699 | goto out; 700 | 701 | half1 = half2 = *shared; 702 | half1.shared1.aligned_size /= 2; 703 | half2.shared1.aligned += half1.shared1.aligned_size; 704 | half2.shared1.aligned_size = half1.shared1.aligned_size; 705 | N /= 2; 706 | 707 | if (p > 1 && yescrypt_kdf(&half1, &half2.shared1, 708 | param, paramlen, salt, sizeof(salt), N, r, p, 0, 709 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_2, 710 | salt, sizeof(salt))) 711 | goto out; 712 | 713 | if (yescrypt_kdf(&half2, &half1.shared1, 714 | param, paramlen, salt, sizeof(salt), N, r, p, 0, 715 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_1, 716 | salt, sizeof(salt))) 717 | goto out; 718 | 719 | if (yescrypt_kdf(&half1, &half2.shared1, 720 | param, paramlen, salt, sizeof(salt), N, r, p, 0, 721 | YESCRYPT_RW | YESCRYPT_PARALLEL_SMIX | __YESCRYPT_INIT_SHARED_1, 722 | buf, buflen)) 723 | goto out; 724 | 725 | shared->mask1 = mask; 726 | 727 | return 0; 728 | 729 | out: 730 | if (!(flags & YESCRYPT_SHARED_PREALLOCATED)) 731 | free(shared1->base); 732 | return -1; 733 | } 734 | 735 | int 736 | yescrypt_free_shared(yescrypt_shared_t * shared) 737 | { 738 | free(shared->shared1.base); 739 | shared->shared1.base = shared->shared1.aligned = NULL; 740 | shared->shared1.base_size = shared->shared1.aligned_size = 0; 741 | return 0; 742 | } 743 | 744 | int 745 | yescrypt_init_local(yescrypt_local_t * local) 746 | { 747 | /* The reference implementation doesn't use the local structure */ 748 | local->base = local->aligned = NULL; 749 | local->base_size = local->aligned_size = 0; 750 | return 0; 751 | } 752 | 753 | int 754 | yescrypt_free_local(yescrypt_local_t * local) 755 | { 756 | /* The reference implementation frees its memory in yescrypt_kdf() */ 757 | return 0; 758 | } 759 | --------------------------------------------------------------------------------