Skip to content

Commit 24e66b3

Browse files
committed
Move salsa20/8 to C world, this took significant amount of time
1 parent 35b807c commit 24e66b3

File tree

7 files changed

+240
-43
lines changed

7 files changed

+240
-43
lines changed

ext/crypto/CMakeLists.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ IF(APPLE)
6868
ENDIF()
6969
TARGET_LINK_LIBRARIES(sagittarius--secure sagittarius)
7070

71-
# sagittarius--secure
71+
# sagittarius--ec
7272
ADD_LIBRARY(sagittarius--ec MODULE
7373
sagittarius-ec.c
7474
${CMAKE_CURRENT_BINARY_DIR}/ec-fields.c)
@@ -91,6 +91,28 @@ IF(APPLE)
9191
ENDIF()
9292
TARGET_LINK_LIBRARIES(sagittarius--ec sagittarius)
9393

94+
# sagittarius--salsa
95+
ADD_LIBRARY(sagittarius--salsa MODULE
96+
sagittarius-salsa.c
97+
${CMAKE_CURRENT_BINARY_DIR}/salsa.c)
98+
COPY_TARGET(sagittarius--salsa ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} TRUE)
99+
IF (USE_CPP_FOR_BROKEN_LINKER)
100+
# ugly solution
101+
SET_SOURCE_FILES_PROPERTIES(
102+
sagittarius-salsa.c
103+
${CMAKE_CURRENT_BINARY_DIR}/salsa.c
104+
PROPERTIES LANGUAGE CXX)
105+
ENDIF()
106+
ADD_STUBS(sagittarius--salsa
107+
COMMAND ${GENSTUB}
108+
FILES salsa.stub
109+
OUTTREE)
110+
SET_TARGET_PROPERTIES(sagittarius--salsa PROPERTIES PREFIX "")
111+
IF(APPLE)
112+
SET_TARGET_PROPERTIES(sagittarius--salsa PROPERTIES SUFFIX ".dylib")
113+
ENDIF()
114+
TARGET_LINK_LIBRARIES(sagittarius--salsa sagittarius)
115+
94116

95117
INSTALL(TARGETS sagittarius--tomcrypt
96118
DESTINATION ${SAGITTARIUS_DYNLIB_PATH})

ext/crypto/sagittarius-salsa.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/* sagittarius-salsa.c -*- mode: c; coding: utf-8; -*-
2+
*
3+
* Copyright (c) 2024 Takashi Kato <ktakashi@ymail.com>
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+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22+
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include <sagittarius.h>
30+
#define LIBSAGITTARIUS_EXT_BODY
31+
#include <sagittarius/extend.h>
32+
#include "sagittarius-salsa.h"
33+
34+
#ifdef noreturn
35+
# define noreturn_save noreturn
36+
# undef noreturn
37+
#endif
38+
#include <tomcrypt.h>
39+
#ifdef noreturn_save
40+
# define noreturn noreturn_save
41+
#endif
42+
43+
#define QUARTERROUND(a,b,c,d) \
44+
do { \
45+
x[b] ^= (ROL((x[a] + x[d]), 7)); \
46+
x[c] ^= (ROL((x[b] + x[a]), 9)); \
47+
x[d] ^= (ROL((x[c] + x[b]), 13)); \
48+
x[a] ^= (ROL((x[d] + x[c]), 18)); \
49+
} while (0)
50+
51+
static void salsa_core(uint8_t *out, uint8_t *in, int rounds)
52+
{
53+
uint32_t x[16];
54+
int i;
55+
56+
memcpy(x, in, sizeof(x));
57+
58+
for (i = 0; i < rounds; i += 2) {
59+
QUARTERROUND( 0, 4, 8,12);
60+
QUARTERROUND( 5, 9,13, 1);
61+
QUARTERROUND(10,14, 2, 6);
62+
QUARTERROUND(15, 3, 7,11);
63+
QUARTERROUND( 0, 1, 2, 3);
64+
QUARTERROUND( 5, 6, 7, 4);
65+
QUARTERROUND(10,11, 8, 9);
66+
QUARTERROUND(15,12,13,14);
67+
}
68+
69+
for (i = 0; i < 16; i++) {
70+
x[i] += ((uint32_t *)in)[i];
71+
STORE32L(x[i], out + 4 * i);
72+
}
73+
}
74+
75+
76+
SgObject Sg_SalsaCore(SgObject in, int rounds)
77+
{
78+
SgObject bv = Sg_ByteVectorCopy(SG_BVECTOR(in), 0, SG_BVECTOR_SIZE(in));
79+
return Sg_SalsaCoreX(bv, rounds);
80+
81+
}
82+
83+
SgObject Sg_SalsaCoreX(SgObject in, int rounds)
84+
{
85+
long i;
86+
if (SG_BVECTOR_SIZE(in) % 64 != 0) {
87+
Sg_AssertionViolation(SG_INTERN("salsa-core!"),
88+
SG_MAKE_STRING("input of salsa-core! must be multiple of 64"),
89+
in);
90+
}
91+
for (i = 0; i < SG_BVECTOR_SIZE(in); i += 64) {
92+
salsa_core(SG_BVECTOR_ELEMENTS(in) + i, SG_BVECTOR_ELEMENTS(in) + i, rounds);
93+
}
94+
return in;
95+
}
96+
97+
extern void Sg__Init_salsa(SgLibrary *lib);
98+
99+
SG_EXTENSION_ENTRY void CDECL Sg_Init_sagittarius__salsa()
100+
{
101+
SgLibrary *lib;
102+
SG_INIT_EXTENSION(sagittarius__salsa);
103+
lib = SG_LIBRARY(Sg_FindLibrary(SG_INTERN("(sagittarius crypto logic salsa)"),
104+
FALSE));
105+
Sg__Init_salsa(lib);
106+
}

ext/crypto/sagittarius-salsa.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* sagittarius-salsa.h -*- mode: c; coding: utf-8; -*-
2+
*
3+
* Copyright (c) 2024 Takashi Kato <ktakashi@ymail.com>
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+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22+
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#ifndef SAGITTARIUS_SALSA_H_
30+
#define SAGITTARIUS_SALSA_H_
31+
32+
#include <sagittarius.h>
33+
34+
SgObject Sg_SalsaCore(SgObject in, int rounds);
35+
SgObject Sg_SalsaCoreX(SgObject in, int rounds);
36+
37+
#endif /* SAGITTARIUS_SALSA_H_ */

ext/crypto/sagittarius/crypto/kdfs/scrypt.scm

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
(sagittarius crypto digests)
4141
(sagittarius crypto mac)
4242
(sagittarius crypto kdfs pbkdf-2)
43+
(sagittarius crypto logic salsa)
4344
(util bytevector)
4445
(srfi :1 lists))
4546

@@ -96,46 +97,5 @@
9697
(loop (+ i 1) X (cons X Y))))))
9798

9899
;; B = 64 octets
99-
(define (salsa20/8 B)
100-
(define (u32 bv i) (bytevector-u32-ref bv (* i 4) (endianness little)))
101-
(define (u32! bv i v) (bytevector-u32-set! bv (* i 4) v (endianness little)))
102-
(define x (bytevector-copy B))
103-
;; (((a) << (b)) | ((a) >> (32 - (b))))
104-
(define (R a b)
105-
(bitwise-and
106-
(bitwise-ior
107-
(bitwise-arithmetic-shift-left a b)
108-
(bitwise-arithmetic-shift-right (bitwise-and a #xFFFFFFFF) (- 32 b)))
109-
#xFFFFFFFF))
110-
(define (^= bv i v) (u32! bv i (bitwise-xor (u32 bv i) v)))
111-
(define r u32)
112-
(do ((i 0 (+ i 2)))
113-
((= i 8)
114-
(do ((i 0 (+ i 1)))
115-
((= i 16) B)
116-
(u32! B i (bitwise-and (+ (u32 B i) (u32 x i)) #xFFFFFFFF))))
117-
(^= x 4 (R (+ (r x 0) (r x 12)) 7)) (^= x 8 (R (+ (r x 4) (r x 0)) 9))
118-
(^= x 12 (R (+ (r x 8) (r x 4)) 13)) (^= x 0 (R (+ (r x 12) (r x 8)) 18))
119-
120-
(^= x 9 (R (+ (r x 5) (r x 1)) 7)) (^= x 13 (R (+ (r x 9) (r x 5)) 9))
121-
(^= x 1 (R (+ (r x 13) (r x 9)) 13)) (^= x 5 (R (+ (r x 1) (r x 13)) 18))
122-
123-
(^= x 14 (R (+ (r x 10) (r x 6)) 7)) (^= x 2 (R (+ (r x 14) (r x 10)) 9))
124-
(^= x 6 (R (+ (r x 2) (r x 14)) 13)) (^= x 10 (R (+ (r x 6) (r x 2)) 18))
125-
126-
(^= x 3 (R (+ (r x 15) (r x 11)) 7)) (^= x 7 (R (+ (r x 3) (r x 15)) 9))
127-
(^= x 11 (R (+ (r x 7) (r x 3)) 13)) (^= x 15 (R (+ (r x 11) (r x 7)) 18))
128-
129-
(^= x 1 (R (+ (r x 0) (r x 3)) 7)) (^= x 2 (R (+ (r x 1) (r x 0)) 9))
130-
(^= x 3 (R (+ (r x 2) (r x 1)) 13)) (^= x 0 (R (+ (r x 3) (r x 2)) 18))
131-
132-
(^= x 6 (R (+ (r x 5) (r x 4)) 7)) (^= x 7 (R (+ (r x 6) (r x 5)) 9))
133-
(^= x 4 (R (+ (r x 7) (r x 6)) 13)) (^= x 5 (R (+ (r x 4) (r x 7)) 18))
134-
135-
(^= x 11 (R (+ (r x 10) (r x 9)) 7)) (^= x 8 (R (+ (r x 11) (r x 10)) 9))
136-
(^= x 9 (R (+ (r x 8) (r x 11)) 13)) (^= x 10 (R (+ (r x 9) (r x 8)) 18))
137-
138-
(^= x 12 (R (+ (r x 15) (r x 14)) 7)) (^= x 13 (R (+ (r x 12) (r x 15)) 9))
139-
(^= x 14 (R (+ (r x 13) (r x 12)) 13)) (^= x 15 (R (+ (r x 14) (r x 13)) 18))
140-
))
100+
(define (salsa20/8 B) (salsa-core! B 8))
141101
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
;;; -*- mode:scheme; coding:utf-8; -*-
2+
;;;
3+
;;; sagittarius/crypto/logic/salsa.scm - Salsa 20 core
4+
;;;
5+
;;; Copyright (c) 2024 Takashi Kato <ktakashi@ymail.com>
6+
;;;
7+
;;; Redistribution and use in source and binary forms, with or without
8+
;;; modification, are permitted provided that the following conditions
9+
;;; are met:
10+
;;;
11+
;;; 1. Redistributions of source code must retain the above copyright
12+
;;; notice, this list of conditions and the following disclaimer.
13+
;;;
14+
;;; 2. Redistributions in binary form must reproduce the above copyright
15+
;;; notice, this list of conditions and the following disclaimer in the
16+
;;; documentation and/or other materials provided with the distribution.
17+
;;;
18+
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24+
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
;;;
30+
31+
;; Salsa20 core
32+
#!nounbound
33+
(library (sagittarius crypto logic salsa)
34+
(export salsa-core salsa-core!)
35+
(import (sagittarius dynamic-module))
36+
(load-dynamic-module "sagittarius--salsa"))

ext/crypto/salsa.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
;; -*- mode: scheme; coding: utf-8; -*-
2+
3+
(decl-code
4+
(.include <sagittarius/private.h>)
5+
(.define "LIBSAGITTARIUS_EXT_BODY")
6+
(.include <sagittarius/extend.h>
7+
<sagittarius-salsa.h>))
8+
9+
(define-c-proc salsa-core (bv::<bytevector> rounds::<fixnum>) Sg_SalsaCore)
10+
(define-c-proc salsa-core! (bv::<bytevector> rounds::<fixnum>) Sg_SalsaCoreX)

ext/crypto/tests/test-scrypt.scm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,30 @@
3030
"f7ce0b653d2d72a4108cf5abe912ffdd777616dbbb27a70e8204f3ae2d0f6fad89f68f4811d1e87bcc3bd7400a9ffd29094f0184639574f39ae5a1315217bcd7894991447213bb226c25b54da86370fbcd984380374666bb8ffcb5bf40c254b067d27c51ce4ad5fed829c90b505a571b7f4d1cad6a523cda770e67bceaaf7e89"
3131
16 1)
3232

33+
(define (test-scrypt P S N r p dk-len expected)
34+
(if (string=? "" P)
35+
(test-error (list "Empty password not allowed" N r p)
36+
(hex-string->bytevector expected)
37+
(scrypt (string->utf8 P)
38+
(string->utf8 S)
39+
N r p dk-len))
40+
(test-equal (list P S N r p)
41+
(hex-string->bytevector expected)
42+
(scrypt (string->utf8 P)
43+
(string->utf8 S)
44+
N r p dk-len))))
45+
46+
(test-scrypt "" "" 16 1 1 64
47+
"77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906")
48+
49+
(test-scrypt "password" "NaCl" 1024 8 16 64
50+
"fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640")
51+
52+
(test-scrypt "pleaseletmein" "SodiumChloride" 16384 8 1 64
53+
"7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887")
54+
55+
(unless (getenv "CI")
56+
(test-scrypt "pleaseletmein" "SodiumChloride" 1048576 8 1 64
57+
"2101cb9b6a511aaeaddbbe09cf70f881ec568d574a2ffd4dabe5ee9820adaa478e56fd8f4ba5d09ffa1c6d927c40f4c337304049e8a952fbcbf45c6fa77a41a4"))
58+
3359
(test-end)

0 commit comments

Comments
 (0)