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

Jack Lloyd lloyd at randombit.net
Tue Jun 28 10:25:17 EDT 2005


I suspect a lot of it has to do with you putting LibraryInitializer objects
into various member functions. This doesn't work that well, because when the
object leaves scope, all objects allocated through Botan between initialization
and shutdown are deleted. So effectively they key doesn't exit anymore by the
time you print it.

Generally speaking a LibraryInitializer is created towards the start of main()
and then it goes out of scope when the application shuts down. If you really
want the style you're using here to work, you'll have to use the
Init::initialize() function along with a static flag to say if the library has
been initialized yet or not (since doing init+init is the same as doing
init+shutdown+init).

You would still have to shut down the library when your program exits
(technically you don't have to, but it ensures that memory that had been
allocated is sanitized before releasing it back to the system). atexit() should
work, and on my Linux/x86 box you can pass C++ functions to it directly and it
seems to work. On some systems/compilers you may need to have a special wrapper
set up for it, though.

So, something like the following untested code:

void initialize_botan()
   {
   static bool started = false;
   if(!started)
      {
      Botan::Init::initialize();
      atexit(Botan::Init::deinitialize);
      started = true;
      }
   }

I can't say I recommend this approach, it's fragile and easy to mess up (and as
you've seen, it can cause some quite bizarre memory errors if you mess anything
up). But it should work.

-Jack

On Tue, Jun 28, 2005 at 12:52:45PM +0200, Ruben Quintero Lores wrote:
> 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
> 
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://www.randombit.net/mailman/listinfo/botan-devel



More information about the botan-devel mailing list