sign.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 EpidSign implementation.
  19. */
  20. #include <string.h>
  21. #include "epid/common/src/endian_convert.h"
  22. #include "epid/common/src/memory.h"
  23. #include "epid/common/src/sigrlvalid.h"
  24. #include "epid/member/api.h"
  25. #include "epid/member/src/context.h"
  26. EpidStatus EpidSign(MemberCtx const* ctx, void const* msg, size_t msg_len,
  27. void const* basename, size_t basename_len,
  28. SigRl const* sig_rl, size_t sig_rl_size, EpidSignature* sig,
  29. size_t sig_len) {
  30. EpidStatus result = kEpidErr;
  31. uint32_t num_sig_rl = 0;
  32. OctStr32 octstr32_0 = {{0x00, 0x00, 0x00, 0x00}};
  33. if (!ctx || !sig) {
  34. return kEpidBadArgErr;
  35. }
  36. if (!ctx->pub_key) {
  37. return kEpidBadArgErr;
  38. }
  39. if (!msg && (0 != msg_len)) {
  40. // if message is non-empty it must have both length and content
  41. return kEpidBadArgErr;
  42. }
  43. if (!basename && (0 != basename_len)) {
  44. // if basename is non-empty it must have both length and content
  45. return kEpidBadArgErr;
  46. }
  47. if (sig_rl && (sig_rl_size < sizeof(SigRl) - sizeof(SigRlEntry))) {
  48. return kEpidBadArgErr;
  49. }
  50. if (sig_rl && EpidGetSigSize(sig_rl) > sig_len) {
  51. return kEpidBadArgErr;
  52. }
  53. // 11. The member sets sigma0 = (B, K, T, c, sx, sf, sa, sb).
  54. result =
  55. EpidSignBasic(ctx, msg, msg_len, basename, basename_len, &sig->sigma0);
  56. if (kEpidNoErr != result) {
  57. return result;
  58. }
  59. if (!sig_rl) {
  60. // 12. If SigRL is not provided as input,
  61. // a. The member sets RLver = 0 and n2 = 0.
  62. // b. The member outputs (sigma0, RLver, n2) and returns "succeeded".
  63. sig->rl_ver = octstr32_0;
  64. sig->n2 = octstr32_0;
  65. return kEpidNoErr;
  66. } else {
  67. uint32_t i = 0;
  68. EpidStatus nr_prove_status = kEpidNoErr;
  69. // 13. If SigRL is provided as input, the member proceeds with
  70. // the following steps:
  71. // a. The member verifies that gid in public key and in SigRL
  72. // match.
  73. if (!IsSigRlValid(&ctx->pub_key->gid, sig_rl, sig_rl_size)) {
  74. return kEpidBadArgErr;
  75. }
  76. // b. The member copies RLver and n2 values in SigRL to the
  77. // signature.
  78. sig->rl_ver = sig_rl->version;
  79. sig->n2 = 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(sig_rl->n2);
  84. for (i = 0; i < num_sig_rl; i++) {
  85. result = EpidNrProve(ctx, msg, msg_len, &sig->sigma0, &sig_rl->bk[i],
  86. &sig->sigma[i]);
  87. if (kEpidNoErr != result) {
  88. nr_prove_status = result;
  89. }
  90. }
  91. if (kEpidNoErr != nr_prove_status) {
  92. memset(&sig->sigma[0], 0, num_sig_rl * sizeof(sig->sigma[0]));
  93. return nr_prove_status;
  94. }
  95. }
  96. // d. The member outputs (sigma0, RLver, n2, sigma[0], ...,
  97. // sigma[n2-1]).
  98. // e. If any of the nrProve() functions outputs "failed", the
  99. // member returns "revoked", otherwise returns "succeeded".
  100. return kEpidNoErr;
  101. }