]> git.ipfire.org Git - thirdparty/strongswan.git/blob - programs/charon/lib/crypto/prfs/hmac_prf.c
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / programs / charon / lib / crypto / prfs / hmac_prf.c
1 /**
2 * @file hmac_prf.c
3 *
4 * @brief Implementation for hmac_prf_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "hmac_prf.h"
24
25 #include <crypto/hmac.h>
26
27
28 typedef struct private_hmac_prf_t private_hmac_prf_t;
29
30 /**
31 * Private data of a hma_prf_t object.
32 */
33 struct private_hmac_prf_t {
34 /**
35 * Public hmac_prf_t interface.
36 */
37 hmac_prf_t public;
38
39 /**
40 * Hmac to use for generation.
41 */
42 hmac_t *hmac;
43 };
44
45 /**
46 * Implementation of prf_t.get_bytes.
47 */
48 static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
49 {
50 this->hmac->get_mac(this->hmac, seed, buffer);
51 }
52
53 /**
54 * Implementation of prf_t.allocate_bytes.
55 */
56 static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk)
57 {
58 this->hmac->allocate_mac(this->hmac, seed, chunk);
59 }
60
61 /**
62 * Implementation of prf_t.get_block_size.
63 */
64 static size_t get_block_size(private_hmac_prf_t *this)
65 {
66 return this->hmac->get_block_size(this->hmac);
67 }
68
69 /**
70 * Implementation of prf_t.get_block_size.
71 */
72 static size_t get_key_size(private_hmac_prf_t *this)
73 {
74 /* for HMAC prfs, IKEv2 uses block size as key size */
75 return this->hmac->get_block_size(this->hmac);
76 }
77
78 /**
79 * Implementation of prf_t.set_key.
80 */
81 static void set_key(private_hmac_prf_t *this, chunk_t key)
82 {
83 this->hmac->set_key(this->hmac, key);
84 }
85
86 /**
87 * Implementation of prf_t.destroy.
88 */
89 static void destroy(private_hmac_prf_t *this)
90 {
91 free(this);
92 this->hmac->destroy(this->hmac);
93 }
94
95 /*
96 * Described in header.
97 */
98 hmac_prf_t *hmac_prf_create(hash_algorithm_t hash_algorithm)
99 {
100 private_hmac_prf_t *this = malloc_thing(private_hmac_prf_t);
101
102 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
103 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
104 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
105 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
106 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
107 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
108
109 this->hmac = hmac_create(hash_algorithm);
110 if (this->hmac == NULL)
111 {
112 free(this);
113 return NULL;
114 }
115
116 return &(this->public);
117 }