[plt-scheme] FFI - Implicit binding of _int to the correct _int32 or _int64

From: Ray Racine (ray.racine at comcast.net)
Date: Wed Sep 26 22:04:19 EDT 2007

On Wed, 2007-09-26 at 21:24 -0400, Eli Barzilay wrote:
> On Sep 26, Ray Racine wrote:
> > 
> > man hmac
> >  #include <openssl/hmac.h>
> > 
> >         unsigned char *HMAC(const EVP_MD *evp_md, const void *key,
> >                       int key_len, const unsigned char *d, int n,
> >                       unsigned char *md, unsigned int *md_len);
> > 
> > Changing back to _int and using (_ptr o _int) the following is working
> > for me.  Am I using (_ptr o _int) appropriately here?
> > 
> >   (define HMAC-SHA1/raw
> >     (get-ffi-obj 'HMAC openssl-crypto
> >                  (_fun [EVP_MD : _fpointer = (EVP_SHA1)]
> >                        [key : _bytes]
> >                        [key_len : _int = (bytes-length key)]
> >                        [data : _bytes]
> >                        [data_len : _int = (bytes-length data)]
> >                        [md : _int = 0]
> >                        [md_len : (_ptr o _int)]
> >                        ->
> >                        _pointer)))
> 
> This doesn't look right to me.  `md' should be a pointer, not an
> integer, right?  Also, you're not using the resulting `md_len'.  (But
> maybe you don't need to, if it returns the pointer too, and if it
> points to a NUL-terminated string.)
> 

Here is the rest of the story from the man page.

"HMAC() computes the message authentication code of the n bytes at d
using the hash function evp_md and the key key which is key_len bytes
long.

It places the result in md (which must have space for the output of the
hash function, which is no more than EVP_MAX_MD_SIZE bytes).  If md is
NULL, the digest is placed in a static array.  The size of the output is
placed in md_len, unless it is NULL."


I think what Jay was trying to do with
[md : _int = 0]
[md_len : _int = 0]

was to pass in NULL for the pointer values.  Then unsigned char *HMAC
returns the digest in a static array.  SHA-1 is a fixed length hash of
20 bytes.  So we don't care about md or md_len and the goal then becomes
how to call the function with both md and md_len as NULL pointers.

What Jay did works when sizeof _int = sizeof _pointer.  But it breaks
(segfaults) on a 64 bit machine.

What I (think) I want to do here is something like the following:

(define HMAC-SHA1/raw
     (get-ffi-obj 'HMAC openssl-crypto
                  (_fun [EVP_MD : _fpointer = (EVP_SHA1)]
                        [key : _bytes]
                        [key_len : _int = (bytes-length key)]
                        [data : _bytes]
                        [data_len : _int = (bytes-length data)]
                        [md : _pointer = NULL]  
                        [md_len : _pointer = NULL]
                        ->
                        _pointer)))


But this is not valid FFI syntax.  Neither is [md: _pointer = 0]


[md : _int64 = 0]
[md_len : _int64 = 0] 

works because I'm back to punning sizeof _int64 = sizeof _pointer.  But
I've lost generic 32/64 code.

Suggestions????






Posted on the users mailing list.