pcpeccpverifydsaca.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "owndefs.h"
  32. #include "owncp.h"
  33. #include "pcpeccp.h"
  34. #include "pcpeccppoint.h"
  35. #include "pcpeccpmethod.h"
  36. #include "pcpeccpmethodcom.h"
  37. /*F*
  38. // Name: ippsECCPVerifyDSA
  39. //
  40. // Purpose: Verify Signature (DSA version).
  41. //
  42. // Returns: Reason:
  43. // ippStsNullPtrErr NULL == pECC
  44. // NULL == pMsgDigest
  45. // NULL == pSignX
  46. // NULL == pSignY
  47. // NULL == pResult
  48. //
  49. // ippStsContextMatchErr illegal pECC->idCtx
  50. // illegal pMsgDigest->idCtx
  51. // illegal pSignX->idCtx
  52. // illegal pSignY->idCtx
  53. //
  54. // ippStsMessageErr MsgDigest >= order
  55. //
  56. // ippStsNoErr no errors
  57. //
  58. // Parameters:
  59. // pMsgDigest pointer to the message representative to be signed
  60. // pSignX,pSignY pointer to the signature
  61. // pResult pointer to the result: ippECValid/ippECInvalidSignature
  62. // pECC pointer to the ECCP context
  63. //
  64. // Note:
  65. // - signer's key must be set up in ECCP context
  66. // before ippsECCPVerifyDSA() usage
  67. //
  68. *F*/
  69. IPPFUN(IppStatus, ippsECCPVerifyDSA,(const IppsBigNumState* pMsgDigest,
  70. const IppsBigNumState* pSignX, const IppsBigNumState* pSignY,
  71. IppECResult* pResult,
  72. IppsECCPState* pECC))
  73. {
  74. IppsMontState* rMont;
  75. /* test pECC */
  76. IPP_BAD_PTR1_RET(pECC);
  77. /* use aligned EC context */
  78. pECC = (IppsECCPState*)( IPP_ALIGNED_PTR(pECC, ALIGN_VAL) );
  79. IPP_BADARG_RET(!ECP_VALID_ID(pECC), ippStsContextMatchErr);
  80. /* test message representative */
  81. IPP_BAD_PTR1_RET(pMsgDigest);
  82. pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, ALIGN_VAL) );
  83. IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr);
  84. rMont = ECP_RMONT(pECC);
  85. IPP_BADARG_RET((0<=cpBN_cmp(pMsgDigest, ECP_ORDER(pECC))), ippStsMessageErr);
  86. /* test result */
  87. IPP_BAD_PTR1_RET(pResult);
  88. /* test signature */
  89. IPP_BAD_PTR2_RET(pSignX,pSignY);
  90. pSignX = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignX, ALIGN_VAL) );
  91. pSignY = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignY, ALIGN_VAL) );
  92. IPP_BADARG_RET(!BN_VALID_ID(pSignX), ippStsContextMatchErr);
  93. IPP_BADARG_RET(!BN_VALID_ID(pSignY), ippStsContextMatchErr);
  94. /* test signature value */
  95. if( (0>cpBN_tst(pSignX)) || (0>cpBN_tst(pSignY)) ||
  96. (0<=cpBN_cmp(pSignX, ECP_ORDER(pECC))) ||
  97. (0<=cpBN_cmp(pSignY, ECP_ORDER(pECC))) ) {
  98. *pResult = ippECInvalidSignature;
  99. return ippStsNoErr;
  100. }
  101. /* validate signature */
  102. else {
  103. IppsECCPPointState P1;
  104. BigNumNode* pList = ECP_BNCTX(pECC);
  105. IppsBigNumState* pH1 = cpBigNumListGet(&pList);
  106. IppsBigNumState* pH2 = cpBigNumListGet(&pList);
  107. IppsBigNumState* pOrder = cpBigNumListGet(&pList);
  108. BN_Set(MNT_MODULUS(rMont), MNT_SIZE(rMont), pOrder);
  109. ECP_POINT_X(&P1) = cpBigNumListGet(&pList);
  110. ECP_POINT_Y(&P1) = cpBigNumListGet(&pList);
  111. ECP_POINT_Z(&P1) = cpBigNumListGet(&pList);
  112. PMA_inv(pH1, (IppsBigNumState*)pSignY, pOrder);/* h = 1/signY (mod order) */
  113. PMA_enc(pH1, pH1, rMont);
  114. PMA_mule(pH2, (IppsBigNumState*)pSignX, pH1, rMont); /* h2 = pSignX * h (mod order) */
  115. PMA_mule(pH1, (IppsBigNumState*)pMsgDigest, pH1, rMont); /* h1 = pMsgDigest * h (mod order) */
  116. /* compute h1*BasePoint + h2*publicKey */
  117. ECP_METHOD(pECC)->ProdPoint(ECP_GENC(pECC), pH1,
  118. ECP_PUBLIC(pECC), pH2,
  119. &P1, pECC, pList);
  120. if( ECCP_IsPointAtInfinity(&P1) ) {
  121. *pResult = ippECInvalidSignature;
  122. return ippStsNoErr;
  123. }
  124. /* extract X component */
  125. ECP_METHOD(pECC)->GetPointAffine(pH1, NULL, &P1, pECC, pList);
  126. /* compare with signX */
  127. PMA_mod(pH1, pH1, pOrder);
  128. *pResult = (0==cpBN_cmp(pH1, pSignX))? ippECValid : ippECInvalidSignature;
  129. return ippStsNoErr;
  130. }
  131. }