sign.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*############################################################################
  2. # Copyright 2016-2017 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. /// EpidSign implementation.
  17. /*! \file */
  18. #include <epid/member/api.h>
  19. #include <string.h>
  20. #include "epid/common/src/endian_convert.h"
  21. #include "epid/common/src/memory.h"
  22. #include "epid/common/src/sigrlvalid.h"
  23. #include "epid/member/src/context.h"
  24. #include "epid/member/src/nrprove.h"
  25. #include "epid/member/src/signbasic.h"
  26. /// Handle SDK Error with Break
  27. #define BREAK_ON_EPID_ERROR(ret) \
  28. if (kEpidNoErr != (ret)) { \
  29. break; \
  30. }
  31. EpidStatus EpidSign(MemberCtx const* ctx, void const* msg, size_t msg_len,
  32. void const* basename, size_t basename_len,
  33. EpidSignature* sig, size_t sig_len) {
  34. EpidStatus sts = kEpidErr;
  35. uint32_t num_sig_rl = 0;
  36. OctStr32 octstr32_0 = {{0x00, 0x00, 0x00, 0x00}};
  37. BigNumStr rnd_bsn = {0};
  38. if (!ctx || !sig) {
  39. return kEpidBadArgErr;
  40. }
  41. if (!msg && (0 != msg_len)) {
  42. // if message is non-empty it must have both length and content
  43. return kEpidBadArgErr;
  44. }
  45. if (!basename && (0 != basename_len)) {
  46. // if basename is non-empty it must have both length and content
  47. return kEpidBadArgErr;
  48. }
  49. if (!ctx->is_provisioned) {
  50. return kEpidOutOfSequenceError;
  51. }
  52. if (EpidGetSigSize(ctx->sig_rl) > sig_len) {
  53. return kEpidBadArgErr;
  54. }
  55. // 11. The member sets sigma0 = (B, K, T, c, sx, sf, sa, sb).
  56. sts = EpidSignBasic(ctx, msg, msg_len, basename, basename_len, &sig->sigma0,
  57. &rnd_bsn);
  58. if (kEpidNoErr != sts) {
  59. return sts;
  60. }
  61. if (!ctx->sig_rl) {
  62. // 12. If SigRL is not provided as input,
  63. // a. The member sets RLver = 0 and n2 = 0.
  64. // b. The member outputs (sigma0, RLver, n2) and returns "succeeded".
  65. sig->rl_ver = octstr32_0;
  66. sig->n2 = octstr32_0;
  67. return kEpidNoErr;
  68. } else {
  69. uint32_t i = 0;
  70. EpidStatus nr_prove_status = kEpidNoErr;
  71. // 13. If SigRL is provided as input, the member proceeds with
  72. // the following steps:
  73. // a. The member verifies that gid in public key and in SigRL
  74. // match.
  75. // This was done under EpidMemberSetSigRl function.
  76. // b. The member copies RLver and n2 values in SigRL to the
  77. // signature.
  78. sig->rl_ver = ctx->sig_rl->version;
  79. sig->n2 = ctx->sig_rl->n2;
  80. // c. For i = 0, ..., n2-1, the member computes sigma[i] =
  81. // nrProve(f, B, K, B[i], K[i]). The details of nrProve()
  82. // will be given in the next subsection.
  83. num_sig_rl = ntohl(ctx->sig_rl->n2);
  84. for (i = 0; i < num_sig_rl; i++) {
  85. if (basename) {
  86. sts = EpidNrProve(ctx, msg, msg_len, basename, basename_len,
  87. &sig->sigma0, &ctx->sig_rl->bk[i], &sig->sigma[i]);
  88. } else {
  89. sts = EpidNrProve(ctx, msg, msg_len, &rnd_bsn, sizeof(rnd_bsn),
  90. &sig->sigma0, &ctx->sig_rl->bk[i], &sig->sigma[i]);
  91. }
  92. if (kEpidNoErr != sts) {
  93. nr_prove_status = sts;
  94. }
  95. }
  96. if (kEpidNoErr != nr_prove_status) {
  97. memset(&sig->sigma[0], 0, num_sig_rl * sizeof(sig->sigma[0]));
  98. return nr_prove_status;
  99. }
  100. }
  101. // d. The member outputs (sigma0, RLver, n2, sigma[0], ...,
  102. // sigma[n2-1]).
  103. // e. If any of the nrProve() functions outputs "failed", the
  104. // member returns "revoked", otherwise returns "succeeded".
  105. return kEpidNoErr;
  106. }