Skip to content
Snippets Groups Projects
Select Git revision
  • 18e53b9ec83bf41f4b08431fe7e99a8bfaf6cd1f
  • master default
  • cmake_fixes
  • pulls/1772757862/750
  • copilot/fix-770
  • windows_ci_static
  • c_link
  • cpack
  • windows_ci
  • cert_pk_id
  • proxy_push_result
  • cnode_put_id
  • update-windows-build
  • proxy
  • resubscribe_on_token_change
  • actions
  • client_mode
  • llhttp
  • search_node_add
  • crypto_aes_gcm_argon2
  • ios_notifications
  • v3.4.0
  • v3.3.1
  • v3.3.1rc1
  • v3.3.1rc2
  • v3.3.0
  • v3.2.0
  • v3.1.11
  • v3.1.10
  • v3.1.9
  • v3.1.8.2
  • v3.1.8.1
  • v3.1.8
  • v3.1.7
  • v3.1.6
  • v3.1.5
  • v3.1.4
  • v3.1.3
  • v3.1.2
  • v3.1
  • v3.0.1
41 results

crypto.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    crypto.cpp 54.85 KiB
    /*
     *  Copyright (C) 2014-2022 Savoir-faire Linux Inc.
     *  Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
     *           Vsevolod Ivanov <vsevolod.ivanov@savoirfairelinux.com>
     *
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program. If not, see <https://www.gnu.org/licenses/>.
     */
    
    #include "crypto.h"
    #include "rng.h"
    
    extern "C" {
    #include <gnutls/gnutls.h>
    #include <gnutls/abstract.h>
    #include <gnutls/x509.h>
    #include <nettle/gcm.h>
    #include <nettle/aes.h>
    #include <gnutls/crypto.h>
    
    #include <argon2.h>
    }
    
    #include <random>
    #include <sstream>
    #include <fstream>
    #include <stdexcept>
    #include <cassert>
    
    #ifdef _WIN32
    static std::uniform_int_distribution<int> rand_byte{ 0, std::numeric_limits<uint8_t>::max() };
    #else
    static std::uniform_int_distribution<uint8_t> rand_byte;
    #endif
    
    #define DHT_AES_LEGACY_DECRYPT 1
    
    namespace dht {
    namespace crypto {
    
    static constexpr std::array<size_t, 3> AES_LENGTHS {{128/8, 192/8, 256/8}};
    static constexpr size_t PASSWORD_SALT_LENGTH {16};
    
    constexpr gnutls_digest_algorithm_t gnutlsHashAlgo(size_t min_res) {
        return (min_res > 256/8) ? GNUTLS_DIG_SHA512 : (
               (min_res > 160/8) ? GNUTLS_DIG_SHA256 : (
                                   GNUTLS_DIG_SHA1));
    }
    
    constexpr size_t gnutlsHashSize(gnutls_digest_algorithm_t algo) {
        return (algo == GNUTLS_DIG_SHA512) ? 512/8 : (
               (algo == GNUTLS_DIG_SHA256) ? 256/8 : (
               (algo == GNUTLS_DIG_SHA1)   ? 160/8 : 0 ));
    }
    
    size_t aesKeySize(size_t max)
    {
        size_t aes_key_len = 0;
        for (size_t s : AES_LENGTHS) {
            if (s <= max) aes_key_len = s;