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
17 #include <tkm/client.h>
18 #include <tkm/constants.h>
20 #include "tkm_diffie_hellman.h"
22 #include <utils/debug.h>
24 typedef struct private_tkm_diffie_hellman_t private_tkm_diffie_hellman_t
;
27 * Private data of a tkm_diffie_hellman_t object.
29 struct private_tkm_diffie_hellman_t
{
31 * Public tkm_diffie_hellman_t interface.
33 tkm_diffie_hellman_t
public;
36 * Diffie Hellman group number.
41 * Diffie Hellman public value.
43 dh_pubvalue_type pubvalue
;
46 METHOD(diffie_hellman_t
, get_my_public_value
, void,
47 private_tkm_diffie_hellman_t
*this, chunk_t
*value
)
49 *value
= chunk_alloc(this->pubvalue
.size
);
50 memcpy(value
->ptr
, &this->pubvalue
.data
, value
->len
);
53 METHOD(diffie_hellman_t
, get_shared_secret
, status_t
,
54 private_tkm_diffie_hellman_t
*this, chunk_t
*secret
)
56 dh_key_type shared_secret
;
57 if (ike_dh_get_shared_secret(1, &shared_secret
) != TKM_OK
)
62 *secret
= chunk_alloc(shared_secret
.size
);
63 memcpy(secret
->ptr
, &shared_secret
.data
, secret
->len
);
68 METHOD(diffie_hellman_t
, set_other_public_value
, void,
69 private_tkm_diffie_hellman_t
*this, chunk_t value
)
71 // TODO: unvoid this function
73 dh_pubvalue_type othervalue
;
74 othervalue
.size
= value
.len
;
75 memcpy(&othervalue
.data
, value
.ptr
, value
.len
);
77 ike_dh_generate_key(1, othervalue
);
80 METHOD(diffie_hellman_t
, get_dh_group
, diffie_hellman_group_t
,
81 private_tkm_diffie_hellman_t
*this)
86 METHOD(diffie_hellman_t
, destroy
, void,
87 private_tkm_diffie_hellman_t
*this)
89 // TODO: unvoid this function
92 if (ike_dh_reset(1) != TKM_OK
)
94 DBG1(DBG_LIB
, "resetting DH context 1 failed");
99 * Described in header.
101 tkm_diffie_hellman_t
*tkm_diffie_hellman_create(diffie_hellman_group_t group
)
103 private_tkm_diffie_hellman_t
*this;
108 .get_shared_secret
= _get_shared_secret
,
109 .set_other_public_value
= _set_other_public_value
,
110 .get_my_public_value
= _get_my_public_value
,
111 .get_dh_group
= _get_dh_group
,
117 if (ike_dh_create(1, group
, &this->pubvalue
) != TKM_OK
)
125 return &this->public;