implemented XCBC algorithms (signer, prf) for IKE on top of a crypter
[strongswan.git] / src / libstrongswan / plugins / xcbc / xcbc_prf.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "xcbc_prf.h"
19
20 #include "xcbc.h"
21
22 typedef struct private_xcbc_prf_t private_xcbc_prf_t;
23
24 /**
25 * Private data of a xcbc_prf_t object.
26 */
27 struct private_xcbc_prf_t {
28
29 /**
30 * Public xcbc_prf_t interface.
31 */
32 xcbc_prf_t public;
33
34 /**
35 * xcbc to use for generation.
36 */
37 xcbc_t *xcbc;
38 };
39
40 /**
41 * Implementation of prf_t.get_bytes.
42 */
43 static void get_bytes(private_xcbc_prf_t *this, chunk_t seed, u_int8_t *buffer)
44 {
45 this->xcbc->get_mac(this->xcbc, seed, buffer);
46 }
47
48 /**
49 * Implementation of prf_t.allocate_bytes.
50 */
51 static void allocate_bytes(private_xcbc_prf_t *this, chunk_t seed, chunk_t *chunk)
52 {
53 if (chunk)
54 {
55 *chunk = chunk_alloc(this->xcbc->get_block_size(this->xcbc));
56 get_bytes(this, seed, chunk->ptr);
57 }
58 else
59 {
60 get_bytes(this, seed, NULL);
61 }
62 }
63
64 /**
65 * Implementation of prf_t.get_block_size.
66 */
67 static size_t get_block_size(private_xcbc_prf_t *this)
68 {
69 return this->xcbc->get_block_size(this->xcbc);
70 }
71
72 /**
73 * Implementation of prf_t.get_block_size.
74 */
75 static size_t get_key_size(private_xcbc_prf_t *this)
76 {
77 /* in xcbc, block and key size are always equal */
78 return this->xcbc->get_block_size(this->xcbc);
79 }
80
81 /**
82 * Implementation of prf_t.set_key.
83 */
84 static void set_key(private_xcbc_prf_t *this, chunk_t key)
85 {
86 this->xcbc->set_key(this->xcbc, key);
87 }
88
89 /**
90 * Implementation of prf_t.destroy.
91 */
92 static void destroy(private_xcbc_prf_t *this)
93 {
94 this->xcbc->destroy(this->xcbc);
95 free(this);
96 }
97
98 /*
99 * Described in header.
100 */
101 xcbc_prf_t *xcbc_prf_create(pseudo_random_function_t algo)
102 {
103 private_xcbc_prf_t *this;
104 xcbc_t *xcbc;
105
106 switch (algo)
107 {
108 case PRF_AES128_CBC:
109 xcbc = xcbc_create(ENCR_AES_CBC, 16);
110 break;
111 default:
112 return NULL;
113 }
114 if (!xcbc)
115 {
116 return NULL;
117 }
118
119 this = malloc_thing(private_xcbc_prf_t);
120 this->xcbc = xcbc;
121
122 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
123 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
124 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
125 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
126 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
127 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
128
129 return &this->public;
130 }
131