| 1 |
/* |
| 2 |
* Copyright 2005-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <openssl/bn.h> |
| 12 |
#include "internal/cryptlib.h" |
| 13 |
#include "crypto/sparc_arch.h" |
| 14 |
#include "bn_local.h" /* for definition of bn_mul_mont */ |
| 15 |
|
| 16 |
int bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
| 17 |
const BN_ULONG *np, const BN_ULONG *n0, int num) |
| 18 |
{ |
| 19 |
int bn_mul_mont_vis3(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
| 20 |
const BN_ULONG *np, const BN_ULONG *n0, int num); |
| 21 |
int bn_mul_mont_fpu(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
| 22 |
const BN_ULONG *np, const BN_ULONG *n0, int num); |
| 23 |
int bn_mul_mont_int(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp, |
| 24 |
const BN_ULONG *np, const BN_ULONG *n0, int num); |
| 25 |
|
| 26 |
if (!(num & 1) && num >= 6) { |
| 27 |
if ((num & 15) == 0 && num <= 64 && (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) == (CFR_MONTMUL | CFR_MONTSQR)) { |
| 28 |
typedef int (*bn_mul_mont_f)(BN_ULONG *rp, const BN_ULONG *ap, |
| 29 |
const BN_ULONG *bp, |
| 30 |
const BN_ULONG *np, |
| 31 |
const BN_ULONG *n0); |
| 32 |
int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, |
| 33 |
const BN_ULONG *bp, const BN_ULONG *np, |
| 34 |
const BN_ULONG *n0); |
| 35 |
int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap, |
| 36 |
const BN_ULONG *bp, const BN_ULONG *np, |
| 37 |
const BN_ULONG *n0); |
| 38 |
int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap, |
| 39 |
const BN_ULONG *bp, const BN_ULONG *np, |
| 40 |
const BN_ULONG *n0); |
| 41 |
int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap, |
| 42 |
const BN_ULONG *bp, const BN_ULONG *np, |
| 43 |
const BN_ULONG *n0); |
| 44 |
static const bn_mul_mont_f funcs[4] = { |
| 45 |
bn_mul_mont_t4_8, bn_mul_mont_t4_16, |
| 46 |
bn_mul_mont_t4_24, bn_mul_mont_t4_32 |
| 47 |
}; |
| 48 |
bn_mul_mont_f worker = funcs[num / 16 - 1]; |
| 49 |
|
| 50 |
if ((*worker)(rp, ap, bp, np, n0)) |
| 51 |
return 1; |
| 52 |
/* retry once and fall back */ |
| 53 |
if ((*worker)(rp, ap, bp, np, n0)) |
| 54 |
return 1; |
| 55 |
return bn_mul_mont_vis3(rp, ap, bp, np, n0, num); |
| 56 |
} |
| 57 |
if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3)) |
| 58 |
return bn_mul_mont_vis3(rp, ap, bp, np, n0, num); |
| 59 |
else if (num >= 8 && |
| 60 |
/* |
| 61 |
* bn_mul_mont_fpu doesn't use FMADD, we just use the |
| 62 |
* flag to detect when FPU path is preferable in cases |
| 63 |
* when current heuristics is unreliable. [it works |
| 64 |
* out because FMADD-capable processors where FPU |
| 65 |
* code path is undesirable are also VIS3-capable and |
| 66 |
* VIS3 code path takes precedence.] |
| 67 |
*/ |
| 68 |
((OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) || (OPENSSL_sparcv9cap_P[0] & (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) == (SPARCV9_PREFER_FPU | SPARCV9_VIS1))) |
| 69 |
return bn_mul_mont_fpu(rp, ap, bp, np, n0, num); |
| 70 |
} |
| 71 |
return bn_mul_mont_int(rp, ap, bp, np, n0, num); |
| 72 |
} |