2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "cmac_signer.h"
21 typedef struct private_cmac_signer_t private_cmac_signer_t
;
24 * Private data structure with signing context.
26 struct private_cmac_signer_t
{
34 * Assigned cmac function.
39 * Block size (truncation of CMAC MAC)
44 METHOD(signer_t
, get_signature
, void,
45 private_cmac_signer_t
*this, chunk_t data
, u_int8_t
*buffer
)
49 this->cmac
->get_mac(this->cmac
, data
, NULL
);
53 u_int8_t mac
[this->cmac
->get_block_size(this->cmac
)];
55 this->cmac
->get_mac(this->cmac
, data
, mac
);
56 memcpy(buffer
, mac
, this->block_size
);
60 METHOD(signer_t
, allocate_signature
, void,
61 private_cmac_signer_t
*this, chunk_t data
, chunk_t
*chunk
)
65 this->cmac
->get_mac(this->cmac
, data
, NULL
);
69 u_int8_t mac
[this->cmac
->get_block_size(this->cmac
)];
71 this->cmac
->get_mac(this->cmac
, data
, mac
);
73 chunk
->ptr
= malloc(this->block_size
);
74 chunk
->len
= this->block_size
;
76 memcpy(chunk
->ptr
, mac
, this->block_size
);
80 METHOD(signer_t
, verify_signature
, bool,
81 private_cmac_signer_t
*this, chunk_t data
, chunk_t signature
)
83 u_int8_t mac
[this->cmac
->get_block_size(this->cmac
)];
85 if (signature
.len
!= this->block_size
)
90 this->cmac
->get_mac(this->cmac
, data
, mac
);
91 return memeq(signature
.ptr
, mac
, this->block_size
);
94 METHOD(signer_t
, get_key_size
, size_t,
95 private_cmac_signer_t
*this)
97 return this->cmac
->get_block_size(this->cmac
);
100 METHOD(signer_t
, get_block_size
, size_t,
101 private_cmac_signer_t
*this)
103 return this->block_size
;
106 METHOD(signer_t
, set_key
, void,
107 private_cmac_signer_t
*this, chunk_t key
)
109 this->cmac
->set_key(this->cmac
, key
);
112 METHOD(signer_t
, destroy
, void,
113 private_cmac_signer_t
*this)
115 this->cmac
->destroy(this->cmac
);
120 * Described in header
122 cmac_signer_t
*cmac_signer_create(integrity_algorithm_t algo
)
124 private_cmac_signer_t
*this;
130 case AUTH_AES_CMAC_96
:
131 cmac
= cmac_create(ENCR_AES_CBC
, 16);
145 .get_signature
= _get_signature
,
146 .allocate_signature
= _allocate_signature
,
147 .verify_signature
= _verify_signature
,
148 .get_key_size
= _get_key_size
,
149 .get_block_size
= _get_block_size
,
155 .block_size
= min(truncation
, cmac
->get_block_size(cmac
)),
158 return &this->public;