Skip to content

Commit

Permalink
Merge pull request #96 from mortenpi/upgradathon
Browse files Browse the repository at this point in the history
Upgradathon
  • Loading branch information
staticfloat committed Aug 28, 2018
2 parents 1de4de6 + 316e67a commit 04afab9
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 166 deletions.
25 changes: 17 additions & 8 deletions .travis.yml
Expand Up @@ -2,16 +2,25 @@ language: julia
os:
- osx
- linux

julia:
- 0.6
- 0.7
- 1.0
- nightly

# # uncomment the following lines to allow failures on nightly julia
# # (tests will run but not make your overall status red)
# matrix:
# allow_failures:
# - julia: 0.7
# - julia: 1.0
# - julia: nightly

notifications:
email: false
# Avoids a Travis bug involving shallow clones
git:
depth: 999999
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd()); Pkg.build("Nettle"); Pkg.test("Nettle"; coverage=true)'

after_success:
- julia -e 'cd(Pkg.dir("Nettle")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
# submit test coverage
- cd ${TRAVIS_BUILD_DIR}
- julia -e 'using Pkg; Pkg.add("Coverage")'
- julia -e 'using Coverage; Coveralls.submit(Coveralls.process_folder())'
3 changes: 1 addition & 2 deletions REQUIRE
@@ -1,4 +1,3 @@
julia 0.6
julia 0.7
BinDeps
BinaryProvider
Compat 0.8.0
41 changes: 20 additions & 21 deletions appveyor.yml
@@ -1,9 +1,20 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
- julia_version: 0.7
- julia_version: 1.0
- julia_version: latest

platform:
- x86 # 32-bit
- x64 # 64-bit

# # uncomment the following lines to allow failures on nightly julia
# # (tests will run but not make your overall status red)
# matrix:
# allow_failures:
# - julia_version: 0.7
# - julia_version: 1.0
# - julia_version: latest

branches:
only:
Expand All @@ -17,24 +28,12 @@ notifications:
on_build_status_changed: false

install:
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# If there's a newer build queued for the same PR, cancel this one
- ps: if ($env:APPVEYOR_PULL_REQUEST_NUMBER -and $env:APPVEYOR_BUILD_NUMBER -ne ((Invoke-RestMethod `
https://ci.appveyor.com/api/projects/$env:APPVEYOR_ACCOUNT_NAME/$env:APPVEYOR_PROJECT_SLUG/history?recordsNumber=50).builds | `
Where-Object pullRequestId -eq $env:APPVEYOR_PULL_REQUEST_NUMBER)[0].buildNumber) { `
throw "There are newer queued builds for this pull request, failing early." }
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
$env:JULIA_URL,
"C:\projects\julia-binary.exe")
# Run installer silently, output to C:\projects\julia
- C:\projects\julia-binary.exe /S /D=C:\projects\julia
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))

build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone(pwd(), \"Nettle\"); Pkg.build(\"Nettle\")"
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"

test_script:
- C:\projects\julia\bin\julia -e "Pkg.test(\"Nettle\")"
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"
3 changes: 0 additions & 3 deletions src/Nettle.jl
@@ -1,7 +1,4 @@
__precompile__()
module Nettle
using Compat
import Compat.String

# Load libnettle from our deps.jl
const depsjl_path = joinpath(dirname(@__FILE__), "..", "deps", "deps.jl")
Expand Down
120 changes: 51 additions & 69 deletions src/cipher.jl
Expand Up @@ -8,35 +8,35 @@ export gen_key32_iv16, add_padding_PKCS5, trim_padding_PKCS5
export Encryptor, Decryptor, decrypt, decrypt!, encrypt, encrypt!

# This is a mirror of the nettle-meta.h:nettle_cipher struct
immutable NettleCipher
struct NettleCipher
name::Ptr{UInt8}
context_size::Cuint
block_size::Cuint
key_size::Cuint
set_encrypt_key::Ptr{Void}
set_decrypt_key::Ptr{Void}
encrypt::Ptr{Void}
decrypt::Ptr{Void}
set_encrypt_key::Ptr{Cvoid}
set_decrypt_key::Ptr{Cvoid}
encrypt::Ptr{Cvoid}
decrypt::Ptr{Cvoid}
end

# For much the same reasons as in hash_common.jl, we define a separate, more "Julia friendly" type
immutable CipherType
struct CipherType
name::AbstractString
context_size::Cuint
block_size::Cuint
key_size::Cuint
set_encrypt_key::Ptr{Void}
set_decrypt_key::Ptr{Void}
encrypt::Ptr{Void}
decrypt::Ptr{Void}
set_encrypt_key::Ptr{Cvoid}
set_decrypt_key::Ptr{Cvoid}
encrypt::Ptr{Cvoid}
decrypt::Ptr{Cvoid}
end

# These are the user-facing types that are used to actually {en,de}cipher stuff
immutable Encryptor
struct Encryptor
cipher_type::CipherType
state::Vector{UInt8}
end
immutable Decryptor
struct Decryptor
cipher_type::CipherType
state::Vector{UInt8}
end
Expand All @@ -60,7 +60,7 @@ function get_cipher_types()
cipher_idx = 1
# nettle_ciphers is an array of pointers ended by a NULL pointer, continue reading hash types until we hit it
while( true )
ncptr = unsafe_load(cglobal(("nettle_ciphers",libnettle),Ptr{Ptr{Void}}),cipher_idx)
ncptr = unsafe_load(cglobal(("nettle_ciphers",libnettle),Ptr{Ptr{Cvoid}}),cipher_idx)
if ncptr == C_NULL
break
end
Expand Down Expand Up @@ -104,8 +104,8 @@ function Encryptor(name::AbstractString, key)
throw(ArgumentError("Key must be $(cipher_type.key_size) bytes long"))
end

state = Vector{UInt8}(cipher_type.context_size)
ccall( cipher_type.set_encrypt_key, Void, (Ptr{Void}, Ptr{UInt8}), state, pointer(key))
state = Vector{UInt8}(undef, cipher_type.context_size)
ccall( cipher_type.set_encrypt_key, Cvoid, (Ptr{Cvoid}, Ptr{UInt8}), state, pointer(key))

return Encryptor(cipher_type, state)
end
Expand All @@ -122,8 +122,8 @@ function Decryptor(name::AbstractString, key)
throw(ArgumentError("Key must be $(cipher_type.key_size) bytes long"))
end

state = Vector{UInt8}(cipher_type.context_size)
ccall( cipher_type.set_decrypt_key, Void, (Ptr{Void}, Ptr{UInt8}), state, pointer(key))
state = Vector{UInt8}(undef, cipher_type.context_size)
ccall( cipher_type.set_decrypt_key, Cvoid, (Ptr{Cvoid}, Ptr{UInt8}), state, pointer(key))

return Decryptor(cipher_type, state)
end
Expand All @@ -141,32 +141,23 @@ function decrypt!(state::Decryptor, e::Symbol, iv::Vector{UInt8}, result, data)
if sizeof(iv) != state.cipher_type.block_size
throw(ArgumentError("Iv must be $(state.cipher_type.block_size) bytes long"))
end
@compat if ! (Symbol(uppercase(string(e))) in _cipher_suites)
if ! (Symbol(uppercase(string(e))) in _cipher_suites)
throw(ArgumentError("now supports $(_cipher_suites) only but ':$(e)'"))
end
if VERSION >= v"0.4.0"
hdl = Libdl.dlopen_e(libnettle)
s = Symbol("nettle_", lowercase(string(e)), "_decrypt")
c = Libdl.dlsym(hdl, s)
if c == C_NULL
throw(ArgumentError("not found function '$(s)' for ':$(e)'"))
end
# c points (:nettle_***_decrypt, nettle) may be loaded as another instance
iiv = copy(iv)
ccall(c, Void, (
Ptr{Void}, Ptr{Void}, Csize_t, Ptr{UInt8},
Csize_t, Ptr{UInt8}, Ptr{UInt8}),
state.state, state.cipher_type.decrypt, sizeof(iiv), iiv,
sizeof(data), pointer(result), pointer(data))
Libdl.dlclose(hdl)
else
iiv = copy(iv)
ccall((:nettle_cbc_decrypt, libnettle), Void, (
Ptr{Void}, Ptr{Void}, Csize_t, Ptr{UInt8},
Csize_t, Ptr{UInt8}, Ptr{UInt8}),
state.state, state.cipher_type.decrypt, sizeof(iiv), iiv,
sizeof(data), pointer(result), pointer(data))
hdl = Libdl.dlopen_e(libnettle)
s = Symbol("nettle_", lowercase(string(e)), "_decrypt")
c = Libdl.dlsym(hdl, s)
if c == C_NULL
throw(ArgumentError("not found function '$(s)' for ':$(e)'"))
end
# c points (:nettle_***_decrypt, nettle) may be loaded as another instance
iiv = copy(iv)
ccall(c, Cvoid, (
Ptr{Cvoid}, Ptr{Cvoid}, Csize_t, Ptr{UInt8},
Csize_t, Ptr{UInt8}, Ptr{UInt8}),
state.state, state.cipher_type.decrypt, sizeof(iiv), iiv,
sizeof(data), pointer(result), pointer(data))
Libdl.dlclose(hdl)
return result
end

Expand All @@ -180,19 +171,19 @@ function decrypt!(state::Decryptor, result, data)
if sizeof(data) % state.cipher_type.block_size > 0
throw(ArgumentError("Input array of length $(sizeof(data)) must be N times $(state.cipher_type.block_size) bytes long"))
end
ccall(state.cipher_type.decrypt, Void, (Ptr{Void},Csize_t,Ptr{UInt8},Ptr{UInt8}),
ccall(state.cipher_type.decrypt, Cvoid, (Ptr{Cvoid},Csize_t,Ptr{UInt8},Ptr{UInt8}),
state.state, sizeof(data), pointer(result), pointer(data))
return result
end

function decrypt(state::Decryptor, e::Symbol, iv::Vector{UInt8}, data)
result = Vector{UInt8}(sizeof(data))
result = Vector{UInt8}(undef, sizeof(data))
decrypt!(state, e, iv, result, data)
return result
end

function decrypt(state::Decryptor, data)
result = Vector{UInt8}(sizeof(data))
result = Vector{UInt8}(undef, sizeof(data))
decrypt!(state, result, data)
return result
end
Expand All @@ -210,32 +201,23 @@ function encrypt!(state::Encryptor, e::Symbol, iv::Vector{UInt8}, result, data)
if sizeof(iv) != state.cipher_type.block_size
throw(ArgumentError("Iv must be $(state.cipher_type.block_size) bytes long"))
end
@compat if ! (Symbol(uppercase(string(e))) in _cipher_suites)
if ! (Symbol(uppercase(string(e))) in _cipher_suites)
throw(ArgumentError("now supports $(_cipher_suites) only but ':$(e)'"))
end
if VERSION >= v"0.4.0"
hdl = Libdl.dlopen_e(libnettle)
s = Symbol("nettle_", lowercase(string(e)), "_encrypt")
c = Libdl.dlsym(hdl, s)
if c == C_NULL
throw(ArgumentError("not found function '$(s)' for ':$(e)'"))
end
# c points (:nettle_***_encrypt, nettle) may be loaded as another instance
iiv = copy(iv)
ccall(c, Void, (
Ptr{Void}, Ptr{Void}, Csize_t, Ptr{UInt8},
Csize_t, Ptr{UInt8}, Ptr{UInt8}),
state.state, state.cipher_type.encrypt, sizeof(iiv), iiv,
sizeof(data), pointer(result), pointer(data))
Libdl.dlclose(hdl)
else
iiv = copy(iv)
ccall((:nettle_cbc_encrypt, libnettle), Void, (
Ptr{Void}, Ptr{Void}, Csize_t, Ptr{UInt8},
Csize_t, Ptr{UInt8}, Ptr{UInt8}),
state.state, state.cipher_type.encrypt, sizeof(iiv), iiv,
sizeof(data), pointer(result), pointer(data))
hdl = Libdl.dlopen_e(libnettle)
s = Symbol("nettle_", lowercase(string(e)), "_encrypt")
c = Libdl.dlsym(hdl, s)
if c == C_NULL
throw(ArgumentError("not found function '$(s)' for ':$(e)'"))
end
# c points (:nettle_***_encrypt, nettle) may be loaded as another instance
iiv = copy(iv)
ccall(c, Cvoid, (
Ptr{Cvoid}, Ptr{Cvoid}, Csize_t, Ptr{UInt8},
Csize_t, Ptr{UInt8}, Ptr{UInt8}),
state.state, state.cipher_type.encrypt, sizeof(iiv), iiv,
sizeof(data), pointer(result), pointer(data))
Libdl.dlclose(hdl)
return result
end

Expand All @@ -249,19 +231,19 @@ function encrypt!(state::Encryptor, result, data)
if sizeof(data) % state.cipher_type.block_size > 0
throw(ArgumentError("Input array of length $(sizeof(data)) must be N times $(state.cipher_type.block_size) bytes long"))
end
ccall(state.cipher_type.encrypt, Void, (Ptr{Void},Csize_t,Ptr{UInt8},Ptr{UInt8}),
ccall(state.cipher_type.encrypt, Cvoid, (Ptr{Cvoid},Csize_t,Ptr{UInt8},Ptr{UInt8}),
state.state, sizeof(data), pointer(result), pointer(data))
return result
end

function encrypt(state::Encryptor, e::Symbol, iv::Vector{UInt8}, data)
result = Vector{UInt8}(sizeof(data))
result = Vector{UInt8}(undef, sizeof(data))
encrypt!(state, e, iv, result, data)
return result
end

function encrypt(state::Encryptor, data)
result = Vector{UInt8}(sizeof(data))
result = Vector{UInt8}(undef, sizeof(data))
encrypt!(state, result, data)
return result
end
Expand Down
12 changes: 6 additions & 6 deletions src/hash.jl
Expand Up @@ -4,7 +4,7 @@
import Base: show
export Hasher, update!, digest, digest!, hexdigest!, hexdigest

immutable Hasher
struct Hasher
hash_type::HashType
state::Vector{UInt8}
end
Expand All @@ -19,21 +19,21 @@ function Hasher(name::AbstractString)

# Construct Hasher object for this type and initialize using Nettle's init functions
hash_type = hash_types[name]
state = Vector{UInt8}(hash_type.context_size)
ccall(hash_type.init, Void, (Ptr{Void},), state)
state = Vector{UInt8}(undef, hash_type.context_size)
ccall(hash_type.init, Cvoid, (Ptr{Cvoid},), state)
return Hasher(hash_type, state)
end

# Update hash state with new data
function update!(state::Hasher, data)
ccall(state.hash_type.update, Void, (Ptr{Void},Csize_t,Ptr{UInt8}), state.state, sizeof(data), pointer(data))
ccall(state.hash_type.update, Cvoid, (Ptr{Cvoid},Csize_t,Ptr{UInt8}), state.state, sizeof(data), pointer(data))
return state
end

# Spit out a digest of the current hash state and reset it
function digest!(state::Hasher)
digest = Vector{UInt8}(state.hash_type.digest_size)
ccall(state.hash_type.digest, Void, (Ptr{Void},UInt32,Ptr{UInt8}), state.state, sizeof(digest), pointer(digest))
digest = Vector{UInt8}(undef, state.hash_type.digest_size)
ccall(state.hash_type.digest, Cvoid, (Ptr{Cvoid},UInt32,Ptr{UInt8}), state.state, sizeof(digest), pointer(digest))
return digest
end

Expand Down

0 comments on commit 04afab9

Please sign in to comment.