Skip to content

Commit

Permalink
Remove non-existent property (#75)
Browse files Browse the repository at this point in the history
* [bugfix] Remove non-existent property

* [test] Remove trailing spaces

* [test] add test for `getproperty`

* [test] fix typo

* Uniform error message style

* fix type param

* [test] add test for `state_type`

Test `short_blocklen` - call -> `state_type`

* [test] import `subtypes`

* [test] not use subtypes
  • Loading branch information
inkydragon authored Nov 13, 2023
1 parent 068f85d commit 88e1c83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ function Base.getproperty(ctx::SHA2_CTX, fieldname::Symbol)
return getfield(ctx, :bytecount)::Union{UInt64,UInt128}
elseif fieldname === :buffer
return getfield(ctx, :buffer)::Vector{UInt8}
elseif fieldname === :W
return getfield(ctx, :W)::Vector{UInt32}
elseif fieldname === :used
return getfield(ctx, :used)::Bool
else
error("SHA2_CTX has no field ", fieldname)
error("type ", typeof(ctx), " has no field ", fieldname)
end
end

Expand Down Expand Up @@ -133,7 +131,7 @@ state_type(::Type{SHA2_224_CTX}) = UInt32
state_type(::Type{SHA2_256_CTX}) = UInt32
state_type(::Type{SHA2_384_CTX}) = UInt64
state_type(::Type{SHA2_512_CTX}) = UInt64
state_type(::Type{SHA3_CTX}) = UInt64
state_type(::Type{T}) where {T<:SHA3_CTX} = UInt64

# blocklen is the number of bytes of data processed by the transform!() function at once
blocklen(::Type{SHA1_CTX}) = UInt64(64)
Expand Down
22 changes: 18 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ end
@testset "Chunked clumsily" begin
for sha_idx in 1:length(sha_funcs)
ctx = sha_types[sha_funcs[sha_idx]]()

# Get indices awkwardly placed for the blocklength of this hash type
idx0 = round(Int, 0.3*SHA.blocklen(typeof(ctx)))
idx1 = round(Int, 1.7*SHA.blocklen(typeof(ctx)))
idx2 = round(Int, 2.6*SHA.blocklen(typeof(ctx)))

# Feed data in according to our dastardly blocking scheme
SHA.update!(ctx, so_many_as_array[0 + 1:1*idx0])
SHA.update!(ctx, so_many_as_array[1*idx0 + 1:2*idx0])
Expand All @@ -63,7 +63,7 @@ end
SHA.update!(ctx, so_many_as_array[4*idx0 + 1:idx1])
SHA.update!(ctx, so_many_as_array[idx1 + 1:idx2])
SHA.update!(ctx, so_many_as_array[idx2 + 1:end])

# Ensure the hash is the appropriate one
hash = bytes2hex(SHA.digest!(ctx))
@test hash == answers[sha_funcs[sha_idx]][end]
Expand Down Expand Up @@ -142,6 +142,21 @@ end
end
end

@testset "codecov" begin
@test_throws ErrorException("type SHA2_256_CTX has no field _not_exist") SHA.SHA2_256_CTX()._not_exist
@test_throws ErrorException("type SHA3_256_CTX has no field _not_exist") SHA.SHA3_256_CTX()._not_exist
# default fallback
@test_throws ErrorException("type SHA1_CTX has no field _not_exist") SHA.SHA1_CTX()._not_exist

# Table 3: Input block sizes for HMAC
# https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
# SHA3-224 -256 -384 -512
block_size = [144, 136, 104, 72]
byte_count = 2 * sizeof(SHA.state_type(SHA.SHA3_CTX))
sha3_types = [SHA.SHA3_224_CTX, SHA.SHA3_256_CTX, SHA.SHA3_384_CTX, SHA.SHA3_512_CTX]
@test [ SHA.short_blocklen(T) for T in sha3_types ] == (block_size .- byte_count)
end

@testset "SHAKE" begin
# test some official testvectors from https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing
@testset "shake128" begin
Expand All @@ -160,4 +175,3 @@ end
@time SHA.shake256(b"abc",UInt(100000))
@time SHA.shake128(b"abc",UInt(100000))
end

0 comments on commit 88e1c83

Please sign in to comment.