-
Notifications
You must be signed in to change notification settings - Fork 1
/
Herradura cryptographic suite.go
285 lines (255 loc) · 11 KB
/
Herradura cryptographic suite.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Herradura Cryptographic Suite
Copyright (C) 2024 Omar Alejandro Herrera Reyna
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License or the GNU General Public License
as published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
Under the terms of the GNU General Public License, please also consider that:
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
package main
import (
"crypto/rand"
"fmt"
"log"
"github.com/tunabay/go-bitarray"
)
func New_rand_bitarray(bitlength int) *bitarray.BitArray {
buf := make([]byte, bitlength/8)
_, err := rand.Read(buf)
if err != nil {
log.Fatalf("ERROR while generating random string: %s", err)
}
result := bitarray.NewFromBytes(buf, 0, bitlength)
return result
}
func Fscx_revolve(ba *bitarray.BitArray, bb *bitarray.BitArray, steps int, verbose bool) *bitarray.BitArray {
result := ba
for i := 1; i <= steps; i++ {
result = Fscx(result, bb)
if verbose {
fmt.Printf("Step %d: %x\n", i, result)
}
}
return result
}
func Fscx(ba *bitarray.BitArray, bb *bitarray.BitArray) *bitarray.BitArray {
result := ba.Xor(bb)
ba = ba.RotateLeft(1)
bb = bb.RotateLeft(1)
result = result.Xor(ba).Xor(bb) // result ^= A ^ B
ba = ba.RotateLeft(-2)
bb = bb.RotateLeft(-2)
result = result.Xor(ba).Xor(bb) // result ^= A ^ B
return result
}
/*
let, Alice, Bob: i + r == bitlength b; i == 1/4 bitlength; r == 3/4 bitlength; bitlength is a power of 2 >= 8
P be a plaintext message of bitlength b,
E the encrypted version of plaintext P,
D == P the decrypted version of E.
let, Alice: A,B be random values of bitlength b,
Bob: A2,B2 be random values of bitlength b
let, Alice: C = fscx_revolve(A, B, i) ,
Bob: C2 = fscx_revolve(A2, B2, i)
then, Alice: D = fscx_revolve(C2, B, r) ^ A ,
Bob: D2 = fscx_revolve(C, B2, r) ^ A2
where, Alice, Bob: D == D2
then, fscx_revolve(C2, B, r) ^ A == fscx_revolve(C, B2, r) ^ A2,
fscx_revolve(C2, B, r) ^ A ^ P == fscx_revolve(C, B2, r) ^ A2 ^ P,
fscx_revolve(C2, B, R) ^ A ^ A2 ^ P == fscx_revolve(C, B2, r) ^ P #Note that this form breaks trapdoor
also, fscx_revolve(C2, B, r) ^ A ^ P == fscx_revolve(C2 ^ P, B, r) ^ A
let, public key => {C,B2,A2,r},
private key => {C2,B,A,r}
then, E = fscx_revolve(C, B2, r) ^ A2 ^ P,
P == (D = fscx_revolve(C2, B, r) ^ A ^ E)
let, E = fscx_revolve(C2, B, r) ^ A ^ P
then, fscx_revolve(E, B2, i) ^ A2 ^ P == 0
fscx_revolve(E ^ P, B2, i) == 0
HKEX (key exchange)
Alice: C = fscx_revolve(A,B,i)
send C to Bob and get C2
shared_key = fscx_revolve(C2, B, r) ^ A,
Bob: C2 = fscx_revolve(A2,B2,i)
send C2 to Alice and get C
shared_key => fscx_revolve(C, B2, r) ^ A2
HSKE (symmetric key encryption):
Alice,Bob: share key of bitlength b
Alice: E = fscx_revolve(P , key , i)
shares E with Bob
Bob: P = fscx_revolve(E , key , r)
HPKS (public key signature)
Alice: C = fscx_revolve(A,B,i)
C2 = fscx_revolve(A2,B2,i)
{publish (C,B2,A2,r) as public key, also disclose b,r,i; keep the rest of parameters (C2,B,A) as private key},
S = fscx_revolve(C2, B, r) ^ A ^ P
shares E, S with Bob
Bob: P = fscx_revolve(C,B2, r) ^ A2 ^ S
HPKE (public key encryption)
Alice: C = fscx_revolve(A,B,i),
C2 = fscx_revolve(A2,B2,i),
{publish (C,B2,A2,r) as public key, keep the rest of parameters as private key},
Bob: E = fscx_revolve(C, B2, r) ^ A2 ^ P
shares E with Alice
Alice: P = fscx_revolve(C2, B, r) ^ A ^ E
*/
func main() {
/*
A := New_rand_bitarray(256)
B := New_rand_bitarray(256)
C := Fscx(A, B)
D := Fscx_revolve(A, B, 256, false)
fmt.Printf("A : %x\n", A)
fmt.Printf("B : %x\n", B)
fmt.Printf("C : %x\n", C)
fmt.Printf("D : %x\n\n", D)
*/
// Example Usage:
r_value := 192 // Adjust as needed
i_value := 64 // Adjust as needed
A := New_rand_bitarray(256)
fmt.Printf("A : %x\n", A)
A2 := New_rand_bitarray(256)
fmt.Printf("A2 : %x\n", A2)
B := New_rand_bitarray(256)
fmt.Printf("B : %x\n", B)
B2 := New_rand_bitarray(256)
fmt.Printf("B2 : %x\n", B2)
C := Fscx_revolve(A, B, i_value, false)
fmt.Printf("C : %x\n", C)
C2 := Fscx_revolve(A2, B2, i_value, false)
fmt.Printf("C2 : %x\n", C2)
nonce := New_rand_bitarray(256)
fmt.Printf("nonce : %x\n", nonce)
preshared := New_rand_bitarray(256)
fmt.Printf("preshared : %x\n", preshared)
plaintext := New_rand_bitarray(256)
fmt.Printf("plaintext : %x\n", plaintext)
fmt.Printf("\n--- HKEX (key exchange)\n")
skeyA := Fscx_revolve(C2, B, r_value, false).Xor(A)
fmt.Printf("skeyA : %x\n", skeyA)
skeyB := Fscx_revolve(C, B2, r_value, false).Xor(A2)
fmt.Printf("skeyB : %x\n", skeyB)
if skeyA.Equal(skeyB) { // Assert equality
fmt.Printf("+ session keys skeyA and skeyB are equal!\n")
} else {
fmt.Printf("- session keys skeyA and skeyB are different!\n")
}
fmt.Printf("\n--- HSKE (symmetric key encryption)\n")
E := Fscx_revolve(plaintext, preshared, i_value, false)
fmt.Printf("E (Alice) : %x\n", E)
D := Fscx_revolve(E, preshared, r_value, false)
fmt.Printf("D (Bob) : %x\n", D)
if D.Equal(plaintext) { // Assert equality
fmt.Printf("+ plaintext is correctly decrypted from E with preshared key\n")
} else {
fmt.Printf("- plaintext is different from decrypted E with preshared key!\n")
}
fmt.Printf("\n--- HPKS (public key signature)\n")
S := Fscx_revolve(C2, B, r_value, false).Xor(A).Xor(plaintext)
fmt.Printf("S (Alice) : %x\n", S)
V := Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(S) // == plaintext !!!!
fmt.Printf("V (Bob) : %x\n", V)
if V.Equal(plaintext) { // Assert equality
fmt.Printf("+ signature S from plaintext is correct!\n")
} else {
fmt.Printf("- signature S from plaintext is incorrect!\n")
}
fmt.Printf("\n--- HPKS (public key signature) + HSKE (symmetric key encryption) with preshared key made public\n")
E = Fscx_revolve(plaintext, preshared, i_value, false)
fmt.Printf("E (Alice) : %x\n", E)
S = Fscx_revolve(C2, B, r_value, false).Xor(A).Xor(E) // A+B2+C is the trapdoor for deceiving EVE!!!!
fmt.Printf("S (Alice) : %x\n", S)
V = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(S) // == encryptedText
fmt.Printf("V (Bob) : %x\n", V)
D = Fscx_revolve(V, preshared, r_value, false) // => plaintext
fmt.Printf("D (Bob) : %x\n", D)
if D.Equal(plaintext) { // Assert equality
fmt.Printf("+ signature S(E) from plaintext is correct!\n")
} else {
fmt.Printf("- signature S(E) from plaintext is incorrect!\n")
}
fmt.Printf("\n--- HPKE (public key encryption)\n")
E = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(plaintext)
fmt.Printf("E (Bob) : %x\n", E)
D = Fscx_revolve(C2, B, r_value, false).Xor(A).Xor(E) // == plaintext !!!!
fmt.Printf("D (Alice) : %x\n", D)
if D.Equal(plaintext) { // Assert equality
fmt.Printf("+ plaintext is correctly decrypted from E with private key!\n")
} else {
fmt.Printf("- plaintext is different from decrypted E with private key!\n")
}
fmt.Printf("\n\n*** EVE bypass TESTS\n")
fmt.Printf("*** HPKS (public key signature)\n")
S = Fscx_revolve(C, B2, r_value, false).Xor(nonce) // ^ bruteForceValue // w/o A+A2+C2 Eve would be forced to do a Brute force attack to find it.
fmt.Printf("S (Eve) : %x\n", S)
V = Fscx_revolve(C, B2, r_value, false).Xor(A2) // X
fmt.Printf("V (Bob) : %x\n", V)
if V.Equal(nonce) { // Assert equality
fmt.Printf("+ nonce fake signature 1 verification with Alice public key is correct!\n")
} else {
fmt.Printf("- nonce fake signature 1 verification with Alice public key is incorrect!\n")
}
S2 := V.Xor(nonce)
fmt.Printf("S2 (Eve) : %x\n", S2)
V2 := Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(S2) // KK
fmt.Printf("V2 (Bob) : %x\n", V2)
if V2.Equal(nonce) { // Assert equality
fmt.Printf("+ nonce fake signature 2 verification with Alice public key is correct!\n")
} else {
fmt.Printf("- nonce fake signature 2 verification with Alice public key is incorrect!\n")
}
fmt.Printf("\n*** HPKS (public key signature) + HSKE (symmetric key encryption) with preshared key made public\n")
E = Fscx_revolve(nonce, preshared, i_value, false)
fmt.Printf("E (Eve) : %x\n", E)
S = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(E) // ^ bruteForceValue // w/o A+B2+C Eve would be forced to do a Brute force attack to find it.
fmt.Printf("S (Eve) : %x\n", S)
V = Fscx_revolve(C, B2, r_value, false).Xor(A2) // X
fmt.Printf("V (Eve) : %x\n", V)
S2 = V.Xor(S)
fmt.Printf("S2 (Eve) : %x\n", S2)
V2 = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(S2) // KK
fmt.Printf("V2 (Bob) : %x\n", V2)
D = Fscx_revolve(V2, preshared, r_value, false)
fmt.Printf("D (Bob) : %x\n", D) //X
if D.Equal(nonce) { // Assert equality
fmt.Printf("+ fake signature(encrypted nonce) verification with Alice public key is correct!\n")
} else {
fmt.Printf("- fake signature(encrypted nonce) verification with Alice public key is incorrect!\n")
}
fmt.Printf("\n*** HPKS (public key signature) + HSKE (symmetric key encryption) with preshared key made public - v2\n")
S = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(nonce) // ^ bruteForceValue // w/o A+B2+C Eve would be forced to do a Brute force attack to find it.
fmt.Printf("S (Eve) : %x\n", S)
E = Fscx_revolve(S, preshared, i_value, false)
fmt.Printf("E (Eve) : %x\n", E)
V = Fscx_revolve(C, B2, r_value, false).Xor(A2) // X
fmt.Printf("V (Eve) : %x\n", V)
S2 = V.Xor(E)
fmt.Printf("S2 (Eve) : %x\n", S2)
V2 = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(S2) // KK
fmt.Printf("V2 (Bob) : %x\n", V2)
D = Fscx_revolve(V2, preshared, r_value, false)
fmt.Printf("D (Bob) : %x\n", D) //X
if D.Equal(nonce) { // Assert equality
fmt.Printf("+ fake signature(encrypted nonce) v2 verification with Alice public key is correct!\n")
} else {
fmt.Printf("- fake signature(encrypted nonce) v2 verification with Alice public key is incorrect!\n")
}
fmt.Printf("\n*** HPKE (public key encryption)\n")
E = Fscx_revolve(C, B2, r_value, false).Xor(A2).Xor(plaintext) // ^ bruteForceValue // w/o A+B2+C Eve would be forced to do a Brute force attack to find it.
fmt.Printf("E (Bob) : %x\n", E)
D = Fscx_revolve(C, B2, r_value, false).Xor(A2) //X, but == fsession from private/public key generation if components had been reused from an HKEX!?
fmt.Printf("D (Eve) : %x\n", D)
E2 := D.Xor(E)
D2 := Fscx_revolve(C, B2, r_value, false).Xor(E2) // KK
fmt.Printf("D2 (Eve) : %x\n", V2)
if D.Equal(nonce) || D2.Equal(nonce) { // Assert equality
fmt.Printf("+ Eve could decrypt plaintext without Alice's private key!\n")
} else {
fmt.Printf("- Eve could not decrypt plaintext without Alice's private key!\n")
}
}