Determining length of hashed value

View: New views
5 Messages — Rating Filter:   Alert me  

Determining length of hashed value

by Ralph Holz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

Sorry if this question may sound a bit dim, but I've only started to dig my
way into cryptlib.

When I extract a hash value from a cryptContext, I do something like this:

char hash[ CRYPT_MAX_HASHSIZE ];
int hashLength;
errStatus = cryptGetAttributeString( context, CRYPT_CTXINFO_HASHVALUE, hash,
&hashLength );

char resultHash[hashLength];
resultHash = copy hashLength bytes over from hash
return resultHash[];

I would like to directly initialize the char hash[] to char
hash[lengthOfHashValue], so I don't have to do this copy operation.

Is there no way to extract hashLength directly? Because - correct me if I am
wrong - all supported hash functions evaluate to fixed-length?

Or am I getting this wrong, and CRYPT_MAX_HASHSIZE is really what I want?

Thanks,
Ralph

--
For contact details, please see www.ralphholz.de.


_______________________________________________
Cryptlib mailing list
Cryptlib@... via Mail: cryptlib-request@...
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.

signature.asc (196 bytes) Download Attachment

Re: Determining length of hashed value

by Peter Gutmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ralph Holz <ralph-cryptlib@...> writes:

>When I extract a hash value from a cryptContext, I do something like this:
>
>char hash[ CRYPT_MAX_HASHSIZE ];
>int hashLength;
>errStatus = cryptGetAttributeString( context, CRYPT_CTXINFO_HASHVALUE, hash,
&hashLength );
>
>char resultHash[hashLength];
>resultHash = copy hashLength bytes over from hash
>return resultHash[];
>
>I would like to directly initialize the char hash[] to char
>hash[lengthOfHashValue], so I don't have to do this copy operation.

I don't understand what you're trying to do here, at the end of this block of
code both hash and resultHash will contain the same data.  In addition if
you're returning a pointer to ephemeral stack data to the caller then it may
be overwritten at any point.

Peter.


_______________________________________________
Cryptlib mailing list
Cryptlib@... via Mail: cryptlib-request@...
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.

Re: Determining length of hashed value

by Ralph Holz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Peter,

> >char hash[ CRYPT_MAX_HASHSIZE ];
> >int hashLength;
> >errStatus = cryptGetAttributeString( context, CRYPT_CTXINFO_HASHVALUE,
> > hash,
>
> &hashLength );
>
> >char resultHash[hashLength];
> >resultHash = copy hashLength bytes over from hash
> >return resultHash[];
> >
> >I would like to directly initialize the char hash[] to char
> >hash[lengthOfHashValue], so I don't have to do this copy operation.
>
> I don't understand what you're trying to do here, at the end of this block
> of code both hash and resultHash will contain the same data.  In addition

This code is part of a method that hashes some data and returns an array with
the hash value.

Now, my understanding is that the char[] hash is initialized to a size of
CRYPT_MAX_HASHSIZE, i.e. it would normally allocate more bytes than required.
I guess that's why your cryptGetAttributeString returns the length of the
hash value as well, so you can extract the actual (shorter) hash value from
that array.

However, I would like to return an array that contains exactly the hash value,
without having to indicate how much of that array must be read to obtain the
hash value.

Regards,
Ralph

_______________________________________________
Cryptlib mailing list
Cryptlib@... via Mail: cryptlib-request@...
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.

Re: Determining length of hashed value

by Peter Gutmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

ralph-cryptlib@... writes:

>However, I would like to return an array that contains exactly the hash value,
>without having to indicate how much of that array must be read to obtain the
>hash value.

You can read the length without reading the value using the method described
in "Working with Object Attributes" on p.35 of the manual, however the
overhead of calling cryptGetAttributeString() twice and/or of calling malloc()
to allocate the correct-sized data block is going to be vastly higher than the
12 bytes you'll save by doing this (in fact because of the overhead of the
heap data structures and the granularity of the allocator you won't save any
memory at all).

Peter.


_______________________________________________
Cryptlib mailing list
Cryptlib@... via Mail: cryptlib-request@...
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.

Re: Determining length of hashed value

by Ralph Holz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear Peter,

> You can read the length without reading the value using the method
> described in "Working with Object Attributes" on p.35 of the manual,
> however the overhead of calling cryptGetAttributeString() twice and/or of
> calling malloc() to allocate the correct-sized data block is going to be
> vastly higher than the 12 bytes you'll save by doing this (in fact because
> of the overhead of the heap data structures and the granularity of the
> allocator you won't save any memory at all).

OK, that answer helps, thanks.

Ralph


--
For contact details, please see www.ralphholz.de.


_______________________________________________
Cryptlib mailing list
Cryptlib@... via Mail: cryptlib-request@...
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.

signature.asc (196 bytes) Download Attachment
LightInTheBox - Buy quality products at wholesale price!