switch to NON CRYPTOGRAPHICALLY SECURE rand

master
nixo 2020-01-06 14:39:36 +01:00
parent db31dd1fc7
commit faecf1abf5
3 changed files with 25 additions and 13 deletions

View File

@ -7,8 +7,7 @@ const error = ccall((:olm_error, libolm), Csize_t, ())
struct OlmAccount
ptr::Ptr{Cvoid}
# Should I store it?
# memory::Vector{UInt}
memory::Vector{UInt8}
end
struct OlmSession
@ -26,10 +25,11 @@ export OlmAccount
# # Save/Load account!
# a = OlmAccount()
# generate_one_time_keys(a, 10)
# Olm.generate_one_time_keys(a, 10)
# # deleted after use
# enc_key = "pass" |> collect
# dec_key = deepcopy(enc_key)
# enc_key = dec_key = Char[]
# p = pickle!(a, enc_key)
# write("account.bin", p)

View File

@ -2,7 +2,8 @@
allocate(size) = zeros(UInt8, size)
const SRND = Random.RandomDevice()
"Allocate a criptographycally secure random UInt8 array of length size"
rallocate(size) = rand(SRND, UInt8, size)
rallocate(size) = rand(# SRND, # FIXME: Sometimes it freeze...
UInt8, size)
"""Replace all elements of an array with zeros. A custom function accepting
"type" may be passed as FUNC argument used instead of ZERO"""

View File

@ -5,7 +5,7 @@ account_size() = ccall((:olm_account_size, libolm), Csize_t, ())
The supplied memory must be at least olm_account_size() bytes"""
function account()
memory = allocate(account_size())
ccall((:olm_account, libolm), Ptr{Cvoid}, (Ptr{Cvoid},), memory)
(ccall((:olm_account, libolm), Ptr{Cvoid}, (Ptr{Cvoid},), memory), memory)
end
"The number of random bytes needed to create an account."
@ -26,7 +26,7 @@ function create(a::OlmAccount)
end
function OlmAccount()
a = OlmAccount(account())
a = OlmAccount(account()...)
create(a)
a
end
@ -37,7 +37,7 @@ used to encrypt the account then olm_account_last_error() will be
"BAD_ACCOUNT_KEY". If the base64 couldn't be decoded then
olm_account_last_error() will be "INVALID_BASE64". The input pickled buffer is
destroyed"""
function unpickle!(a::OlmAccount, pickle::Vector{Char}, passphrase::Vector{Char})
function unpickle!(a::OlmAccount, pickle::Vector{UInt8}, passphrase::Vector{UInt8})
memlength = pickle_length(a)
res = ccall((:olm_unpickle_account, libolm), Csize_t,
(Ptr{Cvoid},
@ -45,7 +45,7 @@ function unpickle!(a::OlmAccount, pickle::Vector{Char}, passphrase::Vector{Char}
Ptr{Cvoid}, Csize_t,),
a.ptr,
passphrase, length(passphrase),
collect(pickle), length(pickle))
pickle, length(pickle))
# If passphrase is empty, pickle is not encrypted, delete it.
# Else, deleting the key is fine
erase!(length(passphrase) == 0 ? passphrase : pickle, func = rand)
@ -58,10 +58,16 @@ end
"""Initialize a pickled account. Note htat passphrase is cleared after use.
"""
function OlmAccount(pickle::Vector{Char}, passphrase::Vector{Char})
a = OlmAccount(account())
function OlmAccount(pickle::Vector{UInt8}, passphrase::Vector{UInt8})
a = OlmAccount(account()...)
unpickle!(a, pickle, passphrase)
end
OlmAccount(pickle::Vector{UInt8}) = OlmAccount(pickle, Char[])
function OlmAccount(pickle::Vector{UInt8}, b::Base.SecretBuffer)
res = OlmAccount(pickle, b.data)
Base.shred!(b)
res
end
"""A null terminated string describing the most recent error to happen to an
account"""
@ -88,7 +94,7 @@ supplied key. Returns the length of the pickled account on success.
Returns olm_error() on failure. If the pickle output buffer
is smaller than olm_pickle_account_length() then olm_account_last_error()
will be "OUTPUT_BUFFER_TOO_SMALL"."""
function pickle!(a::OlmAccount, passphrase::Vector{Char})
function pickle!(a::OlmAccount, passphrase::Vector{UInt8})
memlength = pickle_length(a)
memory = allocate(memlength)
res = ccall((:olm_pickle_account, libolm), Csize_t,
@ -104,11 +110,16 @@ function pickle!(a::OlmAccount, passphrase::Vector{Char})
if res == memlength
memory
else
# TODO: Custom exceptions
throw(last_error(a))
end
end
pickle!(a::OlmAccount) = pickle!(a, Char[])
pickle!(a::OlmAccount) = pickle!(a, UInt8[])
function pickle!(a::OlmAccount, s::Base.SecretBuffer)
res = pickle!(a, s.data)
Base.shred!(s)
res
end
# Base.getpass("Account encryption key")
"The size of the output buffer needed to hold the identity keys"