used rsa coeff field in OpenPGP secret key payload
[strongswan.git] / src / libstrongswan / plugins / hmac / hmac_prf.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "hmac_prf.h"
18
19 #include "hmac.h"
20
21
22 typedef struct private_hmac_prf_t private_hmac_prf_t;
23
24 /**
25 * Private data of a hma_prf_t object.
26 */
27 struct private_hmac_prf_t {
28 /**
29 * Public hmac_prf_t interface.
30 */
31 hmac_prf_t public;
32
33 /**
34 * Hmac to use for generation.
35 */
36 hmac_t *hmac;
37 };
38
39 /**
40 * Implementation of prf_t.get_bytes.
41 */
42 static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
43 {
44 this->hmac->get_mac(this->hmac, seed, buffer);
45 }
46
47 /**
48 * Implementation of prf_t.allocate_bytes.
49 */
50 static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk)
51 {
52 this->hmac->allocate_mac(this->hmac, seed, chunk);
53 }
54
55 /**
56 * Implementation of prf_t.get_block_size.
57 */
58 static size_t get_block_size(private_hmac_prf_t *this)
59 {
60 return this->hmac->get_block_size(this->hmac);
61 }
62
63 /**
64 * Implementation of prf_t.get_block_size.
65 */
66 static size_t get_key_size(private_hmac_prf_t *this)
67 {
68 /* for HMAC prfs, IKEv2 uses block size as key size */
69 return this->hmac->get_block_size(this->hmac);
70 }
71
72 /**
73 * Implementation of prf_t.set_key.
74 */
75 static void set_key(private_hmac_prf_t *this, chunk_t key)
76 {
77 this->hmac->set_key(this->hmac, key);
78 }
79
80 /**
81 * Implementation of prf_t.destroy.
82 */
83 static void destroy(private_hmac_prf_t *this)
84 {
85 this->hmac->destroy(this->hmac);
86 free(this);
87 }
88
89 /*
90 * Described in header.
91 */
92 hmac_prf_t *hmac_prf_create(pseudo_random_function_t algo)
93 {
94 private_hmac_prf_t *this;
95 hash_algorithm_t hash;
96
97 switch (algo)
98 {
99 case PRF_HMAC_SHA1:
100 hash = HASH_SHA1;
101 break;
102 case PRF_HMAC_MD5:
103 hash = HASH_MD5;
104 break;
105 case PRF_HMAC_SHA2_256:
106 hash = HASH_SHA256;
107 break;
108 case PRF_HMAC_SHA2_384:
109 hash = HASH_SHA384;
110 break;
111 case PRF_HMAC_SHA2_512:
112 hash = HASH_SHA512;
113 break;
114 default:
115 return NULL;
116 }
117
118 this = malloc_thing(private_hmac_prf_t);
119 this->hmac = hmac_create(hash);
120 if (this->hmac == NULL)
121 {
122 free(this);
123 return NULL;
124 }
125
126 this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
127 this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
128 this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
129 this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size;
130 this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
131 this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
132
133 return &(this->public);
134 }
135