1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2006
8  *
9  */
10 
11 #ifndef DAA_STRUCT_H_
12 #define DAA_STRUCT_H_
13 
14 #include <string.h>
15 #include <stdlib.h>
16 #include <malloc.h>
17 
18 #include "tss/tcs.h"
19 #include "bi.h"
20 #include "arpa/inet.h"
21 
22 // for message digest
23 #include <openssl/evp.h>
24 
25 #define init_tss_version(b) \
26 do {\
27 	(b)->versionInfo.bMajor = DAA_PARAM_TSS_VERSION[0];\
28 	(b)->versionInfo.bMinor = DAA_PARAM_TSS_VERSION[1];\
29 	(b)->versionInfo.bRevMajor = DAA_PARAM_TSS_VERSION[2];\
30 	(b)->versionInfo.bRevMinor = DAA_PARAM_TSS_VERSION[3];\
31 } while(0);
32 
33 BYTE *convert_alloc( TCS_CONTEXT_HANDLE tcsContext,
34 			UINT32 length,
35 			BYTE *source);
36 
37 BYTE *copy_alloc(  TCS_CONTEXT_HANDLE tcsContext,
38 			UINT32 length,
39 			BYTE *source);
40 
41 void store_bi( UINT32 *length,
42 		BYTE **buffer,
43 		const bi_ptr i,
44 		void * (*daa_alloc)(size_t size, TSS_HOBJECT object),
45 		TSS_HOBJECT object);
46 
47 /* length is in network format: big indian */
48 void dump_field( int length, BYTE *buffer);
49 
50 /********************************************************************************************
51 	TSS_DAA_ATTRIB_COMMIT
52  ********************************************************************************************/
53 
54 typedef struct tdTSS_DAA_ATTRIB_COMMIT_internal {
55 	bi_ptr beta;
56 	bi_ptr sMu;
57 } TSS_DAA_ATTRIB_COMMIT_internal;
58 
59 TSS_DAA_ATTRIB_COMMIT_internal *create_TSS_DAA_ATTRIB_COMMIT( bi_ptr beta, bi_ptr sMu);
60 
61 /********************************************************************************************
62  *   TSS_DAA_SELECTED_ATTRIB
63  * this struct is used internally and externally, only a call to internal_2_DAA_SELECTED_ATTRIB
64  * DAA_SELECTED_ATTRIB_2_internal will change the struct to be internal or external
65  ********************************************************************************************/
66 
67 void i_2_e_TSS_DAA_SELECTED_ATTRIB( TSS_DAA_SELECTED_ATTRIB *selected_attrib);
68 
69 void e_2_i_TSS_DAA_SELECTED_ATTRIB( TSS_DAA_SELECTED_ATTRIB *selected_attrib);
70 
71 /* work ONLY with internal format */
72 BYTE *to_bytes_TSS_DAA_SELECTED_ATTRIB_internal( int *length, TSS_DAA_SELECTED_ATTRIB *selected_attrib);
73 
74 /*
75 create a TSS_DAA_SELECTED_ATTRIB of length <length> with given selected attributes.
76 example of selections of the second and third attributes upon 5:
77 create_TSS_DAA_SELECTED_ATTRIB( &selected_attrib, 5, 0, 1, 1, 0, 0);
78 */
79 void create_TSS_DAA_SELECTED_ATTRIB( TSS_DAA_SELECTED_ATTRIB *attrib, int length, ...);
80 
81 /********************************************************************************************
82  *   DAA PRIVATE KEY
83  ********************************************************************************************/
84 
85 /**
86  * DAA private key. Contains p', q' and the product of it, where n = p*q, p =
87  * 2*p'+1 and q = 2*q'+1. n is part of the public key.
88  * (from com.ibm.zurich.tcg.daa.issuer.DAAPrivateKey.java)
89  */
90 typedef struct {
91 	bi_ptr p_prime;
92 	bi_ptr q_prime;
93 	bi_ptr productPQprime;
94 } DAA_PRIVATE_KEY_internal;
95 
96 /**
97  * allocate: 	ret->p_prime
98  * 					ret->q_prime
99  * 				  	ret->productPQprime
100  */
101 DAA_PRIVATE_KEY_internal *create_TSS_DAA_PRIVATE_KEY(
102 	bi_ptr pPrime,
103 	bi_ptr qPrime
104 );
105 #if 0
106 int save_DAA_PRIVATE_KEY(
107 	FILE *file,
108 	const DAA_PRIVATE_KEY_internal *private_key
109 );
110 
111 DAA_PRIVATE_KEY_internal *load_DAA_PRIVATE_KEY(
112 	FILE *file
113 );
114 TSS_DAA_PRIVATE_KEY* i_2_e_TSS_DAA_PRIVATE_KEY(
115 	DAA_PRIVATE_KEY_internal *private_key_internal,
116 	void * (*daa_alloc)(size_t size, TSS_HOBJECT object),
117 	TSS_HOBJECT object
118 );
119 
120 DAA_PRIVATE_KEY_internal *e_2_i_TSS_DAA_PRIVATE_KEY(
121 	TSS_DAA_PRIVATE_KEY *private_key
122 );
123 
124 #endif
125 /********************************************************************************************
126  *   TSS_DAA_PK
127  ********************************************************************************************/
128 
129 typedef struct tdTSS_DAA_PK_internal {
130 	bi_ptr modulus;
131 	bi_ptr capitalS;
132 	bi_ptr capitalZ;
133 	bi_ptr capitalR0;
134 	bi_ptr capitalR1;
135 	bi_ptr gamma;
136 	bi_ptr capitalGamma;
137 	bi_ptr rho;
138 	bi_array_ptr capitalRReceiver;
139 	bi_array_ptr capitalRIssuer;
140     	bi_array_ptr capitalY;
141 	int issuerBaseNameLength;
142 	BYTE *issuerBaseName;
143  	// capitalSprime calculated at each init of this structure as :
144  	//    (capitalS ^ ( 1 << DAA_PARAM_SIZE_SPLIT_EXPONENT)) % modulus
145 	bi_ptr capitalSprime;
146 } TSS_DAA_PK_internal;
147 
148 TSS_DAA_PK_internal *create_DAA_PK(
149 	const bi_ptr modulus,
150 	const bi_ptr capitalS,
151 	const bi_ptr capitalZ,
152 	const bi_ptr capitalR0,
153 	const bi_ptr capitalR1,
154 	const bi_ptr gamma,
155 	const bi_ptr capitalGamma,
156 	const bi_ptr rho,
157 	const bi_array_ptr capitalRReceiver,
158 	const bi_array_ptr capitalRIssuer,
159 	int  issuerBaseNameLength,
160 	BYTE * const issuerBaseName);
161 
162 /*
163  * create anf feel a TSS_DAA_PK structures
164  */
165 TSS_DAA_PK_internal *e_2_i_TSS_DAA_PK(
166 	TSS_DAA_PK *pk
167 );
168 
169 TSS_DAA_PK	*i_2_e_TSS_DAA_PK(
170 	TSS_DAA_PK_internal *pk_internal,
171 	void * (*daa_alloc)(size_t size, TSS_HOBJECT object),
172 	TSS_HOBJECT param_alloc
173 );
174 #if 0
175 
176 /* moved to daa_debug.h */
177 int save_DAA_PK_internal(
178 	FILE *file,
179 	const TSS_DAA_PK_internal *pk_internal
180 );
181 
182 TSS_DAA_PK_internal *load_DAA_PK_internal(
183 	FILE *file
184 );
185 
186 #endif
187 
188 void dump_DAA_PK_internal(
189 	char *name,
190 	TSS_DAA_PK_internal *pk_internal
191 );
192 
193 TPM_DAA_ISSUER *convert2issuer_settings(
194 	TSS_DAA_PK_internal *pk_internal
195 );
196 
197 void free_TSS_DAA_PK_internal(
198 	TSS_DAA_PK_internal *pk_internal
199 );
200 
201 void free_TSS_DAA_PK( TSS_DAA_PK *pk);
202 
203 BYTE *issuer_2_byte_array(
204 	TPM_DAA_ISSUER *tpm_daa_issuer,
205 	int *length
206 );
207 
208 /********************************************************************************************
209  *   TSS_DAA_PK_PROOF
210  ********************************************************************************************/
211 
212 typedef struct tdTSS_DAA_PK_PROOF_internal {
213 	BYTE *challenge;
214 	int length_challenge;
215 	bi_array_ptr *response;
216 	int length_response;
217 } TSS_DAA_PK_PROOF_internal;
218 
219 TSS_DAA_PK_PROOF_internal *create_DAA_PK_PROOF(
220 	BYTE* const challenge,
221 	const int length_challenge,
222 	bi_array_ptr *response,
223 	int length_reponse);
224 
225 /*
226  * create anf feel a TSS_DAA_PK structures
227  */
228 TSS_DAA_PK *TSS_convert_DAA_PK_PROOF(
229 	TSS_DAA_PK_PROOF_internal *proof
230 );
231 #if 0
232 int save_DAA_PK_PROOF_internal(
233 	FILE *file,
234 	TSS_DAA_PK_PROOF_internal *pk_internal
235 );
236 
237 TSS_DAA_PK_PROOF_internal *load_DAA_PK_PROOF_internal(
238 	FILE *file
239 );
240 #endif
241 TSS_DAA_PK_PROOF_internal *e_2_i_TSS_DAA_PK_PROOF(
242 	TSS_DAA_PK_PROOF *pk_proof
243 );
244 
245 TSS_DAA_PK_PROOF *i_2_e_TSS_DAA_PK_PROOF(
246 	TSS_DAA_PK_PROOF_internal*pk_internal_proof,
247 	void * (*daa_alloc)(size_t size, TSS_HOBJECT object),
248 	TSS_HOBJECT param_alloc
249 );
250 
251 /*
252  * Encode the DAA_PK like java.security.Key#getEncoded
253  */
254 BYTE *encoded_DAA_PK_internal(
255 	int *result_length,
256 	const TSS_DAA_PK_internal *pk
257 );
258 
259 /********************************************************************************************
260  *   KEY PAIR WITH PROOF
261  ********************************************************************************************/
262 
263 typedef struct tdKEY_PAIR_WITH_PROOF_internal {
264 	TSS_DAA_PK_internal *pk;
265 	DAA_PRIVATE_KEY_internal *private_key;
266 	TSS_DAA_PK_PROOF_internal *proof;
267 } KEY_PAIR_WITH_PROOF_internal;
268 
269 #if 0
270 
271 /* moved to daa_debug.h */
272 
273 int save_KEY_PAIR_WITH_PROOF(
274 	FILE *file,
275 	KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof
276 );
277 
278 KEY_PAIR_WITH_PROOF_internal *load_KEY_PAIR_WITH_PROOF(
279 	FILE *file
280 );
281 
282 #endif
283 
284 TSS_DAA_KEY_PAIR *get_TSS_DAA_KEY_PAIR(
285 	KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof,
286 	void * (*daa_alloc)(size_t size, TSS_HOBJECT object),
287 	TSS_HOBJECT param_alloc
288 );
289 
290 
291 /********************************************************************************************
292  *   TSS_DAA_PSEUDONYM_PLAIN
293  ********************************************************************************************/
294 
295 typedef struct {
296 	bi_ptr nV;
297 } TSS_DAA_PSEUDONYM_PLAIN_internal;
298 
299 TSS_DAA_PSEUDONYM_PLAIN_internal *create_TSS_DAA_PSEUDONYM_PLAIN(
300 	bi_ptr nV
301 );
302 
303 /********************************************************************************************
304  *   TSS_DAA_PSEUDONYM_ENCRYPTED
305  ********************************************************************************************/
306 
307 typedef struct {
308 	bi_ptr sTau;
309 	struct tdCS_ENCRYPTION_RESULT *cs_enc_result;
310 } TSS_DAA_PSEUDONYM_ENCRYPTED_internal;
311 
312 
313 /********************************************************************************************
314  *   TSS_DAA_SIGNATURE
315  ********************************************************************************************/
316 
317 typedef struct {
318 	bi_ptr zeta;
319 	bi_ptr capitalT;
320 	int challenge_length;
321 	BYTE *challenge;
322 	int nonce_tpm_length;
323 	BYTE *nonce_tpm;
324 	bi_ptr sV;
325 	bi_ptr sF0;
326 	bi_ptr sF1;
327 	bi_ptr sE;
328 	int sA_length;
329 	bi_array_ptr sA;
330 } TSS_DAA_SIGNATURE_internal;
331 
332 TSS_DAA_SIGNATURE_internal *e_2_i_TSS_DAA_SIGNATURE(
333 	TSS_DAA_SIGNATURE*signature
334 );
335 
336 void free_TSS_DAA_SIGNATURE_internal(
337 	TSS_DAA_SIGNATURE_internal *signature
338 );
339 
340 /********************************************************************************************
341  *   TSS_DAA_JOIN_ISSUER_SESSION
342  ********************************************************************************************/
343 
344 typedef struct td_TSS_DAA_JOIN_ISSUER_SESSION_internal {
345 	TPM_DAA_ISSUER *issuerAuthKey;
346 	TSS_DAA_PK_PROOF_internal *issuerKeyPair;
347 	TSS_DAA_IDENTITY_PROOF *identityProof;
348 	bi_ptr capitalUprime;
349 	int daaCounter;
350 	int nonceIssuerLength;
351 	BYTE *nonceIssuer;
352 	int nonceEncryptedLength;
353 	BYTE *nonceEncrypted;
354 } TSS_DAA_JOIN_ISSUER_SESSION_internal;
355 
356 
357 /********************************************************************************************
358 	TSS_DAA_CRED_ISSUER
359 ********************************************************************************************/
360 #if 0
361 TSS_DAA_CRED_ISSUER *load_TSS_DAA_CRED_ISSUER( FILE *file);
362 
363 int save_TSS_DAA_CRED_ISSUER( FILE *file, TSS_DAA_CRED_ISSUER *credential);
364 
365 #endif
366 /********************************************************************************************
367 	TSS_DAA_CREDENTIAL
368 ********************************************************************************************/
369 #if 0
370 TSS_DAA_CREDENTIAL *load_TSS_DAA_CREDENTIAL( FILE *file);
371 
372 int save_TSS_DAA_CREDENTIAL(
373 	FILE *file,
374 	TSS_DAA_CREDENTIAL *credential
375 );
376 
377 #endif
378 
379 /********************************************************************************************
380 	TPM_DAA_ISSUER
381 ********************************************************************************************/
382 
383 void free_TPM_DAA_ISSUER( TPM_DAA_ISSUER *tpm_daa_issuer);
384 
385 #endif /*DAA_STRUCT_H_*/
386