1 /*############################################################################
2 # Copyright 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 /// Member pre-computation implementation
17 /*! \file */
18 #include "epid/member/src/precomp.h"
19
20 #include "epid/common/src/epid2params.h"
21 #include "epid/common/src/grouppubkey.h"
22 #include "epid/common/types.h"
23
24 /// Handle SDK Error with Break
25 #define BREAK_ON_EPID_ERROR(ret) \
26 if (kEpidNoErr != (ret)) { \
27 break; \
28 }
29
PrecomputeMemberPairing(Epid2Params_ const * epid2_params,GroupPubKey const * pub_key,G1ElemStr const * A_str,MemberPrecomp * precomp)30 EpidStatus PrecomputeMemberPairing(Epid2Params_ const* epid2_params,
31 GroupPubKey const* pub_key,
32 G1ElemStr const* A_str,
33 MemberPrecomp* precomp) {
34 EpidStatus sts = kEpidErr;
35
36 GroupPubKey_* pub_key_ = NULL;
37 EcPoint* A = NULL;
38 FfElement* e = NULL;
39
40 if (!epid2_params || !pub_key || !A_str || !precomp) return kEpidBadArgErr;
41
42 do {
43 EcGroup* G1 = epid2_params->G1;
44 EcGroup* G2 = epid2_params->G2;
45 FiniteField* GT = epid2_params->GT;
46 PairingState* ps_ctx = epid2_params->pairing_state;
47 EcPoint* g2 = epid2_params->g2;
48
49 sts = CreateGroupPubKey(pub_key, G1, G2, &pub_key_);
50 BREAK_ON_EPID_ERROR(sts);
51
52 sts = NewFfElement(GT, &e);
53 BREAK_ON_EPID_ERROR(sts);
54
55 // 1. The member computes e12 = pairing(h1, g2).
56 sts = Pairing(ps_ctx, pub_key_->h1, g2, e);
57 BREAK_ON_EPID_ERROR(sts);
58 sts = WriteFfElement(GT, e, &precomp->e12, sizeof(precomp->e12));
59 BREAK_ON_EPID_ERROR(sts);
60
61 // 2. The member computes e22 = pairing(h2, g2).
62 sts = Pairing(ps_ctx, pub_key_->h2, g2, e);
63 BREAK_ON_EPID_ERROR(sts);
64 sts = WriteFfElement(GT, e, &precomp->e22, sizeof(precomp->e22));
65 BREAK_ON_EPID_ERROR(sts);
66
67 // 3. The member computes e2w = pairing(h2, w).
68 sts = Pairing(ps_ctx, pub_key_->h2, pub_key_->w, e);
69 BREAK_ON_EPID_ERROR(sts);
70 sts = WriteFfElement(GT, e, &precomp->e2w, sizeof(precomp->e2w));
71 BREAK_ON_EPID_ERROR(sts);
72
73 // 4. The member computes ea2 = pairing(A, g2).
74 sts = NewEcPoint(G1, &A);
75 BREAK_ON_EPID_ERROR(sts);
76 sts = ReadEcPoint(G1, A_str, sizeof(*A_str), A);
77 BREAK_ON_EPID_ERROR(sts);
78 sts = Pairing(ps_ctx, A, g2, e);
79 BREAK_ON_EPID_ERROR(sts);
80 sts = WriteFfElement(GT, e, &precomp->ea2, sizeof(precomp->ea2));
81 BREAK_ON_EPID_ERROR(sts);
82
83 sts = kEpidNoErr;
84 } while (0);
85
86 DeleteGroupPubKey(&pub_key_);
87 DeleteEcPoint(&A);
88 DeleteFfElement(&e);
89
90 return sts;
91 }
92