Home | History | Annotate | Download | only in src
      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 ProvisionKey implementation.
     17 /*! \file */
     18 
     19 #define EXPORT_EPID_APIS
     20 #include "epid/member/api.h"
     21 #include "epid/member/software_member.h"
     22 #include "epid/member/tiny/math/efq.h"
     23 #include "epid/member/tiny/math/efq2.h"
     24 #include "epid/member/tiny/math/fp.h"
     25 #include "epid/member/tiny/math/mathtypes.h"
     26 #include "epid/member/tiny/math/pairing.h"
     27 #include "epid/member/tiny/math/serialize.h"
     28 #include "epid/member/tiny/src/context.h"
     29 #include "epid/member/tiny/src/native_types.h"
     30 #include "epid/member/tiny/src/serialize.h"
     31 #include "epid/member/tiny/src/validate.h"
     32 #include "epid/member/tiny/stdlib/tiny_stdlib.h"
     33 
     34 static EccPointFq2 const epid20_g2 = {
     35     {{{{0xBF282394, 0xF6021343, 0x3D32470E, 0xD25D5268, 0x743CCF22, 0x21670413,
     36         0x4AA3DA05, 0xE20171C5}}},
     37      {{{0xBAA189BE, 0x7DF7B212, 0x289653E2, 0x43433BF6, 0x4FBB5656, 0x46CCDC25,
     38         0x53A85A80, 0x592D1EF6}}}},
     39     {{{{0xDD2335AE, 0x414DB822, 0x4D916838, 0x55E8B59A, 0x312826BD, 0xC621E703,
     40         0x51FFD350, 0xAE60A4E7}}},
     41      {{{0x51B92421, 0x2C90FE89, 0x9093D613, 0x2CDC6181, 0x7645E253, 0xF80274F8,
     42         0x89AFE5AD, 0x1AB442F9}}}}};
     43 
     44 EpidStatus EPID_API EpidProvisionKey(MemberCtx* ctx, GroupPubKey const* pub_key,
     45                                      PrivKey const* priv_key,
     46                                      MemberPrecomp const* precomp_str) {
     47   NativeGroupPubKey native_pub_key;
     48   NativePrivKey native_priv_key;
     49   if (!pub_key || !priv_key || !ctx) {
     50     return kEpidBadArgErr;
     51   }
     52   if (0 != memcmp(&pub_key->gid, &priv_key->gid, sizeof(GroupId))) {
     53     return kEpidBadArgErr;
     54   }
     55   GroupPubKeyDeserialize(&native_pub_key, pub_key);
     56   if (!GroupPubKeyIsInRange(&native_pub_key)) {
     57     return kEpidBadArgErr;
     58   }
     59 
     60   PrivKeyDeserialize(&native_priv_key, priv_key);
     61   if (!PrivKeyIsInRange(&native_priv_key) ||
     62       !PrivKeyIsInGroup(&native_priv_key, &native_pub_key,
     63                         &ctx->pairing_state)) {
     64     memset(&native_priv_key.f, 0, sizeof(native_priv_key.f));
     65     return kEpidBadArgErr;
     66   }
     67   ctx->f = native_priv_key.f;
     68   memset(&native_priv_key.f, 0, sizeof(native_priv_key.f));
     69   ctx->f_is_set = 1;
     70 
     71   ctx->credential.gid = priv_key->gid;
     72   ctx->credential.A = priv_key->A;
     73   ctx->credential.x = priv_key->x;
     74   ctx->pub_key = *pub_key;
     75   ctx->is_provisioned = 1;
     76 
     77   if (precomp_str) {
     78     PreCompDeserialize(&ctx->precomp, precomp_str);
     79   } else {
     80     PairingCompute(&ctx->precomp.ea2, &native_priv_key.cred.A, &epid20_g2,
     81                    &ctx->pairing_state);
     82     PairingCompute(&ctx->precomp.e12, &native_pub_key.h1, &epid20_g2,
     83                    &ctx->pairing_state);
     84     PairingCompute(&ctx->precomp.e22, &native_pub_key.h2, &epid20_g2,
     85                    &ctx->pairing_state);
     86     PairingCompute(&ctx->precomp.e2w, &native_pub_key.h2, &native_pub_key.w,
     87                    &ctx->pairing_state);
     88   }
     89   return kEpidNoErr;
     90 }
     91