Add initial TKM Diffie-Hellman implementation
[strongswan.git] / src / charon-tkm / src / tkm / tkm_nonceg.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_nonceg.h"
18
19 #include <tkm/client.h>
20 #include <tkm/constants.h>
21
22 typedef struct private_tkm_nonceg_t private_tkm_nonceg_t;
23
24 /**
25 * Private data of a tkm_nonceg_t object.
26 */
27 struct private_tkm_nonceg_t {
28
29 /**
30 * Public tkm_nonceg_t interface.
31 */
32 tkm_nonceg_t public;
33
34 };
35
36 METHOD(nonce_gen_t, get_nonce, bool,
37 private_tkm_nonceg_t *this, size_t size, u_int8_t *buffer)
38 {
39 nonce_type nonce;
40
41 /* request nonce from TKM, the context is not yet used */
42 const result_type result = ike_nc_create(1, size, &nonce);
43 if (result != TKM_OK)
44 {
45 return FALSE;
46 }
47
48 memcpy(buffer, &nonce.data, size);
49 return TRUE;
50 }
51
52 METHOD(nonce_gen_t, allocate_nonce, bool,
53 private_tkm_nonceg_t *this, size_t size, chunk_t *chunk)
54 {
55 *chunk = chunk_alloc(size);
56 return get_nonce(this, chunk->len, chunk->ptr);
57 }
58
59 METHOD(nonce_gen_t, destroy, void,
60 private_tkm_nonceg_t *this)
61 {
62 free(this);
63 }
64
65 /*
66 * Described in header.
67 */
68 tkm_nonceg_t *tkm_nonceg_create()
69 {
70 private_tkm_nonceg_t *this;
71
72 INIT(this,
73 .public = {
74 .nonce_gen = {
75 .get_nonce = _get_nonce,
76 .allocate_nonce = _allocate_nonce,
77 .destroy = _destroy,
78 },
79 },
80 );
81
82 return &this->public;
83 }