1 /**********************************************************************
2 Copyright(c) 2021 Arm Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
13 * Neither the name of Arm Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
30 #include "gcm_common.S"
31 declare_var_vector_reg Key11,27
32 declare_var_vector_reg Key12,28
33 declare_var_vector_reg Key13,29
34 declare_var_vector_reg Key14,30
35 #define KEY_REGS 0,1,2,3,4,5,6,7,8,9,10,11,12
36 .macro aes_encrypt_block block:req
37 aes_encrypt_round \block,Key0
38 aes_encrypt_round \block,Key1
39 aes_encrypt_round \block,Key2
40 aes_encrypt_round \block,Key3
41 aes_encrypt_round \block,Key4
42 aes_encrypt_round \block,Key5
43 aes_encrypt_round \block,Key6
44 aes_encrypt_round \block,Key7
45 aes_encrypt_round \block,Key8
46 aes_encrypt_round \block,Key9
47 aes_encrypt_round \block,Key10
48 aes_encrypt_round \block,Key11
49 aes_encrypt_round \block,Key12
50 aese v\block\().16b,vKey13.16b
51 eor v\block\().16b,v\block\().16b,vKey14.16b
55 Load Aes Keys to [vKey0..vKey8,vKeyLast0,vKeyLast1]
57 .macro load_aes_keys key_addr:req
58 ld1 { vKey0.4s- vKey3.4s},[\key_addr],64
59 ld1 { vKey4.4s- vKey7.4s},[\key_addr],64
60 ld1 { vKey8.4s- vKey11.4s},[\key_addr],64
61 ld1 {vKey12.4s- vKey14.4s},[\key_addr],48
67 [low,middle,tmp0,high] +=aadhash * [hashkey0,hashkey0_ext]
69 enc_dat=aes_encrypt(ctr)^dat
71 [hashkey0,hashkey0_ext] = *hashkey_adr
75 .macro aes_gcm_middle is_enc:req,aadhash:req,dat_adr:req,hashkey_adr:req, \
76 hashkey0:req,hashkey0_ext:req,high:req,low:req, \
77 ctr:req,enc_ctr:req,one:req,out_adr:req, \
78 tmp0:req,tmp1:req,left_count:req
80 pmull2 v\tmp0\().1q,v\aadhash\().2d,v\hashkey0\().2d
81 pmull v\tmp1\().1q,v\aadhash\().1d,v\hashkey0\().1d
83 ldr q\hashkey0,[\hashkey_adr],16
86 add v\ctr\().4s,v\ctr\().4s,v\one\().4s //increase ctr
88 rev32 v\enc_ctr\().16b,v\ctr\().16b
89 aes_encrypt_round \enc_ctr,Key0
90 eor v\high\().16b,v\high\().16b,v\tmp0\().16b
91 pmull v\tmp0\().1q,v\aadhash\().1d,v\hashkey0_ext\().1d
92 eor v\low\().16b,v\low\().16b,v\tmp1\().16b
93 pmull2 v\tmp1\().1q,v\aadhash\().2d,v\hashkey0_ext\().2d
95 ldr q\hashkey0_ext,[\hashkey_adr],16
97 eor v\aadhash\().16b,v\aadhash\().16b,v\aadhash\().16b
98 aes_encrypt_round \enc_ctr,Key1
99 aes_encrypt_round \enc_ctr,Key2
100 eor v\tmp0\().16b,v\tmp1\().16b,v\tmp0\().16b
101 aes_encrypt_round \enc_ctr,Key3
102 ext v\tmp1\().16b,v\aadhash\().16b,v\tmp0\().16b,8
103 ext v\tmp0\().16b,v\tmp0\().16b,v\aadhash\().16b,8
104 aes_encrypt_round \enc_ctr,Key4
105 eor v\low\().16b,v\low\().16b,v\tmp1\().16b
106 eor v\high\().16b,v\high\().16b,v\tmp0\().16b
107 aes_encrypt_round \enc_ctr,Key5
108 ldr q\aadhash,[\dat_adr],16
109 aes_encrypt_round \enc_ctr,Key6
110 aes_encrypt_round \enc_ctr,Key7
111 aes_encrypt_round \enc_ctr,Key8
112 aes_encrypt_round \enc_ctr,Key9
113 aes_encrypt_round \enc_ctr,Key10
114 aes_encrypt_round \enc_ctr,Key11
115 aes_encrypt_round \enc_ctr,Key12
116 aese v\enc_ctr\().16b,vKey13.16b
117 eor v\enc_ctr\().16b,v\enc_ctr\().16b,vKey14.16b
118 eor v\enc_ctr\().16b,v\enc_ctr\().16b,v\aadhash\().16b
119 .ifc \is_enc , encrypt
120 rbit v\aadhash\().16b,v\enc_ctr\().16b
122 .ifc \is_enc , decrypt
123 rbit v\aadhash\().16b,v\aadhash\().16b
125 str q\enc_ctr,[\out_adr],16
128 .macro aes_gcm_init is_enc:req,aadhash:req,dat_adr:req,hashkey_adr:req, \
129 hashkey0:req,hashkey0_ext:req, high:req,low:req, \
130 ctr:req,enc_ctr:req,one:req,out_adr:req, \
131 tmp0:req,tmp1:req,left_count:req
132 ldr q\hashkey0,[\hashkey_adr],16
133 add v\ctr\().4s,v\ctr\().4s,v\one\().4s /*increase ctr */
134 rev32 v\enc_ctr\().16b,v\ctr\().16b
135 aes_encrypt_round \enc_ctr,Key0
136 ldr q\hashkey0_ext,[\hashkey_adr],16
137 aes_encrypt_round \enc_ctr,Key1
138 pmull2 v\high\().1q,v\aadhash\().2d,v\hashkey0\().2d
139 pmull v\low\().1q,v\aadhash\().1d,v\hashkey0\().1d
142 ldr q\hashkey0,[\hashkey_adr],16
144 aes_encrypt_round \enc_ctr,Key2
145 pmull v\tmp1\().1q,v\aadhash\().1d,v\hashkey0_ext\().1d
146 pmull2 v\tmp0\().1q,v\aadhash\().2d,v\hashkey0_ext\().2d
147 eor v\aadhash\().16b,v\aadhash\().16b,v\aadhash\().16b
150 ldr q\hashkey0_ext,[\hashkey_adr],16
152 aes_encrypt_round \enc_ctr,Key3
153 eor v\tmp0\().16b,v\tmp1\().16b,v\tmp0\().16b
155 aes_encrypt_round \enc_ctr,Key4
156 ext v\tmp1\().16b,v\aadhash\().16b,v\tmp0\().16b,8 /*low */
157 ext v\tmp0\().16b,v\tmp0\().16b,v\aadhash\().16b,8 /* high */
158 aes_encrypt_round \enc_ctr,Key5
159 eor v\low\().16b,v\low\().16b,v\tmp1\().16b
160 eor v\high\().16b,v\high\().16b,v\tmp0\().16b
161 aes_encrypt_round \enc_ctr,Key6
162 ldr q\aadhash,[\dat_adr],16
163 aes_encrypt_round \enc_ctr,Key7
164 aes_encrypt_round \enc_ctr,Key8
165 aes_encrypt_round \enc_ctr,Key9
166 aes_encrypt_round \enc_ctr,Key10
167 aes_encrypt_round \enc_ctr,Key11
168 aes_encrypt_round \enc_ctr,Key12
169 aese v\enc_ctr\().16b,vKey13.16b
170 eor v\enc_ctr\().16b,v\enc_ctr\().16b,vKey14.16b
171 eor v\enc_ctr\().16b,v\enc_ctr\().16b,v\aadhash\().16b
172 .ifc \is_enc , encrypt
173 rbit v\aadhash\().16b,v\enc_ctr\().16b
175 .ifc \is_enc , decrypt
176 rbit v\aadhash\().16b,v\aadhash\().16b
178 str q\enc_ctr,[\out_adr],16