pcpeccpsigndsaca.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: ippsECCPSignDSA
  39. //
  40. // Purpose: Signing of message representative.
  41. // (DSA version).
  42. //
  43. // Returns: Reason:
  44. // ippStsNullPtrErr NULL == pECC
  45. // NULL == pMsgDigest
  46. // NULL == pPrivate
  47. // NULL == pSignX
  48. // NULL == pSignY
  49. //
  50. // ippStsContextMatchErr illegal pECC->idCtx
  51. // illegal pMsgDigest->idCtx
  52. // illegal pPrivate->idCtx
  53. // illegal pSignX->idCtx
  54. // illegal pSignY->idCtx
  55. //
  56. // ippStsMessageErr MsgDigest >= order
  57. //
  58. // ippStsRangeErr not enough room for:
  59. // signX
  60. // signY
  61. //
  62. // ippStsEphemeralKeyErr (0==signX) || (0==signY)
  63. //
  64. // ippStsNoErr no errors
  65. //
  66. // Parameters:
  67. // pMsgDigest pointer to the message representative to be signed
  68. // pPrivate pointer to the regular private key
  69. // pSignX,pSignY pointer to the signature
  70. // pECC pointer to the ECCP context
  71. //
  72. // Note:
  73. // - ephemeral key pair extracted from pECC and
  74. // must be generated and before ippsECCPDSASign() usage
  75. // - ephemeral key pair destroy before exit
  76. //
  77. *F*/
  78. IPPFUN(IppStatus, ippsECCPSignDSA,(const IppsBigNumState* pMsgDigest,
  79. const IppsBigNumState* pPrivate,
  80. IppsBigNumState* pSignX, IppsBigNumState* pSignY,
  81. IppsECCPState* pECC))
  82. {
  83. /* test pECC */
  84. IPP_BAD_PTR1_RET(pECC);
  85. /* use aligned EC context */
  86. pECC = (IppsECCPState*)( IPP_ALIGNED_PTR(pECC, ALIGN_VAL) );
  87. IPP_BADARG_RET(!ECP_VALID_ID(pECC), ippStsContextMatchErr);
  88. /* test private key*/
  89. IPP_BAD_PTR1_RET(pPrivate);
  90. pPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPrivate, ALIGN_VAL) );
  91. IPP_BADARG_RET(!BN_VALID_ID(pPrivate), ippStsContextMatchErr);
  92. /* test message representative */
  93. IPP_BAD_PTR1_RET(pMsgDigest);
  94. pMsgDigest = (IppsBigNumState*)( IPP_ALIGNED_PTR(pMsgDigest, ALIGN_VAL) );
  95. IPP_BADARG_RET(!BN_VALID_ID(pMsgDigest), ippStsContextMatchErr);
  96. IPP_BADARG_RET((0<=cpBN_cmp(pMsgDigest, ECP_ORDER(pECC))), ippStsMessageErr);
  97. /* test signature */
  98. IPP_BAD_PTR2_RET(pSignX,pSignY);
  99. pSignX = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignX, ALIGN_VAL) );
  100. pSignY = (IppsBigNumState*)( IPP_ALIGNED_PTR(pSignY, ALIGN_VAL) );
  101. IPP_BADARG_RET(!BN_VALID_ID(pSignX), ippStsContextMatchErr);
  102. IPP_BADARG_RET(!BN_VALID_ID(pSignY), ippStsContextMatchErr);
  103. IPP_BADARG_RET((BN_ROOM(pSignX)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITS(pECC)), ippStsRangeErr);
  104. IPP_BADARG_RET((BN_ROOM(pSignY)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITS(pECC)), ippStsRangeErr);
  105. {
  106. IppsMontState* rMont = ECP_RMONT(pECC);
  107. IppsBigNumState* pOrder = ECP_ORDER(pECC);
  108. BigNumNode* pList = ECP_BNCTX(pECC);
  109. IppsBigNumState* pTmp = cpBigNumListGet(&pList);
  110. /* extract ephemeral public key (X component only) */
  111. ECP_METHOD(pECC)->GetPointAffine(pTmp, NULL, ECP_PUBLIC_E(pECC), pECC, pList);
  112. /*
  113. // compute
  114. // signX = eph_pub_x (mod order)
  115. */
  116. PMA_mod(pSignX, pTmp, pOrder);
  117. if( !IsZero_BN(pSignX) ) {
  118. IppsBigNumState* pEncMsg = cpBigNumListGet(&pList);
  119. IppsBigNumState* pEncSignX = cpBigNumListGet(&pList);
  120. PMA_enc(pEncMsg, (IppsBigNumState*)pMsgDigest, rMont);
  121. PMA_enc(pEncSignX, pSignX, rMont);
  122. /*
  123. // compute
  124. // signY = (1/eph_private)*(pMsgDigest + private*signX) (mod order)
  125. */
  126. PMA_inv(pSignY, ECP_PRIVATE_E(pECC), pOrder);
  127. PMA_enc(ECP_PRIVATE_E(pECC), pPrivate, rMont);
  128. PMA_mule(pTmp, ECP_PRIVATE_E(pECC), pEncSignX, rMont);
  129. PMA_add(pTmp, pTmp, pEncMsg, pOrder);
  130. PMA_mule(pSignY, pSignY, pTmp, rMont);
  131. if( !IsZero_BN(pSignY) )
  132. return ippStsNoErr;
  133. }
  134. return ippStsEphemeralKeyErr;
  135. }
  136. }