Debian Codegraph @

src/scrypt.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2015, Legrandin <[email protected]>
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 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include "common.h"
33 #include "endianess.h"
34
35 FAKE_INIT(scrypt)
36
37 /** Return 1 if the pointer is aligned to the 64-bit boundary **/
38 static inline int aligned64(const uint8_t *p)
39 {
40 if ((uintptr_t)p % 8 == 0)
41 return 1;
42 else
43 return 0;
44 }
45
46 static inline void strxor_inplace(uint8_t *in_out, const uint8_t *in, size_t data_len)
47 {
48 size_t i;
49
50 if (!aligned64(in_out) || !aligned64(in) || (data_len%8)!=0) {
51 for (i=0; i<data_len; i++) {
52 *in_out++ ^= *in++;
53 }
54 } else {
55 uint64_t *in_out64 = (uint64_t*)in_out;
56 uint64_t *in64 = (uint64_t*)in;
57
58 data_len /= 8;
59 for (i=0; i<data_len; i++) {
60 *in_out64++ ^= *in64++;
61 }
62 }
63 }
64
65 typedef int (core_t)(const uint8_t [64], const uint8_t [64], uint8_t [64]);
66
67 /**
68 * scrypt BlockMix function
69 *
70 * @in: input, a sequence of blocks, each 64 bytes long
71 * @out: output, a sequence of blocks, each 64 bytes long
72 * @two_r: the number of 64 bytes blocks in input and output (even)
73 * @core: the Salsa20 core hashing function (with xor)
74 *
75 * @in and @out may not overlap.
76 */
77 static void scryptBlockMix(const uint8_t in[][64], uint8_t out[][64], size_t two_r,
78 core_t *core)
79 {
80 unsigned i;
81 const uint8_t (*x)[64];
82 size_t r = two_r / 2;
83
84 assert((void*)in != (void*)out);
85
86 x = &in[two_r-1]; /* Last 64 byte block */
87 for (i=0; i<two_r; i++) {
88 uint8_t (*y)[64];
89
90 y = &out[(i/2) + (i & 1)*r];
91 (*core)(*x, in[i], *y);
92 x = y;
93 }
94 }
95
96 /**
97 * scrypt ROMix function
98 *
99 * @data_in: input
100 * @data_out: output
101 * @data_len: Length in bytes of the two buffers `data_in` and `data_out`.
102 * It is a multiple of 128.
103 * @N: Memory/CPU cost. It must be a power of 2.
104 *
105 * @data_in and @data_out may overlap.
106 */
107 EXPORT_SYM int scryptROMix(const uint8_t *data_in, uint8_t *data_out,
108 size_t data_len, unsigned N, core_t *core)
109 {
110 size_t two_r;
111 uint8_t (*v)[64], (*x)[64];
112 unsigned i;
113
114 if (NULL==data_in || NULL==data_out || NULL==core) {
115 return ERR_NULL;
116 }
117
118 two_r = data_len / 64;
119 if ((two_r*64 != data_len) || (two_r & 1)) {
120 return ERR_BLOCK_SIZE;
121 }
122
123 /** Allocate a possibly gigantic amount of memory **/
124 /* It takes in total (N+1) * (128r) bytes */
125 v = calloc(N + 1, data_len);
126 if (NULL == v) {
127 return ERR_MEMORY;
128 }
129 memmove(&v[0], data_in, data_len);
130
131 /** Create the V array **/
132 /* The size of each element of V is 64 bytes, so
133 * you need to add `two_r` (not 1) to move forward.
134 */
135 x = v;
136 for (i=0; i<N; i++) {
137 scryptBlockMix(x, &x[two_r], two_r, core);
138 x += two_r;
139 }
140
141 /** Mix via a pseudo random sequence **/
142 x = &v[N * two_r];
143 for (i=0; i<N; i++) {
144 uint32_t index;
145
146 index = LOAD_U32_LITTLE(&x[two_r - 1][0]) & (N - 1); /* Extract pseudo
147 random index
148 from last element
149 */
150 strxor_inplace(*x, v[index * two_r], data_len);
151 scryptBlockMix(x, (uint8_t (*)[64])data_out, two_r, core);
152 memmove(x, data_out, data_len);
153 }
154
155 /** Outro **/
156 /** data_out already contains the right value **/
157 free(v);
158 return 0;
159 }