2 * Copyrigth (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <sa/ikev2/keymat_v2.h>
19 #include <tkm/constants.h>
20 #include <tkm/client.h>
23 #include "tkm_utils.h"
24 #include "tkm_diffie_hellman.h"
25 #include "tkm_keymat.h"
27 typedef struct private_tkm_keymat_t private_tkm_keymat_t
;
30 * Private data of a keymat_t object.
32 struct private_tkm_keymat_t
{
35 * Public tkm_keymat_t interface.
40 * IKEv2 keymat proxy (will be removed).
45 * IKE_SA Role, initiator or responder.
61 METHOD(keymat_t
, get_version
, ike_version_t
,
62 private_tkm_keymat_t
*this)
67 METHOD(keymat_t
, create_dh
, diffie_hellman_t
*,
68 private_tkm_keymat_t
*this, diffie_hellman_group_t group
)
70 return lib
->crypto
->create_dh(lib
->crypto
, group
);
73 METHOD(keymat_t
, create_nonce_gen
, nonce_gen_t
*,
74 private_tkm_keymat_t
*this)
76 return lib
->crypto
->create_nonce_gen(lib
->crypto
);
79 METHOD(tkm_keymat_t
, derive_ike_keys
, bool,
80 private_tkm_keymat_t
*this, proposal_t
*proposal
, diffie_hellman_t
*dh
,
81 chunk_t nonce_i
, chunk_t nonce_r
, ike_sa_id_t
*id
,
82 pseudo_random_function_t rekey_function
, chunk_t rekey_skd
)
84 /* Check encryption and integrity algorithms */
85 u_int16_t enc_alg
, int_alg
, key_size
;
86 if (!proposal
->get_algorithm(proposal
, ENCRYPTION_ALGORITHM
, &enc_alg
, &key_size
))
88 DBG1(DBG_IKE
, "no %N selected", transform_type_names
,
89 ENCRYPTION_ALGORITHM
);
92 if (encryption_algorithm_is_aead(enc_alg
))
94 DBG1(DBG_IKE
, "AEAD algorithm %N not supported",
95 encryption_algorithm_names
, enc_alg
);
98 if (!proposal
->get_algorithm(proposal
, INTEGRITY_ALGORITHM
, &int_alg
, NULL
))
100 DBG1(DBG_IKE
, "no %N selected", transform_type_names
,
101 INTEGRITY_ALGORITHM
);
104 DBG2(DBG_IKE
, "using %N for encryption, %N for integrity",
105 encryption_algorithm_names
, enc_alg
,
106 integrity_algorithm_names
, int_alg
);
108 /* Acquire nonce context id */
109 chunk_t
* const nonce
= this->initiator ?
&nonce_i
: &nonce_r
;
110 const uint64_t nc_id
= tkm
->chunk_map
->get_id(tkm
->chunk_map
, nonce
);
113 DBG1(DBG_IKE
, "unable to acquire context id for nonce");
117 /* Get DH context id */
118 tkm_diffie_hellman_t
* const tkm_dh
= (tkm_diffie_hellman_t
*)dh
;
119 const dh_id_type dh_id
= tkm_dh
->get_id(tkm_dh
);
121 nonce_type nonce_rem
;
122 u_int64_t spi_loc
, spi_rem
;
126 chunk_to_sequence(&nonce_r
, &nonce_rem
);
127 spi_loc
= id
->get_initiator_spi(id
);
128 spi_rem
= id
->get_responder_spi(id
);
132 chunk_to_sequence(&nonce_i
, &nonce_rem
);
133 spi_loc
= id
->get_responder_spi(id
);
134 spi_rem
= id
->get_initiator_spi(id
);
137 key_type sk_ai
, sk_ar
, sk_ei
, sk_er
;
138 DBG1(DBG_IKE
, "deriving IKE keys (nc: %llu, dh: %llu, spi_loc: %llx, "
139 "spi_rem: %llx)", nc_id
, dh_id
, spi_loc
, spi_rem
);
140 /* Fake some data for now */
141 if (ike_isa_create(1, 1, 1, dh_id
, nc_id
, nonce_rem
, 1, spi_loc
, spi_rem
,
142 &sk_ai
, &sk_ar
, &sk_ei
, &sk_er
) != TKM_OK
)
144 DBG1(DBG_IKE
, "key derivation failed");
148 /* Initialize AEAD with crypters and signers */
149 signer_t
* const signer_i
= lib
->crypto
->create_signer(lib
->crypto
, int_alg
);
150 signer_t
* const signer_r
= lib
->crypto
->create_signer(lib
->crypto
, int_alg
);
151 if (signer_i
== NULL
|| signer_r
== NULL
)
153 DBG1(DBG_IKE
, "%N %N not supported!",
154 transform_type_names
, INTEGRITY_ALGORITHM
,
155 integrity_algorithm_names
, int_alg
);
158 crypter_t
* const crypter_i
= lib
->crypto
->create_crypter(lib
->crypto
,
159 enc_alg
, key_size
/ 8);
160 crypter_t
* const crypter_r
= lib
->crypto
->create_crypter(lib
->crypto
,
161 enc_alg
, key_size
/ 8);
162 if (crypter_i
== NULL
|| crypter_r
== NULL
)
164 signer_i
->destroy(signer_i
);
165 signer_r
->destroy(signer_r
);
166 DBG1(DBG_IKE
, "%N %N (key size %d) not supported!",
167 transform_type_names
, ENCRYPTION_ALGORITHM
,
168 encryption_algorithm_names
, enc_alg
, key_size
);
173 sequence_to_chunk(sk_ai
.data
, sk_ai
.size
, &key
);
174 DBG4(DBG_IKE
, "Sk_ai %B", &key
);
175 if (!signer_i
->set_key(signer_i
, key
))
181 sequence_to_chunk(sk_ar
.data
, sk_ar
.size
, &key
);
182 DBG4(DBG_IKE
, "Sk_ar %B", &key
);
183 if (!signer_r
->set_key(signer_r
, key
))
189 sequence_to_chunk(sk_ei
.data
, sk_ei
.size
, &key
);
190 DBG4(DBG_IKE
, "Sk_ei %B", &key
);
191 if (!crypter_i
->set_key(crypter_i
, key
))
197 sequence_to_chunk(sk_er
.data
, sk_er
.size
, &key
);
198 DBG4(DBG_IKE
, "Sk_er %B", &key
);
199 if (!crypter_r
->set_key(crypter_r
, key
))
207 this->aead_in
= aead_create(crypter_r
, signer_r
);
208 this->aead_out
= aead_create(crypter_i
, signer_i
);
212 this->aead_in
= aead_create(crypter_i
, signer_i
);
213 this->aead_out
= aead_create(crypter_r
, signer_r
);
216 /* TODO: Add failure handler (see keymat_v2.c) */
218 if (this->proxy
->derive_ike_keys(this->proxy
, proposal
, dh
, nonce_i
,
219 nonce_r
, id
, rekey_function
, rekey_skd
))
221 tkm
->chunk_map
->remove(tkm
->chunk_map
, nonce
);
222 if (ike_nc_reset(nc_id
) != TKM_OK
)
224 DBG1(DBG_IKE
, "failed to reset nonce context %llu", nc_id
);
226 tkm
->idmgr
->release_id(tkm
->idmgr
, TKM_CTX_NONCE
, nc_id
);
233 METHOD(tkm_keymat_t
, derive_child_keys
, bool,
234 private_tkm_keymat_t
*this, proposal_t
*proposal
, diffie_hellman_t
*dh
,
235 chunk_t nonce_i
, chunk_t nonce_r
, chunk_t
*encr_i
, chunk_t
*integ_i
,
236 chunk_t
*encr_r
, chunk_t
*integ_r
)
238 DBG1(DBG_CHD
, "deriving child keys");
239 return this->proxy
->derive_child_keys(this->proxy
, proposal
, dh
, nonce_i
,
240 nonce_r
, encr_i
, integ_i
, encr_r
, integ_r
);
243 METHOD(keymat_t
, get_aead
, aead_t
*,
244 private_tkm_keymat_t
*this, bool in
)
246 return in ?
this->aead_in
: this->aead_out
;
249 METHOD(tkm_keymat_t
, get_auth_octets
, bool,
250 private_tkm_keymat_t
*this, bool verify
, chunk_t ike_sa_init
,
251 chunk_t nonce
, identification_t
*id
, char reserved
[3], chunk_t
*octets
)
253 DBG1(DBG_IKE
, "returning auth octets");
254 return this->proxy
->get_auth_octets(this->proxy
, verify
, ike_sa_init
, nonce
,
255 id
, reserved
, octets
);
258 METHOD(tkm_keymat_t
, get_skd
, pseudo_random_function_t
,
259 private_tkm_keymat_t
*this, chunk_t
*skd
)
261 DBG1(DBG_IKE
, "returning skd");
262 return this->proxy
->get_skd(this->proxy
, skd
);
265 METHOD(tkm_keymat_t
, get_psk_sig
, bool,
266 private_tkm_keymat_t
*this, bool verify
, chunk_t ike_sa_init
, chunk_t nonce
,
267 chunk_t secret
, identification_t
*id
, char reserved
[3], chunk_t
*sig
)
269 DBG1(DBG_IKE
, "returning PSK signature");
270 return this->proxy
->get_psk_sig(this->proxy
, verify
, ike_sa_init
, nonce
,
271 secret
, id
, reserved
, sig
);
274 METHOD(keymat_t
, destroy
, void,
275 private_tkm_keymat_t
*this)
277 DESTROY_IF(this->aead_in
);
278 DESTROY_IF(this->aead_out
);
279 this->proxy
->keymat
.destroy(&this->proxy
->keymat
);
286 tkm_keymat_t
*tkm_keymat_create(bool initiator
)
288 private_tkm_keymat_t
*this;
293 .get_version
= _get_version
,
294 .create_dh
= _create_dh
,
295 .create_nonce_gen
= _create_nonce_gen
,
296 .get_aead
= _get_aead
,
299 .derive_ike_keys
= _derive_ike_keys
,
300 .derive_child_keys
= _derive_child_keys
,
302 .get_auth_octets
= _get_auth_octets
,
303 .get_psk_sig
= _get_psk_sig
,
305 .initiator
= initiator
,
306 .proxy
= keymat_v2_create(initiator
),
309 return &this->public;