4 * @brief Implementation of hmac_signer_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
25 #include "hmac_signer.h"
27 #include <crypto/prfs/hmac_prf.h>
30 * This class represents a hmac signer with 12 byte (96 bit) output.
34 typedef struct private_hmac_signer_t private_hmac_signer_t
;
37 * Private data structure with signing context.
39 struct private_hmac_signer_t
{
41 * Public interface of hmac_signer_t.
46 * Assigned hmac function.
52 * Implementation of signer_t.get_signature.
54 static void get_signature (private_hmac_signer_t
*this, chunk_t data
, u_int8_t
*buffer
)
56 u_int8_t full_mac
[this->hmac_prf
->get_block_size(this->hmac_prf
)];
58 this->hmac_prf
->get_bytes(this->hmac_prf
,data
,full_mac
);
60 /* copy mac aka signature :-) */
61 memcpy(buffer
,full_mac
,BLOCK_SIZE
);
65 * Implementation of signer_t.allocate_signature.
67 static void allocate_signature (private_hmac_signer_t
*this, chunk_t data
, chunk_t
*chunk
)
70 u_int8_t full_mac
[this->hmac_prf
->get_block_size(this->hmac_prf
)];
72 this->hmac_prf
->get_bytes(this->hmac_prf
,data
,full_mac
);
74 signature
.ptr
= malloc(BLOCK_SIZE
);
75 signature
.len
= BLOCK_SIZE
;
78 memcpy(signature
.ptr
,full_mac
,BLOCK_SIZE
);
84 * Implementation of signer_t.verify_signature.
86 static bool verify_signature (private_hmac_signer_t
*this, chunk_t data
, chunk_t signature
)
88 u_int8_t full_mac
[this->hmac_prf
->get_block_size(this->hmac_prf
)];
90 this->hmac_prf
->get_bytes(this->hmac_prf
,data
,full_mac
);
92 if (signature
.len
!= BLOCK_SIZE
)
97 /* compare mac aka signature :-) */
98 if (memcmp(signature
.ptr
,full_mac
,BLOCK_SIZE
) == 0)
109 * Implementation of signer_t.get_key_size.
111 static size_t get_key_size (private_hmac_signer_t
*this)
113 /* for HMAC signer, IKEv2 uses block size as key size */
114 return this->hmac_prf
->get_block_size(this->hmac_prf
);
118 * Implementation of signer_t.get_block_size.
120 static size_t get_block_size (private_hmac_signer_t
*this)
126 * Implementation of signer_t.set_key.
128 static void set_key (private_hmac_signer_t
*this, chunk_t key
)
130 this->hmac_prf
->set_key(this->hmac_prf
,key
);
134 * Implementation of signer_t.destroy.
136 static status_t
destroy(private_hmac_signer_t
*this)
138 this->hmac_prf
->destroy(this->hmac_prf
);
144 * Described in header
146 hmac_signer_t
*hmac_signer_create(hash_algorithm_t hash_algoritm
)
148 private_hmac_signer_t
*this = malloc_thing(private_hmac_signer_t
);
150 this->hmac_prf
= (prf_t
*) hmac_prf_create(hash_algoritm
);
152 if (this->hmac_prf
== NULL
)
154 /* algorithm not supported */
159 /* interface functions */
160 this->public.signer_interface
.get_signature
= (void (*) (signer_t
*, chunk_t
, u_int8_t
*))get_signature
;
161 this->public.signer_interface
.allocate_signature
= (void (*) (signer_t
*, chunk_t
, chunk_t
*))allocate_signature
;
162 this->public.signer_interface
.verify_signature
= (bool (*) (signer_t
*, chunk_t
, chunk_t
))verify_signature
;
163 this->public.signer_interface
.get_key_size
= (size_t (*) (signer_t
*))get_key_size
;
164 this->public.signer_interface
.get_block_size
= (size_t (*) (signer_t
*))get_block_size
;
165 this->public.signer_interface
.set_key
= (void (*) (signer_t
*,chunk_t
))set_key
;
166 this->public.signer_interface
.destroy
= (void (*) (signer_t
*))destroy
;
168 return &(this->public);