| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- // This file is part of Desktop App Toolkit,
- // a set of libraries for developing nice desktop applications.
- //
- // For license and copyright information please follow this link:
- // https://github.com/desktop-app/legal/blob/master/LEGAL
- //
- #pragma once
- #include "base/bytes.h"
- #include "base/algorithm.h"
- #include "base/assertion.h"
- #include "base/basic_types.h"
- extern "C" {
- #include <openssl/bn.h>
- #include <openssl/sha.h>
- #include <openssl/aes.h>
- #include <openssl/modes.h>
- #include <openssl/crypto.h>
- #include <openssl/evp.h>
- #include <openssl/hmac.h>
- #include <openssl/rsa.h>
- #include <openssl/pem.h>
- #include <openssl/err.h>
- } // extern "C"
- #ifdef small
- #undef small
- #endif // small
- namespace openssl {
- class Context {
- public:
- Context() : _data(BN_CTX_new()) {
- }
- Context(const Context &other) = delete;
- Context(Context &&other) : _data(base::take(other._data)) {
- }
- Context &operator=(const Context &other) = delete;
- Context &operator=(Context &&other) {
- _data = base::take(other._data);
- return *this;
- }
- ~Context() {
- if (_data) {
- BN_CTX_free(_data);
- }
- }
- BN_CTX *raw() const {
- return _data;
- }
- private:
- BN_CTX *_data = nullptr;
- };
- class BigNum {
- public:
- BigNum() = default;
- BigNum(const BigNum &other)
- : _data((other.failed() || other.isZero())
- ? nullptr
- : BN_dup(other.raw()))
- , _failed(other._failed) {
- }
- BigNum(BigNum &&other)
- : _data(std::exchange(other._data, nullptr))
- , _failed(std::exchange(other._failed, false)) {
- }
- BigNum &operator=(const BigNum &other) {
- if (other.failed()) {
- _failed = true;
- } else if (other.isZero()) {
- clear();
- _failed = false;
- } else if (!_data) {
- _data = BN_dup(other.raw());
- _failed = false;
- } else {
- _failed = !BN_copy(raw(), other.raw());
- }
- return *this;
- }
- BigNum &operator=(BigNum &&other) {
- std::swap(_data, other._data);
- std::swap(_failed, other._failed);
- return *this;
- }
- ~BigNum() {
- clear();
- }
- explicit BigNum(unsigned int word) : BigNum() {
- setWord(word);
- }
- explicit BigNum(bytes::const_span bytes) : BigNum() {
- setBytes(bytes);
- }
- BigNum &setWord(unsigned int word) {
- if (!word) {
- clear();
- _failed = false;
- } else {
- _failed = !BN_set_word(raw(), word);
- }
- return *this;
- }
- BigNum &setBytes(bytes::const_span bytes) {
- if (bytes.empty()) {
- clear();
- _failed = false;
- } else {
- _failed = !BN_bin2bn(
- reinterpret_cast<const unsigned char*>(bytes.data()),
- bytes.size(),
- raw());
- }
- return *this;
- }
- BigNum &setAdd(const BigNum &a, const BigNum &b) {
- if (a.failed() || b.failed()) {
- _failed = true;
- } else {
- _failed = !BN_add(raw(), a.raw(), b.raw());
- }
- return *this;
- }
- BigNum &setSub(const BigNum &a, const BigNum &b) {
- if (a.failed() || b.failed()) {
- _failed = true;
- } else {
- _failed = !BN_sub(raw(), a.raw(), b.raw());
- }
- return *this;
- }
- BigNum &setMul(
- const BigNum &a,
- const BigNum &b,
- const Context &context = Context()) {
- if (a.failed() || b.failed()) {
- _failed = true;
- } else {
- _failed = !BN_mul(raw(), a.raw(), b.raw(), context.raw());
- }
- return *this;
- }
- BigNum &setModAdd(
- const BigNum &a,
- const BigNum &b,
- const BigNum &m,
- const Context &context = Context()) {
- if (a.failed() || b.failed() || m.failed()) {
- _failed = true;
- } else if (a.isNegative() || b.isNegative() || m.isNegative()) {
- _failed = true;
- } else if (!BN_mod_add(raw(), a.raw(), b.raw(), m.raw(), context.raw())) {
- _failed = true;
- } else if (isNegative()) {
- _failed = true;
- } else {
- _failed = false;
- }
- return *this;
- }
- BigNum &setModSub(
- const BigNum &a,
- const BigNum &b,
- const BigNum &m,
- const Context &context = Context()) {
- if (a.failed() || b.failed() || m.failed()) {
- _failed = true;
- } else if (a.isNegative() || b.isNegative() || m.isNegative()) {
- _failed = true;
- } else if (!BN_mod_sub(raw(), a.raw(), b.raw(), m.raw(), context.raw())) {
- _failed = true;
- } else if (isNegative()) {
- _failed = true;
- } else {
- _failed = false;
- }
- return *this;
- }
- BigNum &setModMul(
- const BigNum &a,
- const BigNum &b,
- const BigNum &m,
- const Context &context = Context()) {
- if (a.failed() || b.failed() || m.failed()) {
- _failed = true;
- } else if (a.isNegative() || b.isNegative() || m.isNegative()) {
- _failed = true;
- } else if (!BN_mod_mul(raw(), a.raw(), b.raw(), m.raw(), context.raw())) {
- _failed = true;
- } else if (isNegative()) {
- _failed = true;
- } else {
- _failed = false;
- }
- return *this;
- }
- BigNum &setModInverse(
- const BigNum &a,
- const BigNum &m,
- const Context &context = Context()) {
- if (a.failed() || m.failed()) {
- _failed = true;
- } else if (a.isNegative() || m.isNegative()) {
- _failed = true;
- } else if (!BN_mod_inverse(raw(), a.raw(), m.raw(), context.raw())) {
- _failed = true;
- } else if (isNegative()) {
- _failed = true;
- } else {
- _failed = false;
- }
- return *this;
- }
- BigNum &setModExp(
- const BigNum &base,
- const BigNum &power,
- const BigNum &m,
- const Context &context = Context()) {
- if (base.failed() || power.failed() || m.failed()) {
- _failed = true;
- } else if (base.isNegative() || power.isNegative() || m.isNegative()) {
- _failed = true;
- } else if (!BN_mod_exp(raw(), base.raw(), power.raw(), m.raw(), context.raw())) {
- _failed = true;
- } else if (isNegative()) {
- _failed = true;
- } else {
- _failed = false;
- }
- return *this;
- }
- BigNum &setGcd(
- const BigNum &a,
- const BigNum &b,
- const Context &context = Context()) {
- if (a.failed() || b.failed()) {
- _failed = true;
- } else if (a.isNegative() || b.isNegative()) {
- _failed = true;
- } else if (!BN_gcd(raw(), a.raw(), b.raw(), context.raw())) {
- _failed = true;
- } else if (isNegative()) {
- _failed = true;
- } else {
- _failed = false;
- }
- return *this;
- }
- [[nodiscard]] bool isZero() const {
- return !failed() && (!_data || BN_is_zero(raw()));
- }
- [[nodiscard]] bool isOne() const {
- return !failed() && _data && BN_is_one(raw());
- }
- [[nodiscard]] bool isNegative() const {
- return !failed() && _data && BN_is_negative(raw());
- }
- [[nodiscard]] bool isPrime(const Context &context = Context()) const {
- if (failed() || !_data) {
- return false;
- }
- constexpr auto kMillerRabinIterationCount = 64;
- const auto result = BN_is_prime_ex(
- raw(),
- kMillerRabinIterationCount,
- context.raw(),
- nullptr);
- if (result == 1) {
- return true;
- } else if (result != 0) {
- _failed = true;
- }
- return false;
- }
- BigNum &subWord(unsigned int word) {
- if (failed()) {
- return *this;
- } else if (!BN_sub_word(raw(), word)) {
- _failed = true;
- }
- return *this;
- }
- BigNum &divWord(BN_ULONG word, BN_ULONG *mod = nullptr) {
- Expects(word != 0);
- const auto result = failed()
- ? (BN_ULONG)-1
- : BN_div_word(raw(), word);
- if (result == (BN_ULONG)-1) {
- _failed = true;
- }
- if (mod) {
- *mod = result;
- }
- return *this;
- }
- [[nodiscard]] BN_ULONG countModWord(BN_ULONG word) const {
- Expects(word != 0);
- return failed() ? (BN_ULONG)-1 : BN_mod_word(raw(), word);
- }
- [[nodiscard]] int bitsSize() const {
- return failed() ? 0 : BN_num_bits(raw());
- }
- [[nodiscard]] int bytesSize() const {
- return failed() ? 0 : BN_num_bytes(raw());
- }
- [[nodiscard]] bytes::vector getBytes() const {
- if (failed()) {
- return {};
- }
- auto length = BN_num_bytes(raw());
- auto result = bytes::vector(length);
- auto resultSize = BN_bn2bin(
- raw(),
- reinterpret_cast<unsigned char*>(result.data()));
- Assert(resultSize == length);
- return result;
- }
- [[nodiscard]] BIGNUM *raw() {
- if (!_data) _data = BN_new();
- return _data;
- }
- [[nodiscard]] const BIGNUM *raw() const {
- if (!_data) _data = BN_new();
- return _data;
- }
- [[nodiscard]] BIGNUM *takeRaw() {
- return _failed
- ? nullptr
- : _data
- ? std::exchange(_data, nullptr)
- : BN_new();
- }
- [[nodiscard]] bool failed() const {
- return _failed;
- }
- [[nodiscard]] static BigNum Add(const BigNum &a, const BigNum &b) {
- return BigNum().setAdd(a, b);
- }
- [[nodiscard]] static BigNum Sub(const BigNum &a, const BigNum &b) {
- return BigNum().setSub(a, b);
- }
- [[nodiscard]] static BigNum Mul(
- const BigNum &a,
- const BigNum &b,
- const Context &context = Context()) {
- return BigNum().setMul(a, b, context);
- }
- [[nodiscard]] static BigNum ModAdd(
- const BigNum &a,
- const BigNum &b,
- const BigNum &mod,
- const Context &context = Context()) {
- return BigNum().setModAdd(a, b, mod, context);
- }
- [[nodiscard]] static BigNum ModSub(
- const BigNum &a,
- const BigNum &b,
- const BigNum &mod,
- const Context &context = Context()) {
- return BigNum().setModSub(a, b, mod, context);
- }
- [[nodiscard]] static BigNum ModMul(
- const BigNum &a,
- const BigNum &b,
- const BigNum &mod,
- const Context &context = Context()) {
- return BigNum().setModMul(a, b, mod, context);
- }
- [[nodiscard]] static BigNum ModInverse(
- const BigNum &a,
- const BigNum &mod,
- const Context &context = Context()) {
- return BigNum().setModInverse(a, mod, context);
- }
- [[nodiscard]] static BigNum ModExp(
- const BigNum &base,
- const BigNum &power,
- const BigNum &mod,
- const Context &context = Context()) {
- return BigNum().setModExp(base, power, mod, context);
- }
- [[nodiscard]] static int Compare(const BigNum &a, const BigNum &b) {
- return a.failed() ? -1 : b.failed() ? 1 : BN_cmp(a.raw(), b.raw());
- }
- static void Div(
- BigNum *dv,
- BigNum *rem,
- const BigNum &a,
- const BigNum &b,
- const Context &context = Context()) {
- if (!dv && !rem) {
- return;
- } else if (a.failed()
- || b.failed()
- || !BN_div(
- dv ? dv->raw() : nullptr,
- rem ? rem->raw() : nullptr,
- a.raw(),
- b.raw(),
- context.raw())) {
- if (dv) {
- dv->_failed = true;
- }
- if (rem) {
- rem->_failed = true;
- }
- } else {
- if (dv) {
- dv->_failed = false;
- }
- if (rem) {
- rem->_failed = false;
- }
- }
- }
- [[nodiscard]] static BigNum Failed() {
- auto result = BigNum();
- result._failed = true;
- return result;
- }
- private:
- void clear() {
- BN_clear_free(std::exchange(_data, nullptr));
- }
- mutable BIGNUM *_data = nullptr;
- mutable bool _failed = false;
- };
- namespace details {
- template <typename Context, typename Method, typename Arg>
- inline void ShaUpdate(Context context, Method method, Arg &&arg) {
- const auto span = bytes::make_span(arg);
- method(context, span.data(), span.size());
- }
- template <typename Context, typename Method, typename Arg, typename ...Args>
- inline void ShaUpdate(Context context, Method method, Arg &&arg, Args &&...args) {
- const auto span = bytes::make_span(arg);
- method(context, span.data(), span.size());
- ShaUpdate(context, method, args...);
- }
- template <size_type Size, typename Method>
- inline void Sha(
- bytes::span dst,
- Method method,
- bytes::const_span data) {
- Expects(dst.size() >= Size);
- method(
- reinterpret_cast<const unsigned char*>(data.data()),
- data.size(),
- reinterpret_cast<unsigned char*>(dst.data()));
- }
- template <size_type Size, typename Method>
- [[nodiscard]] inline bytes::vector Sha(
- Method method,
- bytes::const_span data) {
- auto bytes = bytes::vector(Size);
- Sha<Size>(bytes, method, data);
- return bytes;
- }
- template <
- size_type Size,
- typename Context,
- typename Init,
- typename Update,
- typename Finalize,
- typename ...Args,
- typename = std::enable_if_t<(sizeof...(Args) > 1)>>
- [[nodiscard]] bytes::vector Sha(
- Context context,
- Init init,
- Update update,
- Finalize finalize,
- Args &&...args) {
- auto bytes = bytes::vector(Size);
- init(&context);
- ShaUpdate(&context, update, args...);
- finalize(reinterpret_cast<unsigned char*>(bytes.data()), &context);
- return bytes;
- }
- template <
- size_type Size,
- typename Evp>
- [[nodiscard]] bytes::vector Pbkdf2(
- bytes::const_span password,
- bytes::const_span salt,
- int iterations,
- Evp evp) {
- auto result = bytes::vector(Size);
- PKCS5_PBKDF2_HMAC(
- reinterpret_cast<const char*>(password.data()),
- password.size(),
- reinterpret_cast<const unsigned char*>(salt.data()),
- salt.size(),
- iterations,
- evp,
- result.size(),
- reinterpret_cast<unsigned char*>(result.data()));
- return result;
- }
- } // namespace details
- constexpr auto kSha1Size = size_type(SHA_DIGEST_LENGTH);
- constexpr auto kSha256Size = size_type(SHA256_DIGEST_LENGTH);
- constexpr auto kSha512Size = size_type(SHA512_DIGEST_LENGTH);
- [[nodiscard]] inline bytes::vector Sha1(bytes::const_span data) {
- return details::Sha<kSha1Size>(SHA1, data);
- }
- inline void Sha1To(bytes::span dst, bytes::const_span data) {
- details::Sha<kSha1Size>(dst, SHA1, data);
- }
- template <
- typename ...Args,
- typename = std::enable_if_t<(sizeof...(Args) > 1)>>
- [[nodiscard]] inline bytes::vector Sha1(Args &&...args) {
- return details::Sha<kSha1Size>(
- SHA_CTX(),
- SHA1_Init,
- SHA1_Update,
- SHA1_Final,
- args...);
- }
- [[nodiscard]] inline bytes::vector Sha256(bytes::const_span data) {
- return details::Sha<kSha256Size>(SHA256, data);
- }
- inline void Sha256To(bytes::span dst, bytes::const_span data) {
- details::Sha<kSha256Size>(dst, SHA256, data);
- }
- template <
- typename ...Args,
- typename = std::enable_if_t<(sizeof...(Args) > 1)>>
- [[nodiscard]] inline bytes::vector Sha256(Args &&...args) {
- return details::Sha<kSha256Size>(
- SHA256_CTX(),
- SHA256_Init,
- SHA256_Update,
- SHA256_Final,
- args...);
- }
- [[nodiscard]] inline bytes::vector Sha512(bytes::const_span data) {
- return details::Sha<kSha512Size>(SHA512, data);
- }
- inline void Sha512To(bytes::span dst, bytes::const_span data) {
- details::Sha<kSha512Size>(dst, SHA512, data);
- }
- template <
- typename ...Args,
- typename = std::enable_if_t<(sizeof...(Args) > 1)>>
- [[nodiscard]] inline bytes::vector Sha512(Args &&...args) {
- return details::Sha<kSha512Size>(
- SHA512_CTX(),
- SHA512_Init,
- SHA512_Update,
- SHA512_Final,
- args...);
- }
- inline bytes::vector Pbkdf2Sha512(
- bytes::const_span password,
- bytes::const_span salt,
- int iterations) {
- return details::Pbkdf2<kSha512Size>(
- password,
- salt,
- iterations,
- EVP_sha512());
- }
- inline bytes::vector HmacSha256(
- bytes::const_span key,
- bytes::const_span data) {
- auto result = bytes::vector(kSha256Size);
- auto length = (unsigned int)kSha256Size;
- HMAC(
- EVP_sha256(),
- key.data(),
- key.size(),
- reinterpret_cast<const unsigned char*>(data.data()),
- data.size(),
- reinterpret_cast<unsigned char*>(result.data()),
- &length);
- return result;
- }
- } // namespace openssl
|