Skip to content

Commit 12dfbf2

Browse files
committed
feature: implement textbook DH
1 parent df1e05b commit 12dfbf2

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/ToyPublicKeys.jl

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ include("utils/random.jl")
55
include("utils/number_theory.jl")
66
include("padding/pkcs_1_v1_5.jl")
77
include("rsa.jl")
8+
include("dh.jl")
89
export RSAKey, RSAPrivateKey, RSAPublicKey, encrypt, decrypt, sign, verify_signature
910
end

src/dh.jl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct DHPrivateKey
2+
modulus::BigInt
3+
public_component::BigInt
4+
private_component::BigInt
5+
end
6+
7+
struct DHPublicKey
8+
modulus::BigInt
9+
public_component::BigInt
10+
end
11+
12+
function DHStep(key::DHPrivateKey)
13+
return Base.GMP.MPZ.powm(key.public_component, key.private_component, key.modulus)
14+
end
15+
16+
function DHStep(intermidiary::BigInt, key::DHPrivateKey)
17+
return Base.GMP.MPZ.powm(intermidiary, key.private_component, key.modulus)
18+
end
19+
20+
function generate_DHKeyPair(modulus::BigInt, public::BigInt, private::BigInt)
21+
return (DHPrivateKey(modulus, public, private), DHPublicKey(modulus, public))
22+
end

test/dh.jl

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
@testset "dh basics" begin
3+
s_modulus = big"23"
4+
s_public = big"5"
5+
a_priv = big"4"
6+
b_priv = big"3"
7+
a_private_key, a_public_key = ToyPublicKeys.generate_DHKeyPair(s_modulus, s_public, a_priv)
8+
b_private_key, b_public_key = ToyPublicKeys.generate_DHKeyPair(s_modulus, s_public, b_priv)
9+
10+
a_interm = ToyPublicKeys.DHStep(a_private_key)
11+
b_interm = ToyPublicKeys.DHStep(b_private_key)
12+
a_shared_s = ToyPublicKeys.DHStep(b_interm, a_private_key)
13+
b_shared_s = ToyPublicKeys.DHStep(a_interm, b_private_key)
14+
@test a_shared_s == b_shared_s
15+
end

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ using ToyPublicKeys
22
using Test
33

44
include("rsa.jl")
5+
include("dh.jl")

0 commit comments

Comments
 (0)