• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /// TPM-SDK data conversion implementation.
17 /*! \file */
18 
19 #include "epid/member/tpm2/ibm_tss/conversion.h"
20 #include <string.h>
21 #include <tss2/TPM_Types.h>
22 #include "epid/common/math/ecgroup.h"
23 #include "epid/common/src/memory.h"
24 #include "epid/common/types.h"
25 
EpidtoTpm2HashAlg(HashAlg hash_alg)26 TPMI_ALG_HASH EpidtoTpm2HashAlg(HashAlg hash_alg) {
27   switch (hash_alg) {
28     case kSha256:
29       return TPM_ALG_SHA256;
30     case kSha384:
31       return TPM_ALG_SHA384;
32     case kSha512:
33       return TPM_ALG_SHA512;
34     default:
35       return TPM_ALG_NULL;
36   }
37 }
38 
Tpm2toEpidHashAlg(TPMI_ALG_HASH tpm_hash_alg)39 HashAlg Tpm2toEpidHashAlg(TPMI_ALG_HASH tpm_hash_alg) {
40   switch (tpm_hash_alg) {
41     case TPM_ALG_SHA256:
42       return kSha256;
43     case TPM_ALG_SHA384:
44       return kSha384;
45     case TPM_ALG_SHA512:
46       return kSha512;
47     default:
48       return kInvalidHashAlg;
49   }
50 }
51 
ReadTpm2FfElement(OctStr256 const * str,TPM2B_ECC_PARAMETER * tpm_data)52 EpidStatus ReadTpm2FfElement(OctStr256 const* str,
53                              TPM2B_ECC_PARAMETER* tpm_data) {
54   if (!str || !tpm_data) {
55     return kEpidBadArgErr;
56   }
57   if (0 !=
58       memcpy_S(tpm_data->b.buffer, MAX_ECC_KEY_BYTES, str, sizeof(OctStr256))) {
59     return kEpidBadArgErr;
60   }
61   tpm_data->b.size = (UINT16)sizeof(OctStr256);
62   return kEpidNoErr;
63 }
64 
WriteTpm2FfElement(TPM2B_ECC_PARAMETER const * tpm_data,OctStr256 * str)65 EpidStatus WriteTpm2FfElement(TPM2B_ECC_PARAMETER const* tpm_data,
66                               OctStr256* str) {
67   if (!tpm_data || !str || tpm_data->b.size > (UINT16)sizeof(OctStr256)) {
68     return kEpidBadArgErr;
69   }
70   uint8_t* buf = (uint8_t*)str;
71   size_t real_size = sizeof(OctStr256);
72   if (tpm_data->b.size < real_size) {
73     memset(buf, 0x00, real_size - tpm_data->b.size);
74     buf += real_size - tpm_data->b.size;
75     real_size = tpm_data->b.size;
76   }
77   if (0 != memcpy_S(buf, real_size, tpm_data->b.buffer, tpm_data->b.size)) {
78     return kEpidBadArgErr;
79   }
80   return kEpidNoErr;
81 }
82 
ReadTpm2EcPoint(G1ElemStr const * p_str,TPM2B_ECC_POINT * tpm_point)83 EpidStatus ReadTpm2EcPoint(G1ElemStr const* p_str, TPM2B_ECC_POINT* tpm_point) {
84   if (!p_str || !tpm_point) {
85     return kEpidBadArgErr;
86   }
87 
88   //  copy X
89   if (0 != memcpy_S(tpm_point->point.x.t.buffer, MAX_ECC_KEY_BYTES, &p_str->x,
90                     sizeof(G1ElemStr) / 2)) {
91     return kEpidErr;
92   }
93   tpm_point->point.x.t.size = sizeof(G1ElemStr) / 2;
94 
95   //  copy Y
96   if (0 != memcpy_S(tpm_point->point.y.t.buffer, MAX_ECC_KEY_BYTES, &p_str->y,
97                     sizeof(G1ElemStr) / 2)) {
98     return kEpidErr;
99   }
100   tpm_point->point.y.t.size = sizeof(G1ElemStr) / 2;
101 
102   tpm_point->size = sizeof(tpm_point->point);
103   return kEpidNoErr;
104 }
105 
WriteTpm2EcPoint(TPM2B_ECC_POINT const * tpm_point,G1ElemStr * p_str)106 EpidStatus WriteTpm2EcPoint(TPM2B_ECC_POINT const* tpm_point,
107                             G1ElemStr* p_str) {
108   if (!p_str || !tpm_point) {
109     return kEpidBadArgErr;
110   }
111 
112   if (tpm_point->point.x.t.size > sizeof(G1ElemStr) / 2 ||
113       tpm_point->point.y.t.size > sizeof(G1ElemStr) / 2) {
114     return kEpidBadArgErr;
115   }
116 
117   memset(p_str, '\0', sizeof(G1ElemStr));
118 
119   //  copy X
120   if (0 !=
121       memcpy_S(&p_str->x + (sizeof(G1ElemStr) / 2 - tpm_point->point.x.t.size),
122                tpm_point->point.x.t.size, tpm_point->point.x.t.buffer,
123                tpm_point->point.x.t.size)) {
124     return kEpidErr;
125   }
126   //  copy Y
127   if (0 !=
128       memcpy_S(&p_str->y + (sizeof(G1ElemStr) / 2 - tpm_point->point.y.t.size),
129                tpm_point->point.y.t.size, tpm_point->point.y.t.buffer,
130                tpm_point->point.y.t.size)) {
131     return kEpidErr;
132   }
133   return kEpidNoErr;
134 }
135