| 1 |
// This file is generated from a similarly-named Perl script in the BoringSSL |
| 2 |
// source tree. Do not edit by hand. |
| 3 |
|
| 4 |
#if !defined(__has_feature) |
| 5 |
#define __has_feature(x) 0 |
| 6 |
#endif |
| 7 |
#if __has_feature(memory_sanitizer) && !defined(OPENSSL_NO_ASM) |
| 8 |
#define OPENSSL_NO_ASM |
| 9 |
#endif |
| 10 |
|
| 11 |
#if !defined(OPENSSL_NO_ASM) && defined(__AARCH64EL__) && defined(__APPLE__) |
| 12 |
#if defined(BORINGSSL_PREFIX) |
| 13 |
#include <boringssl_prefix_symbols_asm.h> |
| 14 |
#endif |
| 15 |
#include "openssl/arm_arch.h" |
| 16 |
|
| 17 |
.text |
| 18 |
.globl _beeu_mod_inverse_vartime |
| 19 |
.private_extern _beeu_mod_inverse_vartime |
| 20 |
|
| 21 |
.align 4 |
| 22 |
_beeu_mod_inverse_vartime: |
| 23 |
// Reserve enough space for 14 8-byte registers on the stack |
| 24 |
// in the first stp call for x29, x30. |
| 25 |
// Then store the remaining callee-saved registers. |
| 26 |
// |
| 27 |
// | x29 | x30 | x19 | x20 | ... | x27 | x28 | x0 | x2 | |
| 28 |
// ^ ^ |
| 29 |
// sp <------------------- 112 bytes ----------------> old sp |
| 30 |
// x29 (FP) |
| 31 |
// |
| 32 |
AARCH64_SIGN_LINK_REGISTER |
| 33 |
stp x29,x30,[sp,#-112]! |
| 34 |
add x29,sp,#0 |
| 35 |
stp x19,x20,[sp,#16] |
| 36 |
stp x21,x22,[sp,#32] |
| 37 |
stp x23,x24,[sp,#48] |
| 38 |
stp x25,x26,[sp,#64] |
| 39 |
stp x27,x28,[sp,#80] |
| 40 |
stp x0,x2,[sp,#96] |
| 41 |
|
| 42 |
// B = b3..b0 := a |
| 43 |
ldp x25,x26,[x1] |
| 44 |
ldp x27,x28,[x1,#16] |
| 45 |
|
| 46 |
// n3..n0 := n |
| 47 |
// Note: the value of input params are changed in the following. |
| 48 |
ldp x0,x1,[x2] |
| 49 |
ldp x2,x30,[x2,#16] |
| 50 |
|
| 51 |
// A = a3..a0 := n |
| 52 |
mov x21, x0 |
| 53 |
mov x22, x1 |
| 54 |
mov x23, x2 |
| 55 |
mov x24, x30 |
| 56 |
|
| 57 |
// X = x4..x0 := 1 |
| 58 |
mov x3, #1 |
| 59 |
eor x4, x4, x4 |
| 60 |
eor x5, x5, x5 |
| 61 |
eor x6, x6, x6 |
| 62 |
eor x7, x7, x7 |
| 63 |
|
| 64 |
// Y = y4..y0 := 0 |
| 65 |
eor x8, x8, x8 |
| 66 |
eor x9, x9, x9 |
| 67 |
eor x10, x10, x10 |
| 68 |
eor x11, x11, x11 |
| 69 |
eor x12, x12, x12 |
| 70 |
|
| 71 |
Lbeeu_loop: |
| 72 |
// if B == 0, jump to .Lbeeu_loop_end |
| 73 |
orr x14, x25, x26 |
| 74 |
orr x14, x14, x27 |
| 75 |
|
| 76 |
// reverse the bit order of x25. This is needed for clz after this macro |
| 77 |
rbit x15, x25 |
| 78 |
|
| 79 |
orr x14, x14, x28 |
| 80 |
cbz x14,Lbeeu_loop_end |
| 81 |
|
| 82 |
|
| 83 |
// 0 < B < |n|, |
| 84 |
// 0 < A <= |n|, |
| 85 |
// (1) X*a == B (mod |n|), |
| 86 |
// (2) (-1)*Y*a == A (mod |n|) |
| 87 |
|
| 88 |
// Now divide B by the maximum possible power of two in the |
| 89 |
// integers, and divide X by the same value mod |n|. |
| 90 |
// When we're done, (1) still holds. |
| 91 |
|
| 92 |
// shift := number of trailing 0s in x25 |
| 93 |
// ( = number of leading 0s in x15; see the "rbit" instruction in TEST_B_ZERO) |
| 94 |
clz x13, x15 |
| 95 |
|
| 96 |
// If there is no shift, goto shift_A_Y |
| 97 |
cbz x13, Lbeeu_shift_A_Y |
| 98 |
|
| 99 |
// Shift B right by "x13" bits |
| 100 |
neg x14, x13 |
| 101 |
lsr x25, x25, x13 |
| 102 |
lsl x15, x26, x14 |
| 103 |
|
| 104 |
lsr x26, x26, x13 |
| 105 |
lsl x19, x27, x14 |
| 106 |
|
| 107 |
orr x25, x25, x15 |
| 108 |
|
| 109 |
lsr x27, x27, x13 |
| 110 |
lsl x20, x28, x14 |
| 111 |
|
| 112 |
orr x26, x26, x19 |
| 113 |
|
| 114 |
lsr x28, x28, x13 |
| 115 |
|
| 116 |
orr x27, x27, x20 |
| 117 |
|
| 118 |
|
| 119 |
// Shift X right by "x13" bits, adding n whenever X becomes odd. |
| 120 |
// x13--; |
| 121 |
// x14 := 0; needed in the addition to the most significant word in SHIFT1 |
| 122 |
eor x14, x14, x14 |
| 123 |
Lbeeu_shift_loop_X: |
| 124 |
tbz x3, #0, Lshift1_0 |
| 125 |
adds x3, x3, x0 |
| 126 |
adcs x4, x4, x1 |
| 127 |
adcs x5, x5, x2 |
| 128 |
adcs x6, x6, x30 |
| 129 |
adc x7, x7, x14 |
| 130 |
Lshift1_0: |
| 131 |
// var0 := [var1|var0]<64..1>; |
| 132 |
// i.e. concatenate var1 and var0, |
| 133 |
// extract bits <64..1> from the resulting 128-bit value |
| 134 |
// and put them in var0 |
| 135 |
extr x3, x4, x3, #1 |
| 136 |
extr x4, x5, x4, #1 |
| 137 |
extr x5, x6, x5, #1 |
| 138 |
extr x6, x7, x6, #1 |
| 139 |
lsr x7, x7, #1 |
| 140 |
|
| 141 |
subs x13, x13, #1 |
| 142 |
bne Lbeeu_shift_loop_X |
| 143 |
|
| 144 |
// Note: the steps above perform the same sequence as in p256_beeu-x86_64-asm.pl |
| 145 |
// with the following differences: |
| 146 |
// - "x13" is set directly to the number of trailing 0s in B |
| 147 |
// (using rbit and clz instructions) |
| 148 |
// - The loop is only used to call SHIFT1(X) |
| 149 |
// and x13 is decreased while executing the X loop. |
| 150 |
// - SHIFT256(B, x13) is performed before right-shifting X; they are independent |
| 151 |
|
| 152 |
Lbeeu_shift_A_Y: |
| 153 |
// Same for A and Y. |
| 154 |
// Afterwards, (2) still holds. |
| 155 |
// Reverse the bit order of x21 |
| 156 |
// x13 := number of trailing 0s in x21 (= number of leading 0s in x15) |
| 157 |
rbit x15, x21 |
| 158 |
clz x13, x15 |
| 159 |
|
| 160 |
// If there is no shift, goto |B-A|, X+Y update |
| 161 |
cbz x13, Lbeeu_update_B_X_or_A_Y |
| 162 |
|
| 163 |
// Shift A right by "x13" bits |
| 164 |
neg x14, x13 |
| 165 |
lsr x21, x21, x13 |
| 166 |
lsl x15, x22, x14 |
| 167 |
|
| 168 |
lsr x22, x22, x13 |
| 169 |
lsl x19, x23, x14 |
| 170 |
|
| 171 |
orr x21, x21, x15 |
| 172 |
|
| 173 |
lsr x23, x23, x13 |
| 174 |
lsl x20, x24, x14 |
| 175 |
|
| 176 |
orr x22, x22, x19 |
| 177 |
|
| 178 |
lsr x24, x24, x13 |
| 179 |
|
| 180 |
orr x23, x23, x20 |
| 181 |
|
| 182 |
|
| 183 |
// Shift Y right by "x13" bits, adding n whenever Y becomes odd. |
| 184 |
// x13--; |
| 185 |
// x14 := 0; needed in the addition to the most significant word in SHIFT1 |
| 186 |
eor x14, x14, x14 |
| 187 |
Lbeeu_shift_loop_Y: |
| 188 |
tbz x8, #0, Lshift1_1 |
| 189 |
adds x8, x8, x0 |
| 190 |
adcs x9, x9, x1 |
| 191 |
adcs x10, x10, x2 |
| 192 |
adcs x11, x11, x30 |
| 193 |
adc x12, x12, x14 |
| 194 |
Lshift1_1: |
| 195 |
// var0 := [var1|var0]<64..1>; |
| 196 |
// i.e. concatenate var1 and var0, |
| 197 |
// extract bits <64..1> from the resulting 128-bit value |
| 198 |
// and put them in var0 |
| 199 |
extr x8, x9, x8, #1 |
| 200 |
extr x9, x10, x9, #1 |
| 201 |
extr x10, x11, x10, #1 |
| 202 |
extr x11, x12, x11, #1 |
| 203 |
lsr x12, x12, #1 |
| 204 |
|
| 205 |
subs x13, x13, #1 |
| 206 |
bne Lbeeu_shift_loop_Y |
| 207 |
|
| 208 |
Lbeeu_update_B_X_or_A_Y: |
| 209 |
// Try T := B - A; if cs, continue with B > A (cs: carry set = no borrow) |
| 210 |
// Note: this is a case of unsigned arithmetic, where T fits in 4 64-bit words |
| 211 |
// without taking a sign bit if generated. The lack of a carry would |
| 212 |
// indicate a negative result. See, for example, |
| 213 |
// https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/condition-codes-1-condition-flags-and-codes |
| 214 |
subs x14, x25, x21 |
| 215 |
sbcs x15, x26, x22 |
| 216 |
sbcs x19, x27, x23 |
| 217 |
sbcs x20, x28, x24 |
| 218 |
bcs Lbeeu_B_greater_than_A |
| 219 |
|
| 220 |
// Else A > B => |
| 221 |
// A := A - B; Y := Y + X; goto beginning of the loop |
| 222 |
subs x21, x21, x25 |
| 223 |
sbcs x22, x22, x26 |
| 224 |
sbcs x23, x23, x27 |
| 225 |
sbcs x24, x24, x28 |
| 226 |
|
| 227 |
adds x8, x8, x3 |
| 228 |
adcs x9, x9, x4 |
| 229 |
adcs x10, x10, x5 |
| 230 |
adcs x11, x11, x6 |
| 231 |
adc x12, x12, x7 |
| 232 |
b Lbeeu_loop |
| 233 |
|
| 234 |
Lbeeu_B_greater_than_A: |
| 235 |
// Continue with B > A => |
| 236 |
// B := B - A; X := X + Y; goto beginning of the loop |
| 237 |
mov x25, x14 |
| 238 |
mov x26, x15 |
| 239 |
mov x27, x19 |
| 240 |
mov x28, x20 |
| 241 |
|
| 242 |
adds x3, x3, x8 |
| 243 |
adcs x4, x4, x9 |
| 244 |
adcs x5, x5, x10 |
| 245 |
adcs x6, x6, x11 |
| 246 |
adc x7, x7, x12 |
| 247 |
b Lbeeu_loop |
| 248 |
|
| 249 |
Lbeeu_loop_end: |
| 250 |
// The Euclid's algorithm loop ends when A == gcd(a,n); |
| 251 |
// this would be 1, when a and n are co-prime (i.e. do not have a common factor). |
| 252 |
// Since (-1)*Y*a == A (mod |n|), Y>0 |
| 253 |
// then out = -Y mod n |
| 254 |
|
| 255 |
// Verify that A = 1 ==> (-1)*Y*a = A = 1 (mod |n|) |
| 256 |
// Is A-1 == 0? |
| 257 |
// If not, fail. |
| 258 |
sub x14, x21, #1 |
| 259 |
orr x14, x14, x22 |
| 260 |
orr x14, x14, x23 |
| 261 |
orr x14, x14, x24 |
| 262 |
cbnz x14, Lbeeu_err |
| 263 |
|
| 264 |
// If Y>n ==> Y:=Y-n |
| 265 |
Lbeeu_reduction_loop: |
| 266 |
// x_i := y_i - n_i (X is no longer needed, use it as temp) |
| 267 |
// (x14 = 0 from above) |
| 268 |
subs x3, x8, x0 |
| 269 |
sbcs x4, x9, x1 |
| 270 |
sbcs x5, x10, x2 |
| 271 |
sbcs x6, x11, x30 |
| 272 |
sbcs x7, x12, x14 |
| 273 |
|
| 274 |
// If result is non-negative (i.e., cs = carry set = no borrow), |
| 275 |
// y_i := x_i; goto reduce again |
| 276 |
// else |
| 277 |
// y_i := y_i; continue |
| 278 |
csel x8, x3, x8, cs |
| 279 |
csel x9, x4, x9, cs |
| 280 |
csel x10, x5, x10, cs |
| 281 |
csel x11, x6, x11, cs |
| 282 |
csel x12, x7, x12, cs |
| 283 |
bcs Lbeeu_reduction_loop |
| 284 |
|
| 285 |
// Now Y < n (Y cannot be equal to n, since the inverse cannot be 0) |
| 286 |
// out = -Y = n-Y |
| 287 |
subs x8, x0, x8 |
| 288 |
sbcs x9, x1, x9 |
| 289 |
sbcs x10, x2, x10 |
| 290 |
sbcs x11, x30, x11 |
| 291 |
|
| 292 |
// Save Y in output (out (x0) was saved on the stack) |
| 293 |
ldr x3, [sp,#96] |
| 294 |
stp x8, x9, [x3] |
| 295 |
stp x10, x11, [x3,#16] |
| 296 |
// return 1 (success) |
| 297 |
mov x0, #1 |
| 298 |
b Lbeeu_finish |
| 299 |
|
| 300 |
Lbeeu_err: |
| 301 |
// return 0 (error) |
| 302 |
eor x0, x0, x0 |
| 303 |
|
| 304 |
Lbeeu_finish: |
| 305 |
// Restore callee-saved registers, except x0, x2 |
| 306 |
add sp,x29,#0 |
| 307 |
ldp x19,x20,[sp,#16] |
| 308 |
ldp x21,x22,[sp,#32] |
| 309 |
ldp x23,x24,[sp,#48] |
| 310 |
ldp x25,x26,[sp,#64] |
| 311 |
ldp x27,x28,[sp,#80] |
| 312 |
ldp x29,x30,[sp],#112 |
| 313 |
|
| 314 |
AARCH64_VALIDATE_LINK_REGISTER |
| 315 |
ret |
| 316 |
|
| 317 |
#endif // !OPENSSL_NO_ASM && defined(__AARCH64EL__) && defined(__APPLE__) |
| 318 |
#if defined(__ELF__) |
| 319 |
// See https://www.airs.com/blog/archives/518. |
| 320 |
.section .note.GNU-stack,"",%progbits |
| 321 |
#endif |