- moved algorithm definitions from payloads to corresponding transforms
[strongswan.git] / Source / charon / transforms / prfs / prf_hmac.c
1 /**
2 * @file prf_hmac.c
3 *
4 * @brief Implementation for prf_hmac_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 "prf_hmac.h"
24
25 #include <utils/allocator.h>
26 #include <transforms/hmac.h>
27
28 typedef struct private_prf_hmac_t private_prf_hmac_t;
29
30 struct private_prf_hmac_t {
31 /**
32 * public interface for this prf
33 */
34 prf_hmac_t public;
35
36 /**
37 * hmac to use for generation
38 */
39 hmac_t *hmac;
40 };
41
42 /**
43 * implementation of prf_t.get_bytes
44 */
45 static status_t get_bytes(private_prf_hmac_t *this, chunk_t seed, u_int8_t *buffer)
46 {
47 return this->hmac->get_mac(this->hmac, seed, buffer);
48 }
49
50 /**
51 * implementation of prf_t.allocate_bytes
52 */
53 static status_t allocate_bytes(private_prf_hmac_t *this, chunk_t seed, chunk_t *chunk)
54 {
55 return this->hmac->allocate_mac(this->hmac, seed, chunk);
56 }
57
58 /**
59 * implementation of prf_t.get_block_size
60 */
61 static size_t get_block_size(private_prf_hmac_t *this)
62 {
63 return this->hmac->get_block_size(this->hmac);
64 }
65
66 /**
67 * implementation of prf_t.set_key
68 */
69 static status_t set_key(private_prf_hmac_t *this, chunk_t key)
70 {
71 this->hmac->set_key(this->hmac, key);
72 return SUCCESS;
73 }
74
75 /**
76 * implementation of prf_t.destroy
77 */
78 static status_t destroy(private_prf_hmac_t *this)
79 {
80 allocator_free(this);
81 this->hmac->destroy(this->hmac);
82 return SUCCESS;
83 }
84
85 /*
86 * Described in header
87 */
88 prf_hmac_t *prf_hmac_create(hash_algorithm_t hash_algorithm)
89 {
90 private_prf_hmac_t *this = allocator_alloc_thing(private_prf_hmac_t);
91
92 if (this == NULL)
93 {
94 return NULL;
95 }
96
97 this->public.prf_interface.get_bytes = (status_t (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
98 this->public.prf_interface.allocate_bytes = (status_t (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
99 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
100 this->public.prf_interface.set_key = (status_t (*) (prf_t *,chunk_t))set_key;
101 this->public.prf_interface.destroy = (status_t (*) (prf_t *))destroy;
102
103 this->hmac = hmac_create(hash_algorithm);
104 if (this->hmac == NULL)
105 {
106 allocator_free(this);
107 return NULL;
108 }
109
110 return &(this->public);
111 }