privkey.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*############################################################################
  2. # Copyright 2016 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /*!
  17. * \file
  18. * \brief Private key implementation.
  19. */
  20. #include "epid/common/src/memory.h"
  21. #include "epid/member/src/privkey.h"
  22. EpidStatus CreatePrivKey(PrivKey const* priv_key_str, EcGroup* G1,
  23. FiniteField* Fp, PrivKey_** priv_key) {
  24. EpidStatus result = kEpidErr;
  25. PrivKey_* priv_key_ = NULL;
  26. // check parameters
  27. if (!priv_key_str || !G1 || !Fp || !priv_key) return kEpidBadArgErr;
  28. do {
  29. priv_key_ = SAFE_ALLOC(sizeof(*priv_key_));
  30. if (!priv_key_) {
  31. result = kEpidMemAllocErr;
  32. break;
  33. }
  34. result = NewEcPoint(G1, &priv_key_->A);
  35. if (kEpidNoErr != result) break;
  36. result = NewFfElement(Fp, &priv_key_->x);
  37. if (kEpidNoErr != result) break;
  38. result = NewFfElement(Fp, &priv_key_->f);
  39. if (kEpidNoErr != result) break;
  40. priv_key_->gid = priv_key_str->gid;
  41. result = ReadEcPoint(G1, &priv_key_str->A, sizeof(priv_key_str->A),
  42. priv_key_->A);
  43. if (kEpidNoErr != result) break;
  44. result = ReadFfElement(Fp, &priv_key_str->x, sizeof(priv_key_str->x),
  45. priv_key_->x);
  46. if (kEpidNoErr != result) break;
  47. result = ReadFfElement(Fp, &priv_key_str->f, sizeof(priv_key_str->f),
  48. priv_key_->f);
  49. if (kEpidNoErr != result) break;
  50. *priv_key = priv_key_;
  51. result = kEpidNoErr;
  52. } while (0);
  53. if (kEpidNoErr != result) {
  54. DeletePrivKey(&priv_key_);
  55. }
  56. return (result);
  57. }
  58. void DeletePrivKey(PrivKey_** priv_key) {
  59. if (priv_key) {
  60. if (*priv_key) {
  61. DeleteEcPoint(&((*priv_key)->A));
  62. DeleteFfElement(&((*priv_key)->x));
  63. DeleteFfElement(&((*priv_key)->f));
  64. }
  65. SAFE_FREE(*priv_key);
  66. }
  67. }