Skip to content

Commit 2887895

Browse files
authored
resolving missing cipher test from issue #272 (#283)
* resolving missing cipher test from issue #272 Signed-off-by: Zzocker <[email protected]> * coorecting test file name Signed-off-by: Zzocker <[email protected]>
1 parent 46af415 commit 2887895

File tree

3 files changed

+82
-69
lines changed

3 files changed

+82
-69
lines changed

ciphers/diffieHellmanKeyExchange.go

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Package diffiehelkeyexchange implements Deffie Hellman Key Exchange Algorithm
2+
// for more information watch : https://www.youtube.com/watch?v=NmM9HA2MQGI
3+
package diffiehelkeyexchange
4+
5+
const (
6+
generator = 3
7+
primeNumber int64 = 6700417 // prime number discovered by Leonhard Euler
8+
)
9+
10+
// GenerateShareKey : generates a key using client private key , generator and primeNumber
11+
// this key can be made public
12+
// shareKey = (g^key)%primeNumber
13+
func GenerateShareKey(prvKey int64) int64 {
14+
return modularExponentiation(generator, prvKey, primeNumber)
15+
}
16+
17+
// GenerateMutualKey : generates a mutual key that can be used by only alice and bob
18+
// mutualKey = (shareKey^prvKey)%primeNumber
19+
func GenerateMutualKey(prvKey, shareKey int64) int64 {
20+
return modularExponentiation(shareKey, prvKey, primeNumber)
21+
}
22+
23+
// r = (b^e)%mod
24+
func modularExponentiation(b, e, mod int64) int64 {
25+
26+
//runs in O(log(n)) where n = e
27+
//uses exponentiation by squaring to speed up the process
28+
if mod == 1 {
29+
return 0
30+
}
31+
var r int64 = 1
32+
b = b % mod
33+
for e > 0 {
34+
if e&1 == 1 {
35+
r = (r * b) % mod
36+
}
37+
e = e >> 1
38+
b = (b * b) % mod
39+
}
40+
return r
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package diffiehelkeyexchange
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/rsa"
6+
"testing"
7+
)
8+
9+
func TestDiffieHellmanKeyExchange(t *testing.T) {
10+
t.Run("Test 1: modularExponentiation", func(t *testing.T) {
11+
var want int64 = 9 // (3^5)mod13 = 243mod13 = 9
12+
var prvKey int64 = 5
13+
var generator int64 = 3
14+
var primeNumber int64 = 13
15+
got := modularExponentiation(generator, prvKey, primeNumber)
16+
if got != want {
17+
t.Errorf(`with privateKey=%d, generator=%d and primeNumber=%d
18+
modularExponentiation should result=%d, but resulted=%d`, prvKey, generator, primeNumber, want, got)
19+
}
20+
})
21+
22+
t.Run("Test 2: Key Exchange", func(t *testing.T) {
23+
// generating a small sized rsa key for testing
24+
alicePrvKey, _ := rsa.GenerateKey(rand.Reader, 31)
25+
bobPrvKey, _ := rsa.GenerateKey(rand.Reader, 31)
26+
27+
// alice and bob generates their respective share key with their privateKey
28+
shareKeyByAlice := GenerateShareKey(alicePrvKey.D.Int64())
29+
shareKeyByBob := GenerateShareKey(bobPrvKey.D.Int64())
30+
31+
// generated share key now can be exchanged even via insecure channel
32+
33+
// mutualKey can be computed using shared key
34+
mutualKeyComputedByAlice := GenerateMutualKey(alicePrvKey.D.Int64(), shareKeyByBob)
35+
mutualKeyComputedByBob := GenerateMutualKey(bobPrvKey.D.Int64(), shareKeyByAlice)
36+
37+
if mutualKeyComputedByAlice != mutualKeyComputedByBob {
38+
t.Errorf("mutual key computed by alice and bob should be same, but got un-equal")
39+
}
40+
})
41+
}

0 commit comments

Comments
 (0)