Skip to content

Commit 6a1d139

Browse files
committed
feature: rsassa_pss_sign + rsassa_pss_verify
1 parent e7b33ba commit 6a1d139

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/rsa.jl

+22
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,28 @@ function rsaes_pkvs1_v1_5_decrypt(C::String, key::RSAPrivateKey)
189189
return m
190190
end
191191

192+
function rsassa_pss_sign(M::Vector{UInt8}, key::RSAPrivateKey)
193+
modBits = Base.GMP.MPZ.sizeinbase(key.modulus, 2)
194+
EM = emsa_pss_encode(M, modBits - 1)
195+
m = OS2IP(EM)
196+
s = RSASP1(pkcs1_v1_5, m, key)
197+
k = (modBits/8) |> ceil |> Integer
198+
S = I2OSP(s, k)
199+
return S
200+
end
201+
202+
function rsassa_pss_verify(M::Vector{UInt8}, S::Vector{UInt8}, key::RSAPublicKey)
203+
modBits = Base.GMP.MPZ.sizeinbase(key.modulus, 2)
204+
k = (modBits/8) |> ceil |> Integer
205+
length(S) != k && error("invalid signature") |> throw
206+
s = OS2IP(S)
207+
m = RSAVP1(pkcs1_v1_5, s, key)
208+
emLen = ceil((modBits - 1)/8) |> Integer
209+
EM = I2OSP(m, emLen)
210+
result = emsa_pss_verify(M, EM, modBits - 1)
211+
return result
212+
end
213+
192214
"""
193215
encrypt(::pkcs1_v1_5_t,
194216
msg::Union{AbstractString,AbstractVector},

test/rsa.jl

+9
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,12 @@ end
126126
ret = ToyPublicKeys.rsaes_oaep_decrypt(C, private_key)
127127
@test ret == msg
128128
end
129+
130+
@testset "rsassa_pss_verify(rsassa_pss_sign) is true" begin
131+
Random.seed!(42)
132+
private_key, public_key = ToyPublicKeys.generate_rsa_key_pair(ToyPublicKeys.pkcs1_v1_5, 2048)
133+
M = Vector{UInt8}("123")
134+
S = ToyPublicKeys.rsassa_pss_sign(M, private_key)
135+
valid = ToyPublicKeys.rsassa_pss_verify(M, S, public_key)
136+
@test valid == true
137+
end

0 commit comments

Comments
 (0)