Annotation of embedaddon/strongswan/src/libstrongswan/plugins/wolfssl/wolfssl_hmac.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2019 Sean Parkinson, wolfSSL Inc.
3: *
4: * Permission is hereby granted, free of charge, to any person obtaining a copy
5: * of this software and associated documentation files (the "Software"), to deal
6: * in the Software without restriction, including without limitation the rights
7: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: * copies of the Software, and to permit persons to whom the Software is
9: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: * THE SOFTWARE.
21: */
22:
23: #include "wolfssl_common.h"
24:
25: #ifndef NO_HMAC
26:
27: #include <wolfssl/wolfcrypt/hmac.h>
28:
29: #include "wolfssl_hmac.h"
30: #include "wolfssl_util.h"
31:
32: #include <crypto/mac.h>
33: #include <crypto/prfs/mac_prf.h>
34: #include <crypto/signers/mac_signer.h>
35:
36: typedef struct private_mac_t private_mac_t;
37:
38: /**
39: * Private data of a mac_t object.
40: */
41: struct private_mac_t {
42:
43: /**
44: * Public interface
45: */
46: mac_t public;
47:
48: /**
49: * Current HMAC
50: */
51: Hmac hmac;
52:
53: /**
54: * Hasher to use
55: */
56: enum wc_HashType type;
57:
58: /**
59: * Key set on Hmac?
60: */
61: bool key_set;
62: };
63:
64: METHOD(mac_t, set_key, bool,
65: private_mac_t *this, chunk_t key)
66: {
67: int ret = wc_HmacSetKey(&this->hmac, this->type, key.ptr, key.len);
68: this->key_set = (ret == 0);
69: return ret == 0;
70: }
71:
72: METHOD(mac_t, get_mac, bool,
73: private_mac_t *this, chunk_t data, uint8_t *out)
74: {
75: int ret = -1;
76:
77: if (this->key_set)
78: {
79: ret = wc_HmacUpdate(&this->hmac, data.ptr, data.len);
80: if (ret == 0 && out)
81: {
82: ret = wc_HmacFinal(&this->hmac, out);
83: }
84: }
85: return ret == 0;
86: }
87:
88: METHOD(mac_t, get_mac_size, size_t,
89: private_mac_t *this)
90: {
91: return wc_HmacSizeByType(this->type);
92: }
93:
94: METHOD(mac_t, destroy, void,
95: private_mac_t *this)
96: {
97: wc_HmacFree(&this->hmac);
98: free(this);
99: }
100:
101: /*
102: * Create an wolfSSL-backed implementation of the mac_t interface
103: */
104: static mac_t *hmac_create(hash_algorithm_t algo)
105: {
106: private_mac_t *this;
107: enum wc_HashType type;
108:
109: if (!wolfssl_hash2type(algo, &type))
110: {
111: return NULL;
112: }
113:
114: INIT(this,
115: .public = {
116: .get_mac = _get_mac,
117: .get_mac_size = _get_mac_size,
118: .set_key = _set_key,
119: .destroy = _destroy,
120: },
121: .type = type,
122: );
123:
124: if (wc_HmacInit(&this->hmac, NULL, INVALID_DEVID) != 0)
125: {
126: DBG1(DBG_LIB, "HMAC init failed, hmac create failed\n");
127: free(this);
128: return NULL;
129: }
130: return &this->public;
131: }
132:
133: /*
134: * Described in header
135: */
136: prf_t *wolfssl_hmac_prf_create(pseudo_random_function_t algo)
137: {
138: mac_t *hmac;
139:
140: hmac = hmac_create(hasher_algorithm_from_prf(algo));
141: if (hmac)
142: {
143: return mac_prf_create(hmac);
144: }
145: return NULL;
146: }
147:
148: /*
149: * Described in header
150: */
151: signer_t *wolfssl_hmac_signer_create(integrity_algorithm_t algo)
152: {
153: mac_t *hmac;
154: size_t trunc;
155:
156: hmac = hmac_create(hasher_algorithm_from_integrity(algo, &trunc));
157: if (hmac)
158: {
159: return mac_signer_create(hmac, trunc);
160: }
161: return NULL;
162: }
163:
164: #endif /* NO_HMAC */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>