Skip to content

Commit a1c50d1

Browse files
committed
feature: rsassa_pss_sign + rsassa_pss_verify
1 parent bc80771 commit a1c50d1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/rsa.jl

+21
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ 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+
length(S) != k && error("invalid signature") |> throw
204+
s = OS2IP(S)
205+
m = RSAVP1(pkcs1_v1_5, s, key)
206+
modBits = Base.GMP.MPZ.sizeinbase(key.modulus, 2)
207+
emLen = ceil((modBits - 1)/8) |> Integer
208+
EM = I2OSP(m, emLen)
209+
result = emsa_pss_verify(M, EM, modBits - 1)
210+
return result
211+
end
212+
192213
"""
193214
encrypt(::pkcs1_v1_5_t,
194215
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+
msg = Vector{UInt8}("123")
134+
signature = ToyPublicKeys.rsassa_pss_sign(msg, private_key)
135+
valid = ToyPublicKeys.rsassa_pss_verify(C, public_key)
136+
@test valid == true
137+
end

0 commit comments

Comments
 (0)