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