lightning-onion 0.2.9 → 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lightning/onion/chacha20.rb +6 -86
- data/lib/lightning/onion/chacha20/openssl.rb +17 -0
- data/lib/lightning/onion/chacha20/pure.rb +103 -0
- data/lib/lightning/onion/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88d9b7022522a5b5b33668e3ae002aec6629053f6bb307a679cf5b1958a5a0bb
|
4
|
+
data.tar.gz: e38b29beacabf8f39a306fe3ffe3b6decae6eec162c0db6bb9ab57153c4fa6dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56127d29ed60e1f0d279972d69e7fdd37c38818d805765c79255114def8b742b9ec0233026467974dea2f6f350639902367412fa21c593916d1df866e42badc8
|
7
|
+
data.tar.gz: d26a1b3c29ded836aa05874def820cb32ad51597d7378d6ed17ac30c4d4be0f51ed2318b10380eda14786235dbf2bee8f95b5b6f3d934de4d48c072c574aa3ae
|
@@ -3,95 +3,15 @@
|
|
3
3
|
module Lightning
|
4
4
|
module Onion
|
5
5
|
module ChaCha20
|
6
|
-
|
7
|
-
|
8
|
-
end
|
6
|
+
autoload :OpenSSL, 'lightning/onion/chacha20/openssl'
|
7
|
+
autoload :Pure, 'lightning/onion/chacha20/pure'
|
9
8
|
|
10
9
|
def self.chacha20_encrypt(key, counter, nonce, plaintext)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
encrypted_message += xor(block, key_stream)
|
16
|
-
end
|
17
|
-
if plaintext.length % 64 != 0
|
18
|
-
i = plaintext.length / 64
|
19
|
-
key_stream = chacha20_block(key, counter + i, nonce)
|
20
|
-
block = plaintext[(i * 64)...plaintext.length]
|
21
|
-
block = block.ljust(64, "\x00")
|
22
|
-
encrypted_message += xor(block, key_stream)[0...(plaintext.length % 64)]
|
23
|
-
end
|
24
|
-
encrypted_message
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.xor(a, b)
|
28
|
-
a = a.unpack('N*')
|
29
|
-
b = b.unpack('N*')
|
30
|
-
a.zip(b).map { |x, y| (x ^ y) & 0xffffffff }.pack('N*')
|
31
|
-
end
|
32
|
-
|
33
|
-
# key: 32 bytes
|
34
|
-
# counter: integer (4 bytes)
|
35
|
-
# nonce: 12 bytes
|
36
|
-
def self.chacha20_block(key, counter, nonce)
|
37
|
-
# reverse order
|
38
|
-
key = key.unpack('V*').pack('N*')
|
39
|
-
counter = [counter].pack('N*')
|
40
|
-
nonce = nonce.unpack('V*').pack('N*')
|
41
|
-
state = constants + key + counter + nonce
|
42
|
-
working_state = state.unpack('N*')
|
43
|
-
10.times do
|
44
|
-
inner_block(working_state)
|
10
|
+
if ::OpenSSL::Cipher.ciphers.include?("ChaCha20")
|
11
|
+
Lightning::Onion::ChaCha20::OpenSSL.chacha20_encrypt(key, counter, nonce, plaintext)
|
12
|
+
else
|
13
|
+
Lightning::Onion::ChaCha20::Pure.chacha20_encrypt(key, counter, nonce, plaintext)
|
45
14
|
end
|
46
|
-
plus_for_string(state, working_state)
|
47
|
-
end
|
48
|
-
|
49
|
-
def self.inner_block(x)
|
50
|
-
# column rounds
|
51
|
-
x[0], x[4], x[8], x[12] = quater_round(x[0], x[4], x[8], x[12])
|
52
|
-
x[1], x[5], x[9], x[13] = quater_round(x[1], x[5], x[9], x[13])
|
53
|
-
x[2], x[6], x[10], x[14] = quater_round(x[2], x[6], x[10], x[14])
|
54
|
-
x[3], x[7], x[11], x[15] = quater_round(x[3], x[7], x[11], x[15])
|
55
|
-
# diagonal rounds
|
56
|
-
x[0], x[5], x[10], x[15] = quater_round(x[0], x[5], x[10], x[15])
|
57
|
-
x[1], x[6], x[11], x[12] = quater_round(x[1], x[6], x[11], x[12])
|
58
|
-
x[2], x[7], x[8], x[13] = quater_round(x[2], x[7], x[8], x[13])
|
59
|
-
x[3], x[4], x[9], x[14] = quater_round(x[3], x[4], x[9], x[14])
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.plus_for_string(a, b)
|
63
|
-
a.unpack('N*').map.with_index do |x, i|
|
64
|
-
plus(x, b[i])
|
65
|
-
end.pack('V*')
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.plus(x, y)
|
69
|
-
(x + y) & 0xffffffff
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.rotate(x, n)
|
73
|
-
y = x << n
|
74
|
-
z = x >> (32 - n)
|
75
|
-
(y | z) & 0xffffffff
|
76
|
-
end
|
77
|
-
|
78
|
-
def self.quater_round(a, b, c, d)
|
79
|
-
a = plus(a, b)
|
80
|
-
d ^= a
|
81
|
-
d = rotate(d, 16)
|
82
|
-
|
83
|
-
c = plus(c, d)
|
84
|
-
b ^= c
|
85
|
-
b = rotate(b, 12)
|
86
|
-
|
87
|
-
a = plus(a, b)
|
88
|
-
d ^= a
|
89
|
-
d = rotate(d, 8)
|
90
|
-
|
91
|
-
c = plus(c, d)
|
92
|
-
b ^= c
|
93
|
-
b = rotate(b, 7)
|
94
|
-
[a, b, c, d]
|
95
15
|
end
|
96
16
|
end
|
97
17
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lightning
|
4
|
+
module Onion
|
5
|
+
module ChaCha20
|
6
|
+
class OpenSSL
|
7
|
+
def self.chacha20_encrypt(key, counter, nonce, plaintext)
|
8
|
+
cipher = ::OpenSSL::Cipher.new("ChaCha20")
|
9
|
+
cipher.encrypt
|
10
|
+
cipher.iv = [counter].pack('V*') + nonce
|
11
|
+
cipher.key = key
|
12
|
+
cipher.update(plaintext) + cipher.final
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lightning
|
4
|
+
module Onion
|
5
|
+
module ChaCha20
|
6
|
+
class Pure
|
7
|
+
|
8
|
+
def self.chacha20_encrypt(key, counter, nonce, plaintext)
|
9
|
+
encrypted_message = +''
|
10
|
+
(plaintext.length / 64).times do |i|
|
11
|
+
key_stream = chacha20_block(key, counter + i, nonce)
|
12
|
+
block = plaintext[(i * 64)...(i + 1) * 64]
|
13
|
+
encrypted_message += xor(block, key_stream)
|
14
|
+
end
|
15
|
+
if plaintext.length % 64 != 0
|
16
|
+
i = plaintext.length / 64
|
17
|
+
key_stream = chacha20_block(key, counter + i, nonce)
|
18
|
+
block = plaintext[(i * 64)...plaintext.length]
|
19
|
+
block = block.ljust(64, "\x00")
|
20
|
+
encrypted_message += xor(block, key_stream)[0...(plaintext.length % 64)]
|
21
|
+
end
|
22
|
+
encrypted_message
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.constants
|
28
|
+
[0x61707865, 0x3320646e, 0x79622d32, 0x6b206574].pack('N*')
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.xor(a, b)
|
32
|
+
a = a.unpack('N*')
|
33
|
+
b = b.unpack('N*')
|
34
|
+
a.zip(b).map { |x, y| (x ^ y) & 0xffffffff }.pack('N*')
|
35
|
+
end
|
36
|
+
|
37
|
+
# key: 32 bytes
|
38
|
+
# counter: integer (4 bytes)
|
39
|
+
# nonce: 12 bytes
|
40
|
+
def self.chacha20_block(key, counter, nonce)
|
41
|
+
# reverse order
|
42
|
+
key = key.unpack('V*').pack('N*')
|
43
|
+
counter = [counter].pack('N*')
|
44
|
+
nonce = nonce.unpack('V*').pack('N*')
|
45
|
+
state = constants + key + counter + nonce
|
46
|
+
working_state = state.unpack('N*')
|
47
|
+
10.times do
|
48
|
+
inner_block(working_state)
|
49
|
+
end
|
50
|
+
plus_for_string(state, working_state)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.inner_block(x)
|
54
|
+
# column rounds
|
55
|
+
x[0], x[4], x[8], x[12] = quater_round(x[0], x[4], x[8], x[12])
|
56
|
+
x[1], x[5], x[9], x[13] = quater_round(x[1], x[5], x[9], x[13])
|
57
|
+
x[2], x[6], x[10], x[14] = quater_round(x[2], x[6], x[10], x[14])
|
58
|
+
x[3], x[7], x[11], x[15] = quater_round(x[3], x[7], x[11], x[15])
|
59
|
+
# diagonal rounds
|
60
|
+
x[0], x[5], x[10], x[15] = quater_round(x[0], x[5], x[10], x[15])
|
61
|
+
x[1], x[6], x[11], x[12] = quater_round(x[1], x[6], x[11], x[12])
|
62
|
+
x[2], x[7], x[8], x[13] = quater_round(x[2], x[7], x[8], x[13])
|
63
|
+
x[3], x[4], x[9], x[14] = quater_round(x[3], x[4], x[9], x[14])
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.plus_for_string(a, b)
|
67
|
+
a.unpack('N*').map.with_index do |x, i|
|
68
|
+
plus(x, b[i])
|
69
|
+
end.pack('V*')
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.plus(x, y)
|
73
|
+
(x + y) & 0xffffffff
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.rotate(x, n)
|
77
|
+
y = x << n
|
78
|
+
z = x >> (32 - n)
|
79
|
+
(y | z) & 0xffffffff
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.quater_round(a, b, c, d)
|
83
|
+
a = plus(a, b)
|
84
|
+
d ^= a
|
85
|
+
d = rotate(d, 16)
|
86
|
+
|
87
|
+
c = plus(c, d)
|
88
|
+
b ^= c
|
89
|
+
b = rotate(b, 12)
|
90
|
+
|
91
|
+
a = plus(a, b)
|
92
|
+
d ^= a
|
93
|
+
d = rotate(d, 8)
|
94
|
+
|
95
|
+
c = plus(c, d)
|
96
|
+
b ^= c
|
97
|
+
b = rotate(b, 7)
|
98
|
+
[a, b, c, d]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lightning-onion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hajime Yamaguchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: algebrick
|
@@ -115,6 +115,8 @@ files:
|
|
115
115
|
- bin/setup
|
116
116
|
- lib/lightning/onion.rb
|
117
117
|
- lib/lightning/onion/chacha20.rb
|
118
|
+
- lib/lightning/onion/chacha20/openssl.rb
|
119
|
+
- lib/lightning/onion/chacha20/pure.rb
|
118
120
|
- lib/lightning/onion/error_packet.rb
|
119
121
|
- lib/lightning/onion/failure_messages.rb
|
120
122
|
- lib/lightning/onion/failure_messages/amount_below_minimum.rb
|