| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "owndefs.h" |
| 18 |
|
| 19 |
#if (_IPP32E >= _IPP32E_K1) |
| 20 |
|
| 21 |
#include "gfpec/pcpgfpecstuff.h" |
| 22 |
#include "gfpec/pcpgfpstuff.h" |
| 23 |
|
| 24 |
#include "gfpec/sm2/ifma_defs_sm2.h" |
| 25 |
#include "gfpec/sm2/ifma_ecpoint_sm2.h" |
| 26 |
#include "gfpec/sm2/ifma_arith_method_sm2.h" |
| 27 |
|
| 28 |
|
| 29 |
IPP_OWN_DEFN(IppStatus, gfec_Sign_sm2_avx512, (const IppsBigNumState* pMsgDigest, |
| 30 |
const IppsBigNumState* pRegPrivate, |
| 31 |
IppsBigNumState* pEphPrivate, |
| 32 |
IppsBigNumState* pSignR, |
| 33 |
IppsBigNumState* pSignS, |
| 34 |
IppsGFpECState* pEC, |
| 35 |
Ipp8u* pScratchBuffer)) |
| 36 |
|
| 37 |
{ |
| 38 |
IPP_UNREFERENCED_PARAMETER(pScratchBuffer); |
| 39 |
|
| 40 |
IppStatus sts = ippStsNoErr; |
| 41 |
|
| 42 |
gsModEngine* pME = GFP_PMA(ECP_GFP(pEC)); |
| 43 |
gsModEngine* nME = ECP_MONT_R(pEC); |
| 44 |
|
| 45 |
const int orderBits = ECP_ORDBITSIZE(pEC); |
| 46 |
const int orderLen = BITS_BNU_CHUNK(orderBits); |
| 47 |
|
| 48 |
ifmaArithMethod* pmeth = (ifmaArithMethod*)GFP_METHOD_ALT(pME); |
| 49 |
ifmaArithMethod* nmeth = (ifmaArithMethod*)GFP_METHOD_ALT(nME); |
| 50 |
|
| 51 |
ifma_import to_radix52 = pmeth->import_to52; |
| 52 |
ifma_export from_radix52 = pmeth->export_to64; |
| 53 |
|
| 54 |
|
| 55 |
ifma_decode p_from_mont = pmeth->decode; |
| 56 |
|
| 57 |
|
| 58 |
ifma_encode n_to_mont = nmeth->encode; |
| 59 |
ifma_decode n_from_mont = nmeth->decode; |
| 60 |
ifma_mul n_mul = nmeth->mul; |
| 61 |
ifma_add n_add = nmeth->add; |
| 62 |
ifma_add n_sub = nmeth->sub; |
| 63 |
ifma_inv n_inv = nmeth->inv; |
| 64 |
ifma_red n_red = nmeth->red; |
| 65 |
|
| 66 |
fesm2 sign_r, sign_s; |
| 67 |
sign_r = sign_s = setzero_i64(); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
BNU_CHUNK_T* pExtendedScalar = cpGFpGetPool(2, pME); |
| 76 |
cpGFpElementCopyPad(pExtendedScalar, |
| 77 |
orderLen + 1, |
| 78 |
BN_NUMBER(pEphPrivate), |
| 79 |
BN_SIZE(pEphPrivate)); |
| 80 |
|
| 81 |
__ALIGN64 PSM2_POINT_IFMA P; |
| 82 |
|
| 83 |
if (ECP_PREMULBP(pEC)) { |
| 84 |
gesm2_mul_base(&P, (Ipp8u*)pExtendedScalar); |
| 85 |
} else { |
| 86 |
BNU_CHUNK_T* pPool = cpGFpGetPool(3, pME); |
| 87 |
|
| 88 |
|
| 89 |
__ALIGN64 PSM2_POINT_IFMA G52; |
| 90 |
recode_point_to_mont52(&G52, ECP_G(pEC), pPool, pmeth, pME); |
| 91 |
|
| 92 |
gesm2_mul(&P, &G52, (Ipp8u*)pExtendedScalar, orderBits); |
| 93 |
|
| 94 |
cpGFpReleasePool(3, pME); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
gesm2_to_affine( &(P.x), NULL, &P); |
| 99 |
P.x = p_from_mont(P.x); |
| 100 |
P.x = n_red(P.x); |
| 101 |
|
| 102 |
|
| 103 |
BNU_CHUNK_T* pTmp = cpGFpGetPool(1, pME); |
| 104 |
fesm2 msg = setzero_i64(); |
| 105 |
ZEXPAND_COPY_BNU(pTmp, orderLen, BN_NUMBER(pMsgDigest), BN_SIZE(pMsgDigest)); |
| 106 |
msg = to_radix52((Ipp64u*)pTmp); |
| 107 |
msg = n_red(msg); |
| 108 |
|
| 109 |
msg = n_to_mont(msg); |
| 110 |
P.x = n_to_mont(P.x); |
| 111 |
|
| 112 |
sign_r = n_add(msg, P.x); |
| 113 |
|
| 114 |
|
| 115 |
const mask8 sign_r_err_zeros = FESM2_IS_ZERO(sign_r); |
| 116 |
|
| 117 |
|
| 118 |
fesm2 eph_key = setzero_i64(); |
| 119 |
ZEXPAND_COPY_BNU(pTmp, orderLen, BN_NUMBER(pEphPrivate), BN_SIZE(pEphPrivate)); |
| 120 |
eph_key = to_radix52((Ipp64u*)pTmp); |
| 121 |
eph_key = n_to_mont(eph_key); |
| 122 |
|
| 123 |
fesm2 t; |
| 124 |
t = n_add(eph_key, sign_r); |
| 125 |
|
| 126 |
const mask8 rpk_err_zeros = FESM2_IS_ZERO(t); |
| 127 |
|
| 128 |
if (((mask8)0xFF == sign_r_err_zeros) || ((mask8)0xFF == rpk_err_zeros)) |
| 129 |
sts = ippStsEphemeralKeyErr; |
| 130 |
|
| 131 |
fesm2 reg_key = setzero_i64(); |
| 132 |
ZEXPAND_COPY_BNU(pTmp, orderLen, BN_NUMBER(pRegPrivate), BN_SIZE(pRegPrivate)); |
| 133 |
reg_key = to_radix52((Ipp64u*)pTmp); |
| 134 |
|
| 135 |
const fesm2 one = FESM2_LOADU(PSM2_ONE52); |
| 136 |
|
| 137 |
t = n_to_mont(one); |
| 138 |
reg_key = n_to_mont(reg_key); |
| 139 |
|
| 140 |
|
| 141 |
sign_s = n_mul(sign_r, reg_key); |
| 142 |
|
| 143 |
reg_key = n_add(reg_key, t); |
| 144 |
|
| 145 |
reg_key = n_inv(reg_key); |
| 146 |
|
| 147 |
sign_s = n_sub(eph_key, sign_s); |
| 148 |
|
| 149 |
sign_s = n_mul(sign_s, reg_key); |
| 150 |
|
| 151 |
|
| 152 |
sign_s = ifma_lnorm52(sign_s); |
| 153 |
sign_s = n_from_mont(sign_s); |
| 154 |
sign_r = n_from_mont(sign_r); |
| 155 |
|
| 156 |
const mask8 is_zero = (mask8)(FESM2_IS_ZERO(sign_r) | FESM2_IS_ZERO(sign_s)); |
| 157 |
if ((mask8)0xFF == is_zero) |
| 158 |
sts = ippStsEphemeralKeyErr; |
| 159 |
|
| 160 |
|
| 161 |
from_radix52((Ipp64u*)pTmp, sign_s); |
| 162 |
BN_SIGN(pSignS) = ippBigNumPOS; |
| 163 |
BN_SIZE(pSignS) = orderLen; |
| 164 |
ZEXPAND_COPY_BNU(BN_NUMBER(pSignS), BN_ROOM(pSignS), pTmp, orderLen); |
| 165 |
|
| 166 |
from_radix52((Ipp64u*)pTmp, sign_r); |
| 167 |
BN_SIGN(pSignR) = ippBigNumPOS; |
| 168 |
BN_SIZE(pSignR) = orderLen; |
| 169 |
ZEXPAND_COPY_BNU(BN_NUMBER(pSignR), BN_ROOM(pSignR), pTmp, orderLen); |
| 170 |
|
| 171 |
|
| 172 |
clear_secrets(&eph_key, &(P.x), &t); |
| 173 |
clear_secrets(&sign_r, &sign_s, ®_key); |
| 174 |
|
| 175 |
|
| 176 |
cpGFpReleasePool(3, pME); |
| 177 |
return sts; |
| 178 |
} |
| 179 |
|
| 180 |
#endif |