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 /// Tiny member CreateJoinRequest implementation.
17 /*! \file */
18 
19 #define EXPORT_EPID_APIS
20 #include <epid/member/api.h>
21 
22 #include "epid/common/types.h"
23 #include "epid/member/tiny/math/efq.h"
24 #include "epid/member/tiny/math/fp.h"
25 #include "epid/member/tiny/math/hashwrap.h"
26 #include "epid/member/tiny/math/mathtypes.h"
27 #include "epid/member/tiny/math/serialize.h"
28 #include "epid/member/tiny/src/context.h"
29 #include "epid/member/tiny/src/native_types.h"
30 #include "epid/member/tiny/src/serialize.h"
31 #include "epid/member/tiny/src/validate.h"
32 #include "epid/member/tiny/stdlib/tiny_stdlib.h"
33 
34 static const FpElemStr epid20_p_str = {
35     {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
36       0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
37       0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D}}};
38 
39 static const G2ElemStr epid20_g2_str = {
40     {{{{0xE2, 0x01, 0x71, 0xC5, 0x4A, 0xA3, 0xDA, 0x05, 0x21, 0x67, 0x04,
41         0x13, 0x74, 0x3C, 0xCF, 0x22, 0xD2, 0x5D, 0x52, 0x68, 0x3D, 0x32,
42         0x47, 0x0E, 0xF6, 0x02, 0x13, 0x43, 0xBF, 0x28, 0x23, 0x94}}},
43      {{{0x59, 0x2D, 0x1E, 0xF6, 0x53, 0xA8, 0x5A, 0x80, 0x46, 0xCC, 0xDC,
44         0x25, 0x4F, 0xBB, 0x56, 0x56, 0x43, 0x43, 0x3B, 0xF6, 0x28, 0x96,
45         0x53, 0xE2, 0x7D, 0xF7, 0xB2, 0x12, 0xBA, 0xA1, 0x89, 0xBE}}}},
46     {{{{0xAE, 0x60, 0xA4, 0xE7, 0x51, 0xFF, 0xD3, 0x50, 0xC6, 0x21, 0xE7,
47         0x03, 0x31, 0x28, 0x26, 0xBD, 0x55, 0xE8, 0xB5, 0x9A, 0x4D, 0x91,
48         0x68, 0x38, 0x41, 0x4D, 0xB8, 0x22, 0xDD, 0x23, 0x35, 0xAE}}},
49      {{{0x1A, 0xB4, 0x42, 0xF9, 0x89, 0xAF, 0xE5, 0xAD, 0xF8, 0x02, 0x74,
50         0xF8, 0x76, 0x45, 0xE2, 0x53, 0x2C, 0xDC, 0x61, 0x81, 0x90, 0x93,
51         0xD6, 0x13, 0x2C, 0x90, 0xFE, 0x89, 0x51, 0xB9, 0x24, 0x21}}}}};
52 
53 static const G1ElemStr epid20_g1_str = {
54     {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}},
57     {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}}}};
60 
EpidCreateJoinRequest(MemberCtx * ctx,GroupPubKey const * pub_key,IssuerNonce const * ni,JoinRequest * join_request)61 EpidStatus EPID_API EpidCreateJoinRequest(MemberCtx* ctx,
62                                           GroupPubKey const* pub_key,
63                                           IssuerNonce const* ni,
64                                           JoinRequest* join_request) {
65   EccPointFq F;
66   EccPointFq R_str;
67   FpElem r;
68   FpElem c;
69   FpElem t;
70   tiny_sha sha_state;
71   sha_digest digest;
72   G1ElemStr g1_str;
73   NativeGroupPubKey deserialized_pkey;
74 
75   if (!ctx || !pub_key || !ni || !join_request) {
76     return kEpidBadArgErr;
77   }
78   if (!ctx->f_is_set) {
79     return kEpidBadArgErr;
80   }
81 
82   GroupPubKeyDeserialize(&deserialized_pkey, pub_key);
83   if (!GroupPubKeyIsInRange(&deserialized_pkey)) {
84     return kEpidBadArgErr;
85   }
86   // pick number r between 1 - (p-1)
87   if (!FpRandNonzero(&r, ctx->rnd_func, ctx->rnd_param)) {
88     return kEpidMathErr;
89   }
90 
91   // F = h1^f
92   EFqAffineExp(&F, &deserialized_pkey.h1, &ctx->f);
93   // R_str = h1^r
94   EFqAffineExp(&R_str, &deserialized_pkey.h1, &r);
95 
96   // c = hash(p || g1 || g2 || h1 || h2 || w || F || R || NI) mod p
97   tinysha_init(ctx->hash_alg, &sha_state);
98   tinysha_update(&sha_state, (void const*)&epid20_p_str, sizeof(epid20_p_str));
99   tinysha_update(&sha_state, (void const*)&epid20_g1_str,
100                  sizeof(epid20_g1_str));
101   tinysha_update(&sha_state, (void const*)&epid20_g2_str,
102                  sizeof(epid20_g2_str));
103   tinysha_update(&sha_state, (void const*)&pub_key->h1, sizeof(pub_key->h1));
104   tinysha_update(&sha_state, (void const*)&pub_key->h2, sizeof(pub_key->h2));
105   tinysha_update(&sha_state, (void const*)&pub_key->w, sizeof(pub_key->w));
106   EFqSerialize(&join_request->F, &F);
107   tinysha_update(&sha_state, (void const*)&join_request->F,
108                  sizeof(join_request->F));
109   EFqSerialize(&g1_str, &R_str);
110   tinysha_update(&sha_state, (void const*)&g1_str, sizeof(g1_str));
111   tinysha_update(&sha_state, (void const*)ni, sizeof(*ni));
112   tinysha_final(digest.digest, &sha_state);
113   FpFromHash(&c, digest.digest, tinysha_digest_size(&sha_state));
114   FpSerialize(&join_request->c, &c);
115 
116   // computes s = (r + c * f) mod p
117   FpMul(&t, &c, &ctx->f);
118   FpAdd(&t, &t, &r);
119   FpSerialize(&join_request->s, &t);
120   return kEpidNoErr;
121 }
122