File: fapi-data-crypt.int.c

package info (click to toggle)
tpm2-tss 4.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,544 kB
  • sloc: ansic: 160,641; makefile: 924; sh: 654; xml: 431; python: 239
file content (351 lines) | stat: -rw-r--r-- 11,258 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* SPDX-License-Identifier: BSD-2-Clause */
/*******************************************************************************
 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
 * All rights reserved.
 *******************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/pem.h>

#include "tss2_fapi.h"

#include "test-fapi.h"

#define LOGMODULE test
#include "util/log.h"
#include "util/aux_util.h"

#define SIZE 128

const char *priv_pem =
    "-----BEGIN PRIVATE KEY-----\n"
    "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCgYvoisJIDOeYg\n"
    "jMF6ywiZbu085TLvy5ZMhq5vfYqdgefvwpemutnfKSnpYOs5B4yO/gAD7XiYluDv\n"
    "tqlVhfISeQV04xrWGzNImenpwm/HsgueAu8VTHNtWSL96G+BLedGTrs2NqX6cxN7\n"
    "yGl7dQpB5X8iP4XSvpjP3Vb7gs+adwCJR6xFkt60jYFmwrAdhEzOeakhimi5rU21\n"
    "LxCkRdEyaxS57X15L9dEA+aYJ+dvkFfZOfTqIKmTrA75F8yj161xflwtIC4hgRBg\n"
    "K9Xb/RdN8TDrTu+20E3RjngutU4qejW9Fd3mzHJGV8HRYvjYXhUblN9wmjm7Veru\n"
    "T2b0rnvzAgMBAAECggEBAIwHvoJ5DRJ6A50Zp3dROxHTEphfOEi6xF/OGxBGWLbK\n"
    "C7l+eS9d5gj8BJa5QsXI/IR/6X2EYQ1AdeV04oVD7CUKuqPiALU8jFrv3pV0aGm+\n"
    "3nu37gv3crPe5jkvLeNoM4tkA/oCXom63SDuyoG6nxkHiSdatLlaJUse4em3vRAL\n"
    "QivziZIMyswcleMe0xAoMi7LO+nUFFxBS8/xGya0vsU0dsMQEl1SRITv1VCXmPQD\n"
    "T4dEI4+1cufv6Ax0EDbFKmnjyiGTjOeQKrGIqETUSQolbg5PgL1XZehaaxM822OY\n"
    "Qpnp5T0XhUEmVrOb2Wrboj+dC/2tgAN/fWXjAAxnm2ECgYEA02UTZuZ+QnD6tqo3\n"
    "/y3n5kaM9uA2mdOIqgECI9psGF1IBIC/iP2diKyuvmQL8hzymComb5YzZl3TOAga\n"
    "WHQYbIeU3JhnYTG75/Dv5Zh32H4NjkIJHT2/8LUM25Ove9u6QAniVgIQpBZ47LjX\n"
    "9jHjTYCW5n79qNSfu0egYJUvypECgYEAwjqWzzEINqnX/xIVCoB4XpuDuSdkM0JW\n"
    "MZDIH9xHjZPp07/5XYEoITylk6Zwbh+djvWDNP4gzPtuK26VsqrNxoWMsFZeXn6U\n"
    "xSOYL2UNCZiOgchdZCOr+6r8LRUuo8xHjbawVoJVK1+tZ2WsR3ilt3Gw34O8Z5ep\n"
    "f4v7GOXw+EMCgYAUHjFrgJIRhqkFi0uK+HZyXtJ5iDsKBqyh6Tin6tiQtQfujcYs\n"
    "pl5ArJZwvhq47vJTcud3hSbdHh7E3ViMhHfylDChkct833vPhgl+ozT8oHpvyG8P\n"
    "nlnO8ZwIpZR0yCOAhrBImSe2RgE6HhlHb9X/ATbbNsizMZEGBLoJlwkWUQKBgQCy\n"
    "4U7fh2LvJUF+82JZh7RUPZn1Pmg0JVZI0/TcEv37UEy77kR1b2xMIBTGhTVq1sc/\n"
    "ULIEbkA7SR1P9sr7//8AZSMLjJ/hG2dcoMmabNCzE8O7l5MblRbh87nIs4d+57bG\n"
    "t4h0RBi4l6eWYLdoI59L8fNaB3PPXIiIpZ0eczeZDQKBgQC2vuFYpUZqDb9CaJsn\n"
    "Luee6P6n5v3ZBTAT4E+GG1kWS28BiebcCuLKNAY4ZtLo08ozaTWcMxooOTeka2ux\n"
    "fQDE4M/LTNpam8QOJ2hqECF5a0uBYNcbmaGtfA9KwIgwCZZYuwb5IDq/DRPuR690\n"
    "i8Kp6jR2wY0suObmZHKvbCB1Dw==\n"
    "-----END PRIVATE KEY-----\n";

const char *pub_pem =
    "-----BEGIN PUBLIC KEY-----\n"
    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoGL6IrCSAznmIIzBessI\n"
    "mW7tPOUy78uWTIaub32KnYHn78KXprrZ3ykp6WDrOQeMjv4AA+14mJbg77apVYXy\n"
    "EnkFdOMa1hszSJnp6cJvx7ILngLvFUxzbVki/ehvgS3nRk67Njal+nMTe8hpe3UK\n"
    "QeV/Ij+F0r6Yz91W+4LPmncAiUesRZLetI2BZsKwHYRMznmpIYpoua1NtS8QpEXR\n"
    "MmsUue19eS/XRAPmmCfnb5BX2Tn06iCpk6wO+RfMo9etcX5cLSAuIYEQYCvV2/0X\n"
    "TfEw607vttBN0Y54LrVOKno1vRXd5sxyRlfB0WL42F4VG5TfcJo5u1Xq7k9m9K57\n"
    "8wIDAQAB\n"
    "-----END PUBLIC KEY-----\n";

#define RSA_SIG_SCHEME RSA_PKCS1_PSS_PADDING

char *userDataTest = "test";

#define chknull(X) if (!X) { LOG_ERROR(str(X) "should not be null"); \
                             r = TSS2_FAPI_RC_GENERAL_FAILURE; \
                             goto error_cleanup; }

static  uint8_t *global_signature = NULL;

static TSS2_RC
signatureCallback(
    char    const  *objectPath,
    char    const  *description,
    char    const  *publicKey,
    char    const  *publicKeyHint,
    uint32_t        hashAlg,
    uint8_t const  *dataToSign,
    size_t          dataToSignSize,
    uint8_t const **signature,
    size_t         *signatureSize,
    void           *userData)
{
    UNUSED(description);
    UNUSED(publicKey);
    UNUSED(publicKeyHint);
    uint8_t *aux_signature = NULL;
    size_t profile_len = strlen(FAPI_PROFILE);

    if (strcmp(objectPath + profile_len, "/HS/SRK/myRsaCryptKey") ||
        strncmp(objectPath, "P_RSA", 5))
        return_error(TSS2_FAPI_RC_BAD_VALUE, "Unexpected path") {
    }

    if (userData != userDataTest) {
        LOG_ERROR("userData is not correct, %p != %p", userData, userDataTest);
        return TSS2_FAPI_RC_GENERAL_FAILURE;
    }

    if (hashAlg != TPM2_ALG_SHA256) {
        LOG_ERROR("hashAlg is not correct, %u != %u", hashAlg, TPM2_ALG_SHA256);
        return TSS2_FAPI_RC_GENERAL_FAILURE;
    }

    TSS2_RC r = TSS2_RC_SUCCESS;
    EVP_PKEY *priv_key = NULL;
    BIO *bufio = NULL;
    EVP_MD_CTX *mdctx =NULL;
    EVP_PKEY_CTX *pctx = NULL;

    const EVP_MD *ossl_hash = EVP_sha256();
    chknull(ossl_hash);

    LOGBLOB_DEBUG(dataToSign, dataToSignSize, "Data to be signed");

    bufio = BIO_new_mem_buf((void *)priv_pem, strlen(priv_pem));
    priv_key = PEM_read_bio_PrivateKey(bufio, NULL, NULL, NULL);
    chknull(priv_key);

    mdctx = EVP_MD_CTX_create();
    chknull(mdctx);

    if (1 != EVP_DigestSignInit(mdctx, &pctx, ossl_hash, NULL, priv_key)) {
        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign init.",
                   error_cleanup);
    }
    if (EVP_PKEY_base_id(priv_key) == EVP_PKEY_RSA) {
        int signing_scheme = RSA_SIG_SCHEME;
        if (1 != EVP_PKEY_CTX_set_rsa_padding(pctx, signing_scheme)) {
            goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL set RSA padding.",
                       error_cleanup);
        }
    }
    if (1 != EVP_DigestSignUpdate(mdctx, dataToSign, dataToSignSize)) {
        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign update.",
                   error_cleanup);
    }
    if (1 != EVP_DigestSignFinal(mdctx, NULL, signatureSize)) {
        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign final.",
                   error_cleanup);
    }
    aux_signature = malloc(*signatureSize);
    global_signature = aux_signature;

    chknull(aux_signature);
    if (1 != EVP_DigestSignFinal(mdctx, aux_signature, signatureSize)) {
        goto_error(r, TSS2_FAPI_RC_GENERAL_FAILURE, "OSSL sign final.",
                   error_cleanup);
    }
    *signature = aux_signature;

    if (priv_key)
        EVP_PKEY_free(priv_key);
    if (mdctx)
        EVP_MD_CTX_destroy(mdctx);
    if (bufio)
        BIO_free(bufio);
    return r;

 error_cleanup:
    if (priv_key)
        EVP_PKEY_free(priv_key);
    if (mdctx)
        EVP_MD_CTX_destroy(mdctx);
    if (bufio)
        BIO_free(bufio);
    return r;
}

/** Test the FAPI functions for encryption and decryption.
 *
 * Tested FAPI commands:
 *  - Fapi_Provision()
 *  - Fapi_Import()
 *  - Fapi_CreateKey()
 *  - Fapi_SetSignCB()
 *  - Fapi_Encrypt()
 *  - Fapi_Decrypt()
 *  - Fapi_Free()
 *
 * Tested Policies:
 *  - PolicySigned
 *
 * @param[in,out] context The FAPI_CONTEXT.
 * @retval EXIT_FAILURE
 * @retval EXIT_SUCCESS
 */
int
test_fapi_data_crypt(FAPI_CONTEXT *context)
{

    TSS2_RC r;
    long policy_size;
    const char *policy_name = "/policy/pol_signed";
    const char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_signed.json";
    FILE *stream = NULL;
    char *json_policy = NULL;

    uint8_t *cipherText = NULL;
    size_t cipherTextSize;
    char *export_data = NULL;

    r = Fapi_Provision(context, NULL, NULL, NULL);
    goto_if_error(r, "Error Fapi_Provision", error);

    r = pcr_reset(context, 16);
    goto_if_error(r, "Error pcr_reset", error);

    stream = fopen(policy_file, "r");
    if (!stream) {
        LOG_ERROR("File %s does not exist", policy_file);
        goto error;
    }
    fseek(stream, 0L, SEEK_END);
    policy_size = ftell(stream);
    fclose(stream);
    json_policy = malloc(policy_size + 1);
    goto_if_null(json_policy,
            "Could not allocate memory for the JSON policy",
            TSS2_FAPI_RC_MEMORY, error);
    stream = fopen(policy_file, "r");
    ssize_t ret = read(fileno(stream), json_policy, policy_size);
    if (ret != policy_size) {
        LOG_ERROR("IO error %s.", policy_file);
        goto error;
    }
    json_policy[policy_size] = '\0';

    r = Fapi_Import(context, policy_name, json_policy);
    goto_if_error(r, "Error Fapi_Import", error);

#ifdef PERSISTENT
    r = Fapi_CreateKey(context, "HS/SRK/myRsaCryptKey", "decrypt,0x81000004",
                       policy_name, NULL);
#else
    r = Fapi_CreateKey(context, "HS/SRK/myRsaCryptKey", "decrypt",
                       policy_name, NULL);
#endif
    goto_if_error(r, "Error Fapi_CreateKey", error);

    uint8_t  plainText[SIZE];
    int i;

    for (i = 0; i < SIZE; i++)
        plainText[i] = i % 256;

    r = Fapi_SetSignCB(context, signatureCallback, userDataTest);
    goto_if_error(r, "Error SetPolicySignatureCallback", error);

    r = Fapi_Encrypt(context, "HS/SRK/myRsaCryptKey", &plainText[0],
                     SIZE, &cipherText, &cipherTextSize);

    if (r == TSS2_FAPI_RC_NOT_IMPLEMENTED) {
        goto skip;
    }

    goto_if_error(r, "Error Fapi_Encrypt", error);

    uint8_t *plainText2 = NULL;
    size_t plainText2_size = 0;

    r = Fapi_Decrypt(context, "HS/SRK/myRsaCryptKey", cipherText, cipherTextSize,
                     &plainText2, &plainText2_size);
    goto_if_error(r, "Error Fapi_Encrypt", error);

    if (plainText2_size != SIZE ||
            memcmp(plainText, plainText2, plainText2_size) != 0) {
        LOG_ERROR("Error: decrypted text not  equal to origin");
        goto error;
    }

    Fapi_Free(cipherText);
    Fapi_Free(plainText2);
    SAFE_FREE(global_signature);

    /*
     * Run test with openssl encryption with the public RSA key.
     */
    r = Fapi_ExportKey(context, "HS/SRK/myRsaCryptKey", NULL, &export_data);
    goto_if_error(r, "Export.", error);

    r = Fapi_Import(context, "/ext/myRsaCryptKey", export_data);
    goto_if_error(r, "Error Fapi_Import", error);

    r = Fapi_Encrypt(context, "/ext/myRsaCryptKey", &plainText[0],
                     SIZE, &cipherText, &cipherTextSize);

    goto_if_error(r, "Error Fapi_Encrypt", error);

    plainText2 = NULL;
    plainText2_size = 0;

    r = Fapi_Decrypt(context, "HS/SRK/myRsaCryptKey", cipherText, cipherTextSize,
                     &plainText2, &plainText2_size);

    if (r == TSS2_FAPI_RC_NOT_IMPLEMENTED) {
        goto skip;
    }
    goto_if_error(r, "Error Fapi_Encrypt", error);

    if (plainText2_size != SIZE ||
        memcmp(plainText, plainText2, plainText2_size) != 0) {
        LOG_ERROR("Error: decrypted text not  equal to origin");
        goto error;
    }

    SAFE_FREE(export_data);
    Fapi_Free(cipherText);
    Fapi_Free(plainText2);
    Fapi_Free(json_policy);
    Fapi_Delete(context, "/");
    SAFE_FREE(global_signature);

    return EXIT_SUCCESS;

error:
    SAFE_FREE(export_data);
    Fapi_Delete(context, "/");
    Fapi_Free(cipherText);
    Fapi_Free(json_policy);
    Fapi_Delete(context, "/");
    SAFE_FREE(global_signature);

    return EXIT_FAILURE;

 skip:
    SAFE_FREE(export_data);
    Fapi_Delete(context, "/");
    Fapi_Free(json_policy);
    Fapi_Delete(context, "/");
    SAFE_FREE(global_signature);

    return EXIT_SKIP;
}

int
test_invoke_fapi(FAPI_CONTEXT *fapi_context)
{
    return test_fapi_data_crypt(fapi_context);
}