示例#1
0
/*
* Default_ELG_Op Constructor
*/
Default_ELG_Op::Default_ELG_Op(const DL_Group& group, const BigInt& y,
                               const BigInt& x) : p(group.get_p())
   {
   powermod_g_p = Fixed_Base_Power_Mod(group.get_g(), p);
   powermod_y_p = Fixed_Base_Power_Mod(y, p);
   mod_p = Modular_Reducer(p);

   if(x != 0)
      powermod_x_p = Fixed_Exponent_Power_Mod(x, p);
   }
示例#2
0
/*************************************************
* DH_Core Constructor                            *
*************************************************/
DH_Core::DH_Core(RandomNumberGenerator& rng,
                 const DL_Group& group, const BigInt& x)
   {
   op = Engine_Core::dh_op(group, x);

   const BigInt& p = group.get_p();

   BigInt k(rng, std::min(p.bits()-1, BLINDING_BITS));

   if(k != 0)
      blinder = Blinder(k, power_mod(inverse_mod(k, p), x, p), p);
   }
示例#3
0
/*************************************************
* ELG_Core Constructor                           *
*************************************************/
ELG_Core::ELG_Core(RandomNumberGenerator& rng,
                   const DL_Group& group, const BigInt& y, const BigInt& x)
   {
   op = Engine_Core::elg_op(group, y, x);

   const BigInt& p = group.get_p();
   p_bytes = p.bytes();

   if(BLINDING_BITS)
      {
      BigInt k(rng, std::min(p.bits()-1, BLINDING_BITS));
      blinder = Blinder(k, power_mod(k, x, p), p);
      }
   }
bool ne7ssh_keys::generateDSAKeys (const char* fqdn, const char* privKeyFileName, const char* pubKeyFileName, uint16 keySize)
{
  DER_Encoder encoder;
  BigInt p, q, g, y, x;
  ne7ssh_string pubKeyBlob;
  FILE *privKeyFile, *pubKeyFile;
  std::string privKeyEncoded;

  if (keySize != 1024)
  {
    ne7ssh::errors()->push (-1, "DSA keys must be 1024 bits.");
    return false;
  }

#if BOTAN_PRE_18 || BOTAN_PRE_15
  DL_Group dsaGroup (keySize, DL_Group::DSA_Kosherizer);
  DSA_PrivateKey privDsaKey (dsaGroup);
#else
  DL_Group dsaGroup (*ne7ssh::rng, Botan::DL_Group::DSA_Kosherizer, keySize);
  DSA_PrivateKey privDsaKey (*ne7ssh::rng, dsaGroup);
#endif

  DSA_PublicKey pubDsaKey = privDsaKey;

  p = dsaGroup.get_p();
  q = dsaGroup.get_q();
  g = dsaGroup.get_g();
  y = pubDsaKey.get_y();
  x = privDsaKey.get_x();

  pubKeyBlob.addString ("ssh-dss");
  pubKeyBlob.addBigInt (p);
  pubKeyBlob.addBigInt (q);
  pubKeyBlob.addBigInt (g);
  pubKeyBlob.addBigInt (y);

  Pipe base64it (new Base64_Encoder);
  base64it.process_msg(pubKeyBlob.value());

  SecureVector<Botan::byte> pubKeyBase64 = base64it.read_all (PIPE_DEFAULT_MESSAGE);

  pubKeyFile = fopen (pubKeyFileName, "w");

  if (!pubKeyFile)
  {
    ne7ssh::errors()->push (-1, "Cannot open file where public key is stored. Filename: %s", pubKeyFileName);
    return false;
  }

  if ((!fwrite ("ssh-dss ", 8, 1, pubKeyFile)) ||
      (!fwrite (pubKeyBase64.begin(), (size_t) pubKeyBase64.size(), 1, pubKeyFile)) ||
      (!fwrite (" ", 1, 1, pubKeyFile)) ||
      (!fwrite (fqdn, strlen(fqdn), 1, pubKeyFile)) ||
      (!fwrite ("\n", 1, 1, pubKeyFile)))
  {
    ne7ssh::errors()->push (-1, "I/O error while writting to file: %s.", pubKeyFileName);
    return false;
  }
  fclose (pubKeyFile);

#if BOTAN_PRE_15
  encoder.start_sequence();
  DER::encode (encoder, 0U);
  DER::encode (encoder, p);
  DER::encode (encoder, q);
  DER::encode (encoder, g);
  DER::encode (encoder, y);
  DER::encode (encoder, x);
  encoder.end_sequence();
#else
  encoder.start_cons(SEQUENCE)
    .encode (0U)
    .encode (p)
    .encode (q)
    .encode (g)
    .encode (y)
    .encode (x)
    .end_cons();
#endif
  privKeyEncoded = PEM_Code::encode (encoder.get_contents(), "DSA PRIVATE KEY");

  privKeyFile = fopen (privKeyFileName, "w");

  if (!privKeyFile)
  {
    ne7ssh::errors()->push (-1, "Cannot open file where private key is stored. Filename: %s", privKeyFileName);
    return false;
  }

  if (!fwrite (privKeyEncoded.c_str(), (size_t) privKeyEncoded.length(), 1, privKeyFile))
  {
    ne7ssh::errors()->push (-1, "I/O error while writting to file: %s.", privKeyFileName);
    return false;
  }
  fclose (privKeyFile);

//  delete dsaGroup;

  return true;
}