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