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