Following the license change, modify the boilerplates in crypto/aes/
[openssl.git] / crypto / aes / asm / bsaes-armv7.pl
index 8515c9d0abd99b3dc735579ab7c1ebd8a7297b27..5df195b9efb6652dad77a16346f04481e831d334 100644 (file)
@@ -1,4 +1,11 @@
-#!/usr/bin/env perl
+#! /usr/bin/env perl
+# Copyright 2012-2018 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
 
 # ====================================================================
 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
@@ -7,8 +14,7 @@
 # details see http://www.openssl.org/~appro/cryptogams/.
 #
 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
-# <ard.biesheuvel@linaro.org>. Permission to use under GPL terms is
-# granted.
+# of Linaro. Permission to use under GPL terms is granted.
 # ====================================================================
 
 # Bit-sliced AES for ARM NEON
 # to collect performance results, which for Cortex-A8 core are:
 #
 # encrypt      19.5 cycles per byte processed with 128-bit key
-# decrypt      24.0 cycles per byte processed with 128-bit key
+# decrypt      22.1 cycles per byte processed with 128-bit key
 # key conv.    440  cycles per 128-bit key/0.18 of 8x block
 #
-# Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 22.6,
+# Snapdragon S4 encrypts byte in 17.6 cycles and decrypts in 19.7,
 # which is [much] worse than anticipated (for further details see
 # http://www.openssl.org/~appro/Snapdragon-S4.html).
 #
-# Cortex-A15 manages in 14.2/19.6 cycles [when integer-only code
+# Cortex-A15 manages in 14.2/16.1 cycles [when integer-only code
 # manages in 20.0 cycles].
 #
 # When comparing to x86_64 results keep in mind that NEON unit is
 #                                              <appro@openssl.org>
 
 # April-August 2013
-#
-# Add CBC, CTR and XTS subroutines, adapt for kernel use.
-#
-#                                      <ard.biesheuvel@linaro.org>
+# Add CBC, CTR and XTS subroutines and adapt for kernel use; courtesy of Ard.
 
-while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
-open STDOUT,">$output";
+$flavour = shift;
+if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
+else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
+
+if ($flavour && $flavour ne "void") {
+    $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
+    ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
+    ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
+    die "can't locate arm-xlate.pl";
+
+    open STDOUT,"| \"$^X\" $xlate $flavour $output";
+} else {
+    open STDOUT,">$output";
+}
 
 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
 my @XMM=map("q$_",(0..15));
@@ -72,7 +87,7 @@ my @s=@_[12..15];
 
 sub InBasisChange {
 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
-# output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb 
+# output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
 my @b=@_[0..7];
 $code.=<<___;
        veor    @b[2], @b[2], @b[1]
@@ -377,6 +392,7 @@ sub MixColumns {
 # modified to emit output in order suitable for feeding back to aesenc[last]
 my @x=@_[0..7];
 my @t=@_[8..15];
+my $inv=@_[16];        # optional
 $code.=<<___;
        vext.8  @t[0], @x[0], @x[0], #12        @ x0 <<< 32
        vext.8  @t[1], @x[1], @x[1], #12
@@ -417,8 +433,9 @@ $code.=<<___;
        veor    @t[3], @t[3], @x[7]
         vext.8 @x[6], @x[2], @x[2], #8
        veor    @x[7], @t[1], @t[5]
+___
+$code.=<<___ if (!$inv);
        veor    @x[2], @t[0], @t[4]
-
        veor    @x[4], @x[4], @t[3]
        veor    @x[5], @x[5], @t[7]
        veor    @x[3], @x[3], @t[6]
@@ -426,9 +443,18 @@ $code.=<<___;
        veor    @x[6], @x[6], @t[2]
         @ vmov @x[7], @t[1]
 ___
+$code.=<<___ if ($inv);
+       veor    @t[3], @t[3], @x[4]
+       veor    @x[5], @x[5], @t[7]
+       veor    @x[2], @x[3], @t[6]
+       veor    @x[3], @t[0], @t[4]
+       veor    @x[4], @x[6], @t[2]
+       vmov    @x[6], @t[3]
+        @ vmov @x[7], @t[1]
+___
 }
 
-sub InvMixColumns {
+sub InvMixColumns_orig {
 my @x=@_[0..7];
 my @t=@_[8..15];
 
@@ -581,6 +607,54 @@ $code.=<<___;
 ___
 }
 
+sub InvMixColumns {
+my @x=@_[0..7];
+my @t=@_[8..15];
+
+# Thanks to Jussi Kivilinna for providing pointer to
+#
+# | 0e 0b 0d 09 |   | 02 03 01 01 |   | 05 00 04 00 |
+# | 09 0e 0b 0d | = | 01 02 03 01 | x | 00 05 00 04 |
+# | 0d 09 0e 0b |   | 01 01 02 03 |   | 04 00 05 00 |
+# | 0b 0d 09 0e |   | 03 01 01 02 |   | 00 04 00 05 |
+
+$code.=<<___;
+       @ multiplication by 0x05-0x00-0x04-0x00
+       vext.8  @t[0], @x[0], @x[0], #8
+       vext.8  @t[6], @x[6], @x[6], #8
+       vext.8  @t[7], @x[7], @x[7], #8
+       veor    @t[0], @t[0], @x[0]
+       vext.8  @t[1], @x[1], @x[1], #8
+       veor    @t[6], @t[6], @x[6]
+       vext.8  @t[2], @x[2], @x[2], #8
+       veor    @t[7], @t[7], @x[7]
+       vext.8  @t[3], @x[3], @x[3], #8
+       veor    @t[1], @t[1], @x[1]
+       vext.8  @t[4], @x[4], @x[4], #8
+       veor    @t[2], @t[2], @x[2]
+       vext.8  @t[5], @x[5], @x[5], #8
+       veor    @t[3], @t[3], @x[3]
+       veor    @t[4], @t[4], @x[4]
+       veor    @t[5], @t[5], @x[5]
+
+        veor   @x[0], @x[0], @t[6]
+        veor   @x[1], @x[1], @t[6]
+        veor   @x[2], @x[2], @t[0]
+        veor   @x[4], @x[4], @t[2]
+        veor   @x[3], @x[3], @t[1]
+        veor   @x[1], @x[1], @t[7]
+        veor   @x[2], @x[2], @t[7]
+        veor   @x[4], @x[4], @t[6]
+        veor   @x[5], @x[5], @t[3]
+        veor   @x[3], @x[3], @t[6]
+        veor   @x[6], @x[6], @t[4]
+        veor   @x[4], @x[4], @t[7]
+        veor   @x[5], @x[5], @t[7]
+        veor   @x[7], @x[7], @t[5]
+___
+       &MixColumns     (@x,@t,1);      # flipped 2<->3 and 4<->6
+}
+
 sub swapmove {
 my ($a,$b,$n,$mask,$t)=@_;
 $code.=<<___;
@@ -643,23 +717,36 @@ $code.=<<___;
 # define BSAES_ASM_EXTENDED_KEY
 # define XTS_CHAIN_TWEAK
 # define __ARM_ARCH__ __LINUX_ARM_ARCH__
+# define __ARM_MAX_ARCH__ 7
 #endif
 
 #ifdef __thumb__
 # define adrl adr
 #endif
 
-#if __ARM_ARCH__>=7
+#if __ARM_MAX_ARCH__>=7
+.arch  armv7-a
+.fpu   neon
+
 .text
 .syntax        unified         @ ARMv7-capable assembler is expected to handle this
-.fpu   neon
+#if defined(__thumb2__) && !defined(__APPLE__)
+.thumb
+#else
+.code   32
+# undef __thumb2__
+#endif
 
 .type  _bsaes_decrypt8,%function
 .align 4
 _bsaes_decrypt8:
-       adr     $const,_bsaes_decrypt8
+       adr     $const,.
        vldmia  $key!, {@XMM[9]}                @ round 0 key
+#if defined(__thumb2__) || defined(__APPLE__)
+       adr     $const,.LM0ISR
+#else
        add     $const,$const,#.LM0ISR-_bsaes_decrypt8
+#endif
 
        vldmia  $const!, {@XMM[8]}              @ .LM0ISR
        veor    @XMM[10], @XMM[0], @XMM[9]      @ xor with round0 key
@@ -752,9 +839,13 @@ _bsaes_const:
 .type  _bsaes_encrypt8,%function
 .align 4
 _bsaes_encrypt8:
-       adr     $const,_bsaes_encrypt8
+       adr     $const,.
        vldmia  $key!, {@XMM[9]}                @ round 0 key
+#if defined(__thumb2__) || defined(__APPLE__)
+       adr     $const,.LM0SR
+#else
        sub     $const,$const,#_bsaes_encrypt8-.LM0SR
+#endif
 
        vldmia  $const!, {@XMM[8]}              @ .LM0SR
 _bsaes_encrypt8_alt:
@@ -856,9 +947,13 @@ $code.=<<___;
 .type  _bsaes_key_convert,%function
 .align 4
 _bsaes_key_convert:
-       adr     $const,_bsaes_key_convert
+       adr     $const,.
        vld1.8  {@XMM[7]},  [$inp]!             @ load round 0 key
+#if defined(__thumb2__) || defined(__APPLE__)
+       adr     $const,.LM0
+#else
        sub     $const,$const,#_bsaes_key_convert-.LM0
+#endif
        vld1.8  {@XMM[15]}, [$inp]!             @ load round 1 key
 
        vmov.i8 @XMM[8],  #0x01                 @ bit masks
@@ -1266,7 +1361,7 @@ bsaes_cbc_encrypt:
        vmov    @XMM[4],@XMM[15]                @ just in case ensure that IV
        vmov    @XMM[5],@XMM[0]                 @ and input are preserved
        bl      AES_decrypt
-       vld1.8  {@XMM[0]}, [$fp,:64]            @ load result
+       vld1.8  {@XMM[0]}, [$fp]                @ load result
        veor    @XMM[0], @XMM[0], @XMM[4]       @ ^= IV
        vmov    @XMM[15], @XMM[5]               @ @XMM[5] holds input
        vst1.8  {@XMM[0]}, [$rounds]            @ write output
@@ -1325,7 +1420,12 @@ bsaes_ctr32_encrypt_blocks:
        vstmia  r12, {@XMM[7]}                  @ save last round key
 
        vld1.8  {@XMM[0]}, [$ctr]               @ load counter
+#ifdef __APPLE__
+       mov     $ctr, #:lower16:(.LREVM0SR-.LM0)
+       add     $ctr, $const, $ctr
+#else
        add     $ctr, $const, #.LREVM0SR-.LM0   @ borrow $ctr
+#endif
        vldmia  $keysched, {@XMM[4]}            @ load round0 key
 #else
        ldr     r12, [$key, #244]
@@ -1382,7 +1482,12 @@ bsaes_ctr32_encrypt_blocks:
        vldmia          $ctr, {@XMM[8]}                 @ .LREVM0SR
        mov             r5, $rounds                     @ pass rounds
        vstmia          $fp, {@XMM[10]}                 @ save next counter
+#ifdef __APPLE__
+       mov             $const, #:lower16:(.LREVM0SR-.LSR)
+       sub             $const, $ctr, $const
+#else
        sub             $const, $ctr, #.LREVM0SR-.LSR   @ pass constants
+#endif
 
        bl              _bsaes_encrypt8_alt
 
@@ -1483,7 +1588,7 @@ bsaes_ctr32_encrypt_blocks:
        rev     r8, r8
 #endif
        sub     sp, sp, #0x10
-       vst1.8  {@XMM[1]}, [sp,:64]     @ copy counter value
+       vst1.8  {@XMM[1]}, [sp]         @ copy counter value
        sub     sp, sp, #0x10
 
 .Lctr_enc_short_loop:
@@ -1494,7 +1599,7 @@ bsaes_ctr32_encrypt_blocks:
        bl      AES_encrypt
 
        vld1.8  {@XMM[0]}, [r4]!        @ load input
-       vld1.8  {@XMM[1]}, [sp,:64]     @ load encrypted counter
+       vld1.8  {@XMM[1]}, [sp]         @ load encrypted counter
        add     r8, r8, #1
 #ifdef __ARMEL__
        rev     r0, r8
@@ -1730,8 +1835,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_6:
-       vst1.64         {@XMM[14]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[4], @XMM[4], @XMM[12]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1767,8 +1870,6 @@ $code.=<<___;
 
 .align 5
 .Lxts_enc_5:
-       vst1.64         {@XMM[13]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[3], @XMM[3], @XMM[11]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1797,8 +1898,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_4:
-       vst1.64         {@XMM[12]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[2], @XMM[2], @XMM[10]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1824,8 +1923,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_3:
-       vst1.64         {@XMM[11]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[1], @XMM[1], @XMM[9]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1850,8 +1947,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_2:
-       vst1.64         {@XMM[10]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[0], @XMM[0], @XMM[8]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1874,7 +1969,7 @@ $code.=<<___;
 .align 4
 .Lxts_enc_1:
        mov             r0, sp
-       veor            @XMM[0], @XMM[8]
+       veor            @XMM[0], @XMM[0], @XMM[8]
        mov             r1, sp
        vst1.8          {@XMM[0]}, [sp,:128]
        mov             r2, $key
@@ -2011,9 +2106,11 @@ bsaes_xts_decrypt:
        vld1.8  {@XMM[8]}, [r0]                 @ initial tweak
        adr     $magic, .Lxts_magic
 
+#ifndef        XTS_CHAIN_TWEAK
        tst     $len, #0xf                      @ if not multiple of 16
        it      ne                              @ Thumb2 thing, sanity check in ARM
        subne   $len, #0x10                     @ subtract another 16 bytes
+#endif
        subs    $len, #0x80
 
        blo     .Lxts_dec_short
@@ -2184,8 +2281,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_5:
-       vst1.64         {@XMM[13]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[3], @XMM[3], @XMM[11]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2214,8 +2309,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_4:
-       vst1.64         {@XMM[12]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[2], @XMM[2], @XMM[10]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2241,8 +2334,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_3:
-       vst1.64         {@XMM[11]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[1], @XMM[1], @XMM[9]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2267,8 +2358,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_2:
-       vst1.64         {@XMM[10]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[0], @XMM[0], @XMM[8]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2291,12 +2380,12 @@ $code.=<<___;
 .align 4
 .Lxts_dec_1:
        mov             r0, sp
-       veor            @XMM[0], @XMM[8]
+       veor            @XMM[0], @XMM[0], @XMM[8]
        mov             r1, sp
        vst1.8          {@XMM[0]}, [sp,:128]
+       mov             r5, $magic                      @ preserve magic
        mov             r2, $key
        mov             r4, $fp                         @ preserve fp
-       mov             r5, $magic                      @ preserve magic
 
        bl              AES_decrypt