salsa20 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +8 -0
- data/CHANGELOG +3 -0
- data/LICENSE +25 -0
- data/README.rdoc +52 -0
- data/Rakefile +21 -0
- data/ext/salsa20_ext/ecrypt-config.h +272 -0
- data/ext/salsa20_ext/ecrypt-machine.h +46 -0
- data/ext/salsa20_ext/ecrypt-portable.h +303 -0
- data/ext/salsa20_ext/ecrypt-sync.h +279 -0
- data/ext/salsa20_ext/extconf.rb +7 -0
- data/ext/salsa20_ext/salsa20.c +221 -0
- data/ext/salsa20_ext/salsa20_ext.c +87 -0
- data/lib/salsa20.rb +105 -0
- data/salsa20.gemspec +28 -0
- data/test/salsa20_test.rb +140 -0
- metadata +94 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (C) 2011-2011 Dov Murik. All rights reserved.
|
2
|
+
|
3
|
+
|
4
|
+
Original C implementation of the Salsa20 algorithm by Daniel Bernstein.
|
5
|
+
|
6
|
+
|
7
|
+
Redistribution and use in source and binary forms, with or without
|
8
|
+
modification, are permitted provided that the following conditions are met:
|
9
|
+
|
10
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
11
|
+
list of conditions and the following disclaimer.
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
20
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
21
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
22
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
23
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
24
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
25
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
= salsa20
|
2
|
+
|
3
|
+
Ruby-wrapper for the {Salsa20 stream cipher
|
4
|
+
algorithm}[http://cr.yp.to/snuffle.html] designed by Daniel Bernstein. Salsa20
|
5
|
+
is a family of 256-bit stream ciphers designed in 2005 and submitted to
|
6
|
+
eSTREAM, the ECRYPT Stream Cipher Project.
|
7
|
+
|
8
|
+
== How to install
|
9
|
+
|
10
|
+
gem install salsa20
|
11
|
+
|
12
|
+
You'll need a working compiler -- the crypto code is the {original C
|
13
|
+
implementation}[http://cr.yp.to/snuffle.html] from Daniel Bernstein.
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
require 'salsa20'
|
18
|
+
|
19
|
+
key = "VERY_SECRET_256_BIT_KEY_12345678"
|
20
|
+
iv = "-RANDOM-"
|
21
|
+
plain_text = "Salsa20 is a family of 256-bit stream ciphers"
|
22
|
+
|
23
|
+
encryptor = Salsa20.new(key, iv)
|
24
|
+
encrypted_text = encryptor.encrypt(plain_text)
|
25
|
+
p encrypted_text
|
26
|
+
# => "\x9D\x1C\xE4\x83\xAB\x8E\xB7\x85a,\xC3\xF6\x981*\x03\b-\x99\xAD\xDF\xBFS\x96\x94$\xA0\xF0U\v\xABz;=R\xBB\xE1\xB0\xDD\xBC\x1A9\xB8\xBEb"
|
27
|
+
|
28
|
+
decryptor = Salsa20.new(key, iv)
|
29
|
+
decrypted_text = decryptor.decrypt(encrypted_text)
|
30
|
+
p decrypted_text
|
31
|
+
# => "Salsa20 is a family of 256-bit stream ciphers"
|
32
|
+
|
33
|
+
The Salsa20 cipher algorhitm supports efficiently seeking to any 64-bytes
|
34
|
+
boundry position in the stream using the +seek+ method. Use +position+ to tell
|
35
|
+
the current stream position in bytes.
|
36
|
+
|
37
|
+
For more information, see the detailed rdoc of the Salsa20 class.
|
38
|
+
|
39
|
+
== References
|
40
|
+
|
41
|
+
* http://cr.yp.to/snuffle.html
|
42
|
+
* http://www.ecrypt.eu.org/stream/salsa20pf.html
|
43
|
+
* http://en.wikipedia.org/wiki/Salsa20
|
44
|
+
|
45
|
+
== License
|
46
|
+
|
47
|
+
BSD 2-Clause open source license (full license text in the +LICENSE+ file).
|
48
|
+
|
49
|
+
== Contact
|
50
|
+
|
51
|
+
Author :: Dov Murik (dov.murik@gmail.com)
|
52
|
+
Project homepage :: https://github.com/dubek/salsa20-ruby
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
gem 'rdoc'
|
2
|
+
require 'rdoc/task'
|
3
|
+
require 'rake/extensiontask'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
GEMSPEC = eval(File.read(File.expand_path("../salsa20.gemspec", __FILE__)))
|
7
|
+
|
8
|
+
Rake::ExtensionTask.new('salsa20_ext')
|
9
|
+
|
10
|
+
Rake::TestTask.new do |t|
|
11
|
+
t.libs << "test"
|
12
|
+
t.test_files = FileList['test/*test.rb']
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
RDoc::Task.new do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'doc/rdoc'
|
18
|
+
rdoc.options += GEMSPEC.rdoc_options
|
19
|
+
rdoc.template = ENV['TEMPLATE'] if ENV['TEMPLATE']
|
20
|
+
rdoc.rdoc_files.include(*GEMSPEC.extra_rdoc_files)
|
21
|
+
end
|
@@ -0,0 +1,272 @@
|
|
1
|
+
/* ecrypt-config.h */
|
2
|
+
|
3
|
+
/* *** Normally, it should not be necessary to edit this file. *** */
|
4
|
+
|
5
|
+
#ifndef ECRYPT_CONFIG
|
6
|
+
#define ECRYPT_CONFIG
|
7
|
+
|
8
|
+
/* ------------------------------------------------------------------------- */
|
9
|
+
|
10
|
+
/* Guess the endianness of the target architecture. */
|
11
|
+
|
12
|
+
/*
|
13
|
+
* The LITTLE endian machines:
|
14
|
+
*/
|
15
|
+
#if defined(__ultrix) /* Older MIPS */
|
16
|
+
#define ECRYPT_LITTLE_ENDIAN
|
17
|
+
#elif defined(__alpha) /* Alpha */
|
18
|
+
#define ECRYPT_LITTLE_ENDIAN
|
19
|
+
#elif defined(i386) /* x86 (gcc) */
|
20
|
+
#define ECRYPT_LITTLE_ENDIAN
|
21
|
+
#elif defined(__i386) /* x86 (gcc) */
|
22
|
+
#define ECRYPT_LITTLE_ENDIAN
|
23
|
+
#elif defined(_M_IX86) /* x86 (MSC, Borland) */
|
24
|
+
#define ECRYPT_LITTLE_ENDIAN
|
25
|
+
#elif defined(_MSC_VER) /* x86 (surely MSC) */
|
26
|
+
#define ECRYPT_LITTLE_ENDIAN
|
27
|
+
#elif defined(__INTEL_COMPILER) /* x86 (surely Intel compiler icl.exe) */
|
28
|
+
#define ECRYPT_LITTLE_ENDIAN
|
29
|
+
|
30
|
+
/*
|
31
|
+
* The BIG endian machines:
|
32
|
+
*/
|
33
|
+
#elif defined(sun) /* Newer Sparc's */
|
34
|
+
#define ECRYPT_BIG_ENDIAN
|
35
|
+
#elif defined(__ppc__) /* PowerPC */
|
36
|
+
#define ECRYPT_BIG_ENDIAN
|
37
|
+
|
38
|
+
/*
|
39
|
+
* Finally machines with UNKNOWN endianness:
|
40
|
+
*/
|
41
|
+
#elif defined (_AIX) /* RS6000 */
|
42
|
+
#define ECRYPT_UNKNOWN
|
43
|
+
#elif defined(__hpux) /* HP-PA */
|
44
|
+
#define ECRYPT_UNKNOWN
|
45
|
+
#elif defined(__aux) /* 68K */
|
46
|
+
#define ECRYPT_UNKNOWN
|
47
|
+
#elif defined(__dgux) /* 88K (but P6 in latest boxes) */
|
48
|
+
#define ECRYPT_UNKNOWN
|
49
|
+
#elif defined(__sgi) /* Newer MIPS */
|
50
|
+
#define ECRYPT_UNKNOWN
|
51
|
+
#else /* Any other processor */
|
52
|
+
#define ECRYPT_UNKNOWN
|
53
|
+
#endif
|
54
|
+
|
55
|
+
/* ------------------------------------------------------------------------- */
|
56
|
+
|
57
|
+
/*
|
58
|
+
* Find minimal-width types to store 8-bit, 16-bit, 32-bit, and 64-bit
|
59
|
+
* integers.
|
60
|
+
*
|
61
|
+
* Note: to enable 64-bit types on 32-bit compilers, it might be
|
62
|
+
* necessary to switch from ISO C90 mode to ISO C99 mode (e.g., gcc
|
63
|
+
* -std=c99).
|
64
|
+
*/
|
65
|
+
|
66
|
+
#include <limits.h>
|
67
|
+
|
68
|
+
/* --- check char --- */
|
69
|
+
|
70
|
+
#if (UCHAR_MAX / 0xFU > 0xFU)
|
71
|
+
#ifndef I8T
|
72
|
+
#define I8T char
|
73
|
+
#define U8C(v) (v##U)
|
74
|
+
|
75
|
+
#if (UCHAR_MAX == 0xFFU)
|
76
|
+
#define ECRYPT_I8T_IS_BYTE
|
77
|
+
#endif
|
78
|
+
|
79
|
+
#endif
|
80
|
+
|
81
|
+
#if (UCHAR_MAX / 0xFFU > 0xFFU)
|
82
|
+
#ifndef I16T
|
83
|
+
#define I16T char
|
84
|
+
#define U16C(v) (v##U)
|
85
|
+
#endif
|
86
|
+
|
87
|
+
#if (UCHAR_MAX / 0xFFFFU > 0xFFFFU)
|
88
|
+
#ifndef I32T
|
89
|
+
#define I32T char
|
90
|
+
#define U32C(v) (v##U)
|
91
|
+
#endif
|
92
|
+
|
93
|
+
#if (UCHAR_MAX / 0xFFFFFFFFU > 0xFFFFFFFFU)
|
94
|
+
#ifndef I64T
|
95
|
+
#define I64T char
|
96
|
+
#define U64C(v) (v##U)
|
97
|
+
#define ECRYPT_NATIVE64
|
98
|
+
#endif
|
99
|
+
|
100
|
+
#endif
|
101
|
+
#endif
|
102
|
+
#endif
|
103
|
+
#endif
|
104
|
+
|
105
|
+
/* --- check short --- */
|
106
|
+
|
107
|
+
#if (USHRT_MAX / 0xFU > 0xFU)
|
108
|
+
#ifndef I8T
|
109
|
+
#define I8T short
|
110
|
+
#define U8C(v) (v##U)
|
111
|
+
|
112
|
+
#if (USHRT_MAX == 0xFFU)
|
113
|
+
#define ECRYPT_I8T_IS_BYTE
|
114
|
+
#endif
|
115
|
+
|
116
|
+
#endif
|
117
|
+
|
118
|
+
#if (USHRT_MAX / 0xFFU > 0xFFU)
|
119
|
+
#ifndef I16T
|
120
|
+
#define I16T short
|
121
|
+
#define U16C(v) (v##U)
|
122
|
+
#endif
|
123
|
+
|
124
|
+
#if (USHRT_MAX / 0xFFFFU > 0xFFFFU)
|
125
|
+
#ifndef I32T
|
126
|
+
#define I32T short
|
127
|
+
#define U32C(v) (v##U)
|
128
|
+
#endif
|
129
|
+
|
130
|
+
#if (USHRT_MAX / 0xFFFFFFFFU > 0xFFFFFFFFU)
|
131
|
+
#ifndef I64T
|
132
|
+
#define I64T short
|
133
|
+
#define U64C(v) (v##U)
|
134
|
+
#define ECRYPT_NATIVE64
|
135
|
+
#endif
|
136
|
+
|
137
|
+
#endif
|
138
|
+
#endif
|
139
|
+
#endif
|
140
|
+
#endif
|
141
|
+
|
142
|
+
/* --- check int --- */
|
143
|
+
|
144
|
+
#if (UINT_MAX / 0xFU > 0xFU)
|
145
|
+
#ifndef I8T
|
146
|
+
#define I8T int
|
147
|
+
#define U8C(v) (v##U)
|
148
|
+
|
149
|
+
#if (ULONG_MAX == 0xFFU)
|
150
|
+
#define ECRYPT_I8T_IS_BYTE
|
151
|
+
#endif
|
152
|
+
|
153
|
+
#endif
|
154
|
+
|
155
|
+
#if (UINT_MAX / 0xFFU > 0xFFU)
|
156
|
+
#ifndef I16T
|
157
|
+
#define I16T int
|
158
|
+
#define U16C(v) (v##U)
|
159
|
+
#endif
|
160
|
+
|
161
|
+
#if (UINT_MAX / 0xFFFFU > 0xFFFFU)
|
162
|
+
#ifndef I32T
|
163
|
+
#define I32T int
|
164
|
+
#define U32C(v) (v##U)
|
165
|
+
#endif
|
166
|
+
|
167
|
+
#if (UINT_MAX / 0xFFFFFFFFU > 0xFFFFFFFFU)
|
168
|
+
#ifndef I64T
|
169
|
+
#define I64T int
|
170
|
+
#define U64C(v) (v##U)
|
171
|
+
#define ECRYPT_NATIVE64
|
172
|
+
#endif
|
173
|
+
|
174
|
+
#endif
|
175
|
+
#endif
|
176
|
+
#endif
|
177
|
+
#endif
|
178
|
+
|
179
|
+
/* --- check long --- */
|
180
|
+
|
181
|
+
#if (ULONG_MAX / 0xFUL > 0xFUL)
|
182
|
+
#ifndef I8T
|
183
|
+
#define I8T long
|
184
|
+
#define U8C(v) (v##UL)
|
185
|
+
|
186
|
+
#if (ULONG_MAX == 0xFFUL)
|
187
|
+
#define ECRYPT_I8T_IS_BYTE
|
188
|
+
#endif
|
189
|
+
|
190
|
+
#endif
|
191
|
+
|
192
|
+
#if (ULONG_MAX / 0xFFUL > 0xFFUL)
|
193
|
+
#ifndef I16T
|
194
|
+
#define I16T long
|
195
|
+
#define U16C(v) (v##UL)
|
196
|
+
#endif
|
197
|
+
|
198
|
+
#if (ULONG_MAX / 0xFFFFUL > 0xFFFFUL)
|
199
|
+
#ifndef I32T
|
200
|
+
#define I32T long
|
201
|
+
#define U32C(v) (v##UL)
|
202
|
+
#endif
|
203
|
+
|
204
|
+
#if (ULONG_MAX / 0xFFFFFFFFUL > 0xFFFFFFFFUL)
|
205
|
+
#ifndef I64T
|
206
|
+
#define I64T long
|
207
|
+
#define U64C(v) (v##UL)
|
208
|
+
#define ECRYPT_NATIVE64
|
209
|
+
#endif
|
210
|
+
|
211
|
+
#endif
|
212
|
+
#endif
|
213
|
+
#endif
|
214
|
+
#endif
|
215
|
+
|
216
|
+
/* --- check long long --- */
|
217
|
+
|
218
|
+
#ifdef ULLONG_MAX
|
219
|
+
|
220
|
+
#if (ULLONG_MAX / 0xFULL > 0xFULL)
|
221
|
+
#ifndef I8T
|
222
|
+
#define I8T long long
|
223
|
+
#define U8C(v) (v##ULL)
|
224
|
+
|
225
|
+
#if (ULLONG_MAX == 0xFFULL)
|
226
|
+
#define ECRYPT_I8T_IS_BYTE
|
227
|
+
#endif
|
228
|
+
|
229
|
+
#endif
|
230
|
+
|
231
|
+
#if (ULLONG_MAX / 0xFFULL > 0xFFULL)
|
232
|
+
#ifndef I16T
|
233
|
+
#define I16T long long
|
234
|
+
#define U16C(v) (v##ULL)
|
235
|
+
#endif
|
236
|
+
|
237
|
+
#if (ULLONG_MAX / 0xFFFFULL > 0xFFFFULL)
|
238
|
+
#ifndef I32T
|
239
|
+
#define I32T long long
|
240
|
+
#define U32C(v) (v##ULL)
|
241
|
+
#endif
|
242
|
+
|
243
|
+
#if (ULLONG_MAX / 0xFFFFFFFFULL > 0xFFFFFFFFULL)
|
244
|
+
#ifndef I64T
|
245
|
+
#define I64T long long
|
246
|
+
#define U64C(v) (v##ULL)
|
247
|
+
#endif
|
248
|
+
|
249
|
+
#endif
|
250
|
+
#endif
|
251
|
+
#endif
|
252
|
+
#endif
|
253
|
+
|
254
|
+
#endif
|
255
|
+
|
256
|
+
/* --- check __int64 --- */
|
257
|
+
|
258
|
+
#ifdef _UI64_MAX
|
259
|
+
|
260
|
+
#if (_UI64_MAX / 0xFFFFFFFFui64 > 0xFFFFFFFFui64)
|
261
|
+
#ifndef I64T
|
262
|
+
#define I64T __int64
|
263
|
+
#define U64C(v) (v##ui64)
|
264
|
+
#endif
|
265
|
+
|
266
|
+
#endif
|
267
|
+
|
268
|
+
#endif
|
269
|
+
|
270
|
+
/* ------------------------------------------------------------------------- */
|
271
|
+
|
272
|
+
#endif
|
@@ -0,0 +1,46 @@
|
|
1
|
+
/* ecrypt-machine.h */
|
2
|
+
|
3
|
+
/*
|
4
|
+
* This file is included by 'ecrypt-portable.h'. It allows to override
|
5
|
+
* the default macros for specific platforms. Please carefully check
|
6
|
+
* the machine code generated by your compiler (with optimisations
|
7
|
+
* turned on) before deciding to edit this file.
|
8
|
+
*/
|
9
|
+
|
10
|
+
/* ------------------------------------------------------------------------- */
|
11
|
+
|
12
|
+
#if (defined(ECRYPT_DEFAULT_ROT) && !defined(ECRYPT_MACHINE_ROT))
|
13
|
+
|
14
|
+
#define ECRYPT_MACHINE_ROT
|
15
|
+
|
16
|
+
#if (defined(WIN32) && defined(_MSC_VER))
|
17
|
+
|
18
|
+
#undef ROTL32
|
19
|
+
#undef ROTR32
|
20
|
+
#undef ROTL64
|
21
|
+
#undef ROTR64
|
22
|
+
|
23
|
+
#include <stdlib.h>
|
24
|
+
|
25
|
+
#define ROTL32(v, n) _lrotl(v, n)
|
26
|
+
#define ROTR32(v, n) _lrotr(v, n)
|
27
|
+
#define ROTL64(v, n) _rotl64(v, n)
|
28
|
+
#define ROTR64(v, n) _rotr64(v, n)
|
29
|
+
|
30
|
+
#endif
|
31
|
+
|
32
|
+
#endif
|
33
|
+
|
34
|
+
/* ------------------------------------------------------------------------- */
|
35
|
+
|
36
|
+
#if (defined(ECRYPT_DEFAULT_SWAP) && !defined(ECRYPT_MACHINE_SWAP))
|
37
|
+
|
38
|
+
#define ECRYPT_MACHINE_SWAP
|
39
|
+
|
40
|
+
/*
|
41
|
+
* If you want to overwrite the default swap macros, put it here. And so on.
|
42
|
+
*/
|
43
|
+
|
44
|
+
#endif
|
45
|
+
|
46
|
+
/* ------------------------------------------------------------------------- */
|
@@ -0,0 +1,303 @@
|
|
1
|
+
/* ecrypt-portable.h */
|
2
|
+
|
3
|
+
/*
|
4
|
+
* WARNING: the conversions defined below are implemented as macros,
|
5
|
+
* and should be used carefully. They should NOT be used with
|
6
|
+
* parameters which perform some action. E.g., the following two lines
|
7
|
+
* are not equivalent:
|
8
|
+
*
|
9
|
+
* 1) ++x; y = ROTL32(x, n);
|
10
|
+
* 2) y = ROTL32(++x, n);
|
11
|
+
*/
|
12
|
+
|
13
|
+
/*
|
14
|
+
* *** Please do not edit this file. ***
|
15
|
+
*
|
16
|
+
* The default macros can be overridden for specific architectures by
|
17
|
+
* editing 'ecrypt-machine.h'.
|
18
|
+
*/
|
19
|
+
|
20
|
+
#ifndef ECRYPT_PORTABLE
|
21
|
+
#define ECRYPT_PORTABLE
|
22
|
+
|
23
|
+
#include "ecrypt-config.h"
|
24
|
+
|
25
|
+
/* ------------------------------------------------------------------------- */
|
26
|
+
|
27
|
+
/*
|
28
|
+
* The following types are defined (if available):
|
29
|
+
*
|
30
|
+
* u8: unsigned integer type, at least 8 bits
|
31
|
+
* u16: unsigned integer type, at least 16 bits
|
32
|
+
* u32: unsigned integer type, at least 32 bits
|
33
|
+
* u64: unsigned integer type, at least 64 bits
|
34
|
+
*
|
35
|
+
* s8, s16, s32, s64 -> signed counterparts of u8, u16, u32, u64
|
36
|
+
*
|
37
|
+
* The selection of minimum-width integer types is taken care of by
|
38
|
+
* 'ecrypt-config.h'. Note: to enable 64-bit types on 32-bit
|
39
|
+
* compilers, it might be necessary to switch from ISO C90 mode to ISO
|
40
|
+
* C99 mode (e.g., gcc -std=c99).
|
41
|
+
*/
|
42
|
+
|
43
|
+
#ifdef I8T
|
44
|
+
typedef signed I8T s8;
|
45
|
+
typedef unsigned I8T u8;
|
46
|
+
#endif
|
47
|
+
|
48
|
+
#ifdef I16T
|
49
|
+
typedef signed I16T s16;
|
50
|
+
typedef unsigned I16T u16;
|
51
|
+
#endif
|
52
|
+
|
53
|
+
#ifdef I32T
|
54
|
+
typedef signed I32T s32;
|
55
|
+
typedef unsigned I32T u32;
|
56
|
+
#endif
|
57
|
+
|
58
|
+
#ifdef I64T
|
59
|
+
typedef signed I64T s64;
|
60
|
+
typedef unsigned I64T u64;
|
61
|
+
#endif
|
62
|
+
|
63
|
+
/*
|
64
|
+
* The following macros are used to obtain exact-width results.
|
65
|
+
*/
|
66
|
+
|
67
|
+
#define U8V(v) ((u8)(v) & U8C(0xFF))
|
68
|
+
#define U16V(v) ((u16)(v) & U16C(0xFFFF))
|
69
|
+
#define U32V(v) ((u32)(v) & U32C(0xFFFFFFFF))
|
70
|
+
#define U64V(v) ((u64)(v) & U64C(0xFFFFFFFFFFFFFFFF))
|
71
|
+
|
72
|
+
/* ------------------------------------------------------------------------- */
|
73
|
+
|
74
|
+
/*
|
75
|
+
* The following macros return words with their bits rotated over n
|
76
|
+
* positions to the left/right.
|
77
|
+
*/
|
78
|
+
|
79
|
+
#define ECRYPT_DEFAULT_ROT
|
80
|
+
|
81
|
+
#define ROTL8(v, n) \
|
82
|
+
(U8V((v) << (n)) | ((v) >> (8 - (n))))
|
83
|
+
|
84
|
+
#define ROTL16(v, n) \
|
85
|
+
(U16V((v) << (n)) | ((v) >> (16 - (n))))
|
86
|
+
|
87
|
+
#define ROTL32(v, n) \
|
88
|
+
(U32V((v) << (n)) | ((v) >> (32 - (n))))
|
89
|
+
|
90
|
+
#define ROTL64(v, n) \
|
91
|
+
(U64V((v) << (n)) | ((v) >> (64 - (n))))
|
92
|
+
|
93
|
+
#define ROTR8(v, n) ROTL8(v, 8 - (n))
|
94
|
+
#define ROTR16(v, n) ROTL16(v, 16 - (n))
|
95
|
+
#define ROTR32(v, n) ROTL32(v, 32 - (n))
|
96
|
+
#define ROTR64(v, n) ROTL64(v, 64 - (n))
|
97
|
+
|
98
|
+
#include "ecrypt-machine.h"
|
99
|
+
|
100
|
+
/* ------------------------------------------------------------------------- */
|
101
|
+
|
102
|
+
/*
|
103
|
+
* The following macros return a word with bytes in reverse order.
|
104
|
+
*/
|
105
|
+
|
106
|
+
#define ECRYPT_DEFAULT_SWAP
|
107
|
+
|
108
|
+
#define SWAP16(v) \
|
109
|
+
ROTL16(v, 8)
|
110
|
+
|
111
|
+
#define SWAP32(v) \
|
112
|
+
((ROTL32(v, 8) & U32C(0x00FF00FF)) | \
|
113
|
+
(ROTL32(v, 24) & U32C(0xFF00FF00)))
|
114
|
+
|
115
|
+
#ifdef ECRYPT_NATIVE64
|
116
|
+
#define SWAP64(v) \
|
117
|
+
((ROTL64(v, 8) & U64C(0x000000FF000000FF)) | \
|
118
|
+
(ROTL64(v, 24) & U64C(0x0000FF000000FF00)) | \
|
119
|
+
(ROTL64(v, 40) & U64C(0x00FF000000FF0000)) | \
|
120
|
+
(ROTL64(v, 56) & U64C(0xFF000000FF000000)))
|
121
|
+
#else
|
122
|
+
#define SWAP64(v) \
|
123
|
+
(((u64)SWAP32(U32V(v)) << 32) | (u64)SWAP32(U32V(v >> 32)))
|
124
|
+
#endif
|
125
|
+
|
126
|
+
#include "ecrypt-machine.h"
|
127
|
+
|
128
|
+
#define ECRYPT_DEFAULT_WTOW
|
129
|
+
|
130
|
+
#ifdef ECRYPT_LITTLE_ENDIAN
|
131
|
+
#define U16TO16_LITTLE(v) (v)
|
132
|
+
#define U32TO32_LITTLE(v) (v)
|
133
|
+
#define U64TO64_LITTLE(v) (v)
|
134
|
+
|
135
|
+
#define U16TO16_BIG(v) SWAP16(v)
|
136
|
+
#define U32TO32_BIG(v) SWAP32(v)
|
137
|
+
#define U64TO64_BIG(v) SWAP64(v)
|
138
|
+
#endif
|
139
|
+
|
140
|
+
#ifdef ECRYPT_BIG_ENDIAN
|
141
|
+
#define U16TO16_LITTLE(v) SWAP16(v)
|
142
|
+
#define U32TO32_LITTLE(v) SWAP32(v)
|
143
|
+
#define U64TO64_LITTLE(v) SWAP64(v)
|
144
|
+
|
145
|
+
#define U16TO16_BIG(v) (v)
|
146
|
+
#define U32TO32_BIG(v) (v)
|
147
|
+
#define U64TO64_BIG(v) (v)
|
148
|
+
#endif
|
149
|
+
|
150
|
+
#include "ecrypt-machine.h"
|
151
|
+
|
152
|
+
/*
|
153
|
+
* The following macros load words from an array of bytes with
|
154
|
+
* different types of endianness, and vice versa.
|
155
|
+
*/
|
156
|
+
|
157
|
+
#define ECRYPT_DEFAULT_BTOW
|
158
|
+
|
159
|
+
#if (!defined(ECRYPT_UNKNOWN) && defined(ECRYPT_I8T_IS_BYTE))
|
160
|
+
|
161
|
+
#define U8TO16_LITTLE(p) U16TO16_LITTLE(((u16*)(p))[0])
|
162
|
+
#define U8TO32_LITTLE(p) U32TO32_LITTLE(((u32*)(p))[0])
|
163
|
+
#define U8TO64_LITTLE(p) U64TO64_LITTLE(((u64*)(p))[0])
|
164
|
+
|
165
|
+
#define U8TO16_BIG(p) U16TO16_BIG(((u16*)(p))[0])
|
166
|
+
#define U8TO32_BIG(p) U32TO32_BIG(((u32*)(p))[0])
|
167
|
+
#define U8TO64_BIG(p) U64TO64_BIG(((u64*)(p))[0])
|
168
|
+
|
169
|
+
#define U16TO8_LITTLE(p, v) (((u16*)(p))[0] = U16TO16_LITTLE(v))
|
170
|
+
#define U32TO8_LITTLE(p, v) (((u32*)(p))[0] = U32TO32_LITTLE(v))
|
171
|
+
#define U64TO8_LITTLE(p, v) (((u64*)(p))[0] = U64TO64_LITTLE(v))
|
172
|
+
|
173
|
+
#define U16TO8_BIG(p, v) (((u16*)(p))[0] = U16TO16_BIG(v))
|
174
|
+
#define U32TO8_BIG(p, v) (((u32*)(p))[0] = U32TO32_BIG(v))
|
175
|
+
#define U64TO8_BIG(p, v) (((u64*)(p))[0] = U64TO64_BIG(v))
|
176
|
+
|
177
|
+
#else
|
178
|
+
|
179
|
+
#define U8TO16_LITTLE(p) \
|
180
|
+
(((u16)((p)[0]) ) | \
|
181
|
+
((u16)((p)[1]) << 8))
|
182
|
+
|
183
|
+
#define U8TO32_LITTLE(p) \
|
184
|
+
(((u32)((p)[0]) ) | \
|
185
|
+
((u32)((p)[1]) << 8) | \
|
186
|
+
((u32)((p)[2]) << 16) | \
|
187
|
+
((u32)((p)[3]) << 24))
|
188
|
+
|
189
|
+
#ifdef ECRYPT_NATIVE64
|
190
|
+
#define U8TO64_LITTLE(p) \
|
191
|
+
(((u64)((p)[0]) ) | \
|
192
|
+
((u64)((p)[1]) << 8) | \
|
193
|
+
((u64)((p)[2]) << 16) | \
|
194
|
+
((u64)((p)[3]) << 24) | \
|
195
|
+
((u64)((p)[4]) << 32) | \
|
196
|
+
((u64)((p)[5]) << 40) | \
|
197
|
+
((u64)((p)[6]) << 48) | \
|
198
|
+
((u64)((p)[7]) << 56))
|
199
|
+
#else
|
200
|
+
#define U8TO64_LITTLE(p) \
|
201
|
+
((u64)U8TO32_LITTLE(p) | ((u64)U8TO32_LITTLE((p) + 4) << 32))
|
202
|
+
#endif
|
203
|
+
|
204
|
+
#define U8TO16_BIG(p) \
|
205
|
+
(((u16)((p)[0]) << 8) | \
|
206
|
+
((u16)((p)[1]) ))
|
207
|
+
|
208
|
+
#define U8TO32_BIG(p) \
|
209
|
+
(((u32)((p)[0]) << 24) | \
|
210
|
+
((u32)((p)[1]) << 16) | \
|
211
|
+
((u32)((p)[2]) << 8) | \
|
212
|
+
((u32)((p)[3]) ))
|
213
|
+
|
214
|
+
#ifdef ECRYPT_NATIVE64
|
215
|
+
#define U8TO64_BIG(p) \
|
216
|
+
(((u64)((p)[0]) << 56) | \
|
217
|
+
((u64)((p)[1]) << 48) | \
|
218
|
+
((u64)((p)[2]) << 40) | \
|
219
|
+
((u64)((p)[3]) << 32) | \
|
220
|
+
((u64)((p)[4]) << 24) | \
|
221
|
+
((u64)((p)[5]) << 16) | \
|
222
|
+
((u64)((p)[6]) << 8) | \
|
223
|
+
((u64)((p)[7]) ))
|
224
|
+
#else
|
225
|
+
#define U8TO64_BIG(p) \
|
226
|
+
(((u64)U8TO32_BIG(p) << 32) | (u64)U8TO32_BIG((p) + 4))
|
227
|
+
#endif
|
228
|
+
|
229
|
+
#define U16TO8_LITTLE(p, v) \
|
230
|
+
do { \
|
231
|
+
(p)[0] = U8V((v) ); \
|
232
|
+
(p)[1] = U8V((v) >> 8); \
|
233
|
+
} while (0)
|
234
|
+
|
235
|
+
#define U32TO8_LITTLE(p, v) \
|
236
|
+
do { \
|
237
|
+
(p)[0] = U8V((v) ); \
|
238
|
+
(p)[1] = U8V((v) >> 8); \
|
239
|
+
(p)[2] = U8V((v) >> 16); \
|
240
|
+
(p)[3] = U8V((v) >> 24); \
|
241
|
+
} while (0)
|
242
|
+
|
243
|
+
#ifdef ECRYPT_NATIVE64
|
244
|
+
#define U64TO8_LITTLE(p, v) \
|
245
|
+
do { \
|
246
|
+
(p)[0] = U8V((v) ); \
|
247
|
+
(p)[1] = U8V((v) >> 8); \
|
248
|
+
(p)[2] = U8V((v) >> 16); \
|
249
|
+
(p)[3] = U8V((v) >> 24); \
|
250
|
+
(p)[4] = U8V((v) >> 32); \
|
251
|
+
(p)[5] = U8V((v) >> 40); \
|
252
|
+
(p)[6] = U8V((v) >> 48); \
|
253
|
+
(p)[7] = U8V((v) >> 56); \
|
254
|
+
} while (0)
|
255
|
+
#else
|
256
|
+
#define U64TO8_LITTLE(p, v) \
|
257
|
+
do { \
|
258
|
+
U32TO8_LITTLE((p), U32V((v) )); \
|
259
|
+
U32TO8_LITTLE((p) + 4, U32V((v) >> 32)); \
|
260
|
+
} while (0)
|
261
|
+
#endif
|
262
|
+
|
263
|
+
#define U16TO8_BIG(p, v) \
|
264
|
+
do { \
|
265
|
+
(p)[0] = U8V((v) ); \
|
266
|
+
(p)[1] = U8V((v) >> 8); \
|
267
|
+
} while (0)
|
268
|
+
|
269
|
+
#define U32TO8_BIG(p, v) \
|
270
|
+
do { \
|
271
|
+
(p)[0] = U8V((v) >> 24); \
|
272
|
+
(p)[1] = U8V((v) >> 16); \
|
273
|
+
(p)[2] = U8V((v) >> 8); \
|
274
|
+
(p)[3] = U8V((v) ); \
|
275
|
+
} while (0)
|
276
|
+
|
277
|
+
#ifdef ECRYPT_NATIVE64
|
278
|
+
#define U64TO8_BIG(p, v) \
|
279
|
+
do { \
|
280
|
+
(p)[0] = U8V((v) >> 56); \
|
281
|
+
(p)[1] = U8V((v) >> 48); \
|
282
|
+
(p)[2] = U8V((v) >> 40); \
|
283
|
+
(p)[3] = U8V((v) >> 32); \
|
284
|
+
(p)[4] = U8V((v) >> 24); \
|
285
|
+
(p)[5] = U8V((v) >> 16); \
|
286
|
+
(p)[6] = U8V((v) >> 8); \
|
287
|
+
(p)[7] = U8V((v) ); \
|
288
|
+
} while (0)
|
289
|
+
#else
|
290
|
+
#define U64TO8_BIG(p, v) \
|
291
|
+
do { \
|
292
|
+
U32TO8_BIG((p), U32V((v) >> 32)); \
|
293
|
+
U32TO8_BIG((p) + 4, U32V((v) )); \
|
294
|
+
} while (0)
|
295
|
+
#endif
|
296
|
+
|
297
|
+
#endif
|
298
|
+
|
299
|
+
#include "ecrypt-machine.h"
|
300
|
+
|
301
|
+
/* ------------------------------------------------------------------------- */
|
302
|
+
|
303
|
+
#endif
|