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 
17 /*!
18  * \brief TPM2_CreatePrimary command implementation.
19  * \file
20  */
21 #include "epid/member/tpm2/createprimary.h"
22 #include "epid/common/math/finitefield.h"
23 #include "epid/common/src/epid2params.h"
24 #include "epid/member/software_member.h"
25 #include "epid/member/tpm2/load_external.h"
26 #include "epid/member/tpm2/src/state.h"
27 
28 /// Handle Intel(R) EPID Error with Break
29 #define BREAK_ON_EPID_ERROR(ret) \
30   if (kEpidNoErr != (ret)) {     \
31     break;                       \
32   }
33 
Tpm2CreatePrimary(Tpm2Ctx * ctx,G1ElemStr * p_str)34 EpidStatus Tpm2CreatePrimary(Tpm2Ctx* ctx, G1ElemStr* p_str) {
35   EpidStatus sts = kEpidErr;
36   FfElement* ff_elem;
37   FpElemStr ff_elem_str;
38   if (!ctx || !ctx->epid2_params) {
39     return kEpidBadArgErr;
40   }
41   (void)p_str;
42   do {
43     const BigNumStr kOne = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
45     FiniteField* Fp = ctx->epid2_params->Fp;
46     sts = NewFfElement(Fp, &ff_elem);
47     BREAK_ON_EPID_ERROR(sts);
48     sts = FfGetRandom(Fp, &kOne, ctx->rnd_func, ctx->rnd_param, ff_elem);
49     BREAK_ON_EPID_ERROR(sts);
50     sts = WriteFfElement(Fp, ff_elem, &ff_elem_str, sizeof(ff_elem_str));
51     BREAK_ON_EPID_ERROR(sts);
52   } while (0);
53   DeleteFfElement(&ff_elem);
54   if (kEpidNoErr == sts) {
55     sts = Tpm2LoadExternal(ctx, &ff_elem_str);
56   }
57   return sts;
58 }
59