File: LibgcryptDHKeyExchange.cc

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (164 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2010 Tatsuhiro Tsujikawa
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#include "LibgcryptDHKeyExchange.h"
#include "DlAbortEx.h"
#include "fmt.h"

namespace aria2 {

namespace {
void handleError(gcry_error_t err)
{
  throw DL_ABORT_EX
    (fmt("Exception in libgcrypt routine(DHKeyExchange class): %s",
         gcry_strerror(err)));
}
} // namespace

DHKeyExchange::DHKeyExchange()
  : keyLength_(0),
    prime_(nullptr),
    generator_(nullptr),
    privateKey_(nullptr),
    publicKey_(nullptr)
{}

DHKeyExchange::~DHKeyExchange()
{
  gcry_mpi_release(prime_);
  gcry_mpi_release(generator_);
  gcry_mpi_release(privateKey_);
  gcry_mpi_release(publicKey_);
}

void DHKeyExchange::init(const unsigned char* prime, size_t primeBits,
                         const unsigned char* generator,
                         size_t privateKeyBits)
{
  gcry_mpi_release(prime_);
  gcry_mpi_release(generator_);
  gcry_mpi_release(privateKey_);
  {
    gcry_error_t r = gcry_mpi_scan(&prime_, GCRYMPI_FMT_HEX,
                                   prime, 0, nullptr);
    if(r) {
      handleError(r);
    }
  }
  {
    gcry_error_t r = gcry_mpi_scan(&generator_, GCRYMPI_FMT_HEX,
                                   generator, 0, nullptr);
    if(r) {
      handleError(r);
    }
  }
  privateKey_ = gcry_mpi_new(0);
  gcry_mpi_randomize(privateKey_, privateKeyBits, GCRY_STRONG_RANDOM);
  keyLength_ = (primeBits+7)/8;
}

void DHKeyExchange::generatePublicKey()
{
  gcry_mpi_release(publicKey_);
  publicKey_ = gcry_mpi_new(0);
  gcry_mpi_powm(publicKey_, generator_, privateKey_, prime_);
}

size_t DHKeyExchange::getPublicKey(unsigned char* out, size_t outLength) const
{
  if(outLength < keyLength_) {
    throw DL_ABORT_EX
      (fmt("Insufficient buffer for public key. expect:%lu, actual:%lu",
           static_cast<unsigned long>(keyLength_),
           static_cast<unsigned long>(outLength)));
  }
  memset(out, 0, outLength);
  size_t publicKeyBytes = (gcry_mpi_get_nbits(publicKey_)+7)/8;
  size_t offset = keyLength_-publicKeyBytes;
  size_t nwritten;
  gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out+offset,
                                  outLength-offset, &nwritten, publicKey_);
  if(r) {
    handleError(r);
  }
  return nwritten;
}

void DHKeyExchange::generateNonce(unsigned char* out, size_t outLength) const
{
  gcry_create_nonce(out, outLength);
}

size_t DHKeyExchange::computeSecret(unsigned char* out, size_t outLength,
                                    const unsigned char* peerPublicKeyData,
                                    size_t peerPublicKeyLength) const
{
  if(outLength < keyLength_) {
    throw DL_ABORT_EX
      (fmt("Insufficient buffer for secret. expect:%lu, actual:%lu",
           static_cast<unsigned long>(keyLength_),
           static_cast<unsigned long>(outLength)));
  }
  gcry_mpi_t peerPublicKey;
  {
    gcry_error_t r = gcry_mpi_scan(&peerPublicKey,
                                   GCRYMPI_FMT_USG,
                                   peerPublicKeyData,
                                   peerPublicKeyLength,
                                   nullptr);
    if(r) {
      handleError(r);
    }
  }
  gcry_mpi_t secret = gcry_mpi_new(0);
  gcry_mpi_powm(secret, peerPublicKey, privateKey_, prime_);
  gcry_mpi_release(peerPublicKey);

  memset(out, 0, outLength);
  size_t secretBytes = (gcry_mpi_get_nbits(secret)+7)/8;
  size_t offset = keyLength_-secretBytes;
  size_t nwritten;
  {
    gcry_error_t r = gcry_mpi_print(GCRYMPI_FMT_USG, out+offset,
                                    outLength-offset, &nwritten, secret);
    gcry_mpi_release(secret);
    if(r) {
      handleError(r);
    }
  }
  return nwritten;
}

} // namespace aria2