[Botan-devel] Please help. How can I print a Diffie Helmman public key??

Ruben Quintero Lores rubenql at iti.upv.es
Tue Jun 28 06:52:45 EDT 2005


Hi!!

I have this classes definition to implement the Diffie Helmann private
and public keys separatly. I think it's possible in Botan.

class DHPrivKey {
  friend class DHPubKey;
  private:
    Botan::DH_PrivateKey* aprivatekey;
    int len;
  public:
    DHPrivKey();
    inline int size() {return len;}
    void print();
    ~DHPrivKey();
};

class DHPubKey
{
  private:
    Botan::DH_PublicKey* apublickey;
    int len;
    DHPrivKey *priv;
  public:
    DHPubKey();
    inline int size() {return len;}
    void print();
    ~DHPubKey();
};

For later join both clasess in one...

class SR_DH
  {
   private:
      DHPubKey* publickey;
      DHPrivKey* privatekey;  
      int len;
   public:
      SR_DH();
      int size();
      void print();
     ~SR_DH();
   };


Then I do the implementation of this as follow...

DHPrivKey::DHPrivKey()
{
  LibraryInitializer init;
  aprivatekey=new DH_PrivateKey(get_dl_group("IETF-1024"));
  len = sizeof(DH_PrivateKey*);
  //print();
}

void DHPrivKey::print()
  {
   LibraryInitializer init;
   byte *buffer;
   buffer = (byte*)malloc(sizeof(byte)*1024);
   memcpy(buffer,aprivatekey->public_value(),(sizeof(byte)*1024));
   for(int i = 0; i < sizeof(byte)*1024; i++)
       printf("%02X", (int)buffer[i]);
   printf("\n");
   free(buffer);  
  }

DHPrivKey::~DHPrivKey()
{
  delete aprivatekey;
}

DHPubKey::DHPubKey()
{
   priv = new DHPrivKey();
   apublickey = priv->aprivatekey;
   len = sizeof(DH_PublicKey*);
  //print();
}

void DHPubKey::print()
  {
   LibraryInitializer init;
   byte *buffer;
   buffer = (byte*)malloc(sizeof(byte)*1024);
   memcpy(buffer,apublickey->public_value(),(sizeof(byte)*1024));
   for(int i = 0; i < sizeof(byte)*1024; i++)
       printf("%02X", (int)buffer[i]);
   printf("\n");
   free(buffer);   
//   std::cout << "Y Pub= " << apublickey->get_y() << "\n";  
  }

DHPubKey::~DHPubKey()
{
  delete priv;
  delete apublickey;
}

when I try to call the print() method of DHPubKey I obtain a
segmentation fault. The curious is that when I call print() in DHPrivKey
constructor and then call later print() in DHPubKey it work perfectly,
printing both keys values...BUT if I comment the call in DHPrivKey I get
a Segmentation Fault error.

In other hands, When I try to visualize the value of aprivatekey->get_y
() it's a big number as is correct BUT when I try to visualize
apublickey->get_y() it's zero...

What I doing bad??? I don understand nothing...

I need your help please.

Best regards,

Rubén




More information about the botan-devel mailing list