Add initial TKM Diffie-Hellman implementation
[strongswan.git] / src / charon-tkm / src / tkm / tkm_diffie_hellman.c
1 /*
2 * Copyrigth (C) 2012 Reto Buerki
3 * Copyright (C) 2012 Adrian-Ken Rueegsegger
4 * Hochschule fuer Technik Rapperswil
5 *
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>.
10 *
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
14 * for more details.
15 */
16
17 #include <tkm/client.h>
18 #include <tkm/constants.h>
19
20 #include "tkm_diffie_hellman.h"
21
22 #include <utils/debug.h>
23
24 typedef struct private_tkm_diffie_hellman_t private_tkm_diffie_hellman_t;
25
26 /**
27 * Private data of a tkm_diffie_hellman_t object.
28 */
29 struct private_tkm_diffie_hellman_t {
30 /**
31 * Public tkm_diffie_hellman_t interface.
32 */
33 tkm_diffie_hellman_t public;
34
35 /**
36 * Diffie Hellman group number.
37 */
38 u_int16_t group;
39
40 /**
41 * Diffie Hellman public value.
42 */
43 dh_pubvalue_type pubvalue;
44 };
45
46 METHOD(diffie_hellman_t, get_my_public_value, void,
47 private_tkm_diffie_hellman_t *this, chunk_t *value)
48 {
49 *value = chunk_alloc(this->pubvalue.size);
50 memcpy(value->ptr, &this->pubvalue.data, value->len);
51 }
52
53 METHOD(diffie_hellman_t, get_shared_secret, status_t,
54 private_tkm_diffie_hellman_t *this, chunk_t *secret)
55 {
56 dh_key_type shared_secret;
57 if (ike_dh_get_shared_secret(1, &shared_secret) != TKM_OK)
58 {
59 return FAILED;
60 }
61
62 *secret = chunk_alloc(shared_secret.size);
63 memcpy(secret->ptr, &shared_secret.data, secret->len);
64 return SUCCESS;
65 }
66
67
68 METHOD(diffie_hellman_t, set_other_public_value, void,
69 private_tkm_diffie_hellman_t *this, chunk_t value)
70 {
71 // TODO: unvoid this function
72
73 dh_pubvalue_type othervalue;
74 othervalue.size = value.len;
75 memcpy(&othervalue.data, value.ptr, value.len);
76
77 ike_dh_generate_key(1, othervalue);
78 }
79
80 METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
81 private_tkm_diffie_hellman_t *this)
82 {
83 return this->group;
84 }
85
86 METHOD(diffie_hellman_t, destroy, void,
87 private_tkm_diffie_hellman_t *this)
88 {
89 // TODO: unvoid this function
90
91 free(this);
92 if (ike_dh_reset(1) != TKM_OK)
93 {
94 DBG1(DBG_LIB, "resetting DH context 1 failed");
95 }
96 }
97
98 /*
99 * Described in header.
100 */
101 tkm_diffie_hellman_t *tkm_diffie_hellman_create(diffie_hellman_group_t group)
102 {
103 private_tkm_diffie_hellman_t *this;
104
105 INIT(this,
106 .public = {
107 .dh = {
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,
112 .destroy = _destroy,
113 },
114 },
115 );
116
117 if (ike_dh_create(1, group, &this->pubvalue) != TKM_OK)
118 {
119 free(this);
120 return NULL;
121 }
122
123 this->group = group;
124
125 return &this->public;
126 }