linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* AW: Best way to align key in AES context
       [not found] <12EF8D94C6F8734FB2FF37B9FBEDD1735F9DD863@EXCHANGE.collogia.de>
@ 2015-02-11 10:28 ` Markus Stockhausen
  2015-02-13 14:47   ` Tadeusz Struk
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Stockhausen @ 2015-02-11 10:28 UTC (permalink / raw)
  To: linux-crypto@vger.kernel.org

[-- Attachment #1: Type: text/plain, Size: 603 bytes --]

Hi,

I want to ensure that the key data in an AES ctx structure is 8 byte aligned
to avoid aligment exceptions afterwards. Other fields don't need that
restriction. At the moment I'm using the following (ugly) implementation.

struct ppc_aes_ctx {
        u32 rounds;
        u32 *key_enc;
        u32 *key_dec;
        char data[AES_MAX_KEYLENGTH * 2 + 8];
};
...
char *ptr;
ptr = ctx->data;
ptr = PTR_ALIGN(ptr, 8);
ctx->key_enc = (u32 *)(ptr);
ctx->key_dec = (u32 *)(ptr + AES_MAX_KEYLENGTH);

Can anyone show me the recommended way for doing that.

Thanks in advance.

Markus=

[-- Attachment #2: InterScan_Disclaimer.txt --]
[-- Type: text/plain, Size: 1650 bytes --]

****************************************************************************
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail
irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und
vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser Mail ist nicht gestattet.

Über das Internet versandte E-Mails können unter fremden Namen erstellt oder
manipuliert werden. Deshalb ist diese als E-Mail verschickte Nachricht keine
rechtsverbindliche Willenserklärung.

Collogia
Unternehmensberatung AG
Ubierring 11
D-50678 Köln

Vorstand:
Kadir Akin
Dr. Michael Höhnerbach

Vorsitzender des Aufsichtsrates:
Hans Kristian Langva

Registergericht: Amtsgericht Köln
Registernummer: HRB 52 497

This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorized copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.

e-mails sent over the internet may have been written under a wrong name or
been manipulated. That is why this message sent as an e-mail is not a
legally binding declaration of intention.

Collogia
Unternehmensberatung AG
Ubierring 11
D-50678 Köln

executive board:
Kadir Akin
Dr. Michael Höhnerbach

President of the supervisory board:
Hans Kristian Langva

Registry office: district court Cologne
Register number: HRB 52 497

****************************************************************************

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: AW: Best way to align key in AES context
  2015-02-11 10:28 ` AW: Best way to align key in AES context Markus Stockhausen
@ 2015-02-13 14:47   ` Tadeusz Struk
  2015-02-13 16:49     ` AW: " Markus Stockhausen
  0 siblings, 1 reply; 4+ messages in thread
From: Tadeusz Struk @ 2015-02-13 14:47 UTC (permalink / raw)
  To: Markus Stockhausen; +Cc: linux-crypto@vger.kernel.org

On 02/11/2015 02:28 AM, Markus Stockhausen wrote:
> I want to ensure that the key data in an AES ctx structure is 8 byte aligned
> to avoid aligment exceptions afterwards. Other fields don't need that
> restriction. At the moment I'm using the following (ugly) implementation.
> 
> struct ppc_aes_ctx {
>         u32 rounds;
>         u32 *key_enc;
>         u32 *key_dec;
>         char data[AES_MAX_KEYLENGTH * 2 + 8];
> };
> ...
> char *ptr;
> ptr = ctx->data;
> ptr = PTR_ALIGN(ptr, 8);
> ctx->key_enc = (u32 *)(ptr);
> ctx->key_dec = (u32 *)(ptr + AES_MAX_KEYLENGTH);
> 
> Can anyone show me the recommended way for doing that.


You can use gcc attributes.

struct ppc_aes_ctx {
        u8 key_enc[AES_MAX_KEYLENGTH];
        u8 key_dec[AES_MAX_KEYLENGTH];
        u32 rounds;
} __aligned(8);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* AW: AW: Best way to align key in AES context
  2015-02-13 14:47   ` Tadeusz Struk
@ 2015-02-13 16:49     ` Markus Stockhausen
  2015-02-14  0:59       ` Tadeusz Struk
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Stockhausen @ 2015-02-13 16:49 UTC (permalink / raw)
  To: Tadeusz Struk; +Cc: linux-crypto@vger.kernel.org

[-- Attachment #1: Type: text/plain, Size: 2598 bytes --]

> Von: linux-crypto-owner@vger.kernel.org [linux-crypto-owner@vger.kernel.org]&quot; im Auftrag von &quot;Tadeusz Struk [tadeusz.struk@intel.com]
> Gesendet: Freitag, 13. Februar 2015 15:47
> An: Markus Stockhausen
> Cc: linux-crypto@vger.kernel.org
> Betreff: Re: AW: Best way to align key in AES context
> 
> On 02/11/2015 02:28 AM, Markus Stockhausen wrote:
> > I want to ensure that the key data in an AES ctx structure is 8 byte aligned
> > to avoid aligment exceptions afterwards. Other fields don't need that
> > restriction. At the moment I'm using the following (ugly) implementation.
> >
> > struct ppc_aes_ctx {
> >         u32 rounds;
> >         u32 *key_enc;
> >         u32 *key_dec;
> >         char data[AES_MAX_KEYLENGTH * 2 + 8];
> > };
> > ...
> > char *ptr;
> > ptr = ctx->data;
> > ptr = PTR_ALIGN(ptr, 8);
> > ctx->key_enc = (u32 *)(ptr);
> > ctx->key_dec = (u32 *)(ptr + AES_MAX_KEYLENGTH);
> >
> > Can anyone show me the recommended way for doing that.
> 
> 
> You can use gcc attributes.
> 
> struct ppc_aes_ctx {
>         u8 key_enc[AES_MAX_KEYLENGTH];
>         u8 key_dec[AES_MAX_KEYLENGTH];
>         u32 rounds;
> } __aligned(8);

Hi Tadeusz,

thanks for the tip.  I will at least move the data definitions to the beginning of 
my structure.

But while it sounds logical for data types that are directly created from that 
structure I'm unsure about a context. If I understand it correctly a context is 
dynamically allocated. Could it be possible that the start address is then only 
4 bytes aligned?

So I tried to dive into the magic of alignmask and contexts. This even confuses
me a little bit more. Nearly all implementations in the crypto tree make use of
alignmasks >= 3. I guess to be at least 4 bytes aligned. But when accessing the
context they "only" use crypto_blkcipher_ctx(). This will just return the context
address while crypto_blkcipher_ctx_aligned() seems to be the right place to 
return a pointer that is aligned according to the predefined mask.

Would you recommend to

a) ignore alignmask and use only __aligned(8) for the structure

b) to assume ctx will be automatically 8 bytes aligned regardless of __aligned(8)
flag or cra_alignmask. So nothing to take care about.

b) or to define structure without __aligned(8) but work with cra_alignmask=7. 
I fear that this might impose alignment of input/output data to 8 too and lead 
to unneccessary memcpy() operations.

Sorry for driving myself nuts but I want to understand if before I send patches.

Best regards.

Markus
=

[-- Attachment #2: InterScan_Disclaimer.txt --]
[-- Type: text/plain, Size: 1650 bytes --]

****************************************************************************
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte
Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail
irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und
vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe dieser Mail ist nicht gestattet.

Über das Internet versandte E-Mails können unter fremden Namen erstellt oder
manipuliert werden. Deshalb ist diese als E-Mail verschickte Nachricht keine
rechtsverbindliche Willenserklärung.

Collogia
Unternehmensberatung AG
Ubierring 11
D-50678 Köln

Vorstand:
Kadir Akin
Dr. Michael Höhnerbach

Vorsitzender des Aufsichtsrates:
Hans Kristian Langva

Registergericht: Amtsgericht Köln
Registernummer: HRB 52 497

This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorized copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.

e-mails sent over the internet may have been written under a wrong name or
been manipulated. That is why this message sent as an e-mail is not a
legally binding declaration of intention.

Collogia
Unternehmensberatung AG
Ubierring 11
D-50678 Köln

executive board:
Kadir Akin
Dr. Michael Höhnerbach

President of the supervisory board:
Hans Kristian Langva

Registry office: district court Cologne
Register number: HRB 52 497

****************************************************************************

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: AW: AW: Best way to align key in AES context
  2015-02-13 16:49     ` AW: " Markus Stockhausen
@ 2015-02-14  0:59       ` Tadeusz Struk
  0 siblings, 0 replies; 4+ messages in thread
From: Tadeusz Struk @ 2015-02-14  0:59 UTC (permalink / raw)
  To: Markus Stockhausen; +Cc: linux-crypto@vger.kernel.org

On 02/13/2015 08:49 AM, Markus Stockhausen wrote:
> thanks for the tip.  I will at least move the data definitions to the beginning of 
> my structure.
> 
> But while it sounds logical for data types that are directly created from that 
> structure I'm unsure about a context. If I understand it correctly a context is 
> dynamically allocated. Could it be possible that the start address is then only 
> 4 bytes aligned?
> 
> So I tried to dive into the magic of alignmask and contexts. This even confuses
> me a little bit more. Nearly all implementations in the crypto tree make use of
> alignmasks >= 3. I guess to be at least 4 bytes aligned. But when accessing the
> context they "only" use crypto_blkcipher_ctx(). This will just return the context
> address while crypto_blkcipher_ctx_aligned() seems to be the right place to 
> return a pointer that is aligned according to the predefined mask.
> 
> Would you recommend to
> 
> a) ignore alignmask and use only __aligned(8) for the structure
> 
> b) to assume ctx will be automatically 8 bytes aligned regardless of __aligned(8)
> flag or cra_alignmask. So nothing to take care about.
> 
> b) or to define structure without __aligned(8) but work with cra_alignmask=7. 
> I fear that this might impose alignment of input/output data to 8 too and lead 
> to unneccessary memcpy() operations.
> 
> Sorry for driving myself nuts but I want to understand if before I send patches.

In this case I think the best way is to define your algorithm with

	.cra_ctxsize = sizeof(struct ppc_aes_ctx) + 8,

and then in enc/dec/setkey do

	struct ppc_aes_ctx *ctx = PTR_ALIGN(crypto_tfm_ctx(tfm), 8);

and you don't need the __aligned(8) in the struct definition.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-02-14  1:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <12EF8D94C6F8734FB2FF37B9FBEDD1735F9DD863@EXCHANGE.collogia.de>
2015-02-11 10:28 ` AW: Best way to align key in AES context Markus Stockhausen
2015-02-13 14:47   ` Tadeusz Struk
2015-02-13 16:49     ` AW: " Markus Stockhausen
2015-02-14  0:59       ` Tadeusz Struk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).