This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
XSalsa20Poly1305.scala
184 lines (153 loc) · 6.65 KB
/
XSalsa20Poly1305.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package tsec.cipher.symmetric.bouncy
import java.security.MessageDigest
import cats.effect.Sync
import org.bouncycastle.crypto.engines.XSalsa20Engine
import org.bouncycastle.crypto.macs.Poly1305
import org.bouncycastle.crypto.params.{KeyParameter, ParametersWithIV}
import tsec.Bouncy
import tsec.cipher._
import tsec.cipher.symmetric.{IvGen, _}
import tsec.common.ManagedRandom
import tsec.keygen.symmetric.SymmetricKeyGen
/** https://tools.ietf.org/html/rfc7539#section-2.5
* impl derived from:
* https://cr.yp.to/snuffle/xsalsa-20081128.pdf
* https://github.com/jedisct1/libsodium/blob/master/src/libsodium/crypto_secretbox/xsalsa20poly1305/secretbox_xsalsa20poly1305.c
*
*/
trait XSalsa20Poly1305
object XSalsa20Poly1305 extends AuthCipherAPI[XSalsa20Poly1305, BouncySecretKey] {
private val KeyLen = 32
private val tagLen = 16
private val NonceLenBytes = 24
implicit def defaultKeyGen[F[_]](implicit F: Sync[F]): SymmetricKeyGen[F, XSalsa20Poly1305, BouncySecretKey] =
new SymmetricKeyGen[F, XSalsa20Poly1305, BouncySecretKey] with ManagedRandom {
def generateKey: F[BouncySecretKey[XSalsa20Poly1305]] = F.delay {
val kBytes = new Array[Byte](KeyLen) //same as key len, 32 bytes
nextBytes(kBytes)
BouncySecretKey(kBytes)
}
def build(rawKey: Array[Byte]): F[BouncySecretKey[XSalsa20Poly1305]] =
if (rawKey.length != KeyLen)
F.raiseError(CipherKeyBuildError("Invalid key length"))
else
F.pure(BouncySecretKey(rawKey))
}
def defaultIvGen[F[_]](implicit F: Sync[F]): IvGen[F, XSalsa20Poly1305] =
new IvGen[F, XSalsa20Poly1305] with ManagedRandom {
def genIv: F[Iv[XSalsa20Poly1305]] =
F.delay(genIvUnsafe)
def genIvUnsafe: Iv[XSalsa20Poly1305] = {
val nonce = new Array[Byte](NonceLenBytes)
nextBytes(nonce)
Iv[XSalsa20Poly1305](nonce)
}
}
implicit def authEncryptor[F[_]](
implicit F: Sync[F]
): AuthEncryptor[F, XSalsa20Poly1305, BouncySecretKey] =
new AuthEncryptor[F, XSalsa20Poly1305, BouncySecretKey] {
def encryptDetached(
plainText: PlainText,
key: BouncySecretKey[XSalsa20Poly1305],
iv: Iv[XSalsa20Poly1305]
): F[(CipherText[XSalsa20Poly1305], AuthTag[XSalsa20Poly1305])] =
F.delay(impl.unsafeEncryptDetached(plainText, key, iv))
def decryptDetached(
cipherText: CipherText[XSalsa20Poly1305],
key: BouncySecretKey[XSalsa20Poly1305],
authTag: AuthTag[XSalsa20Poly1305]
): F[PlainText] =
F.delay(impl.unsafeDecryptDetached(cipherText, authTag, key))
def encrypt(
plainText: PlainText,
key: BouncySecretKey[XSalsa20Poly1305],
iv: Iv[XSalsa20Poly1305]
): F[CipherText[XSalsa20Poly1305]] =
F.delay(impl.unsafeEncrypt(plainText, key, iv))
def decrypt(cipherText: CipherText[XSalsa20Poly1305], key: BouncySecretKey[XSalsa20Poly1305]): F[PlainText] =
F.delay(impl.unsafeDecrypt(cipherText, key))
}
object impl {
def unsafeEncrypt(
plainText: PlainText,
k: BouncySecretKey[XSalsa20Poly1305],
iv: Iv[XSalsa20Poly1305]
): CipherText[XSalsa20Poly1305] = {
val xsalsa20 = new XSalsa20Engine()
val poly1305 = new Poly1305()
val MacKey = new Array[Byte](KeyLen)
val out = RawCipherText[XSalsa20Poly1305](new Array[Byte](plainText.length + tagLen))
xsalsa20.init(true, new ParametersWithIV(new KeyParameter(k), iv))
xsalsa20.processBytes(MacKey, 0, KeyLen, MacKey, 0)
xsalsa20.processBytes(plainText, 0, plainText.length, out, tagLen)
poly1305.init(new KeyParameter(MacKey))
poly1305.update(out, tagLen, plainText.length)
poly1305.doFinal(out, 0)
CipherText(out, iv)
}
def unsafeDecrypt(
ct: CipherText[XSalsa20Poly1305],
k: BouncySecretKey[XSalsa20Poly1305]
): PlainText = {
if (ct.content.length < tagLen + 1)
throw CipherTextError("Ciphertext is 0 or less bytes")
val xsalsa20 = new XSalsa20Engine()
val poly1305 = new Poly1305()
val subkey = new Array[Byte](KeyLen)
val out = PlainText(new Array[Byte](ct.content.length - tagLen))
val inTag = new Array[Byte](tagLen)
System.arraycopy(ct.content, 0, inTag, 0, tagLen)
val computedTag = new Array[Byte](tagLen)
xsalsa20.init(false, new ParametersWithIV(new KeyParameter(k), ct.nonce))
xsalsa20.processBytes(subkey, 0, KeyLen, subkey, 0)
xsalsa20.processBytes(ct.content, tagLen, ct.content.length - tagLen, out, 0)
poly1305.init(new KeyParameter(subkey))
poly1305.update(ct.content, tagLen, ct.content.length - tagLen)
poly1305.doFinal(computedTag, 0)
if (!MessageDigest.isEqual(computedTag, inTag))
throw AuthTagError("Tags do not match")
PlainText(out)
}
def unsafeEncryptDetached(
plainText: PlainText,
k: BouncySecretKey[XSalsa20Poly1305],
iv: Iv[XSalsa20Poly1305]
): (CipherText[XSalsa20Poly1305], AuthTag[XSalsa20Poly1305]) = {
val xsalsa20 = new XSalsa20Engine()
val poly1305 = new Poly1305()
val subkey = new Array[Byte](KeyLen)
val out = RawCipherText[XSalsa20Poly1305](new Array[Byte](plainText.length))
val tag = AuthTag[XSalsa20Poly1305](new Array[Byte](tagLen))
xsalsa20.init(true, new ParametersWithIV(new KeyParameter(k), iv))
xsalsa20.processBytes(subkey, 0, KeyLen, subkey, 0)
xsalsa20.processBytes(plainText, 0, plainText.length, out, 0)
poly1305.init(new KeyParameter(subkey))
poly1305.update(out, 0, plainText.length)
poly1305.doFinal(tag, 0)
(CipherText(out, iv), tag)
}
def unsafeDecryptDetached(
ct: CipherText[XSalsa20Poly1305],
authTag: AuthTag[XSalsa20Poly1305],
k: BouncySecretKey[XSalsa20Poly1305]
): PlainText = {
if (ct.content.length < 1)
throw CipherTextError("Ciphertext is 0 or less bytes")
val xsalsa20 = new XSalsa20Engine()
val poly1305 = new Poly1305()
val subkey = new Array[Byte](KeyLen)
val out = PlainText(new Array[Byte](ct.content.length))
val computedTag = new Array[Byte](tagLen)
xsalsa20.init(false, new ParametersWithIV(new KeyParameter(k), ct.nonce))
xsalsa20.processBytes(subkey, 0, KeyLen, subkey, 0)
xsalsa20.processBytes(ct.content, 0, ct.content.length, out, 0)
poly1305.init(new KeyParameter(subkey))
poly1305.update(ct.content, 0, ct.content.length)
poly1305.doFinal(computedTag, 0)
if (!MessageDigest.isEqual(computedTag, authTag))
throw AuthTagError("Tags do not match")
PlainText(out)
}
}
}