Hi,
I am working on a proprietary ASIC chip that requires the memory buffers passed to it to be 16 bit aligned. Does Openssl support 16-bit alignment of the memory being allocated or is there any way that I can force 16 bit alignment of the data?
-Riaz
|
Isn't this the job of the compiler?
I don't believe that OpenSSL goes out of it's way to access
values larger than 8-bits on unaligned addresses. One exception is the
hand-optimised crypto routines, such as AES, where 32-bit values may be accessed
on arbitrary addresses. Check out the macros in aes_locl.h for
examples of handling this properly for processors that require it. Typical
RISC processors are more restrictive than what you're asking for, eg. expecting
32-bit values to be aligned to 32-bit addresses, and these are definitely
supported by OpenSSL. The ARM processor is one example. I did the
port for this (for Windows CE) and ran into this problem with the AES
module. If you search for use of the OPENSSL_SYS_WINCE macro which I added
to aes_locl.h you may find other places where this was necessary, which I've now
forgotten.
Your compiler should
be generating code that packs structures appropriately for the target
processor. Except for the optimisations mentioned above, OpenSSL simply
accesses the fields within the structure, so it's the job of the compiler to
ensure that they are aligned appropriately. And it's the job of the
runtime system to ensure that the memory is allocated on an appropriate boundary
(typically a 16-byte boundary).
Regards,
Steven
From: [hidden email] [mailto:[hidden email]] On Behalf Of Riaz Farnaz Sent: Monday, 4 July 2005 4:04 PM To: [hidden email] Subject: does openssl allocate memory with 16 bit alignment? Hi,
I am working on a proprietary ASIC chip that requires the
memory buffers passed to it to be 16 bit aligned. Does Openssl support
16-bit alignment of the memory being allocated or is there any way that I can
force 16 bit alignment of the data?
-Riaz
|
Riaz, I think I misread/misunderstood your question.
OpenSSL uses malloc to allocate memory. malloc should return an allocated
buffer which is aligned appropriately for the underlying platform.
Typically this alignment will be greater than you require, such as to a 16-byte
boundary. From: [hidden email] [mailto:[hidden email]] On Behalf Of Steven Reddie Sent: Monday, 4 July 2005 4:23 PM To: [hidden email] Subject: RE: does openssl allocate memory with 16 bit alignment? Isn't this the job of the compiler?
I don't believe that OpenSSL goes out of it's way to access
values larger than 8-bits on unaligned addresses. One exception is the
hand-optimised crypto routines, such as AES, where 32-bit values may be accessed
on arbitrary addresses. Check out the macros in aes_locl.h for
examples of handling this properly for processors that require it. Typical
RISC processors are more restrictive than what you're asking for, eg. expecting
32-bit values to be aligned to 32-bit addresses, and these are definitely
supported by OpenSSL. The ARM processor is one example. I did the
port for this (for Windows CE) and ran into this problem with the AES
module. If you search for use of the OPENSSL_SYS_WINCE macro which I added
to aes_locl.h you may find other places where this was necessary, which I've now
forgotten.
Your compiler should
be generating code that packs structures appropriately for the target
processor. Except for the optimisations mentioned above, OpenSSL simply
accesses the fields within the structure, so it's the job of the compiler to
ensure that they are aligned appropriately. And it's the job of the
runtime system to ensure that the memory is allocated on an appropriate boundary
(typically a 16-byte boundary).
Regards,
Steven
From: [hidden email] [mailto:[hidden email]] On Behalf Of Riaz Farnaz Sent: Monday, 4 July 2005 4:04 PM To: [hidden email] Subject: does openssl allocate memory with 16 bit alignment? Hi,
I am working on a proprietary ASIC chip that requires the
memory buffers passed to it to be 16 bit aligned. Does Openssl support
16-bit alignment of the memory being allocated or is there any way that I can
force 16 bit alignment of the data?
-Riaz
|
Malloc doesnt guarantee an alignment of any sort. ASIC chip requres that the buffer address that is passed to it should be a strictly 16 bit ( not byte ) aligned...I was going thru this link
http://sources.redhat.com/ml/crossgcc/2000-08/msg00049.html and here he has suggested a method of defining
#define HANDLE_PRAGMA_PACK_PUSH_POP 1 rebuild the compiler and
use #pragma pack(push,<2>) for 2 byte ( which I believe is 16 bit alignment ) alignment. So basically I think i can create a macro for the openssl malloc function to {
#pragma pack(push,<2>)
malloc(....)
}
I am not sure if this will work, this is mainly for gcc compiler, so this is a compiler dependent solution. I have to yet check this.
On 7/4/05, Steven Reddie <[hidden email]> wrote:
|
If you are using libc for malloc then using pragma requires
recompilation of libc. So even if you use the pragma in openssl it will not force the alignment in libc. Correct me if my understanding of what you meant was wrong. In case if you use glibc, here is something that may be of use to you. Glibc uses alignment of the size of double. It is hard coded. So you have to change the code and recompile glibc for your use. As size of double is generally a multiple of 2 bytes in most places you may not need any recompilation. Check it out here: http://www.itborder.com/SOFTWARE/linux/gnu/glibc-2.3.2/glibc-2.3.2/elf/dl-minimal.c reg JB On 7/4/05, Riaz Farnaz <[hidden email]> wrote: > Malloc doesnt guarantee an alignment of any sort. ASIC chip requres that the > buffer address that is passed to it should be a strictly 16 bit ( not byte ) > aligned...I was going thru this link > http://sources.redhat.com/ml/crossgcc/2000-08/msg00049.html > and here he has suggested a method of defining > > #define HANDLE_PRAGMA_PACK_PUSH_POP 1 rebuild the compiler and > > use #pragma pack(push,<2>) for 2 byte ( which I believe is 16 bit alignment > ) alignment. So basically I think i can create a macro for the openssl > malloc function to > > { > #pragma pack(push,<2>) > malloc(....) > } > > I am not sure if this will work, this is mainly for gcc compiler, so this is > a compiler dependent solution. I have to yet check this. > > > On 7/4/05, Steven Reddie <[hidden email]> wrote: > > > > Riaz, I think I misread/misunderstood your question. OpenSSL uses malloc > to allocate memory. malloc should return an allocated buffer which is > aligned appropriately for the underlying platform. Typically this alignment > will be greater than you require, such as to a 16-byte boundary. > > > > ________________________________ > From: [hidden email] [mailto: > [hidden email]] On Behalf Of Steven Reddie > > Sent: Monday, 4 July 2005 4:23 PM > > To: [hidden email] > > Subject: RE: does openssl allocate memory with 16 bit alignment? > > > > > > > > > > Isn't this the job of the compiler? > > > > I don't believe that OpenSSL goes out of it's way to access values larger > than 8-bits on unaligned addresses. One exception is the hand-optimised > crypto routines, such as AES, where 32-bit values may be accessed on > arbitrary addresses. Check out the macros in aes_locl.h for examples of > handling this properly for processors that require it. Typical RISC > processors are more restrictive than what you're asking for, eg. expecting > 32-bit values to be aligned to 32-bit addresses, and these are definitely > supported by OpenSSL. The ARM processor is one example. I did the port for > this (for Windows CE) and ran into this problem with the AES module. If you > search for use of the OPENSSL_SYS_WINCE macro which I added to aes_locl.h > you may find other places where this was necessary, which I've now > forgotten. > > > > Your compiler should be generating code that packs structures > appropriately for the target processor. Except for the optimisations > mentioned above, OpenSSL simply accesses the fields within the structure, so > it's the job of the compiler to ensure that they are aligned appropriately. > And it's the job of the runtime system to ensure that the memory is > allocated on an appropriate boundary (typically a 16-byte boundary). > > > > Regards, > > > > Steven > > > > > > ________________________________ > From: [hidden email] [mailto: > [hidden email]] On Behalf Of Riaz Farnaz > > Sent: Monday, 4 July 2005 4:04 PM > > To: [hidden email] > > Subject: does openssl allocate memory with 16 bit alignment? > > > > > > > > Hi, > > > > I am working on a proprietary ASIC chip that requires the memory > buffers passed to it to be 16 bit aligned. Does Openssl support 16-bit > alignment of the memory being allocated or is there any way that I can force > 16 bit alignment of the data? > > > > -Riaz > > OpenSSL Project http://www.openssl.org User Support Mailing List [hidden email] Automated List Manager [hidden email] |
In reply to this post by Riaz Farnaz
> Malloc doesnt guarantee an alignment of any sort.
Yes it does. It guarantees that what it returns can be used for worst-case alignment so that anything will work, as in int* foo = (int*)malloc(sizeof *foo); *foo = 42; free(foo); In this particular case, malloc may return "three" extra bytes, knowing that they will be wasted by the cast. But then it has to allow for that in the implementation of free(), and that's very hard to do. So malloc returns a pointer that is already worst-case aligned. /r$ -- Rich Salz Chief Security Architect DataPower Technology http://www.datapower.com XS40 XML Security Gateway http://www.datapower.com/products/xs40.html ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List [hidden email] Automated List Manager [hidden email] |
In reply to this post by Riaz Farnaz
Riaz,
I think you'll find that malloc does do the right
thing. Otherwise code such as the following would fail depending on the
alignment requirements of the underlying platform:
double* p =
(double*)malloc(sizeof(double));
*p = 3.14;
You shouldn't need to
mess with alignment pragmas. These are usually used when you want to
reduce the alignment, not increase it, such as when needing to pack data to
match the format expected by communication protocols or storage formats.
They're typically used for packing data more compactly rather than
sparsely.
The compiler and runtime library need to have been
targeted for the underlying platform and as such should have intimate knowledge
of alignment issues for that platform. If
you are having an actual problem, rather than simply worrying about the safety
of the code on your platform, I'd suggest that you have a poorly
targeted compiler and/or runtime library. Try working with the code
above. If it does fail on your platform then you need to get that working
before worrying about OpenSSL. If a malloc of 2 bytes ever returns an odd
address then, again, you're looking in the wrong place by worrying about
OpenSSL.
Steven
From: [hidden email] [mailto:[hidden email]] On Behalf Of Riaz Farnaz Sent: Monday, 4 July 2005 7:51 PM To: [hidden email] Subject: Re: does openssl allocate memory with 16 bit alignment? Malloc doesnt guarantee an alignment of any sort. ASIC chip requres that
the buffer address that is passed to it should be a strictly 16 bit ( not
byte ) aligned...I was going thru this link http://sources.redhat.com/ml/crossgcc/2000-08/msg00049.html and
here he has suggested a method of defining
#define HANDLE_PRAGMA_PACK_PUSH_POP 1 rebuild the compiler and
use #pragma pack(push,<2>) for 2 byte ( which I believe is 16 bit alignment ) alignment. So basically I think i can create a macro for the openssl malloc function to {
#pragma pack(push,<2>)
malloc(....)
}
I am not sure if this will work, this is mainly for gcc compiler, so this
is a compiler dependent solution. I have to yet check this.
On 7/4/05, Steven
Reddie <[hidden email]>
wrote:
|
Hi all,
thanks for your replies.
Yes it will be aligning for data but what I am concerned is about buffers. Presently I am testing on hardware acceleration chip and I have allocated an intermediate buffer using a cacheDmaMalloc which is a 32 bit aligned by default and after I memcpy the contents to the newly allocated buffer I pass the address of the newly allocated buffer to the hardware.
This should solve my problem. I have a question here..does anyone know which functions does the do_cipher function pointer point to ?
Cheers
Riaz
On 7/5/05, Steven Reddie <[hidden email]> wrote:
|
That depends on which cipher you're talking about.
The definition reveals that it's a member of a structure named evp_cipher_st
which is typedef'd as EVP_CIPHER. Grep for EVP_CIPHER in evp.h and you'll
see there are a lot of implementations of this structure.
Steven From: [hidden email] [mailto:[hidden email]] On Behalf Of Riaz Farnaz Sent: Wednesday, 6 July 2005 12:41 AM To: [hidden email] Subject: Re: does openssl allocate memory with 16 bit alignment? Hi all,
thanks for your replies.
Yes it will be aligning for data but what I am concerned is
about buffers. Presently I am testing on hardware acceleration chip and I have
allocated an intermediate buffer using a cacheDmaMalloc which is a 32 bit
aligned by default and after I memcpy the contents to the newly allocated buffer
I pass the address of the newly allocated buffer to the hardware.
This should solve my problem. I have a question here..does anyone know
which functions does the do_cipher function pointer point to ?
Cheers
Riaz
On 7/5/05, Steven
Reddie <[hidden email]>
wrote:
|
Free forum by Nabble | Edit this page |