Skip to content

Commit d81d1d9

Browse files
authored
Simplified ChaCha20 algorithm implementation
1 parent 8757f24 commit d81d1d9

1 file changed

Lines changed: 33 additions & 96 deletions

File tree

‎Sources/CryptoSwift/ChaCha20.swift‎

Lines changed: 33 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// CryptoSwift
33
//
44
// Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
5+
// Copyright (C) 2019 Roger Miret <roger.miret@gmail.com>
56
// This software is provided 'as-is', without any express or implied warranty.
67
//
78
// In no event will the authors be held liable for any damages arising from the use of this software.
@@ -47,6 +48,28 @@ public final class ChaCha20: BlockCipher {
4748
assert(counter.count == 16)
4849
}
4950

51+
fileprivate func rotl(_ a: UInt32, _ b: Int) -> UInt32 {
52+
return (a << b) | (a >> (32 - b))
53+
}
54+
55+
fileprivate func qr(_ a: inout UInt32, _ b: inout UInt32, _ c: inout UInt32, _ d: inout UInt32) {
56+
a = a &+ b
57+
d ^= a
58+
d = rotl(d, 16)
59+
60+
c = c &+ d
61+
b ^= c
62+
b = rotl(b, 12)
63+
64+
a = a &+ b
65+
d ^= a
66+
d = rotl(d, 8)
67+
68+
c = c &+ d
69+
b ^= c
70+
b = rotl(b, 7)
71+
}
72+
5073
/// https://tools.ietf.org/html/rfc7539#section-2.3.
5174
fileprivate func core(block: inout Array<UInt8>, counter: Array<UInt8>, key: Array<UInt8>) {
5275
precondition(block.count == ChaCha20.blockSize)
@@ -74,102 +97,16 @@ public final class ChaCha20: BlockCipher {
7497
var (x8, x9, x10, x11, x12, x13, x14, x15) = (j8, j9, j10, j11, j12, j13, j14, j15)
7598

7699
for _ in 0..<10 { // 20 rounds
77-
x0 = x0 &+ x4
78-
x12 ^= x0
79-
x12 = (x12 << 16) | (x12 >> 16)
80-
x8 = x8 &+ x12
81-
x4 ^= x8
82-
x4 = (x4 << 12) | (x4 >> 20)
83-
x0 = x0 &+ x4
84-
x12 ^= x0
85-
x12 = (x12 << 8) | (x12 >> 24)
86-
x8 = x8 &+ x12
87-
x4 ^= x8
88-
x4 = (x4 << 7) | (x4 >> 25)
89-
x1 = x1 &+ x5
90-
x13 ^= x1
91-
x13 = (x13 << 16) | (x13 >> 16)
92-
x9 = x9 &+ x13
93-
x5 ^= x9
94-
x5 = (x5 << 12) | (x5 >> 20)
95-
x1 = x1 &+ x5
96-
x13 ^= x1
97-
x13 = (x13 << 8) | (x13 >> 24)
98-
x9 = x9 &+ x13
99-
x5 ^= x9
100-
x5 = (x5 << 7) | (x5 >> 25)
101-
x2 = x2 &+ x6
102-
x14 ^= x2
103-
x14 = (x14 << 16) | (x14 >> 16)
104-
x10 = x10 &+ x14
105-
x6 ^= x10
106-
x6 = (x6 << 12) | (x6 >> 20)
107-
x2 = x2 &+ x6
108-
x14 ^= x2
109-
x14 = (x14 << 8) | (x14 >> 24)
110-
x10 = x10 &+ x14
111-
x6 ^= x10
112-
x6 = (x6 << 7) | (x6 >> 25)
113-
x3 = x3 &+ x7
114-
x15 ^= x3
115-
x15 = (x15 << 16) | (x15 >> 16)
116-
x11 = x11 &+ x15
117-
x7 ^= x11
118-
x7 = (x7 << 12) | (x7 >> 20)
119-
x3 = x3 &+ x7
120-
x15 ^= x3
121-
x15 = (x15 << 8) | (x15 >> 24)
122-
x11 = x11 &+ x15
123-
x7 ^= x11
124-
x7 = (x7 << 7) | (x7 >> 25)
125-
x0 = x0 &+ x5
126-
x15 ^= x0
127-
x15 = (x15 << 16) | (x15 >> 16)
128-
x10 = x10 &+ x15
129-
x5 ^= x10
130-
x5 = (x5 << 12) | (x5 >> 20)
131-
x0 = x0 &+ x5
132-
x15 ^= x0
133-
x15 = (x15 << 8) | (x15 >> 24)
134-
x10 = x10 &+ x15
135-
x5 ^= x10
136-
x5 = (x5 << 7) | (x5 >> 25)
137-
x1 = x1 &+ x6
138-
x12 ^= x1
139-
x12 = (x12 << 16) | (x12 >> 16)
140-
x11 = x11 &+ x12
141-
x6 ^= x11
142-
x6 = (x6 << 12) | (x6 >> 20)
143-
x1 = x1 &+ x6
144-
x12 ^= x1
145-
x12 = (x12 << 8) | (x12 >> 24)
146-
x11 = x11 &+ x12
147-
x6 ^= x11
148-
x6 = (x6 << 7) | (x6 >> 25)
149-
x2 = x2 &+ x7
150-
x13 ^= x2
151-
x13 = (x13 << 16) | (x13 >> 16)
152-
x8 = x8 &+ x13
153-
x7 ^= x8
154-
x7 = (x7 << 12) | (x7 >> 20)
155-
x2 = x2 &+ x7
156-
x13 ^= x2
157-
x13 = (x13 << 8) | (x13 >> 24)
158-
x8 = x8 &+ x13
159-
x7 ^= x8
160-
x7 = (x7 << 7) | (x7 >> 25)
161-
x3 = x3 &+ x4
162-
x14 ^= x3
163-
x14 = (x14 << 16) | (x14 >> 16)
164-
x9 = x9 &+ x14
165-
x4 ^= x9
166-
x4 = (x4 << 12) | (x4 >> 20)
167-
x3 = x3 &+ x4
168-
x14 ^= x3
169-
x14 = (x14 << 8) | (x14 >> 24)
170-
x9 = x9 &+ x14
171-
x4 ^= x9
172-
x4 = (x4 << 7) | (x4 >> 25)
100+
// Odd round
101+
qr(&x0, &x4, &x8, &x12)
102+
qr(&x1, &x5, &x9, &x13)
103+
qr(&x2, &x6, &x10, &x14)
104+
qr(&x3, &x7, &x11, &x15)
105+
// Even round
106+
qr(&x0, &x5, &x10, &x15)
107+
qr(&x1, &x6, &x11, &x12)
108+
qr(&x2, &x7, &x8, &x13)
109+
qr(&x3, &x4, &x9, &x14)
173110
}
174111

175112
x0 = x0 &+ j0

0 commit comments

Comments
 (0)