BN_new/BN_init/BN_free

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

BN_new/BN_init/BN_free

Tom marchand
Hi,

I am using the following code to create a temporary BIGNUM  to hold  
the result of multiplication:

BIGNUM *Res;

while(!Done)
{
        Res=BN_new();
        BN_init(Res);

        BN_mul(Res,A,B,Ctx);

        BN_free();

}

This code works with the exception that BN_free() is not releasing the  
memory allocated to Res.  I already have a workaround but I am  
wondering why the memory isn't being released. In production the code  
runs for hours and is causing a significant memory leak.  I am I  
misusing BN_new and BN_free? The code is running on OSX 10.5.8,  
Openssl 0.9.7l.  I get the same result with OpenSSL 1.0.1e.

Thanks
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: BN_new/BN_init/BN_free

J. J. Farrell-2
> From: Tom marchand [mailto:[hidden email]]
> Sent: Friday, May 03, 2013 2:55 AM
>
> I am using the following code to create a temporary BIGNUM  to hold
> the result of multiplication:
>
> BIGNUM *Res;
>
> while(!Done)
> {
> Res=BN_new();
> BN_init(Res);
>
> BN_mul(Res,A,B,Ctx);
>
> BN_free();
>
> }
>
> This code works with the exception that BN_free() is not releasing the
> memory allocated to Res.  I already have a workaround but I am
> wondering why the memory isn't being released. In production the code
> runs for hours and is causing a significant memory leak.  I am I
> misusing BN_new and BN_free? The code is running on OSX 10.5.8,
> Openssl 0.9.7l.  I get the same result with OpenSSL 1.0.1e.

BN_free(Res);  ???
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: BN_new/BN_init/BN_free

Tom marchand
Good eye but that's not it.  The code below should read BN_free(Res).  
Thanks.


On May 2, 2013, at 10:42 PM, Jeremy Farrell wrote:

>> From: Tom marchand [mailto:[hidden email]]
>> Sent: Friday, May 03, 2013 2:55 AM
>>
>> I am using the following code to create a temporary BIGNUM  to hold
>> the result of multiplication:
>>
>> BIGNUM *Res;
>>
>> while(!Done)
>> {
>> Res=BN_new();
>> BN_init(Res);
>>
>> BN_mul(Res,A,B,Ctx);
>>
>> BN_free();
>>
>> }
>>
>> This code works with the exception that BN_free() is not releasing  
>> the
>> memory allocated to Res.  I already have a workaround but I am
>> wondering why the memory isn't being released. In production the code
>> runs for hours and is causing a significant memory leak.  I am I
>> misusing BN_new and BN_free? The code is running on OSX 10.5.8,
>> Openssl 0.9.7l.  I get the same result with OpenSSL 1.0.1e.
>
> BN_free(Res);  ???
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: BN_new/BN_init/BN_free

Jakob Bohm-7
On 5/3/2013 4:57 AM, Tom marchand wrote:

> Good eye but that's not it.  The code below should read BN_free(Res).
> Thanks.
>
>
> On May 2, 2013, at 10:42 PM, Jeremy Farrell wrote:
>
>>> From: Tom marchand [mailto:[hidden email]]
>>> Sent: Friday, May 03, 2013 2:55 AM
>>>
>>> I am using the following code to create a temporary BIGNUM  to hold
>>> the result of multiplication:
>>>
>>> BIGNUM    *Res;
>>>
>>> while(!Done)
>>> {
>>>     Res=BN_new();
>>>     BN_init(Res);
>>>
>>>     BN_mul(Res,A,B,Ctx);
>>>
>>>     BN_free();
>>>
>>> }
>>>
>>> This code works with the exception that BN_free() is not releasing the
>>> memory allocated to Res.  I already have a workaround but I am
>>> wondering why the memory isn't being released. In production the code
>>> runs for hours and is causing a significant memory leak.  I am I
>>> misusing BN_new and BN_free? The code is running on OSX 10.5.8,
>>> Openssl 0.9.7l.  I get the same result with OpenSSL 1.0.1e.
>>
>> BN_free(Res);  ???

Remove the call to BN_init(Res).

BN_new() sets a flag in Res that says that BN_free(Res) should free
both the digit buffer at Res->d and Res itself.

BN_Init() sets the flag that says that BN_free(Res) should free only
Res->d.

The intended usage is EITHER:

void foo(const BIGNUM* A, const BIGNUM* B)
{
    BIGNUM    *Res;
    BN_CTX    *Ctx = BN_CTX_new();

    while(!Done)
    {
        Res=BN_new();

        BN_mul(Res,A,B,Ctx);

        BN_free_clear(Res);
    }

    BN_CTX_free(Ctx)
}

OR

void bar(const BIGNUM* A, const BIGNUM* B)
{
    BIGNUM    Res;
    BN_CTX    Ctx;

    BN_CTX_init(&Ctx);

    while(!Done)
    {
        BN_init(&Res);

        BN_mul(&Res,A,B,&Ctx);

        BN_free_clear(&Res);
    }

    BN_CTX_free(&Ctx)
}

The choice between the two styles is a matter of optimization specific
to the compiler you use and the actual variable lifetimes in you
application.


Enjoy

Jakob
--
Jakob Bohm, CIO, Partner, WiseMo A/S.  http://www.wisemo.com
Transformervej 29, 2730 Herlev, Denmark.  Direct +45 31 13 16 10
This public discussion message is non-binding and may contain errors.
WiseMo - Remote Service Management for PCs, Phones and Embedded
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]