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  * \file
19  * \brief TPM2_LoadExternal command implementation.
20  */
21 
22 #include "epid/member/tpm2/load_external.h"
23 
24 #include "epid/common/math/finitefield.h"
25 #include "epid/common/src/epid2params.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 
Tpm2LoadExternal(Tpm2Ctx * ctx,FpElemStr const * f_str)34 EpidStatus Tpm2LoadExternal(Tpm2Ctx* ctx, FpElemStr const* f_str) {
35   EpidStatus sts = kEpidErr;
36   if (!ctx || !ctx->epid2_params || !f_str) {
37     return kEpidBadArgErr;
38   }
39 
40   do {
41     FiniteField* Fp = ctx->epid2_params->Fp;
42 
43     if (ctx->f) {
44       DeleteFfElement(&ctx->f);
45     }
46 
47     sts = NewFfElement(Fp, &ctx->f);
48     BREAK_ON_EPID_ERROR(sts);
49     sts = ReadFfElement(Fp, f_str, sizeof(*f_str), ctx->f);
50     BREAK_ON_EPID_ERROR(sts);
51   } while (0);
52 
53   return sts;
54 }
55