1/* 2 * Copyright 2005-2021 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 16int 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 && 28 (OPENSSL_sparcv9cap_P[1] & (CFR_MONTMUL | CFR_MONTSQR)) == 29 (CFR_MONTMUL | CFR_MONTSQR)) { 30 typedef int (*bn_mul_mont_f) (BN_ULONG *rp, const BN_ULONG *ap, 31 const BN_ULONG *bp, 32 const BN_ULONG *np, 33 const BN_ULONG *n0); 34 int bn_mul_mont_t4_8(BN_ULONG *rp, const BN_ULONG *ap, 35 const BN_ULONG *bp, const BN_ULONG *np, 36 const BN_ULONG *n0); 37 int bn_mul_mont_t4_16(BN_ULONG *rp, const BN_ULONG *ap, 38 const BN_ULONG *bp, const BN_ULONG *np, 39 const BN_ULONG *n0); 40 int bn_mul_mont_t4_24(BN_ULONG *rp, const BN_ULONG *ap, 41 const BN_ULONG *bp, const BN_ULONG *np, 42 const BN_ULONG *n0); 43 int bn_mul_mont_t4_32(BN_ULONG *rp, const BN_ULONG *ap, 44 const BN_ULONG *bp, const BN_ULONG *np, 45 const BN_ULONG *n0); 46 static const bn_mul_mont_f funcs[4] = { 47 bn_mul_mont_t4_8, bn_mul_mont_t4_16, 48 bn_mul_mont_t4_24, bn_mul_mont_t4_32 49 }; 50 bn_mul_mont_f worker = funcs[num / 16 - 1]; 51 52 if ((*worker) (rp, ap, bp, np, n0)) 53 return 1; 54 /* retry once and fall back */ 55 if ((*worker) (rp, ap, bp, np, n0)) 56 return 1; 57 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num); 58 } 59 if ((OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3)) 60 return bn_mul_mont_vis3(rp, ap, bp, np, n0, num); 61 else if (num >= 8 && 62 /* 63 * bn_mul_mont_fpu doesn't use FMADD, we just use the 64 * flag to detect when FPU path is preferable in cases 65 * when current heuristics is unreliable. [it works 66 * out because FMADD-capable processors where FPU 67 * code path is undesirable are also VIS3-capable and 68 * VIS3 code path takes precedence.] 69 */ 70 ( (OPENSSL_sparcv9cap_P[0] & SPARCV9_FMADD) || 71 (OPENSSL_sparcv9cap_P[0] & 72 (SPARCV9_PREFER_FPU | SPARCV9_VIS1)) == 73 (SPARCV9_PREFER_FPU | SPARCV9_VIS1) )) 74 return bn_mul_mont_fpu(rp, ap, bp, np, n0, num); 75 } 76 return bn_mul_mont_int(rp, ap, bp, np, n0, num); 77} 78