gcrypt-example icon indicating copy to clipboard operation
gcrypt-example copied to clipboard

key size

Open romaad opened this issue 11 years ago • 2 comments

what is the total size of saving the public and private key ?

romaad avatar Aug 30 '15 14:08 romaad

In the current version of the code, it's exactly 32 kilobytes. However, that's much larger than it needs to be.

The size of the keypair file is currently over-approximated:

 size_t get_keypair_size(int nbits)                                              
 {                                                                               
     size_t aes_blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);        
     return nbits * aes_blklen;                                              
 }

For some crazy reason, this code assumes that an entire AES128 block is needed to encrypt each bit in the private key. It ignores the space requirement of the public key, but that's a hidden bug since it over-allocated things to begin with.

This is a hack, but here's something that works a bit better:

size_t get_keypair_size(int nbits)                                              
{                                                                               
    size_t aes_blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES128);        

    // format overhead * {pub,priv}key (2 * bits)                               
    size_t keypair_nbits = 4 * (2 * nbits);                                     

    size_t rem = keypair_nbits % aes_blklen;                                    
    return (keypair_nbits + rem) / 8;                                           
}

This lets us save the keypair using just 2 kilobytes.

vedantk avatar Sep 01 '15 06:09 vedantk

With a better understanding of the format we could reduce the space requirement even more, but I'm going to mark this as closed with commit cab14b.

vedantk avatar Sep 01 '15 06:09 vedantk