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 Member credentials storage helper API implementation. 20 */ 21 22 #include "epid/member/src/storage.h" 23 24 #include "epid/common/src/memory.h" 25 #include "epid/common/types.h" 26 #include "epid/member/tpm2/nv.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 34 EpidStatus EpidNvWriteMembershipCredential( 35 Tpm2Ctx* ctx, GroupPubKey const* pub_key, 36 MembershipCredential const* credential, uint32_t nv_index) { 37 EpidStatus sts = kEpidErr; 38 uint8_t tmp; 39 if (!ctx || !pub_key || !credential) return kEpidBadArgErr; 40 41 do { 42 if (kEpidNoErr != Tpm2NvRead(ctx, nv_index, 1, 0, &tmp)) { 43 sts = Tpm2NvDefineSpace(ctx, nv_index, 44 sizeof(*pub_key) + sizeof(*credential)); 45 BREAK_ON_EPID_ERROR(sts); 46 } 47 sts = Tpm2NvWrite(ctx, nv_index, sizeof(*pub_key), 0, pub_key); 48 BREAK_ON_EPID_ERROR(sts); 49 sts = Tpm2NvWrite(ctx, nv_index, sizeof(*credential), sizeof(*pub_key), 50 credential); 51 BREAK_ON_EPID_ERROR(sts); 52 } while (0); 53 if (kEpidNoErr != sts) Tpm2NvUndefineSpace(ctx, nv_index); 54 EpidZeroMemory(&tmp, sizeof(tmp)); 55 return sts; 56 } 57 58 EpidStatus EpidNvReadMembershipCredential(Tpm2Ctx* ctx, uint32_t nv_index, 59 GroupPubKey* pub_key, 60 MembershipCredential* credential) { 61 EpidStatus sts = kEpidErr; 62 if (!ctx || !pub_key || !credential) return kEpidBadArgErr; 63 do { 64 sts = Tpm2NvRead(ctx, nv_index, sizeof(*pub_key), 0, pub_key); 65 BREAK_ON_EPID_ERROR(sts); 66 sts = Tpm2NvRead(ctx, nv_index, sizeof(*credential), sizeof(*pub_key), 67 credential); 68 BREAK_ON_EPID_ERROR(sts); 69 } while (0); 70 return sts; 71 } 72