Implemented AES-CMAC based PRF and signer.
[strongswan.git] / src / libstrongswan / plugins / cmac / cmac_prf.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "cmac_prf.h"
17
18 #include "cmac.h"
19
20 typedef struct private_cmac_prf_t private_cmac_prf_t;
21
22 /**
23 * Private data of a cmac_prf_t object.
24 */
25 struct private_cmac_prf_t {
26
27 /**
28 * Public cmac_prf_t interface.
29 */
30 cmac_prf_t public;
31
32 /**
33 * cmac to use for generation.
34 */
35 cmac_t *cmac;
36 };
37
38 METHOD(prf_t, get_bytes, void,
39 private_cmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
40 {
41 this->cmac->get_mac(this->cmac, seed, buffer);
42 }
43
44 METHOD(prf_t, allocate_bytes, void,
45 private_cmac_prf_t *this, chunk_t seed, chunk_t *chunk)
46 {
47 if (chunk)
48 {
49 *chunk = chunk_alloc(this->cmac->get_block_size(this->cmac));
50 get_bytes(this, seed, chunk->ptr);
51 }
52 else
53 {
54 get_bytes(this, seed, NULL);
55 }
56 }
57
58 METHOD(prf_t, get_block_size, size_t,
59 private_cmac_prf_t *this)
60 {
61 return this->cmac->get_block_size(this->cmac);
62 }
63
64 METHOD(prf_t, get_key_size, size_t,
65 private_cmac_prf_t *this)
66 {
67 /* in cmac, block and key size are always equal */
68 return this->cmac->get_block_size(this->cmac);
69 }
70
71 METHOD(prf_t, set_key, void,
72 private_cmac_prf_t *this, chunk_t key)
73 {
74 this->cmac->set_key(this->cmac, key);
75 }
76
77 METHOD(prf_t, destroy, void,
78 private_cmac_prf_t *this)
79 {
80 this->cmac->destroy(this->cmac);
81 free(this);
82 }
83
84 /*
85 * Described in header.
86 */
87 cmac_prf_t *cmac_prf_create(pseudo_random_function_t algo)
88 {
89 private_cmac_prf_t *this;
90 cmac_t *cmac;
91
92 switch (algo)
93 {
94 case PRF_AES128_CMAC:
95 cmac = cmac_create(ENCR_AES_CBC, 16);
96 break;
97 default:
98 return NULL;
99 }
100 if (!cmac)
101 {
102 return NULL;
103 }
104
105 INIT(this,
106 .public = {
107 .prf = {
108 .get_bytes = _get_bytes,
109 .allocate_bytes = _allocate_bytes,
110 .get_block_size = _get_block_size,
111 .get_key_size = _get_key_size,
112 .set_key = _set_key,
113 .destroy = _destroy,
114 },
115 },
116 .cmac = cmac,
117 );
118
119 return &this->public;
120 }
121