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>
21 #include "tkm_diffie_hellman.h"
23 #include <utils/debug.h>
25 typedef struct private_tkm_diffie_hellman_t private_tkm_diffie_hellman_t
;
28 * Private data of a tkm_diffie_hellman_t object.
30 struct private_tkm_diffie_hellman_t
{
33 * Public tkm_diffie_hellman_t interface.
35 tkm_diffie_hellman_t
public;
38 * Diffie Hellman group number.
43 * Diffie Hellman public value.
45 dh_pubvalue_type pubvalue
;
50 dh_id_type context_id
;
54 METHOD(diffie_hellman_t
, get_my_public_value
, void,
55 private_tkm_diffie_hellman_t
*this, chunk_t
*value
)
57 *value
= chunk_alloc(this->pubvalue
.size
);
58 memcpy(value
->ptr
, &this->pubvalue
.data
, value
->len
);
61 METHOD(diffie_hellman_t
, get_shared_secret
, status_t
,
62 private_tkm_diffie_hellman_t
*this, chunk_t
*secret
)
64 dh_key_type shared_secret
;
65 if (ike_dh_get_shared_secret(this->context_id
, &shared_secret
) != TKM_OK
)
70 *secret
= chunk_alloc(shared_secret
.size
);
71 memcpy(secret
->ptr
, &shared_secret
.data
, secret
->len
);
76 METHOD(diffie_hellman_t
, set_other_public_value
, void,
77 private_tkm_diffie_hellman_t
*this, chunk_t value
)
79 // TODO: unvoid this function
81 dh_pubvalue_type othervalue
;
82 othervalue
.size
= value
.len
;
83 memcpy(&othervalue
.data
, value
.ptr
, value
.len
);
85 ike_dh_generate_key(this->context_id
, othervalue
);
88 METHOD(diffie_hellman_t
, get_dh_group
, diffie_hellman_group_t
,
89 private_tkm_diffie_hellman_t
*this)
94 METHOD(diffie_hellman_t
, destroy
, void,
95 private_tkm_diffie_hellman_t
*this)
97 if (ike_dh_reset(this->context_id
) != TKM_OK
)
99 DBG1(DBG_LIB
, "failed to reset DH context %d", this->context_id
);
102 tkm
->idmgr
->release_id(tkm
->idmgr
, TKM_CTX_DH
, this->context_id
);
107 * Described in header.
109 tkm_diffie_hellman_t
*tkm_diffie_hellman_create(diffie_hellman_group_t group
)
111 private_tkm_diffie_hellman_t
*this;
116 .get_shared_secret
= _get_shared_secret
,
117 .set_other_public_value
= _set_other_public_value
,
118 .get_my_public_value
= _get_my_public_value
,
119 .get_dh_group
= _get_dh_group
,
124 .context_id
= tkm
->idmgr
->acquire_id(tkm
->idmgr
, TKM_CTX_DH
),
127 if (!this->context_id
)
133 if (ike_dh_create(this->context_id
, group
, &this->pubvalue
) != TKM_OK
)
139 return &this->public;