Skip to content

Commit 8688340

Browse files
committed
feature: adding pkcs #1 v1.5 padding functions
1 parent 96ddd17 commit 8688340

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/ToyPublicKeys.jl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module ToyPublicKeys
33
@enum AsymetricKeyType public_key private_key
44
include("utils/primes.jl")
55
include("utils/random.jl")
6+
include("padding/pkcs_1_v1_5.jl")
67
include("rsa.jl")
78
export RSAKey, encrypt, decrypt
89
end

src/padding/pkcs_1_v1_5.jl

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function pad(msg:: Vector{UInt8}, pad_length=32)
2+
buff = rand(UInt8(1):UInt8(typemax(UInt8)), pad_length + 3)
3+
buff[1] = 0
4+
buff[2] = 2
5+
buff[pad_length + 3] = 0
6+
append!(buff, msg)
7+
return buff
8+
end
9+
10+
function unpad(msg:: Vector{UInt8})
11+
num_of_c_chars = 3
12+
start_of_padding = 3
13+
if msg[1] != 0 && msg[2] != 2
14+
error("Not pkcs_1_v1_5")
15+
end
16+
pos = findfirst(==(0), view(msg, start_of_padding:length(msg))) + num_of_c_chars
17+
return view(msg, pos:length(msg))
18+
end

test/runtests.jl

+8
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,12 @@ end
4949
@test decrypted == msg
5050
end
5151

52+
@testset "padding/pkcs_1_v1_5" begin
53+
test_vector = Vector{UInt8}([1,2,3])
54+
Random.seed!(42)
55+
padded_vector_correct = UInt8[0x00, 0x02, 0x7a, 0xb4, 0xac, 0x2b, 0x9d, 0xab, 0x75, 0x4d, 0xa9, 0xa4, 0x58, 0x45, 0x84, 0x17, 0x46, 0x31, 0x6d, 0x7c, 0x15, 0x84, 0x33, 0x9a, 0x66, 0x51, 0x6f, 0xdb, 0x52, 0x90, 0x53, 0x29, 0xb9, 0x5f, 0x00, 0x01, 0x02, 0x03]
56+
padded_vec = ToyPublicKeys.pad(test_vector)
57+
@test padded_vector_correct == padded_vec
58+
unpadded_vector = ToyPublicKeys.unpad(padded_vector_correct)
59+
@test test_vector == unpadded_vector
5260
end

0 commit comments

Comments
 (0)