Skip to content

Commit b947bc0

Browse files
committed
draft
1 parent 2edaaf4 commit b947bc0

File tree

3 files changed

+95
-24
lines changed

3 files changed

+95
-24
lines changed

docs/src/examples.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ using ToyPublicKeys
1919
Random.seed!(42)
2020
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
2121
msg = "Super authentic message!"
22-
println(msg)
23-
signature = ToyPublicKeys.sign(ToyPublicKeys.pkcs1_v1_5, msg, private_key)
24-
println(signature)
25-
authentic = ToyPublicKeys.verify_signature(ToyPublicKeys.pkcs1_v1_5, msg, signature, public_key)
26-
println(authentic)
22+
println("Currently this feature is disabled")
23+
#println(msg)
24+
#signature = ToyPublicKeys.sign(ToyPublicKeys.pkcs1_v1_5, msg, private_key)
25+
#println(signature)
26+
#authentic = ToyPublicKeys.verify_signature(ToyPublicKeys.pkcs1_v1_5, msg, signature, public_key)
27+
#println(authentic)
2728
```

src/rsa.jl

+80-10
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,50 @@ RSA exponentiation step when only public key is available.
8383
Uses [repeated squares](https://en.wikipedia.org/wiki/Exponentiation_by_squaring)
8484
and other fast modulo exponentiation tricks in its GMP implementation (Base.GMP.MPZ.powm).
8585
"""
86-
function RSADP(::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey)
86+
function RSADP(v::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey)
87+
return RSAStep(v, msg, key)
88+
end
89+
90+
"""
91+
RSASP1(::pkcs1_v1_5_t, msg::BigInt, key::RSAPrivateKey)
92+
93+
"""
94+
function RSASP1(v::pkcs1_v1_5_t, msg::BigInt, key::RSAPrivateKey)
95+
return RSAStep(v, msg, key)
96+
end
97+
98+
"""
99+
RSAVP1(::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey)
100+
101+
"""
102+
function RSAVP1(v::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey)
103+
return RSAStep(v, msg, key)
104+
end
105+
106+
function RSAStep(::pkcs1_v1_5_t, msg::BigInt, key::RSAPrivateKey)
87107
if !(0 <= msg < key.modulus)
88108
error("msg has to be 0 <= msg < n, got: msg = $msg, n = $key.modulus")
89109
end
90-
return Base.GMP.MPZ.powm(msg, key.exponent, key.modulus)
110+
ret = power_crt(
111+
msg,
112+
key.primes[1],
113+
key.primes[2],
114+
key.crt_exponents[1],
115+
key.crt_exponents[2],
116+
key.crt_coefficients[1],
117+
)
118+
ret < 0 && (ret += key.modulus)
119+
return ret
91120
end
92121

93-
RSAStep(a::pkcs1_v1_5_t, msg::BigInt, key::RSAPrivateKey) = RSAEP(a, msg, key)
94-
RSAStep(a::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey) = RSADP(a, msg, key)
122+
function RSAStep(::pkcs1_v1_5_t, msg::BigInt, key::RSAPublicKey)
123+
if !(0 <= msg < key.modulus)
124+
error("msg has to be 0 <= msg < n, got: msg = $msg, n = $key.modulus")
125+
end
126+
ret = Base.GMP.MPZ.powm(msg, key.exponent, key.modulus)
127+
ret < 0 && (ret += key.modulus)
128+
return ret
129+
end
95130

96131
"""
97132
RSAStep(::pkcs1_v1_5_t, msg::AbstractVector{T}, key::RSAKey) where {T<:Base.BitInteger}
@@ -130,6 +165,38 @@ function RSAStep(::pkcs1_v1_5_t, msg::String, key::RSAKey)
130165
return transformed_msg
131166
end
132167

168+
function rsaes_oaep_encrypt(M::String, key::RSAPublicKey; label="", hash=SHA.sha1, MGF=MGF1)
169+
EM = pad(pkcs1_v2_2, M, key, label=label, hash=hash, MGF=MGF)
170+
m = OS2IP(EM)
171+
c = RSAEP(pkcs1_v1_5, m, key)
172+
C = I2OSP(c, k)
173+
return C
174+
end
175+
176+
function rsaes_oaep_decrypt(C::String, key::RSAPublicKey; label="", hash=SHA.sha1, MGF=MGF1)
177+
c = OS2IP(C)
178+
m = RSADP(pkcs1_v1_5, c, key)
179+
EM = I2OSP(m, k)
180+
M = unpad(pkcs1_v2_2, EM, key, label=label, hash=hash, MGF=MGF)
181+
return M
182+
end
183+
184+
function rsaes_pkvs1_v1_5_encrypt(M::String, key::RSAPublicKey)
185+
EM = pad(pkcs1_v1_5, M)
186+
m = OS2IP(EM)
187+
c = RSAEP(pkcs1_v1_5, m, key)
188+
C = I2OSP(c)
189+
return C
190+
end
191+
192+
function rsaes_pkvs1_v1_5_decrypt(C::String, key::RSAPrivateKey)
193+
c = OS2IP(C)
194+
m = RSADP(pkcs1_v1_5, c, key)
195+
EM = I2OSP(m)
196+
m = unpad(pkcs1_v1_5, EM)
197+
return m
198+
end
199+
133200
"""
134201
encrypt(::pkcs1_v1_5_t,
135202
msg::Union{AbstractString,AbstractVector},
@@ -207,8 +274,9 @@ end
207274
208275
Sign string with RSA key.
209276
"""
210-
function sign(::pkcs1_v1_5_t, msg::String, key::RSAPrivateKey; pad_length=32)
211-
digest = SHA.sha256(msg)
277+
function sign(::pkcs1_v1_5_t, msg::String, key::RSAPrivateKey; pad_length=32, hash=SHA.sha1)
278+
error("needs reimplementing to be pkcs1_v1_5_t compliant") |> throw
279+
digest = hash(msg)
212280
msg_padded = ToyPublicKeys.pad(pkcs1_v1_5, digest, pad_length)
213281
return String(RSAStep(pkcs1_v1_5, msg_padded, key))
214282
end
@@ -218,8 +286,9 @@ end
218286
219287
Sign AbstractVector (arbitrary buffer using [SHA256](https://en.wikipedia.org/wiki/SHA-2)) with RSA key.
220288
"""
221-
function sign(::pkcs1_v1_5_t, msg::AbstractVector, key::RSAPrivateKey; pad_length=32)
222-
digest = SHA.sha256(String(msg))
289+
function sign(::pkcs1_v1_5_t, msg::AbstractVector, key::RSAPrivateKey; pad_length=32, hash=SHA.sha1)
290+
error("needs reimplementing to be pkcs1_v1_5_t compliant") |> throw
291+
digest = hash(String(msg))
223292
msg_padded = ToyPublicKeys.pad(pkcs1_v1_5, digest, pad_length)
224293
return RSAStep(pkcs1_v1_5, msg_padded, key)
225294
end
@@ -229,10 +298,11 @@ end
229298
230299
Verify the signature.
231300
"""
232-
function verify_signature(::pkcs1_v1_5_t, msg::String, signature::String, key::RSAPublicKey)
301+
function verify_signature(::pkcs1_v1_5_t, msg::String, signature::String, key::RSAPublicKey, hash=SHA.sha1)
302+
error("needs reimplementing to be pkcs1_v1_5_t compliant") |> throw
233303
signature_ = codeunits(signature)
234304
signature_decr = ToyPublicKeys.RSAStep(pkcs1_v1_5, signature_, key)
235305
unpaded_hash = ToyPublicKeys.unpad(pkcs1_v1_5, vcat(typeof(signature_decr)([0]), signature_decr)) # todo: leading zero is ignored, gotta deal with this
236-
digest = SHA.sha256(msg)
306+
digest = hash(msg)
237307
return unpaded_hash == digest
238308
end

test/rsa.jl

+9-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
@testset "RSAStep(RSAStep) is identity ~ BigInt" begin
6969
Random.seed!(42)
7070
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
71-
msg = big"2"
71+
msg = big"123"
7272
encrypted = ToyPublicKeys.RSAStep(ToyPublicKeys.pkcs1_v1_5, msg, public_key)
7373
decrypted = ToyPublicKeys.RSAStep(ToyPublicKeys.pkcs1_v1_5, encrypted, private_key)
7474
@test msg == decrypted
@@ -77,7 +77,7 @@ end
7777
@testset "RSAStep(RSAStep) is identity ~ CodeUnits" begin
7878
Random.seed!(42)
7979
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
80-
msg = codeunits("2")
80+
msg = codeunits("123")
8181
encrypted = ToyPublicKeys.RSAStep(ToyPublicKeys.pkcs1_v1_5, msg, public_key)
8282
decrypted = ToyPublicKeys.RSAStep(ToyPublicKeys.pkcs1_v1_5, encrypted, private_key)
8383
@test msg == decrypted
@@ -86,7 +86,7 @@ end
8686
@testset "RSAStep(RSAStep) is identity ~ String" begin
8787
Random.seed!(42)
8888
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
89-
msg = "2"
89+
msg = "123"
9090
encrypted = ToyPublicKeys.RSAStep(ToyPublicKeys.pkcs1_v1_5, msg, public_key)
9191
decrypted = ToyPublicKeys.RSAStep(ToyPublicKeys.pkcs1_v1_5, encrypted, private_key)
9292
@test msg == decrypted
@@ -95,7 +95,7 @@ end
9595
@testset "Decryption(Encryption) is identity ~ CodeUnits" begin
9696
Random.seed!(42)
9797
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
98-
msg = Base.CodeUnits("1")
98+
msg = Base.CodeUnits("123")
9999
encrypted = ToyPublicKeys.encrypt(ToyPublicKeys.pkcs1_v1_5, msg, public_key)
100100
decrypted = ToyPublicKeys.decrypt(ToyPublicKeys.pkcs1_v1_5, encrypted, private_key)
101101
@test decrypted == msg
@@ -104,16 +104,16 @@ end
104104
@testset "Decryption(Encryption) is identity ~ String" begin
105105
Random.seed!(42)
106106
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
107-
msg = "1"
107+
msg = "123"
108108
encrypted = ToyPublicKeys.encrypt(ToyPublicKeys.pkcs1_v1_5, msg, public_key)
109109
decrypted = ToyPublicKeys.decrypt(ToyPublicKeys.pkcs1_v1_5, encrypted, private_key)
110110
@test decrypted == msg
111111
end
112112

113-
@testset "verify_signature(sign) is true ~ String" begin
113+
@testset "RSASP1(RSAVP1) is true" begin
114114
Random.seed!(42)
115115
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
116-
msg = "1"
117-
signature = ToyPublicKeys.sign(ToyPublicKeys.pkcs1_v1_5, msg, private_key)
118-
@test ToyPublicKeys.verify_signature(ToyPublicKeys.pkcs1_v1_5, msg, signature, public_key) == true
116+
msg = big"3"
117+
signature = ToyPublicKeys.RSASP1(ToyPublicKeys.pkcs1_v1_5, msg, private_key)
118+
@test ToyPublicKeys.RSAVP1(ToyPublicKeys.pkcs1_v1_5, signature, public_key) == msg
119119
end

0 commit comments

Comments
 (0)