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 /// Basic signature computation.
17 /*! \file */
18
19 #include "epid/member/tiny/src/signbasic.h"
20
21 #include "epid/common/types.h"
22 #include "epid/member/tiny/math/efq.h"
23 #include "epid/member/tiny/math/fp.h"
24 #include "epid/member/tiny/math/hashwrap.h"
25 #include "epid/member/tiny/math/serialize.h"
26 #include "epid/member/tiny/src/context.h"
27 #include "epid/member/tiny/src/native_types.h"
28 #include "epid/member/tiny/src/presig_compute.h"
29 #include "epid/member/tiny/stdlib/tiny_stdlib.h"
30
31 static const FpElemStr epid20_p_str = {
32 {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xF0, 0xCD, 0x46, 0xE5, 0xF2,
33 0x5E, 0xEE, 0x71, 0xA4, 0x9E, 0x0C, 0xDC, 0x65, 0xFB, 0x12, 0x99,
34 0x92, 0x1A, 0xF6, 0x2D, 0x53, 0x6C, 0xD1, 0x0B, 0x50, 0x0D}}};
35
36 static const G2ElemStr epid20_g2_str = {
37 {{{{0xE2, 0x01, 0x71, 0xC5, 0x4A, 0xA3, 0xDA, 0x05, 0x21, 0x67, 0x04,
38 0x13, 0x74, 0x3C, 0xCF, 0x22, 0xD2, 0x5D, 0x52, 0x68, 0x3D, 0x32,
39 0x47, 0x0E, 0xF6, 0x02, 0x13, 0x43, 0xBF, 0x28, 0x23, 0x94}}},
40 {{{0x59, 0x2D, 0x1E, 0xF6, 0x53, 0xA8, 0x5A, 0x80, 0x46, 0xCC, 0xDC,
41 0x25, 0x4F, 0xBB, 0x56, 0x56, 0x43, 0x43, 0x3B, 0xF6, 0x28, 0x96,
42 0x53, 0xE2, 0x7D, 0xF7, 0xB2, 0x12, 0xBA, 0xA1, 0x89, 0xBE}}}},
43 {{{{0xAE, 0x60, 0xA4, 0xE7, 0x51, 0xFF, 0xD3, 0x50, 0xC6, 0x21, 0xE7,
44 0x03, 0x31, 0x28, 0x26, 0xBD, 0x55, 0xE8, 0xB5, 0x9A, 0x4D, 0x91,
45 0x68, 0x38, 0x41, 0x4D, 0xB8, 0x22, 0xDD, 0x23, 0x35, 0xAE}}},
46 {{{0x1A, 0xB4, 0x42, 0xF9, 0x89, 0xAF, 0xE5, 0xAD, 0xF8, 0x02, 0x74,
47 0xF8, 0x76, 0x45, 0xE2, 0x53, 0x2C, 0xDC, 0x61, 0x81, 0x90, 0x93,
48 0xD6, 0x13, 0x2C, 0x90, 0xFE, 0x89, 0x51, 0xB9, 0x24, 0x21}}}}};
49
50 static const G1ElemStr epid20_g1_str = {
51 {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}}},
54 {{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}}}};
57
EpidSignBasic(MemberCtx const * ctx,void const * msg,size_t msg_len,void const * basename,size_t basename_len,NativeBasicSignature * sig)58 EpidStatus EpidSignBasic(MemberCtx const* ctx, void const* msg, size_t msg_len,
59 void const* basename, size_t basename_len,
60 NativeBasicSignature* sig) {
61 EpidStatus sts = kEpidErr;
62 PreComputedSignatureData presig;
63 tiny_sha sha_state;
64 sha_digest digest;
65 G1ElemStr g1_str;
66 Fq12ElemStr fq12_str;
67 FpElemStr fp_str;
68 FpElem x;
69
70 FpDeserialize(&x, &ctx->credential.x);
71 do {
72 sts = EpidMemberComputePreSig(ctx, &presig);
73 if (kEpidNoErr != sts) {
74 break;
75 }
76 // B <- random
77 if (basename) {
78 if (!IsBasenameAllowed(ctx->allowed_basenames, basename, basename_len)) {
79 sts = kEpidBadArgErr;
80 break;
81 }
82 /* Basename, K is linked to fixed B */
83 if (!EFqHash(&sig->B, (const unsigned char*)basename, basename_len,
84 ctx->hash_alg)) {
85 break;
86 }
87 } else {
88 /* No basename, B is random */
89 if (!EFqRand(&sig->B, ctx->rnd_func, ctx->rnd_param)) {
90 break;
91 }
92 }
93 // K <- B^f
94 // guaranteed not to fail, based on f nonzero, B not identity
95 EFqAffineExp(&sig->K, &sig->B, &ctx->f);
96 EFqCp(&sig->T, &presig.T);
97
98 // R1 = B^rf
99 // guaranteed not to fail, if rf != p or 0, but bad inputs could cause it to
100 // fail
101 if (!EFqAffineExp(&presig.R1, &sig->B, &presig.rf)) {
102 break;
103 }
104
105 // 5. The member computes
106 // t3 = Fp.hash(p || g1 || g2 || h1 || h2 || w || B || K || T || R1 || R2).
107 tinysha_init(ctx->hash_alg, &sha_state);
108
109 tinysha_update(&sha_state, (void const*)&epid20_p_str,
110 sizeof(epid20_p_str));
111 tinysha_update(&sha_state, (void const*)&epid20_g1_str,
112 sizeof(epid20_g1_str));
113 tinysha_update(&sha_state, (void const*)&epid20_g2_str,
114 sizeof(epid20_g2_str));
115 tinysha_update(&sha_state, (void const*)&ctx->pub_key.h1,
116 sizeof(ctx->pub_key.h1));
117 tinysha_update(&sha_state, (void const*)&ctx->pub_key.h2,
118 sizeof(ctx->pub_key.h2));
119 tinysha_update(&sha_state, (void const*)&ctx->pub_key.w,
120 sizeof(ctx->pub_key.w));
121 EFqSerialize(&g1_str, &sig->B);
122 tinysha_update(&sha_state, (void const*)&g1_str, sizeof(g1_str));
123 EFqSerialize(&g1_str, &sig->K);
124 tinysha_update(&sha_state, (void const*)&g1_str, sizeof(g1_str));
125 EFqSerialize(&g1_str, &sig->T);
126 tinysha_update(&sha_state, (void const*)&g1_str, sizeof(g1_str));
127 EFqSerialize(&g1_str, &presig.R1);
128 tinysha_update(&sha_state, (void const*)&g1_str, sizeof(g1_str));
129 Fq12Serialize(&fq12_str, &presig.R2);
130 tinysha_update(&sha_state, (void const*)&fq12_str, sizeof(fq12_str));
131 tinysha_final(digest.digest, &sha_state);
132 FpFromHash(&sig->c, digest.digest, tinysha_digest_size(&sha_state));
133
134 // 6. The member computes c = Fp.hash(t3 || m).
135 tinysha_init(ctx->hash_alg, &sha_state);
136 FpSerialize(&fp_str, &sig->c);
137 tinysha_update(&sha_state, (void const*)&fp_str, sizeof(fp_str));
138 tinysha_update(&sha_state, msg, msg_len);
139 tinysha_final(digest.digest, &sha_state);
140
141 FpFromHash(&sig->c, digest.digest, tinysha_digest_size(&sha_state));
142 // The variables sx, sf, sa, sb are computed from x, f, a, b with random
143 // elements
144 // This randomness allows verification but means that the s variables reveal
145 // no secret information
146 FpMul(&sig->sx, &sig->c, &x);
147 FpMul(&sig->sf, &sig->c, &ctx->f);
148 FpMul(&sig->sa, &sig->c, &presig.a);
149 FpMul(&sig->sb, &sig->c, &presig.b);
150 FpAdd(&sig->sx, &sig->sx, &presig.rx);
151 FpAdd(&sig->sf, &sig->sf, &presig.rf);
152 FpAdd(&sig->sa, &sig->sa, &presig.ra);
153 FpAdd(&sig->sb, &sig->sb, &presig.rb);
154 sts = kEpidNoErr;
155 } while (0);
156 // clearing stack-allocated variables before function return
157 (void)memset(&presig, 0, sizeof(presig));
158 return sts;
159 }
160