Skip to content

Commit 5cfb09e

Browse files
committed
initial commit
0 parents  commit 5cfb09e

15 files changed

+1597
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.o
2+
*.bundle
3+
*.so
4+
ext/salsa20/Makefile
5+
doc
6+
pkg
7+
*.class
8+
tmp/

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0.1.0 Aug 04 2010
2+
3+
- Initial release

LICENSE

+25
Original file line numberDiff line numberDiff line change
@@ -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.

README.rdoc

+52
Original file line numberDiff line numberDiff line change
@@ -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 ([email protected])
52+
Project homepage :: https://github.com/dubek/salsa20-ruby

Rakefile

+21
Original file line numberDiff line numberDiff line change
@@ -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

ext/salsa20_ext/ecrypt-config.h

+272
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)