Skip to content

Commit 62dd393

Browse files
authored
Use bouncycastle instead of spongycastle (#1772)
* Use bouncycastle instead of spongycastle * Reformat a few files * Remove wireshark dissector support Fixes #1375
1 parent e14c40d commit 62dd393

File tree

6 files changed

+113
-155
lines changed

6 files changed

+113
-155
lines changed

eclair-core/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@
236236
<artifactId>HikariCP</artifactId>
237237
<version>3.4.2</version>
238238
</dependency>
239+
<dependency>
240+
<groupId>org.bouncycastle</groupId>
241+
<artifactId>bcprov-jdk15on</artifactId>
242+
<version>1.68</version>
243+
</dependency>
239244
<dependency>
240245
<!-- This is to get rid of '[WARNING] warning: Class javax.annotation.Nonnull not found - continuing with a stub.' compile errors -->
241246
<groupId>com.google.code.findbugs</groupId>

eclair-core/src/main/scala/fr/acinq/eclair/crypto/ChaCha20Poly1305.scala

+35-55
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,28 @@
1616

1717
package fr.acinq.eclair.crypto
1818

19-
import java.nio.ByteOrder
20-
2119
import fr.acinq.bitcoin.{ByteVector32, Protocol}
2220
import fr.acinq.eclair.crypto.ChaCha20Poly1305.{DecryptionError, EncryptionError, InvalidCounter}
23-
import grizzled.slf4j.Logger
2421
import grizzled.slf4j.Logging
25-
import org.spongycastle.crypto.engines.ChaCha7539Engine
26-
import org.spongycastle.crypto.params.{KeyParameter, ParametersWithIV}
22+
import org.bouncycastle.crypto.engines.ChaCha7539Engine
23+
import org.bouncycastle.crypto.params.{KeyParameter, ParametersWithIV}
2724
import scodec.bits.ByteVector
2825

26+
import java.nio.ByteOrder
27+
2928
/**
30-
* Poly1305 authenticator
31-
* see https://tools.ietf.org/html/rfc7539#section-2.5
32-
*/
29+
* Poly1305 authenticator
30+
* see https://tools.ietf.org/html/rfc7539#section-2.5
31+
*/
3332
object Poly1305 {
3433
/**
35-
*
36-
* @param key input key
37-
* @param datas input data
38-
* @return a 16 byte authentication tag
39-
*/
34+
* @param key input key
35+
* @param datas input data
36+
* @return a 16 byte authentication tag
37+
*/
4038
def mac(key: ByteVector, datas: ByteVector*): ByteVector = {
4139
val out = new Array[Byte](16)
42-
val poly = new org.spongycastle.crypto.macs.Poly1305()
40+
val poly = new org.bouncycastle.crypto.macs.Poly1305()
4341
poly.init(new KeyParameter(key.toArray))
4442
datas.foreach(data => poly.update(data.toArray, 0, data.length.toInt))
4543
poly.doFinal(out, 0)
@@ -48,13 +46,10 @@ object Poly1305 {
4846
}
4947

5048
/**
51-
* ChaCha20 block cipher
52-
* see https://tools.ietf.org/html/rfc7539#section-2.5
53-
*/
49+
* ChaCha20 block cipher
50+
* see https://tools.ietf.org/html/rfc7539#section-2.5
51+
*/
5452
object ChaCha20 {
55-
// Whenever key rotation happens, we start with a nonce value of 0 and increment it for each message.
56-
val ZeroNonce = ByteVector.fill(12)(0.byteValue)
57-
5853
def encrypt(plaintext: ByteVector, key: ByteVector, nonce: ByteVector, counter: Int = 0): ByteVector = {
5954
val engine = new ChaCha7539Engine()
6055
engine.init(true, new ParametersWithIV(new KeyParameter(key.toArray), nonce.toArray))
@@ -91,65 +86,50 @@ object ChaCha20 {
9186
}
9287

9388
/**
94-
* ChaCha20Poly1305 AEAD (Authenticated Encryption with Additional Data) algorithm
95-
* see https://tools.ietf.org/html/rfc7539#section-2.5
96-
*
97-
* This what we should be using (see BOLT #8)
98-
*/
89+
* ChaCha20Poly1305 AEAD (Authenticated Encryption with Additional Data) algorithm
90+
* see https://tools.ietf.org/html/rfc7539#section-2.5
91+
*
92+
* This what we should be using (see BOLT #8)
93+
*/
9994
object ChaCha20Poly1305 extends Logging {
10095

96+
// @formatter:off
10197
abstract class ChaCha20Poly1305Error(msg: String) extends RuntimeException(msg)
10298
case class InvalidMac() extends ChaCha20Poly1305Error("invalid mac")
10399
case class DecryptionError() extends ChaCha20Poly1305Error("decryption error")
104100
case class EncryptionError() extends ChaCha20Poly1305Error("encryption error")
105101
case class InvalidCounter() extends ChaCha20Poly1305Error("chacha20 counter must be 0 or 1")
106-
107-
// This logger is used to dump encryption keys to enable traffic analysis by the lightning-dissector.
108-
// See https://github.com/nayutaco/lightning-dissector for more details.
109-
// It is disabled by default (in the logback.xml configuration file).
110-
val keyLogger = Logger("keylog")
102+
// @formatter:on
111103

112104
/**
113-
*
114-
* @param key 32 bytes encryption key
115-
* @param nonce 12 bytes nonce
116-
* @param plaintext plain text
117-
* @param aad additional authentication data. can be empty
118-
* @return a (ciphertext, mac) tuple
119-
*/
105+
* @param key 32 bytes encryption key
106+
* @param nonce 12 bytes nonce
107+
* @param plaintext plain text
108+
* @param aad additional authentication data. can be empty
109+
* @return a (ciphertext, mac) tuple
110+
*/
120111
def encrypt(key: ByteVector, nonce: ByteVector, plaintext: ByteVector, aad: ByteVector): (ByteVector, ByteVector) = {
121112
val polykey = ChaCha20.encrypt(ByteVector32.Zeroes, key, nonce)
122113
val ciphertext = ChaCha20.encrypt(plaintext, key, nonce, 1)
123114
val tag = Poly1305.mac(polykey, aad, pad16(aad), ciphertext, pad16(ciphertext), Protocol.writeUInt64(aad.length, ByteOrder.LITTLE_ENDIAN), Protocol.writeUInt64(ciphertext.length, ByteOrder.LITTLE_ENDIAN))
124-
125115
logger.debug(s"encrypt($key, $nonce, $aad, $plaintext) = ($ciphertext, $tag)")
126-
if (nonce === ChaCha20.ZeroNonce) {
127-
keyLogger.debug(s"${tag.toHex} ${key.toHex}")
128-
}
129-
130116
(ciphertext, tag)
131117
}
132118

133119
/**
134-
*
135-
* @param key 32 bytes decryption key
136-
* @param nonce 12 bytes nonce
137-
* @param ciphertext ciphertext
138-
* @param aad additional authentication data. can be empty
139-
* @param mac authentication mac
140-
* @return the decrypted plaintext if the mac is valid.
141-
*/
120+
* @param key 32 bytes decryption key
121+
* @param nonce 12 bytes nonce
122+
* @param ciphertext ciphertext
123+
* @param aad additional authentication data. can be empty
124+
* @param mac authentication mac
125+
* @return the decrypted plaintext if the mac is valid.
126+
*/
142127
def decrypt(key: ByteVector, nonce: ByteVector, ciphertext: ByteVector, aad: ByteVector, mac: ByteVector): ByteVector = {
143128
val polykey = ChaCha20.encrypt(ByteVector32.Zeroes, key, nonce)
144129
val tag = Poly1305.mac(polykey, aad, pad16(aad), ciphertext, pad16(ciphertext), Protocol.writeUInt64(aad.length, ByteOrder.LITTLE_ENDIAN), Protocol.writeUInt64(ciphertext.length, ByteOrder.LITTLE_ENDIAN))
145130
if (tag != mac) throw InvalidMac()
146131
val plaintext = ChaCha20.decrypt(ciphertext, key, nonce, 1)
147-
148132
logger.debug(s"decrypt($key, $nonce, $aad, $ciphertext, $mac) = $plaintext")
149-
if (nonce === ChaCha20.ZeroNonce) {
150-
keyLogger.debug(s"${mac.toHex} ${key.toHex}")
151-
}
152-
153133
plaintext
154134
}
155135

eclair-core/src/main/scala/fr/acinq/eclair/crypto/Mac.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
package fr.acinq.eclair.crypto
1818

1919
import fr.acinq.bitcoin.ByteVector32
20-
import org.spongycastle.crypto.digests.SHA256Digest
21-
import org.spongycastle.crypto.macs.HMac
22-
import org.spongycastle.crypto.params.KeyParameter
20+
import org.bouncycastle.crypto.digests.SHA256Digest
21+
import org.bouncycastle.crypto.macs.HMac
22+
import org.bouncycastle.crypto.params.KeyParameter
2323
import scodec.bits.ByteVector
2424

2525
/**
26-
* Created by t-bast on 04/07/19.
27-
*/
26+
* Created by t-bast on 04/07/19.
27+
*/
2828

2929
/**
30-
* Create and verify message authentication codes.
31-
*/
30+
* Create and verify message authentication codes.
31+
*/
3232
trait Mac32 {
3333

3434
def mac(message: ByteVector): ByteVector32

0 commit comments

Comments
 (0)