File indexing completed on 2025-01-05 04:37:19

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "functions.h"
0007 #include "bigint.h"
0008 #include <util/log.h>
0009 #include <util/sha1hash.h>
0010 
0011 using namespace bt;
0012 
0013 namespace mse
0014 {
0015 /*
0016 static const BigInt P = BigInt(
0017         "0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD"
0018         "129024E088A67CC74020BBEA63B139B22514A08798E3404"
0019         "DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C"
0020         "245E485B576625E7EC6F44C42E9A63A36210000000000090563");
0021 */
0022 static const BigInt P = BigInt(
0023     "0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576"
0024     "625E7EC6F44C42E9A63A36210000000000090563");
0025 
0026 void GeneratePublicPrivateKey(BigInt &priv, BigInt &pub)
0027 {
0028     BigInt G = BigInt("0x02");
0029     priv = BigInt::random();
0030     pub = BigInt::powerMod(G, priv, P);
0031 }
0032 
0033 BigInt DHSecret(const BigInt &our_priv, const BigInt &peer_pub)
0034 {
0035     return BigInt::powerMod(peer_pub, our_priv, P);
0036 }
0037 
0038 bt::SHA1Hash EncryptionKey(bool a, const BigInt &s, const bt::SHA1Hash &skey)
0039 {
0040     Uint8 buf[120];
0041     memcpy(buf, "key", 3);
0042     buf[3] = (Uint8)(a ? 'A' : 'B');
0043     s.toBuffer(buf + 4, 96);
0044     memcpy(buf + 100, skey.getData(), 20);
0045     return bt::SHA1Hash::generate(buf, 120);
0046 }
0047 
0048 void DumpBigInt(const QString &name, const BigInt &bi)
0049 {
0050     static Uint8 buf[512];
0051     Uint32 nb = bi.toBuffer(buf, 512);
0052     bt::Log &lg = Out(SYS_GEN | LOG_DEBUG);
0053     lg << name << " (" << nb << ") = ";
0054     for (Uint32 i = 0; i < nb; i++) {
0055         lg << QString("0x%1 ").arg(buf[i], 0, 16);
0056     }
0057     lg << endl;
0058 }
0059 
0060 }