pcpeccpsecretdhca.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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: ippsECCPSharedSecretDH
  39. //
  40. // Purpose: Shared Secret Value Derivation
  41. // (Diffie-Hellman version).
  42. //
  43. // Returns: Reason:
  44. // ippStsNullPtrErr NULL == pECC
  45. // NULL == pPrivateA
  46. // NULL == pPublicB
  47. // NULL == pShare
  48. //
  49. // ippStsContextMatchErr illegal pECC->idCtx
  50. // illegal pPrivateA->idCtx
  51. // illegal pPublicB->idCtx
  52. // illegal pShare->idCtx
  53. //
  54. // ippStsRangeErr not enough room for share key
  55. //
  56. // ippStsShareKeyErr (infinity) => z
  57. //
  58. // ippStsNoErr no errors
  59. //
  60. // Parameters:
  61. // pPrivateA pointer to own private key
  62. // pPublicB pointer to alien public key
  63. // pShare pointer to the shareds secret value
  64. // pECC pointer to the ECCP context
  65. //
  66. *F*/
  67. IPPFUN(IppStatus, ippsECCPSharedSecretDH,(const IppsBigNumState* pPrivateA,
  68. const IppsECCPPointState* pPublicB,
  69. IppsBigNumState* pShare,
  70. IppsECCPState* pECC))
  71. {
  72. /* test pECC */
  73. IPP_BAD_PTR1_RET(pECC);
  74. /* use aligned EC context */
  75. pECC = (IppsECCPState*)( IPP_ALIGNED_PTR(pECC, ALIGN_VAL) );
  76. /* test ID */
  77. IPP_BADARG_RET(!ECP_VALID_ID(pECC), ippStsContextMatchErr);
  78. /* test private (own) key */
  79. IPP_BAD_PTR1_RET(pPrivateA);
  80. pPrivateA = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPrivateA, ALIGN_VAL) );
  81. IPP_BADARG_RET(!BN_VALID_ID(pPrivateA), ippStsContextMatchErr);
  82. /* test public (other party) key */
  83. IPP_BAD_PTR1_RET(pPublicB);
  84. pPublicB = (IppsECCPPointState*)( IPP_ALIGNED_PTR(pPublicB, ALIGN_VAL) );
  85. IPP_BADARG_RET(!ECP_POINT_VALID_ID(pPublicB), ippStsContextMatchErr);
  86. /* test share secret value */
  87. IPP_BAD_PTR1_RET(pShare);
  88. pShare = (IppsBigNumState*)( IPP_ALIGNED_PTR(pShare, ALIGN_VAL) );
  89. IPP_BADARG_RET(!BN_VALID_ID(pShare), ippStsContextMatchErr);
  90. IPP_BADARG_RET((BN_ROOM(pShare)*BITSIZE(BNU_CHUNK_T)<ECP_GFEBITS(pECC)), ippStsRangeErr);
  91. {
  92. BigNumNode* pList = ECP_BNCTX(pECC);
  93. IppsECCPPointState Tmp;
  94. ECP_POINT_X(&Tmp) = cpBigNumListGet(&pList);
  95. ECP_POINT_Y(&Tmp) = cpBigNumListGet(&pList);
  96. ECP_POINT_Z(&Tmp) = cpBigNumListGet(&pList);
  97. /* Tmp = (own)_private * (alien)_public */
  98. ECP_METHOD(pECC)->MulPoint(pPublicB, pPrivateA, &Tmp, pECC, pList);
  99. /* test: Tmp ~ point at Infinity */
  100. if( ECCP_IsPointAtInfinity(&Tmp) )
  101. return ippStsShareKeyErr;
  102. else {
  103. ECP_METHOD(pECC)->GetPointAffine(pShare, NULL, &Tmp, pECC, pList);
  104. return ippStsNoErr;
  105. }
  106. }
  107. }