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 Init/Deinit implementation.
17 /*! \file */
18 
19 #define EXPORT_EPID_APIS
20 #include <epid/member/api.h>
21 
22 #include <stdint.h>
23 #include "epid/common/types.h"
24 #include "epid/member/software_member.h"
25 #include "epid/member/tiny/math/fp.h"
26 #include "epid/member/tiny/math/mathtypes.h"
27 #include "epid/member/tiny/math/pairing.h"
28 #include "epid/member/tiny/math/serialize.h"
29 #include "epid/member/tiny/src/context.h"
30 #include "epid/member/tiny/src/serialize.h"
31 #include "epid/member/tiny/stdlib/tiny_stdlib.h"
32 
EpidMemberGetSize(MemberParams const * params,size_t * context_size)33 EpidStatus EPID_API EpidMemberGetSize(MemberParams const* params,
34                                       size_t* context_size) {
35   const size_t kMinContextSize =
36       sizeof(MemberCtx) - sizeof(((MemberCtx*)0)->heap);
37   if (!params || !context_size) {
38     return kEpidBadArgErr;
39   }
40   *context_size = kMinContextSize + SIGRL_HEAP_SIZE +
41                   BasenamesGetSize(MAX_ALLOWED_BASENAMES);
42   return kEpidNoErr;
43 }
44 
EpidMemberInit(MemberParams const * params,MemberCtx * ctx)45 EpidStatus EPID_API EpidMemberInit(MemberParams const* params, MemberCtx* ctx) {
46   EpidStatus sts = kEpidNoErr;
47   size_t context_size = 0;
48   if (!params || !ctx) {
49     return kEpidBadArgErr;
50   }
51 
52   sts = EpidMemberGetSize(params, &context_size);
53   if (sts != kEpidNoErr) {
54     return sts;
55   }
56 
57   memset(ctx, 0, context_size);
58 
59   ctx->is_provisioned = 0;
60   ctx->f_is_set = 0;
61 
62   // set the default hash algorithm to sha512
63   ctx->hash_alg = kSha512;
64   ctx->f_is_set = 0;
65   // set allowed basenames pointer to the heap
66   ctx->allowed_basenames = (AllowedBasenames*)&ctx->heap[SIGRL_HEAP_SIZE];
67   InitBasenames(ctx->allowed_basenames, MAX_ALLOWED_BASENAMES);
68   if (params->f) {
69     FpDeserialize(&ctx->f, params->f);
70     if (!FpInField(&ctx->f)) {
71       memset(&ctx->f, 0, sizeof(ctx->f));
72       return kEpidBadArgErr;
73     }
74     ctx->f_is_set = 1;
75   }
76   ctx->rnd_func = params->rnd_func;
77   ctx->rnd_param = params->rnd_param;
78   PairingInit(&ctx->pairing_state);
79   return kEpidNoErr;
80 }
81 
EpidMemberDeinit(MemberCtx * ctx)82 void EPID_API EpidMemberDeinit(MemberCtx* ctx) {
83   (void)ctx;
84   return;
85 }
86 
EpidMemberCreate(MemberParams const * params,MemberCtx ** ctx)87 EpidStatus EPID_API EpidMemberCreate(MemberParams const* params,
88                                      MemberCtx** ctx) {
89   (void)params;
90   (void)ctx;
91   return kEpidNotImpl;
92 }
93 
EpidMemberDelete(MemberCtx ** ctx)94 void EPID_API EpidMemberDelete(MemberCtx** ctx) { (void)ctx; }
95