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