pcpeccpgenkeyca.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "pcpeccppoint.h"
  34. #include "pcpeccpmethod.h"
  35. #include "pcpeccpmethodcom.h"
  36. /*F*
  37. // Name: ippsECCPGenKeyPair
  38. //
  39. // Purpose: Generate (private,public) Key Pair
  40. //
  41. // Returns: Reason:
  42. // ippStsNullPtrErr NULL == pECC
  43. // NULL == pPrivate
  44. // NULL == pPublic
  45. //
  46. // ippStsContextMatchErr illegal pECC->idCtx
  47. // illegal pPrivate->idCtx
  48. // illegal pPublic->idCtx
  49. //
  50. // ippStsNoErr no errors
  51. //
  52. // Parameters:
  53. // pPrivate pointer to the resultant private key
  54. // pPublic pointer to the resultant public key
  55. // pECC pointer to the ECCP context
  56. //
  57. *F*/
  58. IPPFUN(IppStatus, ippsECCPGenKeyPair, (IppsBigNumState* pPrivate, IppsECCPPointState* pPublic,
  59. IppsECCPState* pECC,
  60. IppBitSupplier rndFunc, void* pRndParam))
  61. {
  62. IPP_BAD_PTR2_RET(pECC, rndFunc);
  63. /* use aligned EC context */
  64. pECC = (IppsECCPState*)( IPP_ALIGNED_PTR(pECC, ALIGN_VAL) );
  65. /* test ID */
  66. IPP_BADARG_RET(!ECP_VALID_ID(pECC), ippStsContextMatchErr);
  67. /* test private/public keys */
  68. IPP_BAD_PTR2_RET(pPrivate,pPublic);
  69. pPrivate = (IppsBigNumState*)( IPP_ALIGNED_PTR(pPrivate, ALIGN_VAL) );
  70. pPublic = (IppsECCPPointState*)( IPP_ALIGNED_PTR(pPublic, ALIGN_VAL) );
  71. IPP_BADARG_RET(!BN_VALID_ID(pPrivate), ippStsContextMatchErr);
  72. IPP_BADARG_RET((BN_ROOM(pPrivate)*BITSIZE(BNU_CHUNK_T)<ECP_ORDBITS(pECC)), ippStsSizeErr);
  73. IPP_BADARG_RET(!ECP_POINT_VALID_ID(pPublic), ippStsContextMatchErr);
  74. {
  75. /*
  76. // generate random private key X: 0 < X < R
  77. */
  78. int reqBitLen = ECP_ORDBITS(pECC);
  79. IppsBigNumState* pOrder = ECP_ORDER(pECC);
  80. int xSize;
  81. Ipp32u* pX = (Ipp32u*)BN_NUMBER(pPrivate);
  82. Ipp32u xMask = MAKEMASK32(reqBitLen);
  83. BN_SIGN(pPrivate) = ippBigNumPOS;
  84. do {
  85. xSize = BITS2WORD32_SIZE(reqBitLen);
  86. rndFunc(pX, reqBitLen, pRndParam);
  87. pX[xSize-1] &= xMask;
  88. FIX_BNU(pX, xSize);
  89. BN_SIZE(pPrivate) = INTERNAL_BNU_LENGTH(xSize);
  90. } while( (0 == cpBN_tst(pPrivate)) ||
  91. (0 <= cpBN_cmp(pPrivate, pOrder)) );
  92. /* calculate public key */
  93. ECP_METHOD(pECC)->MulBasePoint(pPrivate, pPublic, pECC, ECP_BNCTX(pECC));
  94. return ippStsNoErr;
  95. }
  96. }