wolfssl: Fixes, code style changes and some refactorings
[strongswan.git] / src / libstrongswan / plugins / wolfssl / wolfssl_util.c
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 #include "wolfssl_util.h"
25
26 #include <utils/debug.h>
27
28 #include <wolfssl/wolfcrypt/hash.h>
29 #include <wolfssl/wolfcrypt/rsa.h>
30
31 /*
32 * Described in header
33 */
34 bool wolfssl_hash_chunk(int hash_type, chunk_t data, chunk_t *hash)
35 {
36 int ret;
37
38 *hash = chunk_alloc(wc_HashGetDigestSize(hash_type));
39 ret = wc_Hash(hash_type, data.ptr, data.len, hash->ptr, hash->len);
40 if (ret < 0)
41 {
42 chunk_free(hash);
43 return FALSE;
44 }
45 return TRUE;
46 }
47
48 /*
49 * Described in header
50 */
51 bool wolfssl_mp2chunk(mp_int *mp, chunk_t *chunk)
52 {
53 *chunk = chunk_alloc(mp_unsigned_bin_size(mp));
54 if (mp_to_unsigned_bin(mp, chunk->ptr) == 0)
55 {
56 if (chunk->len && chunk->ptr[0] & 0x80)
57 { /* if MSB is set, prepend a zero to make it non-negative */
58 *chunk = chunk_cat("cm", chunk_from_chars(0x00), *chunk);
59 }
60 return TRUE;
61 }
62 chunk_free(chunk);
63 return FALSE;
64 }
65
66 /*
67 * Described in header
68 */
69 bool wolfssl_mp_split(chunk_t chunk, mp_int *a, mp_int *b)
70 {
71 int ret;
72 int len;
73
74 if ((chunk.len % 2) == 1)
75 {
76 return FALSE;
77 }
78
79 len = chunk.len / 2;
80 ret = mp_read_unsigned_bin(a, chunk.ptr, len);
81 if (ret == 0)
82 {
83 ret = mp_read_unsigned_bin(b, chunk.ptr + len, len);
84 }
85 return ret == 0;
86 }
87
88 /*
89 * Described in header
90 */
91 bool wolfssl_mp_cat(int len, mp_int *a, mp_int *b, chunk_t *chunk)
92 {
93 int ret;
94 int sz;
95
96 *chunk = chunk_alloc(len);
97 if (b != NULL)
98 {
99 len /= 2;
100 }
101
102 sz = mp_unsigned_bin_size(a);
103 memset(chunk->ptr, 0, len - sz);
104 ret = mp_to_unsigned_bin(a, chunk->ptr + len - sz);
105 if (ret == 0 && b != NULL)
106 {
107 sz = mp_unsigned_bin_size(b);
108 memset(chunk->ptr + len, 0, len - sz);
109 ret = mp_to_unsigned_bin(b, chunk->ptr + 2 * len - sz);
110 }
111 return ret == 0;
112 }
113
114 /*
115 * Described in header
116 */
117 bool wolfssl_hash2type(hash_algorithm_t hash, enum wc_HashType *type)
118 {
119 switch (hash)
120 {
121 #ifndef NO_MD5
122 case HASH_MD5:
123 *type = WC_HASH_TYPE_MD5;
124 break;
125 #endif
126 #ifndef NO_SHA
127 case HASH_SHA1:
128 *type = WC_HASH_TYPE_SHA;
129 break;
130 #endif
131 #ifdef WOLFSSL_SHA224
132 case HASH_SHA224:
133 *type = WC_HASH_TYPE_SHA224;
134 break;
135 #endif
136 #ifndef NO_SHA256
137 case HASH_SHA256:
138 *type = WC_HASH_TYPE_SHA256;
139 break;
140 #endif
141 #ifdef WOLFSSL_SHA384
142 case HASH_SHA384:
143 *type = WC_HASH_TYPE_SHA384;
144 break;
145 #endif
146 #ifdef WOLFSSL_SHA512
147 case HASH_SHA512:
148 *type = WC_HASH_TYPE_SHA512;
149 break;
150 #endif
151 default:
152 return FALSE;
153 }
154 return TRUE;
155 }
156
157 /*
158 * Described in header
159 */
160 bool wolfssl_hash2mgf1(hash_algorithm_t hash, int *mgf1)
161 {
162 switch (hash)
163 {
164 #ifndef NO_SHA
165 case HASH_SHA1:
166 *mgf1 = WC_MGF1SHA1;
167 break;
168 #endif
169 #ifdef WOLFSSL_SHA224
170 case HASH_SHA224:
171 *mgf1 = WC_MGF1SHA224;
172 break;
173 #endif
174 #ifndef NO_SHA256
175 case HASH_SHA256:
176 *mgf1 = WC_MGF1SHA256;
177 break;
178 #endif
179 #ifdef WOLFSSL_SHA384
180 case HASH_SHA384:
181 *mgf1 = WC_MGF1SHA384;
182 break;
183 #endif
184 #ifdef WOLFSSL_SHA512
185 case HASH_SHA512:
186 *mgf1 = WC_MGF1SHA512;
187 break;
188 #endif
189 default:
190 return FALSE;
191 }
192 return TRUE;
193 }