Use of the AES instruction set ? - - PowerPoint PPT Presentation

use of the aes instruction set
SMART_READER_LITE
LIVE PREVIEW

Use of the AES instruction set ? - - PowerPoint PPT Presentation

?


slide-1
SLIDE 1

文 語 選 ロ 日 ー ル カ 日 ロ ロ を 文 コ 本 ト コ で コ 、 で ー ネ 選 ル 日 ロ を ン で ル ネ ロ を カ の ん 本 ¡ で ¿ ¿ 文 パ 本 ル 語 で を ル 文 で 本 、 ト 、 ル 本 ¿ 文 ー の ー の ¿ 語 ル を を ¡ ル の 本 ¡ ネ コ 語 カ ー ¡ 選 ロ で ー ル で ん 字 ル カ で の ¿ 文 字 ネ ロ ¿ ン ロ ロ ¿ 本 を ル ー ロ ? 文 で の で 文 ル を ン ト ー ル パ で ? で で ん 語 日 語 日 ル ん ¿ 文 字 字 ? ル 選 ト 語 ー を で ? ロ ロ 字 ¿ カ 語 で ネ ル パ コ ん を 所 コ 字 パ ? ン 字 日 ん 文 カ ー 語 で ¡ 、 ル ロ 文 ル ン 語 ル 所 、 ん 、 ネ ネ ト で ¿ ¿ ル ネ ト で ロ の ル で で で 文 パ で 、 所 コ ル ん 本 文 ネ 字 で ん ロ の の ン ネ ? ル ロ ¡ ル を ネ 文 字 ン ル 日 ー ン の で 日 ¡ ん コ ル ネ ? パ ネ 字 を の ト ル ン 所 ロ ¿ ン 所 パ ロ ー で ル 日 で で ¡ ル ¡ 選 日 パ ? で ? で パ で ト 語 字 カ ¿ 字 ? 本 選 ネ パ 語 で ル ル で ー ? カ ? ? 選 ー 選 コ ¿ 選 で ¡ ル で 、 の ー で ル ン を ト ン パ ロ で カ の カ 文 選 で ト ト 字 ー ル 字 ト ロ ー ー ¿ コ 本 ん 文 で ネ ¡ ロ 、 で ロ で ー ー ル ? ル 所 で コ 語 ル ル 文 ン パ 所 選 ? で ? を ん ? 、 語 本 ー 本 日 選 ネ ¿ 選 ー ¡ ル ト ん ル コ ル カ ル カ 所 選 ル 日 ん 日 ¿ ¡ 字 ー カ ネ パ コ 所 の ト の ネ 、 ロ 日 ¡ ル で ロ ロ ¿ ? 語 ー ネ で ¡ で ー で ー パ で 、 ¿ で で を ロ ン ー カ ロ の ネ ト 所 カ ¿ の 字 カ 日 ¿ ¿ ? ト 語 を 、 選 所 ネ 字 ル ¿ ル ネ 本 ロ ん 語 字 ー ー で ー ? ン ル ¿ 日 の ル ー で で で ロ ¿ 語 日 カ で ん パ ロ ン カ カ カ ロ ー パ を ル ん ん 本 日 ん の ー 本 ル 所 で ン 語 コ の ? ル ネ ん 選 選 ¡ で カ ¡ 語 パ ン パ 字 所 コ 字 の 日 日 、 で 本 で ? 語 ル ネ 本 ん ー 語 語 ン ト の ? で ロ ル ー ン 所 文 ん ? ¿ 字 ネ ー パ 所 ー コ 文 で 選 、 の ー ロ ? 文 ロ 、 ー で 本 ー で 語 所 ネ ー パ コ ン ル ト の 字 、 日 ん ル ¿ カ ? ン ン ン パ を ル 日 文 本 ル を を ロ パ の 語 パ ¡ ¿ 日 ン ロ を 本 文 ロ 本 選 選 ル ル 文 日 ト で 選 日 を ロ 所 ル コ ル ル で ト で を ん で 選 ? ル 字 ん 選 コ ¿ ル を コ で 字 ト 日 選 文 ? ン ン ¡ ー 選 ー ト で 本 本 日 で 、 ル 本 ン ネ で ロ ¡ ト 語 ル ー 選 の ト ー ¡ 字 カ 、 本 選 文 ン 、 の ¡ 選 所 ル パ ー ー ネ ¡ ん ? で を パ 所 ル ト 語 の で パ 選 日 ¿ カ 、 コ ー パ の ¡ 文 で ン パ ル 文 選 ? ル ー ん ー を 日 日 コ ネ 語 所 文 日 ト 選 で ン ロ を 文 ? 本 で ン ル の 日 ル ¿ で カ ト ん ー コ ロ ん 日 字 、 本 字 ル で の ¿ カ ¡ ロ ル で ー 所 で 本 で ル ¿ ル 日 カ ー カ を 本 ル の ネ 本 コ ル 本 ル ¡ 本 を を を ん パ で ん で ロ 所 ロ 字 ー ? ル コ ル ネ ん を 所 ル を ? ー ー パ パ ロ ロ 所 を 文 ロ ¡ ル カ 字 ロ パ ¿ で ん ん ¿ を ル 選 の の ロ ー で ¡ 所 ー ん 、 ル ロ で 文 で ロ ル 字 文 で ¡ ロ ロ 所 ル 字 ん ? 字 本 ー ¡ ル 所 コ ル ネ ー 字 ¡ ル ル 本 カ ? ? パ 日 所 日 、 で ロ 選 ー ん ル ロ ん コ 本 日 ¡ ネ カ コ で ン 本 パ で ト ル ト 選 ル 、 ー ト ¿ 本 の パ ン 所 で 所 字 ル ロ で 日 ン ル ネ 日 日 パ カ ¡ 文 ネ 日 字 文 日 語 ロ ー 文 選 本 ロ 文 ? 所 ロ ロ ¡ カ ー 文 カ カ ー カ ネ で ー ¡ ¡ ー を パ 所 で 日 ー ル ー で 日 ¿ ル ? 選 ん ル ¡ 文 ル で で 本 語 カ ネ ー ー ん ネ ネ ー で ネ 日 文 ル で ト 所 文 ン コ 所 パ の ト 、 語 ル 選 選 で パ 字 ロ ル ト ¿ で で パ ー パ で ル ロ 語 を で 日 ル ネ ロ パ 本 所 ロ ロ 所 コ ー ル ネ 文 所 日 選 選 ネ 字 で 字 パ カ ト ロ 本 ロ ? ル ー 文 ん ル ロ の カ 日 ロ ¡ ン ル を 所 文 ト ー ー ロ ? ン ロ を ー ト ル コ 文 ¡ カ コ ネ ー 所 ネ 字 日 ー ト ト ロ 語 ル ん で 選 、 パ ー ¡ で 字 ー ル ん で 、 ー ト ー 所 字 で 所 ? 、 字 カ 日 文 語 選 ロ 日 カ 日 ロ ロ を 文 コ 本 ト コ で コ 、 で ー ネ 選 ル 日 ロ を ン で ル ネ ロ を カ の ん 本 ¡ で ¿ ¿ 文 パ 本 ル 語 で を ロ 本 ル 文 で 本 、 ト 、 ¿ 文 ー の ー の ¿ 語 ル を を ¡ ル の 本 ¡ ネ コ 語 カ ー ¡ 選 ロ で ー ル で ん 字 ル カ で の ¿ 文 字 ネ ロ ¿ ン ロ カ カ ロ ¿ 本 を ル ー ロ で の で 文 ル を ン ト ー ル パ で ? で で ん 語 日 語 日 ル ん ¿ 文 字 字 ? ル 選 ト 語 ー を で ? ロ ロ 字 ¿ カ 語 で コ 字 コ ネ ル パ コ ん を 所 パ ? ン 字 日 ん 文 カ ー 語 で ¡ 、 ル ロ 文 ル ン 語 ル 所 、 ん 、 ネ ネ ト で ¿ ¿ ル ネ ト で ロ の ル で で で 文 パ で ー ー で 、 所 コ ル ん 本 字 で ん ロ の の ン ネ ? ル ロ ¡ ル を ネ 文 字 ン ル 日 ー ン の で 日 ¡ ん コ ル ネ ? パ ネ 字 を の ト ル ン 所 ロ ¿ 語 ル ン 所 パ ロ ー で ル で ¡ ル ¡ 選 日 パ ? で ? で パ で ト 語 字 カ ¿ 字 ? 本 選 ネ パ 語 で ル ル で ー ? カ ? ? 選 ー 選 コ ¿ 選 で ¡ ネ ロ 本 ル で 、 の ー で ル ト ン パ ロ で カ の カ 文 選 で ト ト 字 ー ル 字 ト ロ ー ー ¿ コ 本 ん 文 で ネ ¡ ロ 、 で ロ で ー ー ル ? ル 所 で コ ロ ン 語 ル ル 文 ン パ 所 選 ? を ん ? 、 語 本 ー 本 日 選 ネ ¿ 選 ー ¡ ル ト ん ル コ ル カ ル カ 所 選 ル 日 ん 日 ¿ ¡ 字 ー カ ネ パ コ 所 の ト ロ ん ー の ネ 、 ロ 日 ¡ ル ロ ¿ ? 語 ー ネ で ¡ で ー で ー パ で 、 ¿ で で を ロ ン ー カ ロ の ネ ト 所 カ ¿ の 字 カ 日 ¿ ¿ ? ト 語 を 、 選 ロ 選 所 所 ネ 字 ル ¿ ル ネ ん 語 字 ー ー で ー ? ン ル ¿ 日 の ル ー で で で ロ ¿ 語 日 カ で ん パ ロ ン カ カ カ ロ ー パ を ル ん ん 本 日 ん の ル 日 所 ー 本 ル 所 で ン 語 ? ル ネ ん 選 選 ¡ で カ ¡ 語 パ ン パ 字 所 コ 字 の 日 日 、 で 本 で ? 語 ル ネ 本 ん ー 語 語 ン ト の ? で ロ ル ー ト を ロ ン 所 文 ん ? ¿ 字 パ 所 ー コ 文 で 選 、 の ー ロ ? 文 ロ 、 ー で 本 ー で 語 所 ネ ー パ コ ン ル ト の 字 、 日 ん ル ¿ カ ? ン ン ン パ 文 ル ル を ル 日 文 本 ル を パ の 語 パ ¡ ¿ 日 ン ロ を 本 文 ロ 本 選 選 ル ル 文 日 ト で 選 日 を ロ 所 ル コ ル ル で ト で を ん で 選 ? ル 字 ん ? で カ 選 コ ¿ ル を コ で 日 選 文 ? ン ン ¡ ー 選 ー ト で 本 本 日 で 、 ル 本 ン ネ で ロ ¡ ト 語 ル ー 選 の ト ー ¡ 字 カ 、 本 選 文 ン 、 の ト ロ ¡ 選 所 ル パ ー ー ネ ? で を パ 所 ル ト 語 の で パ 選 日 ¿ カ 、 コ ー パ の ¡ 文 で ン パ ル 文 選 ? ル ー ん ー を 日 日 コ ネ 語 所 文 日 ¡ ロ コ ト 選 で ン ロ を 文 で ン ル の 日 ル ¿ で カ ト ん ー コ ロ ん 日 字 、 本 字 ル で の ¿ カ ¡ ロ ル で ー 所 で 本 で ル ¿ ル 日 カ ー カ を 語 ン コ 本 ル の ネ 本 コ ル ¡ 本 を を を ん パ で ん で ロ 所 ロ 字 ー ? ル コ ル ネ ん を 所 ル を ? ー ー パ パ ロ ロ 所 を 文 ロ ¡ ル カ 字 ロ パ ン ロ ¿ で ん ん ¿ を ル 選 ロ ー で ¡ 所 ー ん 、 ル ロ で 文 で ロ ル 字 文 で ¡ ロ ロ 所 ル 字 ん ? 字 本 ー ¡ ル 所 コ ル ネ ー 字 ¡ ル ル 本 カ で の ? ? パ 日 所 日 、 選 ー ん ル ロ ん コ 本 日 ¡ ネ カ コ で ン 本 パ で ト ル ト 選 ル 、 ー ト ¿ 本 の パ ン 所 で 所 字 ル ロ で 日 ン ル ネ コ ? 日 日

Use of the AES instruction set

(ECRYPT II AES Day - Bruges, Belgium)

Ryad BENADJILA

Agence Nationale de la Sécurité des Systèmes

d’Information 18 October 2012

slide-2
SLIDE 2

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion Use of the AES instruction set - 18 October 2012

1

AES-NI

  • a. Overview
  • b. Chronology

2

Instructions detail

  • a. xmm and SSE
  • b. Encrypt
  • c. Decrypt
  • d. Key Schedule

3

Performance

  • a. Latency/throughput
  • b. Core™ and ✖ops
  • c. Results
  • d. GCM

4

Beyond AES

  • a. Rijndael
  • b. Building blocks
  • c. SHA-3

5

Conclusion

slide-3
SLIDE 3

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

AES-NI

AES-NI stands for “AES New Instructions” Introduced by Intel as:

◮ A hardware accelerated implementation of AES subparts ◮ Ways of implementing efficient versions of the algorithm

with constant time operations, offering a mitigation against timing side channel attacks (especially cache based attacks)

1/60

Use of the AES instruction set - 18 October 2012

slide-4
SLIDE 4

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

AES-NI

AES-NI stands for “AES New Instructions” Introduced by Intel as:

◮ A hardware accelerated implementation of AES subparts ◮ Ways of implementing efficient versions of the algorithm

with constant time operations, offering a mitigation against timing side channel attacks (especially cache based attacks)

Access to the instructions from the userland (Ring3) level

1/60

Use of the AES instruction set - 18 October 2012

slide-5
SLIDE 5

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

AES-NI

AES-NI stands for “AES New Instructions” Introduced by Intel as:

◮ A hardware accelerated implementation of AES subparts ◮ Ways of implementing efficient versions of the algorithm

with constant time operations, offering a mitigation against timing side channel attacks (especially cache based attacks)

Access to the instructions from the userland (Ring3) level Six new instructions over the previous SSE4 set:

◮ 4 for encryption and decryption:

aesenc, aesdec, aesenclast and aesdeclast

◮ 2 for the Key Schedule:

aeskeygenassist and aesimc

Plus a companion carry-less multiplication instruction clmul

1/60

Use of the AES instruction set - 18 October 2012

slide-6
SLIDE 6

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

2005 2006 2007 2008 2009 2010 2011 2012 2013 “Cache-timing attacks on AES” Bernstein’s remote cache attacks on AES table based implementations

2/60

Use of the AES instruction set - 18 October 2012

slide-7
SLIDE 7

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

2005 2006 2007 2008 2009 2010 2011 2012 2013 “Cache-timing attacks on AES” Bernstein’s remote cache attacks on AES table based implementations “Cache Attacks and Countermeasures: The Case

  • f AES”

Osvik, Shamir and Tromer local cache timing attacks Software counter measures with performance im- pact Table based implementations using masking Bitslice implementations: interesting but efficiency related to block size Mastsui and Nakajima: ✘10c/B for 2KB data blocks on Core 2 CPU

3/60

Use of the AES instruction set - 18 October 2012

slide-8
SLIDE 8

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

2005 2006 2007 2008 2009 2010 2011 2012 2013 “Cache-timing attacks on AES” Bernstein’s remote cache attacks on AES table based implementations “Cache Attacks and Countermeasures: The Case

  • f AES”

Osvik, Shamir and Tromer local cache timing attacks Software counter measures with performance im- pact Table based implementations using masking Bitslice implementations: interesting but efficiency related to block size Mastsui and Nakajima: ✘10c/B for 2KB data blocks on Core 2 CPU Intel’s proposal for AES-NI instructions White Paper by Gueron AES table based implementation new speed records Bernstein and Shwabe: ✘10c/B on Core 2 (using a custom high level assembler) Bitslice implementations: very efficient SSSE3 im- plementation K¨ asper and Shwabe: ✘7c/B on Core 2 and Nehalem Core i7 by using SSSE3 new instructions, for 128 bytes data blocks

4/60

Use of the AES instruction set - 18 October 2012

slide-9
SLIDE 9

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Overview |Chronology

2005 2006 2007 2008 2009 2010 2011 2012 2013 “Cache-timing attacks on AES” Bernstein’s remote cache attacks on AES table based implementations “Cache Attacks and Countermeasures: The Case

  • f AES”

Osvik, Shamir and Tromer local cache timing attacks Software counter measures with performance im- pact Table based implementations using masking Bitslice implementations: interesting but efficiency related to block size Mastsui and Nakajima: ✘10c/B for 2KB data blocks on Core 2 CPU Intel’s proposal for AES-NI instructions White Paper by Gueron AES table based implementation new speed records Bernstein and Shwabe: ✘10c/B on Core 2 (using a custom high level assembler) Bitslice implementations: very efficient SSSE3 im- plementation K¨ asper and Shwabe: ✘7c/B on Core 2 and Nehalem Core i7 by using SSSE3 new instructions, for 128 bytes data blocks Intel’s first generation CPUs with AES-NI (and clmul) Westmere microarchitecture (not all CPUs concerned) Intel’s second generation CPUs with AES-NI, with AVX support Sandy Bridge microarchitecture (almost all CPUs) AMD’s first generation CPUs with AES-NI (with clmul and AVX support) Bulldozer microarchitecture (all CPUs) Intel’s third generation CPUs with AES-NI Ivy Bridge microarchitecture (almost all CPUs)

5/60

Use of the AES instruction set - 18 October 2012

slide-10
SLIDE 10

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

xmm and ymm registers

xmm are 128-bit registers:

◮ 8 in 32-bit mode xmm0 xmm1 xmm2 xmm3 xmm4 xmm5 xmm6 xmm7

128 bits

3 2 1 7 6 5 4 11 10 9 8 15 14 13 12

6/60

Use of the AES instruction set - 18 October 2012

slide-11
SLIDE 11

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

xmm and ymm registers

xmm are 128-bit registers:

◮ 16 in 64-bit mode xmm0 xmm1 xmm2 xmm3 xmm4 xmm5 xmm6 xmm7 xmm8 xmm9 xmm10 xmm11 xmm12 xmm13 xmm14 xmm15

128 bits

3 2 1 7 6 5 4 11 10 9 8 15 14 13 12

7/60

Use of the AES instruction set - 18 October 2012

slide-12
SLIDE 12

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

xmm and ymm registers

ymm are 256-bit registers (only in 64-bit mode):

◮ xmm extended to 256 bits with AVX new extensions ymm0 ymm1 ymm2 ymm3 ymm4 ymm5 ymm6 ymm7 ymm8 ymm9 ymm10 ymm11 ymm12 ymm13 ymm14 ymm15

256 bits

3 2 1 7 6 5 4 11 10 9 8 15 14 13 12 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 xmm = low part of ymm

8/60

Use of the AES instruction set - 18 October 2012

slide-13
SLIDE 13

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Some useful SSE instructions

SSE = Streaming SIMD (Single Instruction Multiple Data) Extensions

SSE SSE2 SSE3 SSSE3 SSE4.1 SSE4.2 AES-NI

Pentium III Pentium 4 Pentium 4 Prescott Core 2 Core 2 Penryn Nehalem Westmere

clmul SSE4a FMA,XOP,CVT16 SSE5 AVX

Sandy Bridge K6 (3D Now!) AMD64 Athlon 64 Venice Bobcat Barcelona Bulldozer Bulldozer Bulldozer Bulldozer Intel AMD 9/60

Use of the AES instruction set - 18 October 2012

slide-14
SLIDE 14

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Some useful SSE instructions

SSE instructions work on bytes, 16-bit shorts, 32-bit double words, 64-bit quad words and full 128-bit xmm words Moving memory data to and from a xmm register:

xmm1 ✥ [mem128]

  • r

[mem128] ✥ xmm2 movdqu xmm1/[mem128], [mem128]/xmm2

“Xoring” two registers or a register and memory:

xmm1 ✥ xmm1 ✟ (xmm2/[mem128]) pxor xmm1, xmm2/[mem128]

10/60

Use of the AES instruction set - 18 October 2012

slide-15
SLIDE 15

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Some useful SSE instructions

Packed Shuffle Bytes: byte-wise shuffling in xmm according to a mask in xmm (SSSE3)

for(i=0; i<16; i++){ xmm1[i] ✥ xmm1[xmm2[i]] } /∗ With xmm1[i] = 0 for i ✕ 16 ∗/ pshufb xmm1, xmm2/[mem128]

xmm1

3 2 1 7 6 5 4 11 10 9 8 15 14 13 12

xmm2/[mem128]

1 3 1 5 20 30 10 9 4 5 5 15

xmm1

1 3 1 5 10 9 4 5 5 15

... ...

11/60

Use of the AES instruction set - 18 October 2012

slide-16
SLIDE 16

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Some useful SSE instructions

Packed Shuffle Double words: shuffling in xmm according to an immediate bitmask (SSE2)

for(i=0; i<4; i++){ (double word)xmm1[i] ✥ (double word)xmm2[(imm8>>(2∗i)) & 0x3] } pshufd xmm1, xmm2/[mem128], imm8

xmm2/[mem128] xmm1 imm8 = 0xc0 1 1

12/60

Use of the AES instruction set - 18 October 2012

slide-17
SLIDE 17

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Some useful SSE instructions

Blending two 16-bit words xmm registers according to a mask (SSE4.1):

for(i=0; i<8; i++){ if((imm8>>8) & 0x1 == 1){ (short word)xmm1[i] ✥ (short word)xmm2[i] } } pblendw xmm1, xmm2/[mem128], imm8

13/60

Use of the AES instruction set - 18 October 2012

slide-18
SLIDE 18

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AVX extensions

AVX extensions use the VEX prefix that is not compatible with 32-bit mode

✥ ✟ ✥ ✟

14/60

Use of the AES instruction set - 18 October 2012

slide-19
SLIDE 19

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AVX extensions

AVX extensions use the VEX prefix that is not compatible with 32-bit mode Two main advantages over previous SSE:

◮ Twice more data in ymm, which means twice more

“vectorization”

◮ New AVX extensions allow most compatible instructions to

use 3 operands ✮ non destructive operations Legacy pxor AVX extended vpxor

xmm1 ✥ xmm1 ✟ (xmm2/[mem128]) pxor xmm1, xmm2/[mm128] xmm1 ✥ xmm2 ✟ (xmm3/[mem128]) vpxor xmm1, xmm2, xmm3/[mm128]

14/60

Use of the AES instruction set - 18 October 2012

slide-20
SLIDE 20

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AVX extensions

AVX extensions use the VEX prefix that is not compatible with 32-bit mode Two main advantages over previous SSE:

◮ Twice more data in ymm, which means twice more

“vectorization”

◮ New AVX extensions allow most compatible instructions to

use 3 operands ✮ non destructive operations Legacy pxor AVX extended vpxor

xmm1 ✥ xmm1 ✟ (xmm2/[mem128]) pxor xmm1, xmm2/[mm128] xmm1 ✥ xmm2 ✟ (xmm3/[mem128]) vpxor xmm1, xmm2, xmm3/[mm128]

However:

◮ Not all legacy instructions with VEX extension benefit

from ymm (e.g. vpshufd does, vpxor doesn’t) ✮ ymm high part is zeroed then

◮ Possible latencies during AVX and legacy SSE switch

14/60

Use of the AES instruction set - 18 October 2012

slide-21
SLIDE 21

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI encryption instructions

aesenc for rounds:

Tmp ✥ xmm1 Tmp ✥ SubBytes(Tmp) Tmp ✥ ShiftRows(Tmp) Tmp ✥ MixColumns(Tmp) xmm1 ✥ Tmp ✟ xmm2/[mem128] aesenc xmm1, xmm2/[mem128]

S(.) xmm1

4 8 12 1 5 9 13 2 6 10 14 3 7 11 15

SubBytes ShiftRows MixColumns

4 8 12 1 5 9 13 2 6 10 14 3 7 11 15

xmm2/[mem128] AddRoundKey aesenc xmm1,xmm2/[mem128]

  • <

<< <<< x

2 3 1 1 1 2 3 1 1 1 2 3 3 1 1 2

15/60

Use of the AES instruction set - 18 October 2012

slide-22
SLIDE 22

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI encryption instructions

aesenclast for the last round:

Tmp ✥ xmm1 Tmp ✥ SubBytes(Tmp) Tmp ✥ ShiftRows(Tmp) xmm1 ✥ Tmp ✟ xmm2/[mem128] aesenclast xmm1, xmm2/[mem128]

S(.) xmm1

4 8 12 1 5 9 13 2 6 10 14 3 7 11 15

SubBytes ShiftRows

4 8 12 1 5 9 13 2 6 10 14 3 7 11 15

xmm2/[mem128] AddRoundKey aesenclast xmm1, xmm2/[mem128]

  • <

<< <<<

16/60

Use of the AES instruction set - 18 October 2012

slide-23
SLIDE 23

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Block encryption

AES128 (naive) encryption of one plaintext block:

xmm0 ✥ plaintext xmm1xmm11 ✥ scheduled keys pxor xmm0, xmm1 /∗ Round 0 (whitening) ∗/ aesenc xmm0, xmm2 /∗ Round 1 ∗/ aesenc xmm0, xmm3 /∗ Round 2 ∗/ aesenc xmm0, xmm4 /∗ Round 3 ∗/ aesenc xmm0, xmm5 /∗ Round 4 ∗/ aesenc xmm0, xmm6 /∗ Round 5 ∗/ aesenc xmm0, xmm7 /∗ Round 6 ∗/ aesenc xmm0, xmm8 /∗ Round 7 ∗/ aesenc xmm0, xmm9 /∗ Round 8 ∗/ aesenc xmm0, xmm10 /∗ Round 9 ∗/ aesenclast xmm0, xmm11 /∗ Round 10 ∗/ AES128 Encryption (128-bit block)

17/60

Use of the AES instruction set - 18 October 2012

slide-24
SLIDE 24

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI decryption instructions

AES-NI implements the equivalent inverse cipher for decryption

SubBytes ShiftRows MixColumns RK SubBytes ShiftRows Last Ciphertext Plaintext First Encryption InvSubBytes InvShiftRows InvMixColumns InvMixColumns(RK) InvSubBytes InvShiftRows Last Ciphertext Plaintext First Decryption x(Nr-1) x(Nr-1)

18/60

Use of the AES instruction set - 18 October 2012

slide-25
SLIDE 25

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI decryption instructions

aesdec:

Tmp ✥ xmm1 Tmp ✥ SubBytes1(Tmp) Tmp ✥ ShiftRows1(Tmp) Tmp ✥ MixColumns1(Tmp) xmm1 ✥ Tmp ✟ xmm2/[mem128] aesdec xmm1, xmm2/[mem128]

aesdeclast:

Tmp ✥ xmm1 Tmp ✥ SubBytes1(Tmp) Tmp ✥ ShiftRows1(Tmp) xmm1 ✥ Tmp ✟ xmm2/[mem128] aesdeclast xmm1, xmm2/[mem128]

We feed aesdec with the equivalent inverse cipher keys

19/60

Use of the AES instruction set - 18 October 2012

slide-26
SLIDE 26

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Block decryption

AES128 (naive) decryption of one plaintext block:

xmm0 ✥ plaintext xmm1xmm11 ✥ scheduled keys (inverse cipher) pxor xmm0, xmm1 /∗ Round 0 (whitening) ∗/ aesdec xmm0, xmm2 /∗ Round 1 ∗/ aesdec xmm0, xmm3 /∗ Round 2 ∗/ aesdec xmm0, xmm4 /∗ Round 3 ∗/ aesdec xmm0, xmm5 /∗ Round 4 ∗/ aesdec xmm0, xmm6 /∗ Round 5 ∗/ aesdec xmm0, xmm7 /∗ Round 6 ∗/ aesdec xmm0, xmm8 /∗ Round 7 ∗/ aesdec xmm0, xmm9 /∗ Round 8 ∗/ aesdec xmm0, xmm10 /∗ Round 9 ∗/ aesdeclast xmm0, xmm11 /∗ Round 10 ∗/ AES128 Encryption (128-bit block)

20/60

Use of the AES instruction set - 18 October 2012

slide-27
SLIDE 27

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Rijndael Key Schedule

Key Schedule for AES128 and AES192

KeyExpansion(byte Key[4∗Nk] word W[Nb∗(Nr+1)]) { /∗ AES128 => (Nk=4, Nr=10, Nb=4) ∗/ for(i = 0; i < Nk; i++) W[i] = (Key[4∗i],Key[4∗i+1],Key[4∗i+2],Key[4∗i+3]); for(i = Nk; i < Nb ∗ (Nr + 1); i++) { temp = W[i 1]; if (i % Nk == 0) temp = SubByte(RotByte(temp)) ^ Rcon[i / Nk]; W[i] = W[i Nk] ^ temp; } } Rijndael Key Schedule (Nk✔6, i.e. AES128 and AES192)

KEY0 KEY1

W[0] W[1] W[2] W[3] W[4] W[5] W[6] W[7] ...

S(rot(.))+Rcon

AES128

4 bytes 21/60

Use of the AES instruction set - 18 October 2012

slide-28
SLIDE 28

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Rijndael Key Schedule

Key Schedule for AES128 and AES192

KeyExpansion(byte Key[4∗Nk] word W[Nb∗(Nr+1)]) { /∗ AES192 => (Nk=6, Nr=12, Nb=4) ∗/ for(i = 0; i < Nk; i++) W[i] = (Key[4∗i],Key[4∗i+1],Key[4∗i+2],Key[4∗i+3]); for(i = Nk; i < Nb ∗ (Nr + 1); i++) { temp = W[i 1]; if (i % Nk == 0) temp = SubByte(RotByte(temp)) ^ Rcon[i / Nk]; W[i] = W[i Nk] ^ temp; } } Rijndael Key Schedule (Nk✔6, i.e. AES128 and AES192)

KEY0 KEY1

W[0] W[1] W[2] W[3] W[6] W[7] W[8] W[9] ...

S(rot(.))+Rcon

W[4] W[5] ... ...

AES192

4 bytes 22/60

Use of the AES instruction set - 18 October 2012

slide-29
SLIDE 29

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

Rijndael Key Schedule

Key Schedule for AES256

KeyExpansion(byte Key[4∗Nk] word W[Nb∗(Nr+1)]) { /∗ AES256 => (Nk=8, Nr=14, Nb=4) ∗/ for(i = 0; i < Nk; i++) W[i] = (Key[4∗i],Key[4∗i+1],Key[4∗i+2],Key[4∗i+3]); for(i = Nk; i < Nb ∗ (Nr + 1); i++) { temp = W[i 1]; if (i % Nk == 0) temp = SubByte(RotByte(temp)) ^ Rcon[i / Nk]; else if (i % Nk == 4) temp = SubByte(temp); W[i] = W[i Nk] ^ temp; } } Rijndael Key Schedule (Nk>6, i.e. AES256)

KEY0 KEY1

W[0] W[1] W[2] W[3] W[8] W[9]

S(rot(.))+Rcon

W[4] W[5]

AES256

W[6] W[7] W[10]W[11]W[12]

S(.)

... ...

23/60

Use of the AES instruction set - 18 October 2012

slide-30
SLIDE 30

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI Key Schedule instructions

aeskeygenassist:

xmm2 := [xmm2[3]|xmm2[2]|xmm2[1]|xmm2[0]] /∗ split xmm2 in 4 bytes words ∗/ xmm1 ✥ [SubByte(RotByte(xmm2[3]))✟imm8 |SubByte(xmm2[3]) |SubByte(RotByte(xmm2[1]))✟imm8 |SubByte(xmm2[1])] aeskeygenassist xmm1, xmm2/[128], imm8

aesimc: for the equivalent inverse cipher key schedule (apply inverse MixColumns to all the keys scheduled for encryption, except first and last ones)

/∗ Round key scheduled for encryption in xmm2 ∗/ Tmp ✥ xmm2/[mem128] xmm1 ✥ MixColumns1(Tmp) aesimc xmm1, xmm2/[mem128]

24/60

Use of the AES instruction set - 18 October 2012

slide-31
SLIDE 31

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI Key Schedule for AES128

/∗ Key in xmm1 ∗/ xmm1 ✥ Key /∗ Prepare value for W[4] in xmm2 ∗/ aeskeygenassist xmm2, xmm1, Rcon /∗ Rcon=0x1 for the first iteration ∗/ /∗ We only keep the last double word SubByte(RotByte(xmm2[3]))✟Rcon ∗/ pshufd xmm2, xmm2, 0xff // movdqa xmm3, xmm1 pslldq xmm3, 0x4 /∗ W[i1] goes to W[i] place ∗/ pxor xmm1, xmm3 /∗ xor all W[i] with W[i1] ∗/ // pslldq xmm3, 0x4 /∗ W[i2] goes to W[i] place ∗/ pxor xmm1, xmm3 /∗ xor all W[i] with W[i2] ∗/ // pslldq xmm3, 0x4 /∗ W[i3] goes to W[i] place ∗/ pxor xmm1, xmm3 /∗ xor all W[i] with W[i3] ∗/ /∗ Finalize ∗/ pxor xmm1, xmm2 // KeySchedule[16∗i] ✥ xmm1 LOOP /∗ loop with next Rcon ∗/ ... AES-NI Key Schedule for AES128

25/60

Use of the AES instruction set - 18 October 2012

slide-32
SLIDE 32

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |xmm and SSE |Encrypt |Decrypt |Key Schedule

AES-NI Key Schedule for AES128

xmm2

SR(W[3]) S(W[3]) SR(W[1]) S(W[1])

pshufd

SR(W[3]) SR(W[3]) SR(W[3]) SR(W[3])

xmm2 xmm1 = scheduled key i

W[3] W[2] W[1] W[0]

aeskeygenassist xmm3

W[2] W[1] W[0]

xmm3

W[1]

xmm3

W[0] W[0]

pslldq pxor pxor pxor xmm1

W[0] W[i] (0,1) W[i] (0,2) W[i] (0,3) W[7] W[6] W[5] W[4]

xmm1 = scheduled key i+1

26/60

Use of the AES instruction set - 18 October 2012

slide-33
SLIDE 33

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Some definitions

Interdependent instructions: scheduled instructions that share a data dependency forcing a “stall” in the pipeline

movdqu xmm1 , xmm2 pxor xmm3, xmm1

Timeline (cycles)

movdqu pxor

xmm1 updated

27/60

Use of the AES instruction set - 18 October 2012

slide-34
SLIDE 34

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Some definitions

Interdependent instructions: scheduled instructions that share a data dependency forcing a “stall” in the pipeline

movdqu xmm1 , xmm2 pxor xmm3, xmm1

Timeline (cycles)

movdqu pxor

xmm1 updated

Independent instructions: scheduled instructions that don’t share data dependency, and that can be parallelized

movdqu xmm1, xmm2 pxor xmm3, xmm4

Timeline (cycles)

movdqu pxor

27/60

Use of the AES instruction set - 18 October 2012

slide-35
SLIDE 35

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Some definitions

Latency of an instruction: number of cycles taken by the instruction to complete in the worst case

latency

28/60

Use of the AES instruction set - 18 October 2012

slide-36
SLIDE 36

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Some definitions

Latency of an instruction: number of cycles taken by the instruction to complete in the worst case

latency

(Reciprocal) Throughput of an instruction: number of cycles to complete in the best case

throughput

28/60

Use of the AES instruction set - 18 October 2012

slide-37
SLIDE 37

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Some definitions

Latency of an instruction: number of cycles taken by the instruction to complete in the worst case

latency

(Reciprocal) Throughput of an instruction: number of cycles to complete in the best case

throughput

Intel’s Optimization Manual states that aesenc, aesdec, aesenclast, aesdeclast have:

◮ latency of 6 cycles, throughput of 2 cycles (Westmere) ◮ latency of 8 cycles, throughput of 1 cycle (Sandy and

Ivy Bridge)

◮ let’s understand why ...

28/60

Use of the AES instruction set - 18 October 2012

slide-38
SLIDE 38

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM pxor xmm1, xmm2 pshufb xmm3, [mem128] pxor xmm1, xmm2 pshufb xmm3, [mem128]

29/60

Use of the AES instruction set - 18 October 2012

slide-39
SLIDE 39

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM pxor xmm1, xmm2 pshufb xmm3, [mem128] 2 µops 1 µop

30/60

Use of the AES instruction set - 18 October 2012

slide-40
SLIDE 40

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM 2 µops 1 µop NEXT NEXT

31/60

Use of the AES instruction set - 18 October 2012

slide-41
SLIDE 41

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM Sequential execution NEXT NEXT

32/60

Use of the AES instruction set - 18 October 2012

slide-42
SLIDE 42

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM Parallel execution NEXT NEXT

33/60

Use of the AES instruction set - 18 October 2012

slide-43
SLIDE 43

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Throughput and ✖ops

Each Port (execution unit entry) has a 1 cycle latency The latency of an instruction represents the sum of the latencies of its sequential ✖ops

◮ pxor xmm1, [mem128] has to wait the resulting data

from the memory load before doing the xor operation

✖ ✖ ✮ ❂ ❂ ✿

34/60

Use of the AES instruction set - 18 October 2012

slide-44
SLIDE 44

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Throughput and ✖ops

Each Port (execution unit entry) has a 1 cycle latency The latency of an instruction represents the sum of the latencies of its sequential ✖ops

◮ pxor xmm1, [mem128] has to wait the resulting data

from the memory load before doing the xor operation

The throughput of an instruction is directly related to its independent ✖ops decomposition, as well as to its port binding

◮ pxor is composed of a unique ✖op, and can be dispatched

  • n Port0, 1 or 5

◮ latency of the ✖op is 1 cycle ✮ throughput is

1❂3 ❂ 0✿33 cycle

34/60

Use of the AES instruction set - 18 October 2012

slide-45
SLIDE 45

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

AES-NI: ✖op analysis for Westmere

IACA tool (Intel Architecture Code Analyzer):

Throughput Analysis Report

  • Block Throughput: 6.00 Cycles

Throughput Bottleneck: InterIteration | Num Of | Ports pressure in cycles | | | Uops | 0 - DV | 1 | 2

  • D

| 3

  • D

| 4 | 5 | |

  • |

3 | 2.0 | | | | | 1.0 | CP | aesenc xmm0, xmm1 Throughput Analysis Report

  • Block Throughput: 6.00 Cycles

Throughput Bottleneck: InterIteration | Num Of | Ports pressure in cycles | | | Uops | 0 - DV | 1 | 2

  • D

| 3

  • D

| 4 | 5 | |

  • |

3 | 2.0 | | | | | 1.0 | CP | aesdec xmm0, xmm1

3 ✖ops: two on Port0 and one on Port5

35/60

Use of the AES instruction set - 18 October 2012

slide-46
SLIDE 46

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

AES-NI: ✖op analysis for Westmere

Decomposition and latency of aesenc:

xmm1 5 Port5 Implicit ShiftRows Port0 Timeline (cycles) 4 6 SubBytes/MixColumns AddRoundKey Latency 6 Latency Port0 Dependency Data

36/60

Use of the AES instruction set - 18 October 2012

slide-47
SLIDE 47

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

AES-NI: ✖op analysis for Westmere

Throughput of aesenc:

xmm1 5 2 Port5 Implicit ShiftRows Port0 Timeline (cycles) 4 6 AES round on block i AES round on block i+1 Throughput Port5 Latency Port0

37/60

Use of the AES instruction set - 18 October 2012

slide-48
SLIDE 48

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

AES-NI: ✖op analysis for Sandy Bridge

IACA tool:

Throughput Analysis Report

  • Block Throughput: 7.00 Cycles

Throughput Bottleneck: InterIteration | Num Of | Ports pressure in cycles | | | Uops | 0 - DV | 1 | 2

  • D

| 3

  • D

| 4 | 5 | |

  • |

2 | 0.5 | 0.5 | | | | 1.0 | CP | aesenc xmm0, xmm1

Latency is actually 8 cycles (Intel’s Optimization Manual) An AES execution subunit has been added behind Port1 The two ✖ops operating on half states seem to have fused in one 7 cycles latency ✖op that can be dispatched on Port0 or Port1: pressure on Port0 is decreased

38/60

Use of the AES instruction set - 18 October 2012

slide-49
SLIDE 49

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

AES-NI: ✖op analysis for Sandy Bridge

The throughput is reduced to 1 cycle

xmm1 7 1 1 Port0 Timeline (cycles) 8 AES round on block i AES round on block i+1 Throughput Port5 Port5 Port1 xmm1

39/60

Use of the AES instruction set - 18 October 2012

slide-50
SLIDE 50

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Latencies and throughputs summary

Westmere Sandy and Ivy Bridge

Instruction Latency Throughput aesenc 6 2 aesdec 6 2 aesenclast 6 2 aesdeclast 6 2 aeskeygenassist 6 2 aesimc 6 2 pxor 1 0.33 Instructions latencies and reciprocal throughputs (in cycles) Instruction Latency Throughput aesenc 8 1 aesdec 8 1 aesenclast 8 1 aesdeclast 8 1 aeskeygenassist 81 81 aesimc 2 2 pxor 1 0.33 Instructions latencies and reciprocal throughputs (in cycles)

1 Only reported by Agner Fog’s

experimental results

Reported by Intel documentation and confirmed experimentally (Agner Fog)

40/60

Use of the AES instruction set - 18 October 2012

slide-51
SLIDE 51

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Exploiting instruction-level parallelism

AES optimal parallel encryption for Westmere:

xmm4xmm15 ✥ scheduled keys LOOP: xmm0, xmm1, xmm2, xmm3 ✥ 4 plaintext blocks pxor xmm0, xmm4 /∗ Block0 whitening ∗/ pxor xmm1, xmm4 /∗ Block1 whitening ∗/ pxor xmm2, xmm4 /∗ Block2 whitening ∗/ pxor xmm3, xmm4 /∗ Block3 whitening ∗/ 6 cycles aesenc xmm0, xmm5 /∗ Block0 Round 1 ∗/ 2 cycles aesenc xmm1, xmm5 /∗ Block1 Round 1 ∗/ aesenc xmm2, xmm5 /∗ Block2 Round 1 ∗/ aesenc xmm3, xmm5 /∗ Block3 Round 1 ∗/ Port0 delay aesenc xmm0, xmm6 /∗ Block0 Round 2 ∗/ aesenc xmm1, xmm6 /∗ Block1 Round 2 ∗/ aesenc xmm2, xmm6 /∗ Block2 Round 2 ∗/ aesenc xmm3, xmm6 /∗ Block3 Round 2 ∗/ ... aesenclast xmm0, xmm15 /∗ Block0 Round 10 ∗/ aesenclast xmm1, xmm15 /∗ Block1 Round 10 ∗/ aesenclast xmm2, xmm15 /∗ Block2 Round 10 ∗/ aesenclast xmm3, xmm15 /∗ Block3 Round 10 ∗/ CHAIN jmp LOOP AES128 Parallel Encryption (4 blocks in parallel)

41/60

Use of the AES instruction set - 18 October 2012

slide-52
SLIDE 52

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Exploiting instruction-level parallelism

Parallel encryption for Sandy and Ivy Bridge:

xmm8xmm15, [mem][mem+3∗16] ✥ scheduled keys LOOP: xmm0, xmm1, xmm2, xmm3, xmm4,\ xmm5, xmm6, xmm7 ✥ 8 plaintext blocks pxor xmm0, xmm8 /∗ Block0 whitening ∗/ pxor xmm1, xmm8 /∗ Block1 whitening ∗/ pxor xmm2, xmm8 /∗ Block2 whitening ∗/ pxor xmm3, xmm8 /∗ Block3 whitening ∗/ pxor xmm4, xmm8 /∗ Block4 whitening ∗/ pxor xmm5, xmm8 /∗ Block5 whitening ∗/ pxor xmm6, xmm8 /∗ Block6 whitening ∗/ pxor xmm7, xmm8 /∗ Block7 whitening ∗/ 8 cycles aesenc xmm0, xmm9 /∗ Block0 Round 1 ∗/ 1 cycle aesenc xmm1, xmm9 /∗ Block1 Round 1 ∗/ aesenc xmm2, xmm9 /∗ Block2 Round 1 ∗/ aesenc xmm3, xmm9 /∗ Block3 Round 1 ∗/ aesenc xmm4, xmm9 /∗ Block4 Round 1 ∗/ aesenc xmm5, xmm9 /∗ Block5 Round 1 ∗/ aesenc xmm6, xmm9 /∗ Block6 Round 1 ∗/ aesenc xmm7, xmm9 /∗ Block7 Round 1 ∗/ aesenc xmm0, xmm10 /∗ Block0 Round 2 ∗/ ... AES128 Parallel Encryption (8 blocks in parallel)

42/60

Use of the AES instruction set - 18 October 2012

slide-53
SLIDE 53

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Theoretical performance (Westmere)

Mode Formula AES128 AES1922 AES2562 ECB Enc/Dec ✭Nrounds ✂ 2 ✰ 0✿33✮❂16 ❂ 1.27 c/B 1.52 c/B 1.77 c/B CBC Encrypt1 ✭Nrounds ✂ 6 ✰ 0✿33✮❂16 ❂ 3.77 c/B 4.52 c/B 5.27 c/B CBC Decrypt1 ✭Nrounds ✂ 2 ✰ 0✿33✮❂16 ❂ 1.27 c/B 1.52 c/B 1.77 c/B CTR Enc/Dec1 ✭Nrounds ✂ 2 ✰ 0✿33✮❂16 ❂ 1.27 c/B 1.52 c/B 1.77 c/B Theoretical performance for 4-parallel blocks encryption and decryption (in cycles per byte) for ECB and CBC modes (Westmere)

1 Plus a small overhead for chaining operations 2 Plus a small overhead because of register starvation

PCBC and CFB modes are like CBC (non parallel encryption and possible parallel decryption) OFB mode can’t be parallelized (but precomputed for a given key)

43/60

Use of the AES instruction set - 18 October 2012

slide-54
SLIDE 54

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Theoretical performance (Sandy/Ivy Bridge)

Mode Formula AES1282 AES1922 AES2562 ECB Enc/Dec ✭Nrounds ✂ 1 ✰ 0✿33✮❂16 ❂ 0.64 c/B 0.77 c/B 0.89 c/B CBC Encrypt1 ✭Nrounds ✂ 8 ✰ 0✿33✮❂16 ❂ 5.02 c/B 6.02 c/B 7.02 c/B CBC Decrypt1 ✭Nrounds ✂ 1 ✰ 0✿33✮❂16 ❂ 0.64 c/B 0.77 c/B 0.89 c/B CTR Enc/Dec1 ✭Nrounds ✂ 1 ✰ 0✿33✮❂16 ❂ 0.64 c/B 0.77 c/B 0.89 c/B Theoretical performance for 8-parallel blocks encryption and decryption (in cycles per byte) for ECB and CBC modes (Sandy and Ivy Bridge)

1 Plus a small overhead for chaining operations 2 Plus a small overhead because of register starvation

44/60

Use of the AES instruction set - 18 October 2012

slide-55
SLIDE 55

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Practical results

Official results in Intel’s White Paper on Westmere, 4 parallel blocks:

1 2 3 4 5 6 7 8 9 10 11 ECB CBC CTR table based AES128 CTR bitslice AES128 CTR #cycles per byte

AES-NI results (Westmere@2.67GHz, 1KB data)

Encrypt128 Encrypt192 Encrypt256 Decrypt128 Decrypt192 Decrypt256

45/60

Use of the AES instruction set - 18 October 2012

slide-56
SLIDE 56

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Practical results

Differences with theory:

1 2 3 4 5 6 7 8 9 10 11 ECB CBC CTR #cycles per byte

AES-NI results (Westmere@2.67GHz, 1KB data)

Results versus Theory

46/60

Use of the AES instruction set - 18 October 2012

slide-57
SLIDE 57

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

What about the Key Schedule?

Intel has developped AES-NI with encryption and decryption using the same key in mind Key Schedule becomes negligible when encrypting multiple blocks with the same key This explains why aeskeygenassist performs quite poorly on Ivy/Sandy bridge AES-NI provides however better performance - with constant time implementation - than table based Key Schedule: ✘100 cycles against ✘160 cycles

47/60

Use of the AES instruction set - 18 October 2012

slide-58
SLIDE 58

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

VEX encoded AES (AVX extensions)

There are VEX extensions of AES-NI instructions: vaesenc, vaesdec ... However, the instructions only work on the low part xmm of ymm registers The advantage of using three operands versions of the instructions remains:

◮ the Key Schedule can benefit from the extended

instructions ...

◮ ... at the cost of using VEX only instructions (to

avoid VEX/SSE switch latencies)

48/60

Use of the AES instruction set - 18 October 2012

slide-59
SLIDE 59

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

GCM mode

Counter0 Counter1 Counter2 Plaintext1 Plaintext2 Ciphertext2 Ciphertext2 Key Key Encrypt Encrypt Encrypt mult mult mult mult AuthData1 len(A)||len(C) AuthTag incr incr Multiplication of input with fixed hash key in GF(2 )

128

49/60

Use of the AES instruction set - 18 October 2012

slide-60
SLIDE 60

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

pclmulqdq instruction

Not an AES-NI instruction per se Performs a “carry-less multiplication” (polynomial multiplication over GF(2))

/∗ Split in two quadwords ∗/ xmm1 := [xmm1[1]|xmm1[0]] xmm2 := [xmm2[1]|xmm2[0]] if(imm8 == 0x00) xmm1 ✥ xmm2[0] ✂ xmm1[0] if(imm8 == 0x01) xmm1 ✥ xmm2[0] ✂ xmm1[1] if(imm8 == 0x10) xmm1 ✥ xmm2[1] ✂ xmm1[0] if(imm8 == 0x11) xmm1 ✥ xmm2[1] ✂ xmm1[1] pclmulqdq xmm1, xmm2/[mem128], imm8

50/60

Use of the AES instruction set - 18 October 2012

slide-61
SLIDE 61

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Using pclmulqdq

GCM multiplies two 128-bit values over GF(2128) Two issues:

◮ Carry-less multiplication of two 128-bit operands to

give a 255-bit value ✮ use schoolbook or Karatsuba algorithms

◮ Reduction of the resulting value over GF(2128) with the

GCM irreductible polynomial (x 128 ✰ x 7 ✰ x 2 ✰ x ✰ 1) ✮ Intel’s manual gives many optimized reduction algorithms

51/60

Use of the AES instruction set - 18 October 2012

slide-62
SLIDE 62

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Latency/throughput |Core™ and ✖ops |Results |GCM

Using pclmulqdq

GCM multiplies two 128-bit values over GF(2128) Two issues:

◮ Carry-less multiplication of two 128-bit operands to

give a 255-bit value ✮ use schoolbook or Karatsuba algorithms

◮ Reduction of the resulting value over GF(2128) with the

GCM irreductible polynomial (x 128 ✰ x 7 ✰ x 2 ✰ x ✰ 1) ✮ Intel’s manual gives many optimized reduction algorithms

Result on Westmere: AES GCM performs at 3.54 c/B with 4 parallel blocks CTR encryption, to compare with 10.68 c/B bitsliced AES GCM with table lookups (21.99 c/B without table lookups, Käsper et al.)

51/60

Use of the AES instruction set - 18 October 2012

slide-63
SLIDE 63

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Rijndael

Rijndael uses the same building blocks as AES, with a state up to 256-bit and extended possible key lengths AES-NI Key Schedule instructions fit the Rijndael Key Schedule The main issue comes from the ShiftRows on states of length > 128-bit that don’t fit the AES

52/60

Use of the AES instruction set - 18 October 2012

slide-64
SLIDE 64

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Rijndael (256-bit state example)

Solution: prepare the state (xmm1, xmm2) with AESShiftRows1(RijndaelShiftRows(state))

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 10 11 12 13 14 15 4 8 12 5 9 13 14 2 6 10 1 3 7 11 15 4 8 12 5 9 13 14 2 10 1 3 7 11 15 1 6 7 4 5 10 11 8 9 14 15 12 13 2 3 1 6 7 4 5 10 11 8 14 15 12 13 2 3 9 6 9 4 8 12 5 9 13 14 2 6 10 1 3 7 11 15 4 8 12 5 9 13 14 2 10 1 3 7 11 15 6

xmm1 xmm2 pshufb pblendw pshufb Rijndael State 2 2 2 aesenc aesenc AESInvSR(RijndaelSR(state)) AESSR SubBytes MixColumns AESSR SubBytes MixColumns

53/60

Use of the AES instruction set - 18 October 2012

slide-65
SLIDE 65

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Building blocks isolation

We can apply the same function composition strategy to isolate ShiftRows, MixColumns, SubBytes, RotByte

aesdeclast xmm1, 0x00... aesenc xmm1, 0x00... /∗ Tmp ✥ xmm1 Tmp ✥ InvShiftRows(Tmp) xmm1 ✥ InvSubBytes(Tmp) ✟ 0x00... Tmp ✥ xmm1 Tmp ✥ ShiftRows(Tmp) Tmp ✥ SubBytes(Tmp) xmm1 ✥ MixColumns(Tmp) ✟ 0x00... ∗/ MixColumns(xmm1) CST=0x0306090c0f0205080b0e0104070a0d00 pshufb xmm1, CST /∗ pshufb = InvShiftRows∗/ aesenclast xmm1, 0x00... SubBytes(xmm1)

SWAP InvShiftRows

54/60

Use of the AES instruction set - 18 October 2012

slide-66
SLIDE 66

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Building blocks isolation

The same building block can have multiple decompositions: InvMixColumns = {aesimc} = {aesenclast + aesdec}

55/60

Use of the AES instruction set - 18 October 2012

slide-67
SLIDE 67

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Building blocks isolation

The same building block can have multiple decompositions: InvMixColumns = {aesimc} = {aesenclast + aesdec} One must check the resulting latency and throughput, and use the optimal decomposition (or combine decompositions)

◮ using aesenclast and pshufb to compose SubBytes

seems clearly more efficient than composing aesenc, aesimc and pshufb

◮ depends on the microarchitectural details

55/60

Use of the AES instruction set - 18 October 2012

slide-68
SLIDE 68

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Building blocks isolation

In order to achieve maximum throughput, composed building blocks must be parallelized atomically for each instruction (remove critical paths)

aesdeclast xmm0, 0x00... aesdeclast xmm1, 0x00... aesdeclast xmm2, 0x00... aesdeclast xmm3, 0x00... aesenc xmm0, 0x00... aesenc xmm1, 0x00... aesenc xmm2, 0x00... aesenc xmm3, 0x00... Maximum throughput MixColumns (Westmere) aesdeclast xmm0, 0x00... aesenc xmm0, 0x00... aesdeclast xmm1, 0x00... aesenc xmm1, 0x00... aesdeclast xmm2, 0x00... aesenc xmm2, 0x00... aesdeclast xmm3, 0x00... aesenc xmm3, 0x00... MixColumns whith critical paths

56/60

Use of the AES instruction set - 18 October 2012

slide-69
SLIDE 69

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Building blocks isolation

Parts of the building blocks can also be isolated MixColumns sub-matrix multiplication isolation:

2 1 1 2

✵ ❅ ✶ ❆ ✂

x0 x1

✵ ❅ ✶ ❆

2 3 1 1 1 2 3 1 1 1 2 3 3 1 1 2

✵ ❇ ❇ ❇ ❇ ❇ ❅ ✶ ❈ ❈ ❈ ❈ ❈ ❆

✂ x0 x1

✵ ❇ ❇ ❇ ❇ ❅ ✶ ❈ ❈ ❈ ❈ ❆

57/60

Use of the AES instruction set - 18 October 2012

slide-70
SLIDE 70

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Hash functions

The versatility of AES-NI instructions allows them to be used in other areas than AES or Rijndael:

◮ All cryptographic algorithms that use AES building

blocks can benefit from AES-NI ...

◮ ... with performance benefits and/or constant time

implementation

More specifically, many candidates of the recent SHA-3 competition have used AES-NI to improve performance or provide resisance against side channel attacks

58/60

Use of the AES instruction set - 18 October 2012

slide-71
SLIDE 71

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion |Rijndael |Building blocks |SHA-3

Hash functions

Some SHA-3 candidates results:

Algorithm Previous 256/512 AES-NI 256/512 Grøstl 19.9 / 29.2 11.3 / 16.2 ECHO 28.5 / 53.5 6.8 / 12.6 Shavite-3 26.7 / 38.2 5.6 / 5.5 Cheetah 9.3 / 13.1 7.6 /

  • Lane

25.7 / 56.5 4.9 / 13.5 Lesamnta 52.7 / 51.2 29.5 / 19.0 LUX 10.5 / 9.26 6.6 /

  • Vortex

46.3 / 56.1 4.4 / 5.2 AES-NI performance benefits on Westmere (results in cycles per byte)

Final Round Round 2 Round 1

59/60

Use of the AES instruction set - 18 October 2012

slide-72
SLIDE 72

|Outline |AES-NI |Instructions detail |Performance |Beyond AES |Conclusion

Concluding thoughts

Since Intel’s White Paper in 2008, AES-NI has become a reality with Westmere and Sandy/Ivy Bridge Adding AES in the ISA rather than in a dedicated coprocessor has advantages (software compliance across platforms)

◮ ARM and SPARC plan to add similar instructions in their

next generation CPUs

What could be the future of AES-NI?

◮ AVX2 (in the forthcoming Haswell microarchitecture)

don’t include 256-bit AES ymm support: it might be planned for future release (?)

◮ the latency (8 cycles) can be improved, and ✖op

decomposition reduced

60/60

Use of the AES instruction set - 18 October 2012