File: pace.c

package info (click to toggle)
openpace 1.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,392 kB
  • sloc: ansic: 17,468; sh: 4,516; makefile: 568; python: 499; java: 182; ruby: 46
file content (194 lines) | stat: -rw-r--r-- 5,415 bytes parent folder | download
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
/*
 * Copyright (c) 2010-2012 Frank Morgner and Dominik Oepen
 *
 * This file is part of OpenPACE.
 *
 * OpenPACE is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * OpenPACE is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * OpenPACE.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file pace.c
 * @brief OpenPACE implementation
 *
 * @author Frank Morgner <frankmorgner@gmail.com>
 * @author Dominik Oepen <oepen@informatik.hu-berlin.de>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "eac_err.h"
#include "eac_kdf.h"
#include "eac_lib.h"
#include "eac_util.h"
#include "misc.h"
#include "pace_mappings.h"
#include <eac/eac.h>
#include <eac/pace.h>
#include <openssl/crypto.h>
#include <openssl/ecdh.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <string.h>

BUF_MEM *
PACE_STEP1_enc_nonce(const EAC_CTX * ctx, const PACE_SEC * pi)
{
    BUF_MEM * enc_nonce = NULL;
    BUF_MEM * key = NULL;

    check((ctx && ctx->pace_ctx && ctx->pace_ctx->ka_ctx &&
                ctx->pace_ctx->ka_ctx->cipher),
            "Invalid arguments");

    key = kdf_pi(pi, NULL, ctx->pace_ctx->ka_ctx, ctx->md_ctx);
    check(key, "Key derivation function failed");

    BUF_MEM_clear_free(ctx->pace_ctx->nonce);
    ctx->pace_ctx->nonce = randb(EVP_CIPHER_block_size(ctx->pace_ctx->ka_ctx->cipher));
    check(ctx->pace_ctx->nonce, "Failed to create nonce");

    enc_nonce = cipher_no_pad(ctx->pace_ctx->ka_ctx, ctx->cipher_ctx, key, ctx->pace_ctx->nonce, 1);

err:
    BUF_MEM_clear_free(key);

    return enc_nonce;
}

int
PACE_STEP2_dec_nonce(const EAC_CTX * ctx, const PACE_SEC * pi,
        const BUF_MEM * enc_nonce)
{
    BUF_MEM *key = NULL;
    int r = 0;

    check((ctx && ctx->pace_ctx && ctx->pace_ctx->ka_ctx && ctx->pace_ctx->ka_ctx->cipher),
        "Invalid arguments");

    key = kdf_pi(pi, NULL, ctx->pace_ctx->ka_ctx, ctx->md_ctx);
    check(key, "Key derivation function failed");

    BUF_MEM_clear_free(ctx->pace_ctx->nonce);
    ctx->pace_ctx->nonce = cipher_no_pad(ctx->pace_ctx->ka_ctx, ctx->cipher_ctx, key, enc_nonce, 0);
    check(ctx->pace_ctx->nonce, "Failed to decrypt nonce");

    r = 1;

err:
    BUF_MEM_clear_free(key);

    return r;
}

BUF_MEM *
PACE_STEP3A_generate_mapping_data(const EAC_CTX * ctx)
{
    check_return((ctx && ctx->pace_ctx && ctx->pace_ctx->map_generate_key), "Invalid arguments");

    return ctx->pace_ctx->map_generate_key(ctx->pace_ctx, ctx->bn_ctx);
}

int
PACE_STEP3A_map_generator(const EAC_CTX * ctx, const BUF_MEM * in)
{
    if(!ctx || !ctx->pace_ctx || !ctx->pace_ctx->map_compute_key) {
        log_err("Invalid arguments");
        return 0;
    }

    return ctx->pace_ctx->map_compute_key(ctx->pace_ctx, ctx->pace_ctx->nonce, in, ctx->bn_ctx);
}

BUF_MEM *
PACE_STEP3B_generate_ephemeral_key(EAC_CTX * ctx)
{
    check_return((ctx && ctx->pace_ctx), "Invalid arguments");

    ctx->pace_ctx->my_eph_pubkey = KA_CTX_generate_key(ctx->pace_ctx->ka_ctx,
            ctx->bn_ctx);

    if (!ctx->pace_ctx->my_eph_pubkey)
        return NULL;

    return BUF_MEM_create_init(ctx->pace_ctx->my_eph_pubkey->data,
            ctx->pace_ctx->my_eph_pubkey->length);
}

int
PACE_STEP3B_compute_shared_secret(const EAC_CTX * ctx, const BUF_MEM * in)
{
    int r = 0;

    check((ctx  && ctx->pace_ctx  && ctx->pace_ctx->ka_ctx
            && ctx->pace_ctx->ka_ctx->compute_key && in), "Invalid arguments");

    /* Check if the new public key is different from the other party's public
     * key.  Note that this check is only required since TR-03110 v2.02, but it
     * makes sense to always check this... */
    if (!ctx->pace_ctx->my_eph_pubkey
               || (in->length == ctx->pace_ctx->my_eph_pubkey->length
                && memcmp(in->data, ctx->pace_ctx->my_eph_pubkey->data,
                    in->length) == 0)) {
        log_err("Bad DH or ECKEY object");
        goto err;
    }


    check(KA_CTX_compute_key(ctx->pace_ctx->ka_ctx, in, ctx->bn_ctx),
            "Failed to compute shared secret");

    r = 1;

err:
    return r;
}

int
PACE_STEP3C_derive_keys(const EAC_CTX *ctx)
{
    if (!ctx || !ctx->pace_ctx) {
        log_err("Invalid arguments");
        return 0;
    }

    return KA_CTX_derive_keys(ctx->pace_ctx->ka_ctx, NULL, ctx->md_ctx);
}

BUF_MEM *
PACE_STEP3D_compute_authentication_token(const EAC_CTX *ctx, const BUF_MEM *pub)
{
    if (!ctx || !ctx->pace_ctx) {
        log_err("Invalid arguments");
        return NULL;
    }

    return get_authentication_token(ctx->pace_ctx->protocol,
            ctx->pace_ctx->ka_ctx, ctx->bn_ctx,
            ctx->tr_version, pub);
}

int
PACE_STEP3D_verify_authentication_token(const EAC_CTX *ctx, const BUF_MEM *token)
{
    if (!ctx || !token|| !ctx->pace_ctx) {
        log_err("Invalid arguments");
        return -1;
    }

    return verify_authentication_token(ctx->pace_ctx->protocol,
            ctx->pace_ctx->ka_ctx, ctx->bn_ctx, ctx->tr_version,
            token);
}