From 6d4a6739064c4e44e2a2d40ea557b5eeb2307de8 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sat, 13 Feb 2016 11:20:30 +0900 Subject: [PATCH 01/12] add test/runtests.jl --- REQUIRE | 1 + src/OpenSSL.jl | 47 ++++++++++++++++++++++++----------------------- test/runtests.jl | 4 ++++ test/test.jl | 9 +++++---- 4 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 test/runtests.jl diff --git a/REQUIRE b/REQUIRE index e69de29..69345da 100644 --- a/REQUIRE +++ b/REQUIRE @@ -0,0 +1 @@ +julia 0.4- diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index 0a3a1cd..50701af 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -1,28 +1,29 @@ module OpenSSL import Base - - const LIBCRYPTO = "libcrypto" - + +# const LIBCRYPTO = "libcrypto" + const LIBCRYPTO = "libeay32" + module Digest import OpenSSL import Base.ccall - + function init() ccall((:OpenSSL_add_all_digests, OpenSSL.LIBCRYPTO), Void, ()) end function cleanup() ccall((:EVP_cleanup, OpenSSL.LIBCRYPTO), Void, ()) end - - function hexstring(hexes::Array{Uint8,1}) + + function hexstring(hexes::Array{UInt8,1}) join([hex(h,2) for h in hexes], "") end - - function digest(name::String, data::String) + + function digest(name::AbstractString, data::AbstractString) ctx = ccall((:EVP_MD_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) try # Get the message digest struct - md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{Uint8},), bytestring(name)) + md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), bytestring(name)) if(md == C_NULL) error("Unknown message digest $name") end @@ -30,12 +31,12 @@ module OpenSSL ccall((:EVP_DigestInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), ctx, md, C_NULL) # Update the context with the input data bs = bytestring(data) - ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Uint8}, Uint), ctx, bs, length(bs)) + ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, length(bs)) # Figure out the size of the output string for the digest - size = ccall((:EVP_MD_size, OpenSSL.LIBCRYPTO), Uint, (Ptr{Void},), md) - uval = Array(Uint8, size) + size = ccall((:EVP_MD_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), md) + uval = Array(UInt8, size) # Calculate the digest and store it in the uval array - ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Uint8}, Ptr{Uint}), ctx, uval, C_NULL) + ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, uval, C_NULL) # bytestring(uval) # Convert the uval array to a string of hexes return hexstring(uval) @@ -44,11 +45,11 @@ module OpenSSL end end#/digest - function digestinit(name::String) + function digestinit(name::AbstractString) ctx = ccall((:EVP_MD_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) try # Get the message digest struct - md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{Uint8},), bytestring(name)) + md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), bytestring(name)) if(md == C_NULL) error("Unknown message digest $name") end @@ -62,29 +63,29 @@ module OpenSSL end end#/digest - function digestupdate(ctx,data::String) + function digestupdate(ctx,data::AbstractString) try # Update the context with the input data bs = bytestring(data) - ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Uint8}, Uint), ctx, bs, length(bs)) + ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, length(bs)) ctx catch ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) nothing end end#/digest - + function digestfinalize(ctx) try # Get the message digest struct - md = ccall((:EVP_MD_CTX_md, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{Uint8},), ctx) + md = ccall((:EVP_MD_CTX_md, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), ctx) if(md == C_NULL) error("Unknown message digest $name") end - size = ccall((:EVP_MD_size, OpenSSL.LIBCRYPTO), Uint, (Ptr{Void},), md) - uval = Array(Uint8, size) + size = ccall((:EVP_MD_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), md) + uval = Array(UInt8, size) # Calculate the digest and store it in the uval array - ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Uint8}, Ptr{Uint}), ctx, uval, C_NULL) + ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, uval, C_NULL) # bytestring(uval) # Convert the uval array to a string of hexes return hexstring(uval) @@ -94,5 +95,5 @@ module OpenSSL end#/digest end#/Digest - + end#/OpenSSL diff --git a/test/runtests.jl b/test/runtests.jl new file mode 100644 index 0000000..06f888c --- /dev/null +++ b/test/runtests.jl @@ -0,0 +1,4 @@ +using OpenSSL +using Base.Test + +include("test.jl") diff --git a/test/test.jl b/test/test.jl index 89f4342..e47cdf0 100644 --- a/test/test.jl +++ b/test/test.jl @@ -1,12 +1,13 @@ -require("OpenSSL") +import OpenSSL +using Base.Test include("data.jl") -assert(isdefined(:OpenSSL)) +@test isdefined(:OpenSSL) == true OpenSSL.Digest.init() -assert(OpenSSL.Digest.digest("SHA512", "test") == sha512_of_test) -assert(OpenSSL.Digest.digest("MD5", "test") == md5_of_test) +@test OpenSSL.Digest.digest("SHA512", "test") == sha512_of_test +@test OpenSSL.Digest.digest("MD5", "test") == md5_of_test OpenSSL.Digest.cleanup() println("All tests passed") From 19e06846f61c7500c6a1f35c129bb8c57886f5dc Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sat, 13 Feb 2016 11:34:34 +0900 Subject: [PATCH 02/12] modify README.md and src/OpenSSL.jl --- README.md | 29 +++++++++++++++++++++++++++++ src/OpenSSL.jl | 3 +-- test/test.jl | 9 +++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e69de29..abac46a 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,29 @@ +OpenSSL +========== + +fork of [OpenSSL](https://github.com/dirk/OpenSSL.jl) + +convert for julia 0.4- + + +# before use it + +```julia +# src/OpenSSL.jl + +# select which library on your environment +# const LIBCRYPTO = "libcrypto" + const LIBCRYPTO = "libeay32" +``` + + +# see also + +[AES256CBC](https://github.com/HatsuneMiku/AES256CBC.jl) + + +# status + +[![Build Status _dev_aes256cbc](https://travis-ci.org/HatsuneMiku/OpenSSL.jl.svg?branch=_dev_aes256cbc)](https://travis-ci.org/HatsuneMiku/OpenSSL.jl) + +[![Build Status master](https://travis-ci.org/HatsuneMiku/OpenSSL.jl.svg?branch=master)](https://travis-ci.org/HatsuneMiku/OpenSSL.jl) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index 50701af..a2b2d80 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -1,12 +1,11 @@ module OpenSSL - import Base +# select which library on your environment # const LIBCRYPTO = "libcrypto" const LIBCRYPTO = "libeay32" module Digest import OpenSSL - import Base.ccall function init() ccall((:OpenSSL_add_all_digests, OpenSSL.LIBCRYPTO), Void, ()) diff --git a/test/test.jl b/test/test.jl index e47cdf0..1ba6730 100644 --- a/test/test.jl +++ b/test/test.jl @@ -6,8 +6,13 @@ include("data.jl") @test isdefined(:OpenSSL) == true OpenSSL.Digest.init() -@test OpenSSL.Digest.digest("SHA512", "test") == sha512_of_test -@test OpenSSL.Digest.digest("MD5", "test") == md5_of_test +s = OpenSSL.Digest.digest("SHA512", "test") +println(s) +@test s == sha512_of_test + +m = OpenSSL.Digest.digest("MD5", "test") +println(m) +@test m == md5_of_test OpenSSL.Digest.cleanup() println("All tests passed") From c644604f7e5ca72460e6e193c9b7f52e1beedca6 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sat, 13 Feb 2016 17:09:26 +0900 Subject: [PATCH 03/12] select library --- README.md | 11 ++++++----- src/OpenSSL.jl | 22 ++++++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index abac46a..724b8fb 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,15 @@ fork of [OpenSSL](https://github.com/dirk/OpenSSL.jl) convert for julia 0.4- -# before use it +# how to use it ```julia -# src/OpenSSL.jl +import OpenSSL -# select which library on your environment -# const LIBCRYPTO = "libcrypto" - const LIBCRYPTO = "libeay32" +OpenSSL.Digest.init() +s = OpenSSL.Digest.digest("SHA512", "test") +m = OpenSSL.Digest.digest("MD5", "test") +OpenSSL.Digest.cleanup() ``` diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index a2b2d80..0d64443 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -1,8 +1,9 @@ +# OpenSSL + +VERSION >= v"0.4.0-dev+6521" && __precompile__() module OpenSSL -# select which library on your environment -# const LIBCRYPTO = "libcrypto" - const LIBCRYPTO = "libeay32" + const LIBCRYPTO = ENV["OS"] == "Windows_NT" ? "libeay32" : "libcrypto" module Digest import OpenSSL @@ -10,14 +11,11 @@ module OpenSSL function init() ccall((:OpenSSL_add_all_digests, OpenSSL.LIBCRYPTO), Void, ()) end + function cleanup() ccall((:EVP_cleanup, OpenSSL.LIBCRYPTO), Void, ()) end - function hexstring(hexes::Array{UInt8,1}) - join([hex(h,2) for h in hexes], "") - end - function digest(name::AbstractString, data::AbstractString) ctx = ccall((:EVP_MD_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) try @@ -38,7 +36,7 @@ module OpenSSL ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, uval, C_NULL) # bytestring(uval) # Convert the uval array to a string of hexes - return hexstring(uval) + return bytes2hex(uval) finally ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) end @@ -60,7 +58,7 @@ module OpenSSL ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) nothing end - end#/digest + end#/digestinit function digestupdate(ctx,data::AbstractString) try @@ -72,7 +70,7 @@ module OpenSSL ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) nothing end - end#/digest + end#/digestupdate function digestfinalize(ctx) try @@ -87,11 +85,11 @@ module OpenSSL ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, uval, C_NULL) # bytestring(uval) # Convert the uval array to a string of hexes - return hexstring(uval) + return bytes2hex(uval) finally ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) end - end#/digest + end#/digestfinalize end#/Digest From 9c0e27a54dd5c73f99964041108b33dd4daf1382 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sat, 13 Feb 2016 21:40:34 +0900 Subject: [PATCH 04/12] fix big bug : must not use bytestring() --- src/OpenSSL.jl | 18 ++++++------------ test/data.jl | 5 +++-- test/test.jl | 13 +++++++++++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index 0d64443..7f10e15 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -16,7 +16,7 @@ module OpenSSL ccall((:EVP_cleanup, OpenSSL.LIBCRYPTO), Void, ()) end - function digest(name::AbstractString, data::AbstractString) + function digest(name::AbstractString, bs::Array{UInt8,1}) ctx = ccall((:EVP_MD_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) try # Get the message digest struct @@ -26,17 +26,14 @@ module OpenSSL end # Add the digest struct to the context ccall((:EVP_DigestInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), ctx, md, C_NULL) - # Update the context with the input data - bs = bytestring(data) + # Update the context with the input data : bs ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, length(bs)) # Figure out the size of the output string for the digest size = ccall((:EVP_MD_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), md) uval = Array(UInt8, size) # Calculate the digest and store it in the uval array ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, uval, C_NULL) - # bytestring(uval) - # Convert the uval array to a string of hexes - return bytes2hex(uval) + return uval finally ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) end @@ -60,10 +57,9 @@ module OpenSSL end end#/digestinit - function digestupdate(ctx,data::AbstractString) + function digestupdate(ctx, bs::Array{UInt8,1}) try - # Update the context with the input data - bs = bytestring(data) + # Update the context with the input data : bs ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, length(bs)) ctx catch @@ -83,9 +79,7 @@ module OpenSSL uval = Array(UInt8, size) # Calculate the digest and store it in the uval array ccall((:EVP_DigestFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, uval, C_NULL) - # bytestring(uval) - # Convert the uval array to a string of hexes - return bytes2hex(uval) + return uval finally ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) end diff --git a/test/data.jl b/test/data.jl index f5b1036..c508ad5 100644 --- a/test/data.jl +++ b/test/data.jl @@ -1,2 +1,3 @@ -sha512_of_test = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff" -md5_of_test = "098f6bcd4621d373cade4e832627b4f6" +sha512_of_test = hex2bytes("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff") +md5_of_test = hex2bytes("098f6bcd4621d373cade4e832627b4f6") +md5_of_bytes = hex2bytes("e299ff9d8e4831f07e5323913c53e5f0") diff --git a/test/test.jl b/test/test.jl index 1ba6730..a5bac7f 100644 --- a/test/test.jl +++ b/test/test.jl @@ -3,16 +3,25 @@ using Base.Test include("data.jl") +function string2bytes(s::AbstractString) + return read(IOBuffer(s), UInt8, length(s)) +end + @test isdefined(:OpenSSL) == true OpenSSL.Digest.init() -s = OpenSSL.Digest.digest("SHA512", "test") +s = OpenSSL.Digest.digest("SHA512", string2bytes("test")) println(s) @test s == sha512_of_test -m = OpenSSL.Digest.digest("MD5", "test") +m = OpenSSL.Digest.digest("MD5", string2bytes("test")) println(m) @test m == md5_of_test +h = OpenSSL.Digest.digest("MD5", + hex2bytes("5365637265742050617373706872617365a3e550e89e70996c")) +println(h) +@test h == md5_of_bytes + OpenSSL.Digest.cleanup() println("All tests passed") From 6f10beb6c2cfd21188b17785c0017600f488f24b Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sun, 14 Feb 2016 09:53:34 +0900 Subject: [PATCH 05/12] add module OpenSSL.Cipher --- src/OpenSSL.jl | 73 ++++++++++++++++++++++++++++++++++++++++++++++++-- test/data.jl | 2 ++ test/test.jl | 18 +++++++++++-- 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index 7f10e15..acbce19 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -5,6 +5,18 @@ module OpenSSL const LIBCRYPTO = ENV["OS"] == "Windows_NT" ? "libeay32" : "libcrypto" + function init() + # ccall((:OpenSSL_add_all_digests, OpenSSL.LIBCRYPTO), Void, ()) + # ccall((:OpenSSL_add_all_ciphers, OpenSSL.LIBCRYPTO), Void, ()) + # ccall((:OPENSSL_add_all_algorithms_conf, OpenSSL.LIBCRYPTO), Void, ()) + ccall((:OPENSSL_add_all_algorithms_noconf, OpenSSL.LIBCRYPTO), Void, ()) + # alias OpenSSL_add_all_algorithms :OPENSSL_add_all_algorithms_noconf + end + + function cleanup() + ccall((:EVP_cleanup, OpenSSL.LIBCRYPTO), Void, ()) + end + module Digest import OpenSSL @@ -20,7 +32,7 @@ module OpenSSL ctx = ccall((:EVP_MD_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) try # Get the message digest struct - md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), bytestring(name)) + md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), name) if(md == C_NULL) error("Unknown message digest $name") end @@ -43,7 +55,7 @@ module OpenSSL ctx = ccall((:EVP_MD_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) try # Get the message digest struct - md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), bytestring(name)) + md = ccall((:EVP_get_digestbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), name) if(md == C_NULL) error("Unknown message digest $name") end @@ -87,4 +99,61 @@ module OpenSSL end#/Digest + module Cipher + import OpenSSL + + function init() + ccall((:OpenSSL_add_all_ciphers, OpenSSL.LIBCRYPTO), Void, ()) + end + + function cleanup() + ccall((:EVP_cleanup, OpenSSL.LIBCRYPTO), Void, ()) + end + + function encrypt(name::AbstractString, key::Array{UInt8,1}, iv::Array{UInt8,1}, plain::Array{UInt8,1}) + ctx = ccall((:EVP_CIPHER_CTX_new, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + ccall((:EVP_CIPHER_CTX_init, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) + try + enc = Array{UInt8,1}([]) + # ec = ccall((:EVP_get_cipherbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), name) + algorithm = Symbol("EVP_"*name) + if(algorithm != :EVP_aes_256_cbc) + error("Not support cipher algorithm $name") + end + ec = ccall((:EVP_aes_256_cbc, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + if(ec == C_NULL) + error("Unknown cipher algorithm $name") + end + ccall((:EVP_EncryptInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}), ctx, ec, C_NULL, key, iv) + + tmpenc = Array{UInt8,1}([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) + tmplen = UInt(0) + blksize = ccall((:EVP_CIPHER_CTX_block_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), ctx) + + ccall((:EVP_EncryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}, Ptr{UInt8}, UInt), ctx, tmpenc, &tmplen, plain, blksize) + enc = [enc; tmpenc] + + remain = 0 + if(length(plain) > 16) # if(remain > 0) + ccall((:EVP_EncryptFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, tmpenc, &tmplen) + enc = [enc; tmpenc] + end + + ccall((:EVP_CIPHER_CTX_cleanup, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) + return enc + finally + ccall((:EVP_CIPHER_CTX_free, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) + end + end#/encrypt + + function decrypt(name::AbstractString, key::Array{UInt8,1}, iv::Array{UInt8,1}, bs::Array{UInt8,1}) + ctx = ccall((:EVP_CIPHER_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + try + finally + ccall((:EVP_CIPHER_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) + end + end#/decrypt + + end#/Cipher + end#/OpenSSL diff --git a/test/data.jl b/test/data.jl index c508ad5..b2f2390 100644 --- a/test/data.jl +++ b/test/data.jl @@ -1,3 +1,5 @@ sha512_of_test = hex2bytes("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff") md5_of_test = hex2bytes("098f6bcd4621d373cade4e832627b4f6") md5_of_bytes = hex2bytes("e299ff9d8e4831f07e5323913c53e5f0") +aes256cbc_of_shortdata = hex2bytes("da8aab1b904205a7e49c1ecc7118a8f4") +aes256cbc_of_longdata = hex2bytes("da8aab1b904205a7e49c1ecc7118a8f4804bef7be79216196739de7845da182d") diff --git a/test/test.jl b/test/test.jl index a5bac7f..c50c92c 100644 --- a/test/test.jl +++ b/test/test.jl @@ -8,7 +8,7 @@ function string2bytes(s::AbstractString) end @test isdefined(:OpenSSL) == true -OpenSSL.Digest.init() +OpenSSL.init() s = OpenSSL.Digest.digest("SHA512", string2bytes("test")) println(s) @@ -23,5 +23,19 @@ h = OpenSSL.Digest.digest("MD5", println(h) @test h == md5_of_bytes -OpenSSL.Digest.cleanup() +es = OpenSSL.Cipher.encrypt("aes_256_cbc", + hex2bytes("e299ff9d8e4831f07e5323913c53e5f0fec3a040a211d6562fa47607244d0051"), + hex2bytes("7c7ed9434ddb9c2d1e1fcc38b4bf4667"), + string2bytes("Message\t\t\t\t\t\t\t\t\t")) +println(es) +@test es == aes256cbc_of_shortdata + +el = OpenSSL.Cipher.encrypt("aes_256_cbc", + hex2bytes("e299ff9d8e4831f07e5323913c53e5f0fec3a040a211d6562fa47607244d0051"), + hex2bytes("7c7ed9434ddb9c2d1e1fcc38b4bf4667"), + string2bytes("Message\t\t\t\t\t\t\t\t\t\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10")) +println(el) +@test el == aes256cbc_of_longdata + +OpenSSL.cleanup() println("All tests passed") From b363e57669fdb10754e4d56f804d720a4ece8793 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sun, 14 Feb 2016 10:17:25 +0900 Subject: [PATCH 06/12] modify OpenSSL.Cipher.encrypt --- src/OpenSSL.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index acbce19..e460ee7 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -120,13 +120,14 @@ module OpenSSL if(algorithm != :EVP_aes_256_cbc) error("Not support cipher algorithm $name") end + # ec = ccall((algorithm, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) ec = ccall((:EVP_aes_256_cbc, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) if(ec == C_NULL) error("Unknown cipher algorithm $name") end ccall((:EVP_EncryptInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}), ctx, ec, C_NULL, key, iv) - tmpenc = Array{UInt8,1}([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]) + tmpenc = Array(UInt8, 16) tmplen = UInt(0) blksize = ccall((:EVP_CIPHER_CTX_block_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), ctx) From 081cf8692685cc6690171d8b5fe7da7c9322323e Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sun, 14 Feb 2016 16:37:12 +0900 Subject: [PATCH 07/12] modify OpenSSL.Cipher.decrypt selfpad=false default padding PKCS#5 --- src/OpenSSL.jl | 102 ++++++++++++++++++++++++++++++++++++++----------- test/test.jl | 31 +++++++++++---- 2 files changed, 102 insertions(+), 31 deletions(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index e460ee7..f3fbc30 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -110,34 +110,61 @@ module OpenSSL ccall((:EVP_cleanup, OpenSSL.LIBCRYPTO), Void, ()) end - function encrypt(name::AbstractString, key::Array{UInt8,1}, iv::Array{UInt8,1}, plain::Array{UInt8,1}) + function get_EVP_CIPHER(name::AbstractString) + # ec = ccall((:EVP_get_cipherbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), name) + algorithm = Symbol("EVP_"*name) + if(algorithm != :EVP_aes_256_cbc) + error("Not support cipher algorithm $name") + end + # ec = ccall((algorithm, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + ec = ccall((:EVP_aes_256_cbc, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + if(ec == C_NULL) + error("Unknown cipher algorithm $name") + end + return ec + end + + function encrypt(name::AbstractString, key::Array{UInt8,1}, iv::Array{UInt8,1}, plain::Array{UInt8,1}, selfpad::Bool=false) ctx = ccall((:EVP_CIPHER_CTX_new, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) ccall((:EVP_CIPHER_CTX_init, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) try - enc = Array{UInt8,1}([]) - # ec = ccall((:EVP_get_cipherbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), name) - algorithm = Symbol("EVP_"*name) - if(algorithm != :EVP_aes_256_cbc) - error("Not support cipher algorithm $name") - end - # ec = ccall((algorithm, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) - ec = ccall((:EVP_aes_256_cbc, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) - if(ec == C_NULL) - error("Unknown cipher algorithm $name") - end + ec = get_EVP_CIPHER(name) ccall((:EVP_EncryptInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}), ctx, ec, C_NULL, key, iv) - tmpenc = Array(UInt8, 16) - tmplen = UInt(0) + # :EVP_CIPHER_CTX_block_size must be after :EVP_EncryptInit_ex blksize = ccall((:EVP_CIPHER_CTX_block_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), ctx) + if(selfpad) + ccall((:EVP_CIPHER_CTX_set_padding, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void}, UInt), ctx, 0) # disable + end + remain = length(plain) % blksize + padlen = blksize - remain + enclen = length(plain) + padlen + enc = Array(UInt8, enclen) + outlen = UInt(1) # start position = 1 + tmpenc = Array(UInt8, blksize) + tmplen = Ref{Cint}(0) + + for i in 1:div(length(plain), blksize) + ccall((:EVP_EncryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}, Ptr{UInt8}, UInt), ctx, tmpenc, tmplen, plain[outlen:outlen+blksize-1], blksize) + if(tmplen[] > 0) enc[outlen:outlen+blksize-1] = tmpenc[1:blksize] end + outlen += tmplen[] + end - ccall((:EVP_EncryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}, Ptr{UInt8}, UInt), ctx, tmpenc, &tmplen, plain, blksize) - enc = [enc; tmpenc] + if(remain > 0) + ccall((:EVP_EncryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}, Ptr{UInt8}, UInt), ctx, tmpenc, tmplen, plain[outlen:outlen+remain-1], remain) + if(tmplen[] > 0) enc[outlen:outlen+remain-1] = tmpenc[1:remain] end + outlen += tmplen[] + end + + if(selfpad && padlen != 0) # no use (padlen > 0), UInt is everytime > 0 + # skip + # outlen += tmplen[] + end - remain = 0 - if(length(plain) > 16) # if(remain > 0) - ccall((:EVP_EncryptFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt}), ctx, tmpenc, &tmplen) - enc = [enc; tmpenc] + if(!selfpad) + ccall((:EVP_EncryptFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}), ctx, tmpenc, tmplen) + if(tmplen[] > 0) enc[outlen:outlen+blksize-1] = tmpenc[1:blksize] end + outlen += tmplen[] end ccall((:EVP_CIPHER_CTX_cleanup, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) @@ -147,11 +174,40 @@ module OpenSSL end end#/encrypt - function decrypt(name::AbstractString, key::Array{UInt8,1}, iv::Array{UInt8,1}, bs::Array{UInt8,1}) - ctx = ccall((:EVP_CIPHER_CTX_create, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + function decrypt(name::AbstractString, key::Array{UInt8,1}, iv::Array{UInt8,1}, cipher::Array{UInt8,1}, selfpad::Bool=false) + ctx = ccall((:EVP_CIPHER_CTX_new, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + ccall((:EVP_CIPHER_CTX_init, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) try + ec = get_EVP_CIPHER(name) + ccall((:EVP_DecryptInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}), ctx, ec, C_NULL, key, iv) + + # :EVP_CIPHER_CTX_block_size must be after :EVP_DecryptInit_ex + blksize = ccall((:EVP_CIPHER_CTX_block_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), ctx) + if(selfpad) + ccall((:EVP_CIPHER_CTX_set_padding, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void}, UInt), ctx, 0) # disable + end + declen = length(cipher) # trim padlen later + dec = Array(UInt8, declen) + outlen = UInt(1) # start position = 1 + tmpdec = Array(UInt8, blksize) + tmplen = Ref{Cint}(0) + + for i in 1:div(length(cipher), blksize) + ccall((:EVP_DecryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}, Ptr{UInt8}, UInt), ctx, tmpdec, tmplen, cipher[outlen:outlen+blksize-1], blksize) + if(tmplen[] > 0) dec[outlen:outlen+blksize-1] = tmpdec[1:blksize] end + outlen += tmplen[] + end + + if(!selfpad) + ccall((:EVP_DecryptFinal_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}), ctx, tmpdec, tmplen) + if(tmplen[] > 0) dec[outlen:outlen+blksize-1] = tmpdec[1:blksize] end + outlen += tmplen[] + end + + ccall((:EVP_CIPHER_CTX_cleanup, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) + return selfpad ? dec[1:declen-dec[declen]] : dec[1:outlen-1] finally - ccall((:EVP_CIPHER_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) + ccall((:EVP_CIPHER_CTX_free, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) end end#/decrypt diff --git a/test/test.jl b/test/test.jl index c50c92c..c02ba30 100644 --- a/test/test.jl +++ b/test/test.jl @@ -23,19 +23,34 @@ h = OpenSSL.Digest.digest("MD5", println(h) @test h == md5_of_bytes -es = OpenSSL.Cipher.encrypt("aes_256_cbc", - hex2bytes("e299ff9d8e4831f07e5323913c53e5f0fec3a040a211d6562fa47607244d0051"), - hex2bytes("7c7ed9434ddb9c2d1e1fcc38b4bf4667"), - string2bytes("Message\t\t\t\t\t\t\t\t\t")) +key32 = hex2bytes("e299ff9d8e4831f07e5323913c53e5f0"* + "fec3a040a211d6562fa47607244d0051") +iv16 = hex2bytes("7c7ed9434ddb9c2d1e1fcc38b4bf4667") + +### selfpad=true +#plainshort = string2bytes("Message\t\t\t\t\t\t\t\t\t") +#plainlong = string2bytes("Message\t\t\t\t\t\t\t\t\t"* +# "\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10") + +### selfpad=false +plainshort = string2bytes("Message") +plainlong = string2bytes("Message\t\t\t\t\t\t\t\t\t") + +es = OpenSSL.Cipher.encrypt("aes_256_cbc", key32, iv16, plainshort) println(es) @test es == aes256cbc_of_shortdata -el = OpenSSL.Cipher.encrypt("aes_256_cbc", - hex2bytes("e299ff9d8e4831f07e5323913c53e5f0fec3a040a211d6562fa47607244d0051"), - hex2bytes("7c7ed9434ddb9c2d1e1fcc38b4bf4667"), - string2bytes("Message\t\t\t\t\t\t\t\t\t\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10")) +ds = OpenSSL.Cipher.decrypt("aes_256_cbc", key32, iv16, es) +println(ds) +@test ds == plainshort + +el = OpenSSL.Cipher.encrypt("aes_256_cbc", key32, iv16, plainlong) println(el) @test el == aes256cbc_of_longdata +dl = OpenSSL.Cipher.decrypt("aes_256_cbc", key32, iv16, el) +println(dl) +@test dl == plainlong + OpenSSL.cleanup() println("All tests passed") From 7ced7642d9c7e9933a7bb74481b9d1bad9707ab9 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Sun, 14 Feb 2016 17:01:50 +0900 Subject: [PATCH 08/12] modify README.md --- README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 724b8fb..9370599 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,10 @@ convert for julia 0.4- ```julia import OpenSSL -OpenSSL.Digest.init() +OpenSSL.init() s = OpenSSL.Digest.digest("SHA512", "test") m = OpenSSL.Digest.digest("MD5", "test") -OpenSSL.Digest.cleanup() +OpenSSL.cleanup() ``` @@ -22,6 +22,18 @@ OpenSSL.Digest.cleanup() [AES256CBC](https://github.com/HatsuneMiku/AES256CBC.jl) +```julia +# AES256CBC encrypt/decrypt +using AES256CBC +# typealias UBytes Array{UInt8, 1} +plain = string2bytes("Message") # UBytes +passwd = string2bytes("Secret Passphrase") # UBytes +salt = genRandUBytes(8) # UBytes +(key32, iv16) = genKey32Iv16(passwd, salt) # (UBytes, UBytes) +encoded = encryptAES256CBC(key32, iv16, plain) # UBytes +decoded = decryptAES256CBC(key32, iv16, encoded) # UBytes +``` + # status From 33b8f1604f58c9bf330bd6edbca442f0eaf83118 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Mon, 15 Feb 2016 10:20:01 +0900 Subject: [PATCH 09/12] remove string2bytes --- test/test.jl | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/test.jl b/test/test.jl index c02ba30..d385421 100644 --- a/test/test.jl +++ b/test/test.jl @@ -3,18 +3,14 @@ using Base.Test include("data.jl") -function string2bytes(s::AbstractString) - return read(IOBuffer(s), UInt8, length(s)) -end - @test isdefined(:OpenSSL) == true OpenSSL.init() -s = OpenSSL.Digest.digest("SHA512", string2bytes("test")) +s = OpenSSL.Digest.digest("SHA512", "test".data) println(s) @test s == sha512_of_test -m = OpenSSL.Digest.digest("MD5", string2bytes("test")) +m = OpenSSL.Digest.digest("MD5", "test".data) println(m) @test m == md5_of_test @@ -28,13 +24,13 @@ key32 = hex2bytes("e299ff9d8e4831f07e5323913c53e5f0"* iv16 = hex2bytes("7c7ed9434ddb9c2d1e1fcc38b4bf4667") ### selfpad=true -#plainshort = string2bytes("Message\t\t\t\t\t\t\t\t\t") -#plainlong = string2bytes("Message\t\t\t\t\t\t\t\t\t"* -# "\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10") +#plainshort = "Message\t\t\t\t\t\t\t\t\t".data +#plainlong = "Message\t\t\t\t\t\t\t\t\t"* +# "\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10\x10".data ### selfpad=false -plainshort = string2bytes("Message") -plainlong = string2bytes("Message\t\t\t\t\t\t\t\t\t") +plainshort = "Message".data +plainlong = "Message\t\t\t\t\t\t\t\t\t".data es = OpenSSL.Cipher.encrypt("aes_256_cbc", key32, iv16, plainshort) println(es) From 1dc6fbfa6e88036a864d8f2366b9af05e935e5df Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Tue, 16 Feb 2016 04:41:16 +0900 Subject: [PATCH 10/12] not use ENV[OS] --- src/OpenSSL.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index f3fbc30..cfd6ce1 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -3,7 +3,7 @@ VERSION >= v"0.4.0-dev+6521" && __precompile__() module OpenSSL - const LIBCRYPTO = ENV["OS"] == "Windows_NT" ? "libeay32" : "libcrypto" + const LIBCRYPTO = @windows ? "libeay32" : "libcrypto" function init() # ccall((:OpenSSL_add_all_digests, OpenSSL.LIBCRYPTO), Void, ()) From 97cde2c9cab6381109d795b3242675acf227bf20 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Thu, 18 Feb 2016 19:54:05 +0900 Subject: [PATCH 11/12] had better use 'sizeof()' instead of 'length()' --- src/OpenSSL.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index cfd6ce1..d93fc61 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -39,7 +39,7 @@ module OpenSSL # Add the digest struct to the context ccall((:EVP_DigestInit_ex, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), ctx, md, C_NULL) # Update the context with the input data : bs - ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, length(bs)) + ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, sizeof(bs)) # Figure out the size of the output string for the digest size = ccall((:EVP_MD_size, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void},), md) uval = Array(UInt8, size) @@ -72,7 +72,7 @@ module OpenSSL function digestupdate(ctx, bs::Array{UInt8,1}) try # Update the context with the input data : bs - ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, length(bs)) + ccall((:EVP_DigestUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, UInt), ctx, bs, sizeof(bs)) ctx catch ccall((:EVP_MD_CTX_destroy, OpenSSL.LIBCRYPTO), Void, (Ptr{Void},), ctx) @@ -136,15 +136,15 @@ module OpenSSL if(selfpad) ccall((:EVP_CIPHER_CTX_set_padding, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void}, UInt), ctx, 0) # disable end - remain = length(plain) % blksize + remain = sizeof(plain) % blksize padlen = blksize - remain - enclen = length(plain) + padlen + enclen = sizeof(plain) + padlen enc = Array(UInt8, enclen) outlen = UInt(1) # start position = 1 tmpenc = Array(UInt8, blksize) tmplen = Ref{Cint}(0) - for i in 1:div(length(plain), blksize) + for i in 1:div(sizeof(plain), blksize) ccall((:EVP_EncryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}, Ptr{UInt8}, UInt), ctx, tmpenc, tmplen, plain[outlen:outlen+blksize-1], blksize) if(tmplen[] > 0) enc[outlen:outlen+blksize-1] = tmpenc[1:blksize] end outlen += tmplen[] @@ -186,13 +186,13 @@ module OpenSSL if(selfpad) ccall((:EVP_CIPHER_CTX_set_padding, OpenSSL.LIBCRYPTO), UInt, (Ptr{Void}, UInt), ctx, 0) # disable end - declen = length(cipher) # trim padlen later + declen = sizeof(cipher) # trim padlen later dec = Array(UInt8, declen) outlen = UInt(1) # start position = 1 tmpdec = Array(UInt8, blksize) tmplen = Ref{Cint}(0) - for i in 1:div(length(cipher), blksize) + for i in 1:div(sizeof(cipher), blksize) ccall((:EVP_DecryptUpdate, OpenSSL.LIBCRYPTO), Void, (Ptr{Void}, Ptr{UInt8}, Ref{Cint}, Ptr{UInt8}, UInt), ctx, tmpdec, tmplen, cipher[outlen:outlen+blksize-1], blksize) if(tmplen[] > 0) dec[outlen:outlen+blksize-1] = tmpdec[1:blksize] end outlen += tmplen[] From b0eab08aedeb1b529c4f55bf6953b5bdcaa80053 Mon Sep 17 00:00:00 2001 From: Hatsune Miku <999Hatsune@gmail.com> Date: Fri, 19 Feb 2016 15:44:59 +0900 Subject: [PATCH 12/12] get c function EVP_algorithm by name --- src/OpenSSL.jl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/OpenSSL.jl b/src/OpenSSL.jl index d93fc61..c023a41 100644 --- a/src/OpenSSL.jl +++ b/src/OpenSSL.jl @@ -112,12 +112,9 @@ module OpenSSL function get_EVP_CIPHER(name::AbstractString) # ec = ccall((:EVP_get_cipherbyname, OpenSSL.LIBCRYPTO), Ptr{Void}, (Ptr{UInt8},), name) - algorithm = Symbol("EVP_"*name) - if(algorithm != :EVP_aes_256_cbc) - error("Not support cipher algorithm $name") - end - # ec = ccall((algorithm, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) - ec = ccall((:EVP_aes_256_cbc, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) + algorithm = "ccall((:EVP_$(name), OpenSSL.LIBCRYPTO), Ptr{Void}, ())" + ec = eval(parse(algorithm)) + # ec = ccall((:EVP_aes_256_cbc, OpenSSL.LIBCRYPTO), Ptr{Void}, ()) if(ec == C_NULL) error("Unknown cipher algorithm $name") end