Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkcs_1_v1_5.jl cleanup + public interface cleanup #63

Merged
merged 5 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import Random
using ToyPublicKeys
Random.seed!(42)
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
msg = "Super secret message!"
msg = Vector{UInt8}("Super secret message!")

println(msg)
encrypted = ToyPublicKeys.encrypt(ToyPublicKeys.pkcs1_v1_5, msg, public_key)
encrypted = ToyPublicKeys.encrypt(msg, public_key)
println(encrypted)
decrypted = ToyPublicKeys.decrypt(ToyPublicKeys.pkcs1_v1_5, encrypted, private_key)
decrypted = ToyPublicKeys.decrypt(encrypted, private_key)
println(decrypted)
```
## Signatures and their verification
Expand All @@ -18,11 +19,11 @@ import Random
using ToyPublicKeys
Random.seed!(42)
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
msg = "Super authentic message!"
msg = Vector{UInt8}("Super authentic message!")
println("Currently this feature is disabled")
#println(msg)
#signature = ToyPublicKeys.sign(ToyPublicKeys.pkcs1_v1_5, msg, private_key)
#println(signature)
#authentic = ToyPublicKeys.verify_signature(ToyPublicKeys.pkcs1_v1_5, msg, signature, public_key)
#println(authentic)
println(msg)
signature = ToyPublicKeys.sign(msg, private_key)
println(signature)
authentic = ToyPublicKeys.verify_signature(msg, signature, public_key)
println(authentic)
```
3 changes: 1 addition & 2 deletions src/ToyPublicKeys.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ include("utils/number_theory.jl")
include("utils/string.jl")
include("rsa.jl")
include("pkcs1_v2_2.jl")
include("padding/pkcs_1_v1_5.jl")
include("dh.jl")
export RSAKey, RSAPrivateKey, RSAPublicKey, generate_rsa_key_pair, encrypt, decrypt, sign, verify_signature, dh_params
export RSAKey, RSAPrivateKey, RSAPublicKey, generate_rsa_key_pair, encrypt, decrypt, sign, verify_signature
end
52 changes: 0 additions & 52 deletions src/padding/pkcs_1_v1_5.jl

This file was deleted.

31 changes: 26 additions & 5 deletions src/pkcs1_v2_2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ function rsaes_oaep_decode(msg::Vector{UInt8},
return M
end

function eme_pkcs1_v1_5_encode(M::Vector{UInt8}, key::RSAPublicKey)
k = (Base.GMP.MPZ.sizeinbase(key.modulus, 2)/8) |> ceil |> Integer
mLen = length(M)
(mLen > k - 11) && error("(mLen > k - 11)")
PS = rand(UInt8(1):typemax(UInt8), k - 3 - mLen)
EM = vcat(UInt8[0x00, 0x02], PS, UInt8[0x00], M)
return EM
end


function eme_pkcs1_v1_5_decode(EM::Vector{UInt8})
EM[1:2] != UInt8[0x00, 0x02] && error("EM[1:2] != UInt8[0x00, 0x02]")
PSIndex = findfirst(==(0x00), EM[3:end]) + 2
PS = EM[3:PSIndex - 1]
length(PS) < 8 && error("length(PS) < 8")
EM[PSIndex] != 0x00 && error("EM[PSIndex] != 0x00")
M = EM[PSIndex + 1:end]
return M
end

function MGF1(mgfSeed::Vector{UInt8},
maskLen:: Integer;
hash = SHA.sha1)
Expand Down Expand Up @@ -192,22 +212,23 @@ function rsaes_oaep_decrypt(C::Vector{UInt8},
return M
end

function rsaes_pkvs1_v1_5_encrypt(M::String, key::RSAPublicKey)
EM = pad(pkcs1_v1_5, M)
function rsaes_pkvs1_v1_5_encrypt(M::Vector{UInt8},
key::RSAPublicKey)
EM = eme_pkcs1_v1_5_encode(M, key)
m = OS2IP(EM)
c = RSAEP(pkcs1_v1_5, m, key)
k = (Base.GMP.MPZ.sizeinbase(key.modulus, 2)/8) |> ceil |> Integer
C = I2OSP(c, k)
return C
end

function rsaes_pkvs1_v1_5_decrypt(C::String,
function rsaes_pkvs1_v1_5_decrypt(C::Vector{UInt8},
key::RSAPrivateKey)
c = OS2IP(C)
m = RSADP(pkcs1_v1_5, c, key)
k = (Base.GMP.MPZ.sizeinbase(key.modulus, 2)/8) |> ceil |> Integer
EM = I2OSP(m, k)
m = unpad(pkcs1_v1_5, EM)
m = eme_pkcs1_v1_5_decode(EM)
return m
end

Expand Down Expand Up @@ -252,7 +273,7 @@ function rsassa_pkcs1_v1_5_verify(M::Vector{UInt8},
key::RSAPublicKey)
modBits = Base.GMP.MPZ.sizeinbase(key.modulus, 2)
k = (modBits/8) |> ceil |> Integer
length(S) != k && error()
length(S) != k && error("length(S) != k")
s = OS2IP(S)
m = RSAVP1(pkcs1_v1_5, s, key)
EM = I2OSP(m, k)
Expand Down
Loading