2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-10-01 04:14:45 +02:00
|
|
|
|
|
|
|
#include <sodium.h>
|
|
|
|
|
2019-06-13 21:36:17 +02:00
|
|
|
namespace ircd::nacl
|
|
|
|
{
|
|
|
|
struct throw_on_error;
|
2017-10-01 04:14:45 +02:00
|
|
|
|
2019-06-13 21:36:17 +02:00
|
|
|
static void init() __attribute__((constructor));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ircd::nacl::throw_on_error
|
2017-10-01 04:14:45 +02:00
|
|
|
{
|
2019-06-13 21:36:17 +02:00
|
|
|
throw_on_error(const int &val);
|
2017-10-01 12:05:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ircd/nacl.h
|
|
|
|
//
|
|
|
|
|
2019-06-01 01:06:55 +02:00
|
|
|
decltype(ircd::nacl::version_api)
|
|
|
|
ircd::nacl::version_api
|
2017-10-01 12:05:09 +02:00
|
|
|
{
|
2019-06-01 01:06:55 +02:00
|
|
|
"sodium", info::versions::API, 0,
|
|
|
|
{
|
|
|
|
SODIUM_LIBRARY_VERSION_MAJOR,
|
|
|
|
SODIUM_LIBRARY_VERSION_MINOR,
|
|
|
|
0
|
|
|
|
},
|
|
|
|
SODIUM_VERSION_STRING,
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::nacl::version_abi)
|
|
|
|
ircd::nacl::version_abi
|
|
|
|
{
|
|
|
|
"sodium", info::versions::ABI, 0,
|
|
|
|
{
|
|
|
|
::sodium_library_version_major(),
|
|
|
|
::sodium_library_version_minor(),
|
|
|
|
0
|
|
|
|
},
|
|
|
|
::sodium_version_string(),
|
|
|
|
};
|
|
|
|
|
2017-10-01 04:14:45 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ircd/buffer.h
|
|
|
|
//
|
|
|
|
|
2018-11-16 23:13:34 +01:00
|
|
|
size_t
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::buffer::zero(const mutable_buffer &buf)
|
2017-10-01 04:14:45 +02:00
|
|
|
{
|
|
|
|
sodium_memzero(data(buf), size(buf));
|
2018-11-16 23:13:34 +01:00
|
|
|
return size(buf);
|
2017-10-01 04:14:45 +02:00
|
|
|
}
|
2017-10-01 12:05:09 +02:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ircd/ed25519
|
|
|
|
//
|
|
|
|
|
2017-10-03 12:57:18 +02:00
|
|
|
static_assert(ircd::ed25519::SK_SZ == crypto_sign_ed25519_SECRETKEYBYTES);
|
2017-10-01 12:05:09 +02:00
|
|
|
static_assert(ircd::ed25519::PK_SZ == crypto_sign_ed25519_PUBLICKEYBYTES);
|
|
|
|
|
2017-10-03 12:57:18 +02:00
|
|
|
ircd::ed25519::sk::sk(pk *const &pk_arg,
|
2018-02-03 08:20:26 +01:00
|
|
|
const const_buffer &seed)
|
2017-10-03 12:57:18 +02:00
|
|
|
:key
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint8_t *>(::sodium_malloc(crypto_sign_ed25519_SECRETKEYBYTES)),
|
|
|
|
&::sodium_free
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assert(size(seed) >= SEED_SZ);
|
|
|
|
|
|
|
|
pk discard, &pk
|
|
|
|
{
|
|
|
|
pk_arg? *pk_arg : discard
|
|
|
|
};
|
|
|
|
|
2018-02-03 08:20:26 +01:00
|
|
|
const auto pk_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint8_t *>(pk.data())
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto seed_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t *>(data(seed))
|
|
|
|
};
|
|
|
|
|
2019-06-13 21:36:17 +02:00
|
|
|
nacl::throw_on_error
|
2017-10-03 12:57:18 +02:00
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
::crypto_sign_ed25519_seed_keypair(pk_data, key.get(), seed_data)
|
2017-10-03 12:57:18 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-03 02:51:35 +02:00
|
|
|
ircd::ed25519::sk::sk(const string_view &filename,
|
2020-09-16 02:40:10 +02:00
|
|
|
pk *const &pk_arg,
|
|
|
|
const bool &create)
|
2018-03-01 11:53:27 +01:00
|
|
|
try
|
2017-10-01 12:05:09 +02:00
|
|
|
:key
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint8_t *>(::sodium_malloc(crypto_sign_ed25519_SECRETKEYBYTES)),
|
|
|
|
&::sodium_free
|
|
|
|
}
|
|
|
|
{
|
|
|
|
pk discard, &pk
|
|
|
|
{
|
|
|
|
pk_arg? *pk_arg : discard
|
|
|
|
};
|
|
|
|
|
2018-02-03 08:20:26 +01:00
|
|
|
const auto pk_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint8_t *>(pk.data())
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutable_buffer key_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(key.get()), SK_SZ
|
|
|
|
};
|
|
|
|
|
2020-09-11 07:44:09 +02:00
|
|
|
const bool exists
|
|
|
|
{
|
|
|
|
filename && fs::exists(filename)
|
|
|
|
};
|
|
|
|
|
2020-09-16 02:40:10 +02:00
|
|
|
if(!exists && create)
|
2017-10-03 12:57:18 +02:00
|
|
|
{
|
2019-06-13 21:36:17 +02:00
|
|
|
nacl::throw_on_error
|
2017-10-03 12:57:18 +02:00
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
::crypto_sign_ed25519_keypair(pk_data, key.get())
|
2017-10-03 12:57:18 +02:00
|
|
|
};
|
|
|
|
|
2018-02-03 08:20:26 +01:00
|
|
|
fs::write(filename, key_data);
|
2017-10-03 12:57:18 +02:00
|
|
|
}
|
2020-09-11 07:44:09 +02:00
|
|
|
else if(!exists)
|
|
|
|
{
|
|
|
|
zero(pk);
|
|
|
|
key.reset();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-01 11:53:27 +01:00
|
|
|
else fs::read(filename, key_data);
|
2017-10-03 12:57:18 +02:00
|
|
|
|
2019-06-13 21:36:17 +02:00
|
|
|
nacl::throw_on_error
|
2017-10-01 12:05:09 +02:00
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
::crypto_sign_ed25519_sk_to_pk(pk_data, key.get())
|
2017-10-01 12:05:09 +02:00
|
|
|
};
|
|
|
|
}
|
2019-03-17 01:21:42 +01:00
|
|
|
catch(const std::exception &e)
|
2018-03-01 11:53:27 +01:00
|
|
|
{
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Failed to read existing ed25519 secret key in: %s :%s",
|
|
|
|
filename,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2017-10-01 12:05:09 +02:00
|
|
|
|
|
|
|
ircd::ed25519::sig
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::ed25519::sk::sign(const const_buffer &msg)
|
2017-10-01 12:05:09 +02:00
|
|
|
const
|
|
|
|
{
|
2019-10-03 02:11:24 +02:00
|
|
|
if(unlikely(!key))
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"No ed25519 secret key is loaded."
|
|
|
|
};
|
|
|
|
|
2017-10-01 12:05:09 +02:00
|
|
|
struct sig sig;
|
2018-02-03 08:20:26 +01:00
|
|
|
unsigned long long sig_sz;
|
|
|
|
|
|
|
|
const auto sig_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint8_t *>(sig.data())
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto msg_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t *>(buffer::data(msg))
|
|
|
|
};
|
|
|
|
|
2019-06-13 21:36:17 +02:00
|
|
|
nacl::throw_on_error
|
2017-10-01 12:05:09 +02:00
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
::crypto_sign_ed25519_detached(sig_data,
|
2017-10-01 12:05:09 +02:00
|
|
|
&sig_sz,
|
2018-02-03 08:20:26 +01:00
|
|
|
msg_data,
|
2017-10-01 12:05:09 +02:00
|
|
|
buffer::size(msg),
|
|
|
|
key.get())
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(sig_sz <= sig.size());
|
|
|
|
assert(sig.size() >= sig_sz);
|
|
|
|
memset(sig.data() + sig.size() - sig_sz, 0, sig.size() - sig_sz);
|
|
|
|
return sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-03 08:20:26 +01:00
|
|
|
ircd::ed25519::pk::verify(const const_buffer &msg,
|
2017-10-01 12:05:09 +02:00
|
|
|
const sig &sig)
|
|
|
|
const
|
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
const auto sig_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t *>(sig.data())
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto msg_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t *>(buffer::data(msg))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto key_data
|
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t *>(data())
|
|
|
|
};
|
|
|
|
|
2017-10-01 12:05:09 +02:00
|
|
|
const int ret
|
|
|
|
{
|
2018-02-03 08:20:26 +01:00
|
|
|
::crypto_sign_ed25519_verify_detached(sig_data,
|
|
|
|
msg_data,
|
2017-10-01 12:05:09 +02:00
|
|
|
buffer::size(msg),
|
2018-02-03 08:20:26 +01:00
|
|
|
key_data)
|
2017-10-01 12:05:09 +02:00
|
|
|
};
|
|
|
|
|
2019-06-13 21:36:17 +02:00
|
|
|
if(likely(ret == 0))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(likely(ret == -1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
throw nacl::error
|
|
|
|
{
|
|
|
|
"verify failed: %d", ret
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Internal
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::nacl::init()
|
|
|
|
{
|
|
|
|
if(::sodium_init() < 0)
|
|
|
|
throw std::runtime_error
|
|
|
|
{
|
|
|
|
"sodium_init(): error"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::nacl::throw_on_error::throw_on_error(const int &val)
|
|
|
|
{
|
|
|
|
if(unlikely(val != 0))
|
|
|
|
throw ircd::nacl::error
|
|
|
|
{
|
|
|
|
"sodium error"
|
|
|
|
};
|
2017-10-01 12:05:09 +02:00
|
|
|
}
|