Skip to content

Commit

Permalink
Merge pull request #35 from yuyichao/align
Browse files Browse the repository at this point in the history
Fix wrong use of reinterpret
  • Loading branch information
staticfloat authored May 17, 2017
2 parents 08bfbf1 + 2bb0207 commit 337a8f7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ function digest!{T<:Union{SHA1_CTX,SHA2_CTX}}(context::T)
end

# Store the length of the input data (in bits) at the end of the padding
bitcount_buffer = reinterpret(typeof(context.bytecount), context.buffer)
bitcount_idx = div(short_blocklen(T), sizeof(context.bytecount))+1
bitcount_buffer[bitcount_idx] = bswap(context.bytecount*8)
bitcount_idx = div(short_blocklen(T), sizeof(context.bytecount)) + 1
pbuf = Ptr{typeof(context.bytecount)}(pointer(context.buffer))
unsafe_store!(pbuf, bswap(context.bytecount * 8), bitcount_idx)

# Final transform:
transform!(context)
Expand Down
4 changes: 2 additions & 2 deletions src/sha1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ end

function transform!(context::SHA1_CTX)
# Buffer is 16 elements long, we expand to 80
buffer = reinterpret(eltype(context.state), context.buffer)
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
for i in 1:16
context.W[i] = bswap(buffer[i])
context.W[i] = bswap(unsafe_load(pbuf, i))
end

# First round of expansions
Expand Down
32 changes: 18 additions & 14 deletions src/sha2.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function transform!{T<:Union{SHA2_224_CTX,SHA2_256_CTX}}(context::T)
buffer = reinterpret(eltype(context.state), context.buffer)
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
# Initialize registers with the previous intermediate values (our state)
a = context.state[1]
b = context.state[2]
Expand All @@ -14,10 +14,11 @@ function transform!{T<:Union{SHA2_224_CTX,SHA2_256_CTX}}(context::T)
for j = 1:16
@inbounds begin
# We bitswap every input byte
buffer[j] = bswap(buffer[j])
v = bswap(unsafe_load(pbuf, j))
unsafe_store!(pbuf, v, j)

# Apply the SHA-256 compression function to update a..h
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + buffer[j]
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + v
T2 = Sigma0_256(a) + Maj(a, b, c)
h = g
g = f
Expand All @@ -33,14 +34,15 @@ function transform!{T<:Union{SHA2_224_CTX,SHA2_256_CTX}}(context::T)
for j = 17:64
@inbounds begin
# Implicit message block expansion:
s0 = buffer[mod1(j + 1, 16)]
s0 = unsafe_load(pbuf, mod1(j + 1, 16))
s0 = sigma0_256(s0)
s1 = buffer[mod1(j + 14, 16)]
s1 = unsafe_load(pbuf, mod1(j + 14, 16))
s1 = sigma1_256(s1)

# Apply the SHA-256 compression function to update a..h
buffer[mod1(j, 16)] += s1 + buffer[mod1(j + 9,16)] + s0
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + buffer[mod1(j, 16)]
v = unsafe_load(pbuf, mod1(j, 16)) + s1 + unsafe_load(pbuf, mod1(j + 9, 16)) + s0
unsafe_store!(pbuf, v, mod1(j, 16))
T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + v
T2 = Sigma0_256(a) + Maj(a, b, c)
h = g
g = f
Expand All @@ -66,7 +68,7 @@ end


function transform!(context::Union{SHA2_384_CTX,SHA2_512_CTX})
buffer = reinterpret(eltype(context.state), context.buffer)
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
# Initialize registers with the prev. intermediate value
a = context.state[1]
b = context.state[2]
Expand All @@ -79,10 +81,11 @@ function transform!(context::Union{SHA2_384_CTX,SHA2_512_CTX})

for j = 1:16
@inbounds begin
buffer[j] = bswap(buffer[j])
v = bswap(unsafe_load(pbuf, j))
unsafe_store!(pbuf, v, j)

# Apply the SHA-512 compression function to update a..h
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + buffer[j]
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + v
T2 = Sigma0_512(a) + Maj(a, b, c)
h = g
g = f
Expand All @@ -98,14 +101,15 @@ function transform!(context::Union{SHA2_384_CTX,SHA2_512_CTX})
for j = 17:80
@inbounds begin
# Implicit message block expansion:
s0 = buffer[mod1(j + 1, 16)]
s0 = unsafe_load(pbuf, mod1(j + 1, 16))
s0 = sigma0_512(s0)
s1 = buffer[mod1(j+14, 16)]
s1 = unsafe_load(pbuf, mod1(j + 14, 16))
s1 = sigma1_512(s1)

# Apply the SHA-512 compression function to update a..h
buffer[mod1(j, 16)] += s1 + buffer[mod1(j+9, 16)] + s0
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + buffer[mod1(j, 16)]
v = unsafe_load(pbuf, mod1(j, 16)) + s1 + unsafe_load(pbuf, mod1(j + 9, 16)) + s0
unsafe_store!(pbuf, v, mod1(j, 16))
T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + v
T2 = Sigma0_512(a) + Maj(a, b, c)
h = g
g = f
Expand Down
4 changes: 2 additions & 2 deletions src/sha3.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function transform!{T<:SHA3_CTX}(context::T)
# First, update state with buffer
buffer_as_uint64 = reinterpret(eltype(context.state), context.buffer)
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
for idx in 1:div(blocklen(T),8)
context.state[idx] = context.state[idx] buffer_as_uint64[idx]
context.state[idx] = context.state[idx] unsafe_load(pbuf, idx)
end
bc = Vector{UInt64}(5)

Expand Down
8 changes: 4 additions & 4 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ SHA3_384_CTX() = SHA3_384_CTX(zeros(UInt64, 25), 0, zeros(UInt8, blocklen(SHA3_3
SHA3_512_CTX() = SHA3_512_CTX(zeros(UInt64, 25), 0, zeros(UInt8, blocklen(SHA3_512_CTX)))

# Nickname'd outer constructor methods for SHA2
SHA224_CTX = SHA2_224_CTX
SHA256_CTX = SHA2_256_CTX
SHA384_CTX = SHA2_384_CTX
SHA512_CTX = SHA2_512_CTX
const SHA224_CTX = SHA2_224_CTX
const SHA256_CTX = SHA2_256_CTX
const SHA384_CTX = SHA2_384_CTX
const SHA512_CTX = SHA2_512_CTX

# SHA1 is special; he needs extra workspace
SHA1_CTX() = SHA1_CTX(copy(SHA1_initial_hash_value), 0, zeros(UInt8, blocklen(SHA1_CTX)), Vector{UInt32}(80))
Expand Down

0 comments on commit 337a8f7

Please sign in to comment.