xref: /optee_os/lib/libmbedtls/mbedtls/library/error.c (revision 11fa71b9ddb429088f325cfda430183003ccd1db)
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  *  Error message information
4  *
5  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
29 #include <string.h>
30 #endif
31 
32 #if defined(MBEDTLS_PLATFORM_C)
33 #include "mbedtls/platform.h"
34 #else
35 #define mbedtls_snprintf snprintf
36 #define mbedtls_time_t   time_t
37 #endif
38 
39 #if defined(MBEDTLS_ERROR_C)
40 
41 #include <stdio.h>
42 
43 #if defined(MBEDTLS_AES_C)
44 #include "mbedtls/aes.h"
45 #endif
46 
47 #if defined(MBEDTLS_ARC4_C)
48 #include "mbedtls/arc4.h"
49 #endif
50 
51 #if defined(MBEDTLS_ARIA_C)
52 #include "mbedtls/aria.h"
53 #endif
54 
55 #if defined(MBEDTLS_BASE64_C)
56 #include "mbedtls/base64.h"
57 #endif
58 
59 #if defined(MBEDTLS_BIGNUM_C)
60 #include "mbedtls/bignum.h"
61 #endif
62 
63 #if defined(MBEDTLS_BLOWFISH_C)
64 #include "mbedtls/blowfish.h"
65 #endif
66 
67 #if defined(MBEDTLS_CAMELLIA_C)
68 #include "mbedtls/camellia.h"
69 #endif
70 
71 #if defined(MBEDTLS_CCM_C)
72 #include "mbedtls/ccm.h"
73 #endif
74 
75 #if defined(MBEDTLS_CHACHA20_C)
76 #include "mbedtls/chacha20.h"
77 #endif
78 
79 #if defined(MBEDTLS_CHACHAPOLY_C)
80 #include "mbedtls/chachapoly.h"
81 #endif
82 
83 #if defined(MBEDTLS_CIPHER_C)
84 #include "mbedtls/cipher.h"
85 #endif
86 
87 #if defined(MBEDTLS_CMAC_C)
88 #include "mbedtls/cmac.h"
89 #endif
90 
91 #if defined(MBEDTLS_CTR_DRBG_C)
92 #include "mbedtls/ctr_drbg.h"
93 #endif
94 
95 #if defined(MBEDTLS_DES_C)
96 #include "mbedtls/des.h"
97 #endif
98 
99 #if defined(MBEDTLS_DHM_C)
100 #include "mbedtls/dhm.h"
101 #endif
102 
103 #if defined(MBEDTLS_ECP_C)
104 #include "mbedtls/ecp.h"
105 #endif
106 
107 #if defined(MBEDTLS_ENTROPY_C)
108 #include "mbedtls/entropy.h"
109 #endif
110 
111 #if defined(MBEDTLS_ERROR_C)
112 #include "mbedtls/error.h"
113 #endif
114 
115 #if defined(MBEDTLS_GCM_C)
116 #include "mbedtls/gcm.h"
117 #endif
118 
119 #if defined(MBEDTLS_HKDF_C)
120 #include "mbedtls/hkdf.h"
121 #endif
122 
123 #if defined(MBEDTLS_HMAC_DRBG_C)
124 #include "mbedtls/hmac_drbg.h"
125 #endif
126 
127 #if defined(MBEDTLS_MD_C)
128 #include "mbedtls/md.h"
129 #endif
130 
131 #if defined(MBEDTLS_MD2_C)
132 #include "mbedtls/md2.h"
133 #endif
134 
135 #if defined(MBEDTLS_MD4_C)
136 #include "mbedtls/md4.h"
137 #endif
138 
139 #if defined(MBEDTLS_MD5_C)
140 #include "mbedtls/md5.h"
141 #endif
142 
143 #if defined(MBEDTLS_NET_C)
144 #include "mbedtls/net_sockets.h"
145 #endif
146 
147 #if defined(MBEDTLS_OID_C)
148 #include "mbedtls/oid.h"
149 #endif
150 
151 #if defined(MBEDTLS_PADLOCK_C)
152 #include "mbedtls/padlock.h"
153 #endif
154 
155 #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
156 #include "mbedtls/pem.h"
157 #endif
158 
159 #if defined(MBEDTLS_PK_C)
160 #include "mbedtls/pk.h"
161 #endif
162 
163 #if defined(MBEDTLS_PKCS12_C)
164 #include "mbedtls/pkcs12.h"
165 #endif
166 
167 #if defined(MBEDTLS_PKCS5_C)
168 #include "mbedtls/pkcs5.h"
169 #endif
170 
171 #if defined(MBEDTLS_PLATFORM_C)
172 #include "mbedtls/platform.h"
173 #endif
174 
175 #if defined(MBEDTLS_POLY1305_C)
176 #include "mbedtls/poly1305.h"
177 #endif
178 
179 #if defined(MBEDTLS_RIPEMD160_C)
180 #include "mbedtls/ripemd160.h"
181 #endif
182 
183 #if defined(MBEDTLS_RSA_C)
184 #include "mbedtls/rsa.h"
185 #endif
186 
187 #if defined(MBEDTLS_SHA1_C)
188 #include "mbedtls/sha1.h"
189 #endif
190 
191 #if defined(MBEDTLS_SHA256_C)
192 #include "mbedtls/sha256.h"
193 #endif
194 
195 #if defined(MBEDTLS_SHA512_C)
196 #include "mbedtls/sha512.h"
197 #endif
198 
199 #if defined(MBEDTLS_SSL_TLS_C)
200 #include "mbedtls/ssl.h"
201 #endif
202 
203 #if defined(MBEDTLS_THREADING_C)
204 #include "mbedtls/threading.h"
205 #endif
206 
207 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
208 #include "mbedtls/x509.h"
209 #endif
210 
211 #if defined(MBEDTLS_XTEA_C)
212 #include "mbedtls/xtea.h"
213 #endif
214 
215 
216 void mbedtls_strerror( int ret, char *buf, size_t buflen )
217 {
218     size_t len;
219     int use_ret;
220 
221     if( buflen == 0 )
222         return;
223 
224     memset( buf, 0x00, buflen );
225 
226     if( ret < 0 )
227         ret = -ret;
228 
229     if( ret & 0xFF80 )
230     {
231         use_ret = ret & 0xFF80;
232 
233         // High level error codes
234         //
235         // BEGIN generated code
236 #if defined(MBEDTLS_CIPHER_C)
237         if( use_ret == -(MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) )
238             mbedtls_snprintf( buf, buflen, "CIPHER - The selected feature is not available" );
239         if( use_ret == -(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA) )
240             mbedtls_snprintf( buf, buflen, "CIPHER - Bad input parameters" );
241         if( use_ret == -(MBEDTLS_ERR_CIPHER_ALLOC_FAILED) )
242             mbedtls_snprintf( buf, buflen, "CIPHER - Failed to allocate memory" );
243         if( use_ret == -(MBEDTLS_ERR_CIPHER_INVALID_PADDING) )
244             mbedtls_snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" );
245         if( use_ret == -(MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED) )
246             mbedtls_snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" );
247         if( use_ret == -(MBEDTLS_ERR_CIPHER_AUTH_FAILED) )
248             mbedtls_snprintf( buf, buflen, "CIPHER - Authentication failed (for AEAD modes)" );
249         if( use_ret == -(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT) )
250             mbedtls_snprintf( buf, buflen, "CIPHER - The context is invalid. For example, because it was freed" );
251         if( use_ret == -(MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED) )
252             mbedtls_snprintf( buf, buflen, "CIPHER - Cipher hardware accelerator failed" );
253 #endif /* MBEDTLS_CIPHER_C */
254 
255 #if defined(MBEDTLS_DHM_C)
256         if( use_ret == -(MBEDTLS_ERR_DHM_BAD_INPUT_DATA) )
257             mbedtls_snprintf( buf, buflen, "DHM - Bad input parameters" );
258         if( use_ret == -(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED) )
259             mbedtls_snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" );
260         if( use_ret == -(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED) )
261             mbedtls_snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" );
262         if( use_ret == -(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED) )
263             mbedtls_snprintf( buf, buflen, "DHM - Reading of the public values failed" );
264         if( use_ret == -(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED) )
265             mbedtls_snprintf( buf, buflen, "DHM - Making of the public value failed" );
266         if( use_ret == -(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED) )
267             mbedtls_snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" );
268         if( use_ret == -(MBEDTLS_ERR_DHM_INVALID_FORMAT) )
269             mbedtls_snprintf( buf, buflen, "DHM - The ASN.1 data is not formatted correctly" );
270         if( use_ret == -(MBEDTLS_ERR_DHM_ALLOC_FAILED) )
271             mbedtls_snprintf( buf, buflen, "DHM - Allocation of memory failed" );
272         if( use_ret == -(MBEDTLS_ERR_DHM_FILE_IO_ERROR) )
273             mbedtls_snprintf( buf, buflen, "DHM - Read or write of file failed" );
274         if( use_ret == -(MBEDTLS_ERR_DHM_HW_ACCEL_FAILED) )
275             mbedtls_snprintf( buf, buflen, "DHM - DHM hardware accelerator failed" );
276         if( use_ret == -(MBEDTLS_ERR_DHM_SET_GROUP_FAILED) )
277             mbedtls_snprintf( buf, buflen, "DHM - Setting the modulus and generator failed" );
278 #endif /* MBEDTLS_DHM_C */
279 
280 #if defined(MBEDTLS_ECP_C)
281         if( use_ret == -(MBEDTLS_ERR_ECP_BAD_INPUT_DATA) )
282             mbedtls_snprintf( buf, buflen, "ECP - Bad input parameters to function" );
283         if( use_ret == -(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL) )
284             mbedtls_snprintf( buf, buflen, "ECP - The buffer is too small to write to" );
285         if( use_ret == -(MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE) )
286             mbedtls_snprintf( buf, buflen, "ECP - The requested feature is not available, for example, the requested curve is not supported" );
287         if( use_ret == -(MBEDTLS_ERR_ECP_VERIFY_FAILED) )
288             mbedtls_snprintf( buf, buflen, "ECP - The signature is not valid" );
289         if( use_ret == -(MBEDTLS_ERR_ECP_ALLOC_FAILED) )
290             mbedtls_snprintf( buf, buflen, "ECP - Memory allocation failed" );
291         if( use_ret == -(MBEDTLS_ERR_ECP_RANDOM_FAILED) )
292             mbedtls_snprintf( buf, buflen, "ECP - Generation of random value, such as ephemeral key, failed" );
293         if( use_ret == -(MBEDTLS_ERR_ECP_INVALID_KEY) )
294             mbedtls_snprintf( buf, buflen, "ECP - Invalid private or public key" );
295         if( use_ret == -(MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) )
296             mbedtls_snprintf( buf, buflen, "ECP - The buffer contains a valid signature followed by more data" );
297         if( use_ret == -(MBEDTLS_ERR_ECP_HW_ACCEL_FAILED) )
298             mbedtls_snprintf( buf, buflen, "ECP - The ECP hardware accelerator failed" );
299         if( use_ret == -(MBEDTLS_ERR_ECP_IN_PROGRESS) )
300             mbedtls_snprintf( buf, buflen, "ECP - Operation in progress, call again with the same parameters to continue" );
301 #endif /* MBEDTLS_ECP_C */
302 
303 #if defined(MBEDTLS_MD_C)
304         if( use_ret == -(MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE) )
305             mbedtls_snprintf( buf, buflen, "MD - The selected feature is not available" );
306         if( use_ret == -(MBEDTLS_ERR_MD_BAD_INPUT_DATA) )
307             mbedtls_snprintf( buf, buflen, "MD - Bad input parameters to function" );
308         if( use_ret == -(MBEDTLS_ERR_MD_ALLOC_FAILED) )
309             mbedtls_snprintf( buf, buflen, "MD - Failed to allocate memory" );
310         if( use_ret == -(MBEDTLS_ERR_MD_FILE_IO_ERROR) )
311             mbedtls_snprintf( buf, buflen, "MD - Opening or reading of file failed" );
312         if( use_ret == -(MBEDTLS_ERR_MD_HW_ACCEL_FAILED) )
313             mbedtls_snprintf( buf, buflen, "MD - MD hardware accelerator failed" );
314 #endif /* MBEDTLS_MD_C */
315 
316 #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
317         if( use_ret == -(MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) )
318             mbedtls_snprintf( buf, buflen, "PEM - No PEM header or footer found" );
319         if( use_ret == -(MBEDTLS_ERR_PEM_INVALID_DATA) )
320             mbedtls_snprintf( buf, buflen, "PEM - PEM string is not as expected" );
321         if( use_ret == -(MBEDTLS_ERR_PEM_ALLOC_FAILED) )
322             mbedtls_snprintf( buf, buflen, "PEM - Failed to allocate memory" );
323         if( use_ret == -(MBEDTLS_ERR_PEM_INVALID_ENC_IV) )
324             mbedtls_snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" );
325         if( use_ret == -(MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG) )
326             mbedtls_snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" );
327         if( use_ret == -(MBEDTLS_ERR_PEM_PASSWORD_REQUIRED) )
328             mbedtls_snprintf( buf, buflen, "PEM - Private key password can't be empty" );
329         if( use_ret == -(MBEDTLS_ERR_PEM_PASSWORD_MISMATCH) )
330             mbedtls_snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" );
331         if( use_ret == -(MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE) )
332             mbedtls_snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" );
333         if( use_ret == -(MBEDTLS_ERR_PEM_BAD_INPUT_DATA) )
334             mbedtls_snprintf( buf, buflen, "PEM - Bad input parameters to function" );
335 #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
336 
337 #if defined(MBEDTLS_PK_C)
338         if( use_ret == -(MBEDTLS_ERR_PK_ALLOC_FAILED) )
339             mbedtls_snprintf( buf, buflen, "PK - Memory allocation failed" );
340         if( use_ret == -(MBEDTLS_ERR_PK_TYPE_MISMATCH) )
341             mbedtls_snprintf( buf, buflen, "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" );
342         if( use_ret == -(MBEDTLS_ERR_PK_BAD_INPUT_DATA) )
343             mbedtls_snprintf( buf, buflen, "PK - Bad input parameters to function" );
344         if( use_ret == -(MBEDTLS_ERR_PK_FILE_IO_ERROR) )
345             mbedtls_snprintf( buf, buflen, "PK - Read/write of file failed" );
346         if( use_ret == -(MBEDTLS_ERR_PK_KEY_INVALID_VERSION) )
347             mbedtls_snprintf( buf, buflen, "PK - Unsupported key version" );
348         if( use_ret == -(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT) )
349             mbedtls_snprintf( buf, buflen, "PK - Invalid key tag or value" );
350         if( use_ret == -(MBEDTLS_ERR_PK_UNKNOWN_PK_ALG) )
351             mbedtls_snprintf( buf, buflen, "PK - Key algorithm is unsupported (only RSA and EC are supported)" );
352         if( use_ret == -(MBEDTLS_ERR_PK_PASSWORD_REQUIRED) )
353             mbedtls_snprintf( buf, buflen, "PK - Private key password can't be empty" );
354         if( use_ret == -(MBEDTLS_ERR_PK_PASSWORD_MISMATCH) )
355             mbedtls_snprintf( buf, buflen, "PK - Given private key password does not allow for correct decryption" );
356         if( use_ret == -(MBEDTLS_ERR_PK_INVALID_PUBKEY) )
357             mbedtls_snprintf( buf, buflen, "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" );
358         if( use_ret == -(MBEDTLS_ERR_PK_INVALID_ALG) )
359             mbedtls_snprintf( buf, buflen, "PK - The algorithm tag or value is invalid" );
360         if( use_ret == -(MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE) )
361             mbedtls_snprintf( buf, buflen, "PK - Elliptic curve is unsupported (only NIST curves are supported)" );
362         if( use_ret == -(MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE) )
363             mbedtls_snprintf( buf, buflen, "PK - Unavailable feature, e.g. RSA disabled for RSA key" );
364         if( use_ret == -(MBEDTLS_ERR_PK_SIG_LEN_MISMATCH) )
365             mbedtls_snprintf( buf, buflen, "PK - The buffer contains a valid signature followed by more data" );
366         if( use_ret == -(MBEDTLS_ERR_PK_HW_ACCEL_FAILED) )
367             mbedtls_snprintf( buf, buflen, "PK - PK hardware accelerator failed" );
368 #endif /* MBEDTLS_PK_C */
369 
370 #if defined(MBEDTLS_PKCS12_C)
371         if( use_ret == -(MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA) )
372             mbedtls_snprintf( buf, buflen, "PKCS12 - Bad input parameters to function" );
373         if( use_ret == -(MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE) )
374             mbedtls_snprintf( buf, buflen, "PKCS12 - Feature not available, e.g. unsupported encryption scheme" );
375         if( use_ret == -(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT) )
376             mbedtls_snprintf( buf, buflen, "PKCS12 - PBE ASN.1 data not as expected" );
377         if( use_ret == -(MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH) )
378             mbedtls_snprintf( buf, buflen, "PKCS12 - Given private key password does not allow for correct decryption" );
379 #endif /* MBEDTLS_PKCS12_C */
380 
381 #if defined(MBEDTLS_PKCS5_C)
382         if( use_ret == -(MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA) )
383             mbedtls_snprintf( buf, buflen, "PKCS5 - Bad input parameters to function" );
384         if( use_ret == -(MBEDTLS_ERR_PKCS5_INVALID_FORMAT) )
385             mbedtls_snprintf( buf, buflen, "PKCS5 - Unexpected ASN.1 data" );
386         if( use_ret == -(MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE) )
387             mbedtls_snprintf( buf, buflen, "PKCS5 - Requested encryption or digest alg not available" );
388         if( use_ret == -(MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH) )
389             mbedtls_snprintf( buf, buflen, "PKCS5 - Given private key password does not allow for correct decryption" );
390 #endif /* MBEDTLS_PKCS5_C */
391 
392 #if defined(MBEDTLS_RSA_C)
393         if( use_ret == -(MBEDTLS_ERR_RSA_BAD_INPUT_DATA) )
394             mbedtls_snprintf( buf, buflen, "RSA - Bad input parameters to function" );
395         if( use_ret == -(MBEDTLS_ERR_RSA_INVALID_PADDING) )
396             mbedtls_snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" );
397         if( use_ret == -(MBEDTLS_ERR_RSA_KEY_GEN_FAILED) )
398             mbedtls_snprintf( buf, buflen, "RSA - Something failed during generation of a key" );
399         if( use_ret == -(MBEDTLS_ERR_RSA_KEY_CHECK_FAILED) )
400             mbedtls_snprintf( buf, buflen, "RSA - Key failed to pass the validity check of the library" );
401         if( use_ret == -(MBEDTLS_ERR_RSA_PUBLIC_FAILED) )
402             mbedtls_snprintf( buf, buflen, "RSA - The public key operation failed" );
403         if( use_ret == -(MBEDTLS_ERR_RSA_PRIVATE_FAILED) )
404             mbedtls_snprintf( buf, buflen, "RSA - The private key operation failed" );
405         if( use_ret == -(MBEDTLS_ERR_RSA_VERIFY_FAILED) )
406             mbedtls_snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" );
407         if( use_ret == -(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE) )
408             mbedtls_snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" );
409         if( use_ret == -(MBEDTLS_ERR_RSA_RNG_FAILED) )
410             mbedtls_snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" );
411         if( use_ret == -(MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION) )
412             mbedtls_snprintf( buf, buflen, "RSA - The implementation does not offer the requested operation, for example, because of security violations or lack of functionality" );
413         if( use_ret == -(MBEDTLS_ERR_RSA_HW_ACCEL_FAILED) )
414             mbedtls_snprintf( buf, buflen, "RSA - RSA hardware accelerator failed" );
415 #endif /* MBEDTLS_RSA_C */
416 
417 #if defined(MBEDTLS_SSL_TLS_C)
418         if( use_ret == -(MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE) )
419             mbedtls_snprintf( buf, buflen, "SSL - The requested feature is not available" );
420         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_INPUT_DATA) )
421             mbedtls_snprintf( buf, buflen, "SSL - Bad input parameters to function" );
422         if( use_ret == -(MBEDTLS_ERR_SSL_INVALID_MAC) )
423             mbedtls_snprintf( buf, buflen, "SSL - Verification of the message MAC failed" );
424         if( use_ret == -(MBEDTLS_ERR_SSL_INVALID_RECORD) )
425             mbedtls_snprintf( buf, buflen, "SSL - An invalid SSL record was received" );
426         if( use_ret == -(MBEDTLS_ERR_SSL_CONN_EOF) )
427             mbedtls_snprintf( buf, buflen, "SSL - The connection indicated an EOF" );
428         if( use_ret == -(MBEDTLS_ERR_SSL_UNKNOWN_CIPHER) )
429             mbedtls_snprintf( buf, buflen, "SSL - An unknown cipher was received" );
430         if( use_ret == -(MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN) )
431             mbedtls_snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" );
432         if( use_ret == -(MBEDTLS_ERR_SSL_NO_RNG) )
433             mbedtls_snprintf( buf, buflen, "SSL - No RNG was provided to the SSL module" );
434         if( use_ret == -(MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE) )
435             mbedtls_snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" );
436         if( use_ret == -(MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE) )
437             mbedtls_snprintf( buf, buflen, "SSL - Our own certificate(s) is/are too large to send in an SSL message" );
438         if( use_ret == -(MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED) )
439             mbedtls_snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" );
440         if( use_ret == -(MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) )
441             mbedtls_snprintf( buf, buflen, "SSL - The own private key or pre-shared key is not set, but needed" );
442         if( use_ret == -(MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED) )
443             mbedtls_snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" );
444         if( use_ret == -(MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) )
445             mbedtls_snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" );
446         if( use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE) )
447         {
448             mbedtls_snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" );
449             return;
450         }
451         if( use_ret == -(MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED) )
452             mbedtls_snprintf( buf, buflen, "SSL - Verification of our peer failed" );
453         if( use_ret == -(MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) )
454             mbedtls_snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" );
455         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO) )
456             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" );
457         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO) )
458             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" );
459         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE) )
460             mbedtls_snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" );
461         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST) )
462             mbedtls_snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" );
463         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE) )
464             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" );
465         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE) )
466             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" );
467         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE) )
468             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" );
469         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP) )
470             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public" );
471         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS) )
472             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret" );
473         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY) )
474             mbedtls_snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" );
475         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC) )
476             mbedtls_snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" );
477         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_FINISHED) )
478             mbedtls_snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" );
479         if( use_ret == -(MBEDTLS_ERR_SSL_ALLOC_FAILED) )
480             mbedtls_snprintf( buf, buflen, "SSL - Memory allocation failed" );
481         if( use_ret == -(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED) )
482             mbedtls_snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" );
483         if( use_ret == -(MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) )
484             mbedtls_snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" );
485         if( use_ret == -(MBEDTLS_ERR_SSL_COMPRESSION_FAILED) )
486             mbedtls_snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" );
487         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION) )
488             mbedtls_snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" );
489         if( use_ret == -(MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET) )
490             mbedtls_snprintf( buf, buflen, "SSL - Processing of the NewSessionTicket handshake message failed" );
491         if( use_ret == -(MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) )
492             mbedtls_snprintf( buf, buflen, "SSL - Session ticket has expired" );
493         if( use_ret == -(MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH) )
494             mbedtls_snprintf( buf, buflen, "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" );
495         if( use_ret == -(MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) )
496             mbedtls_snprintf( buf, buflen, "SSL - Unknown identity received (eg, PSK identity)" );
497         if( use_ret == -(MBEDTLS_ERR_SSL_INTERNAL_ERROR) )
498             mbedtls_snprintf( buf, buflen, "SSL - Internal error (eg, unexpected failure in lower-level module)" );
499         if( use_ret == -(MBEDTLS_ERR_SSL_COUNTER_WRAPPING) )
500             mbedtls_snprintf( buf, buflen, "SSL - A counter would wrap (eg, too many messages exchanged)" );
501         if( use_ret == -(MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO) )
502             mbedtls_snprintf( buf, buflen, "SSL - Unexpected message at ServerHello in renegotiation" );
503         if( use_ret == -(MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) )
504             mbedtls_snprintf( buf, buflen, "SSL - DTLS client must retry for hello verification" );
505         if( use_ret == -(MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) )
506             mbedtls_snprintf( buf, buflen, "SSL - A buffer is too small to receive or write a message" );
507         if( use_ret == -(MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE) )
508             mbedtls_snprintf( buf, buflen, "SSL - None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages)" );
509         if( use_ret == -(MBEDTLS_ERR_SSL_WANT_READ) )
510             mbedtls_snprintf( buf, buflen, "SSL - No data of requested type currently available on underlying transport" );
511         if( use_ret == -(MBEDTLS_ERR_SSL_WANT_WRITE) )
512             mbedtls_snprintf( buf, buflen, "SSL - Connection requires a write call" );
513         if( use_ret == -(MBEDTLS_ERR_SSL_TIMEOUT) )
514             mbedtls_snprintf( buf, buflen, "SSL - The operation timed out" );
515         if( use_ret == -(MBEDTLS_ERR_SSL_CLIENT_RECONNECT) )
516             mbedtls_snprintf( buf, buflen, "SSL - The client initiated a reconnect from the same port" );
517         if( use_ret == -(MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) )
518             mbedtls_snprintf( buf, buflen, "SSL - Record header looks valid but is not expected" );
519         if( use_ret == -(MBEDTLS_ERR_SSL_NON_FATAL) )
520             mbedtls_snprintf( buf, buflen, "SSL - The alert message received indicates a non-fatal error" );
521         if( use_ret == -(MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH) )
522             mbedtls_snprintf( buf, buflen, "SSL - Couldn't set the hash for verifying CertificateVerify" );
523         if( use_ret == -(MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) )
524             mbedtls_snprintf( buf, buflen, "SSL - Internal-only message signaling that further message-processing should be done" );
525         if( use_ret == -(MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) )
526             mbedtls_snprintf( buf, buflen, "SSL - The asynchronous operation is not completed yet" );
527         if( use_ret == -(MBEDTLS_ERR_SSL_EARLY_MESSAGE) )
528             mbedtls_snprintf( buf, buflen, "SSL - Internal-only message signaling that a message arrived early" );
529         if( use_ret == -(MBEDTLS_ERR_SSL_UNEXPECTED_CID) )
530             mbedtls_snprintf( buf, buflen, "SSL - An encrypted DTLS-frame with an unexpected CID was received" );
531         if( use_ret == -(MBEDTLS_ERR_SSL_VERSION_MISMATCH) )
532             mbedtls_snprintf( buf, buflen, "SSL - An operation failed due to an unexpected version or configuration" );
533         if( use_ret == -(MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) )
534             mbedtls_snprintf( buf, buflen, "SSL - A cryptographic operation is in progress. Try again later" );
535 #endif /* MBEDTLS_SSL_TLS_C */
536 
537 #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
538         if( use_ret == -(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) )
539             mbedtls_snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
540         if( use_ret == -(MBEDTLS_ERR_X509_UNKNOWN_OID) )
541             mbedtls_snprintf( buf, buflen, "X509 - Requested OID is unknown" );
542         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_FORMAT) )
543             mbedtls_snprintf( buf, buflen, "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" );
544         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_VERSION) )
545             mbedtls_snprintf( buf, buflen, "X509 - The CRT/CRL/CSR version element is invalid" );
546         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_SERIAL) )
547             mbedtls_snprintf( buf, buflen, "X509 - The serial tag or value is invalid" );
548         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_ALG) )
549             mbedtls_snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" );
550         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_NAME) )
551             mbedtls_snprintf( buf, buflen, "X509 - The name tag or value is invalid" );
552         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_DATE) )
553             mbedtls_snprintf( buf, buflen, "X509 - The date tag or value is invalid" );
554         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_SIGNATURE) )
555             mbedtls_snprintf( buf, buflen, "X509 - The signature tag or value invalid" );
556         if( use_ret == -(MBEDTLS_ERR_X509_INVALID_EXTENSIONS) )
557             mbedtls_snprintf( buf, buflen, "X509 - The extension tag or value is invalid" );
558         if( use_ret == -(MBEDTLS_ERR_X509_UNKNOWN_VERSION) )
559             mbedtls_snprintf( buf, buflen, "X509 - CRT/CRL/CSR has an unsupported version number" );
560         if( use_ret == -(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG) )
561             mbedtls_snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" );
562         if( use_ret == -(MBEDTLS_ERR_X509_SIG_MISMATCH) )
563             mbedtls_snprintf( buf, buflen, "X509 - Signature algorithms do not match. (see \\c ::mbedtls_x509_crt sig_oid)" );
564         if( use_ret == -(MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) )
565             mbedtls_snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
566         if( use_ret == -(MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT) )
567             mbedtls_snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" );
568         if( use_ret == -(MBEDTLS_ERR_X509_BAD_INPUT_DATA) )
569             mbedtls_snprintf( buf, buflen, "X509 - Input invalid" );
570         if( use_ret == -(MBEDTLS_ERR_X509_ALLOC_FAILED) )
571             mbedtls_snprintf( buf, buflen, "X509 - Allocation of memory failed" );
572         if( use_ret == -(MBEDTLS_ERR_X509_FILE_IO_ERROR) )
573             mbedtls_snprintf( buf, buflen, "X509 - Read/write of file failed" );
574         if( use_ret == -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL) )
575             mbedtls_snprintf( buf, buflen, "X509 - Destination buffer is too small" );
576         if( use_ret == -(MBEDTLS_ERR_X509_FATAL_ERROR) )
577             mbedtls_snprintf( buf, buflen, "X509 - A fatal error occurred, eg the chain is too long or the vrfy callback failed" );
578 #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
579         // END generated code
580 
581         if( strlen( buf ) == 0 )
582             mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
583     }
584 
585     use_ret = ret & ~0xFF80;
586 
587     if( use_ret == 0 )
588         return;
589 
590     // If high level code is present, make a concatenation between both
591     // error strings.
592     //
593     len = strlen( buf );
594 
595     if( len > 0 )
596     {
597         if( buflen - len < 5 )
598             return;
599 
600         mbedtls_snprintf( buf + len, buflen - len, " : " );
601 
602         buf += len + 3;
603         buflen -= len + 3;
604     }
605 
606     // Low level error codes
607     //
608     // BEGIN generated code
609 #if defined(MBEDTLS_AES_C)
610     if( use_ret == -(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH) )
611         mbedtls_snprintf( buf, buflen, "AES - Invalid key length" );
612     if( use_ret == -(MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH) )
613         mbedtls_snprintf( buf, buflen, "AES - Invalid data input length" );
614     if( use_ret == -(MBEDTLS_ERR_AES_BAD_INPUT_DATA) )
615         mbedtls_snprintf( buf, buflen, "AES - Invalid input data" );
616     if( use_ret == -(MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE) )
617         mbedtls_snprintf( buf, buflen, "AES - Feature not available. For example, an unsupported AES key size" );
618     if( use_ret == -(MBEDTLS_ERR_AES_HW_ACCEL_FAILED) )
619         mbedtls_snprintf( buf, buflen, "AES - AES hardware accelerator failed" );
620 #endif /* MBEDTLS_AES_C */
621 
622 #if defined(MBEDTLS_ARC4_C)
623     if( use_ret == -(MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED) )
624         mbedtls_snprintf( buf, buflen, "ARC4 - ARC4 hardware accelerator failed" );
625 #endif /* MBEDTLS_ARC4_C */
626 
627 #if defined(MBEDTLS_ARIA_C)
628     if( use_ret == -(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA) )
629         mbedtls_snprintf( buf, buflen, "ARIA - Bad input data" );
630     if( use_ret == -(MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH) )
631         mbedtls_snprintf( buf, buflen, "ARIA - Invalid data input length" );
632     if( use_ret == -(MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE) )
633         mbedtls_snprintf( buf, buflen, "ARIA - Feature not available. For example, an unsupported ARIA key size" );
634     if( use_ret == -(MBEDTLS_ERR_ARIA_HW_ACCEL_FAILED) )
635         mbedtls_snprintf( buf, buflen, "ARIA - ARIA hardware accelerator failed" );
636 #endif /* MBEDTLS_ARIA_C */
637 
638 #if defined(MBEDTLS_ASN1_PARSE_C)
639     if( use_ret == -(MBEDTLS_ERR_ASN1_OUT_OF_DATA) )
640         mbedtls_snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" );
641     if( use_ret == -(MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) )
642         mbedtls_snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" );
643     if( use_ret == -(MBEDTLS_ERR_ASN1_INVALID_LENGTH) )
644         mbedtls_snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" );
645     if( use_ret == -(MBEDTLS_ERR_ASN1_LENGTH_MISMATCH) )
646         mbedtls_snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" );
647     if( use_ret == -(MBEDTLS_ERR_ASN1_INVALID_DATA) )
648         mbedtls_snprintf( buf, buflen, "ASN1 - Data is invalid" );
649     if( use_ret == -(MBEDTLS_ERR_ASN1_ALLOC_FAILED) )
650         mbedtls_snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
651     if( use_ret == -(MBEDTLS_ERR_ASN1_BUF_TOO_SMALL) )
652         mbedtls_snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" );
653 #endif /* MBEDTLS_ASN1_PARSE_C */
654 
655 #if defined(MBEDTLS_BASE64_C)
656     if( use_ret == -(MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) )
657         mbedtls_snprintf( buf, buflen, "BASE64 - Output buffer too small" );
658     if( use_ret == -(MBEDTLS_ERR_BASE64_INVALID_CHARACTER) )
659         mbedtls_snprintf( buf, buflen, "BASE64 - Invalid character in input" );
660 #endif /* MBEDTLS_BASE64_C */
661 
662 #if defined(MBEDTLS_BIGNUM_C)
663     if( use_ret == -(MBEDTLS_ERR_MPI_FILE_IO_ERROR) )
664         mbedtls_snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" );
665     if( use_ret == -(MBEDTLS_ERR_MPI_BAD_INPUT_DATA) )
666         mbedtls_snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" );
667     if( use_ret == -(MBEDTLS_ERR_MPI_INVALID_CHARACTER) )
668         mbedtls_snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" );
669     if( use_ret == -(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL) )
670         mbedtls_snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" );
671     if( use_ret == -(MBEDTLS_ERR_MPI_NEGATIVE_VALUE) )
672         mbedtls_snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" );
673     if( use_ret == -(MBEDTLS_ERR_MPI_DIVISION_BY_ZERO) )
674         mbedtls_snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" );
675     if( use_ret == -(MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) )
676         mbedtls_snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" );
677     if( use_ret == -(MBEDTLS_ERR_MPI_ALLOC_FAILED) )
678         mbedtls_snprintf( buf, buflen, "BIGNUM - Memory allocation failed" );
679 #endif /* MBEDTLS_BIGNUM_C */
680 
681 #if defined(MBEDTLS_BLOWFISH_C)
682     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) )
683         mbedtls_snprintf( buf, buflen, "BLOWFISH - Bad input data" );
684     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH) )
685         mbedtls_snprintf( buf, buflen, "BLOWFISH - Invalid data input length" );
686     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED) )
687         mbedtls_snprintf( buf, buflen, "BLOWFISH - Blowfish hardware accelerator failed" );
688 #endif /* MBEDTLS_BLOWFISH_C */
689 
690 #if defined(MBEDTLS_CAMELLIA_C)
691     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) )
692         mbedtls_snprintf( buf, buflen, "CAMELLIA - Bad input data" );
693     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH) )
694         mbedtls_snprintf( buf, buflen, "CAMELLIA - Invalid data input length" );
695     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED) )
696         mbedtls_snprintf( buf, buflen, "CAMELLIA - Camellia hardware accelerator failed" );
697 #endif /* MBEDTLS_CAMELLIA_C */
698 
699 #if defined(MBEDTLS_CCM_C)
700     if( use_ret == -(MBEDTLS_ERR_CCM_BAD_INPUT) )
701         mbedtls_snprintf( buf, buflen, "CCM - Bad input parameters to the function" );
702     if( use_ret == -(MBEDTLS_ERR_CCM_AUTH_FAILED) )
703         mbedtls_snprintf( buf, buflen, "CCM - Authenticated decryption failed" );
704     if( use_ret == -(MBEDTLS_ERR_CCM_HW_ACCEL_FAILED) )
705         mbedtls_snprintf( buf, buflen, "CCM - CCM hardware accelerator failed" );
706 #endif /* MBEDTLS_CCM_C */
707 
708 #if defined(MBEDTLS_CHACHA20_C)
709     if( use_ret == -(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA) )
710         mbedtls_snprintf( buf, buflen, "CHACHA20 - Invalid input parameter(s)" );
711     if( use_ret == -(MBEDTLS_ERR_CHACHA20_FEATURE_UNAVAILABLE) )
712         mbedtls_snprintf( buf, buflen, "CHACHA20 - Feature not available. For example, s part of the API is not implemented" );
713     if( use_ret == -(MBEDTLS_ERR_CHACHA20_HW_ACCEL_FAILED) )
714         mbedtls_snprintf( buf, buflen, "CHACHA20 - Chacha20 hardware accelerator failed" );
715 #endif /* MBEDTLS_CHACHA20_C */
716 
717 #if defined(MBEDTLS_CHACHAPOLY_C)
718     if( use_ret == -(MBEDTLS_ERR_CHACHAPOLY_BAD_STATE) )
719         mbedtls_snprintf( buf, buflen, "CHACHAPOLY - The requested operation is not permitted in the current state" );
720     if( use_ret == -(MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) )
721         mbedtls_snprintf( buf, buflen, "CHACHAPOLY - Authenticated decryption failed: data was not authentic" );
722 #endif /* MBEDTLS_CHACHAPOLY_C */
723 
724 #if defined(MBEDTLS_CMAC_C)
725     if( use_ret == -(MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED) )
726         mbedtls_snprintf( buf, buflen, "CMAC - CMAC hardware accelerator failed" );
727 #endif /* MBEDTLS_CMAC_C */
728 
729 #if defined(MBEDTLS_CTR_DRBG_C)
730     if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) )
731         mbedtls_snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" );
732     if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG) )
733         mbedtls_snprintf( buf, buflen, "CTR_DRBG - The requested random buffer length is too big" );
734     if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG) )
735         mbedtls_snprintf( buf, buflen, "CTR_DRBG - The input (entropy + additional data) is too large" );
736     if( use_ret == -(MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR) )
737         mbedtls_snprintf( buf, buflen, "CTR_DRBG - Read or write error in file" );
738 #endif /* MBEDTLS_CTR_DRBG_C */
739 
740 #if defined(MBEDTLS_DES_C)
741     if( use_ret == -(MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH) )
742         mbedtls_snprintf( buf, buflen, "DES - The data input has an invalid length" );
743     if( use_ret == -(MBEDTLS_ERR_DES_HW_ACCEL_FAILED) )
744         mbedtls_snprintf( buf, buflen, "DES - DES hardware accelerator failed" );
745 #endif /* MBEDTLS_DES_C */
746 
747 #if defined(MBEDTLS_ENTROPY_C)
748     if( use_ret == -(MBEDTLS_ERR_ENTROPY_SOURCE_FAILED) )
749         mbedtls_snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" );
750     if( use_ret == -(MBEDTLS_ERR_ENTROPY_MAX_SOURCES) )
751         mbedtls_snprintf( buf, buflen, "ENTROPY - No more sources can be added" );
752     if( use_ret == -(MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED) )
753         mbedtls_snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" );
754     if( use_ret == -(MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE) )
755         mbedtls_snprintf( buf, buflen, "ENTROPY - No strong sources have been added to poll" );
756     if( use_ret == -(MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR) )
757         mbedtls_snprintf( buf, buflen, "ENTROPY - Read/write error in file" );
758 #endif /* MBEDTLS_ENTROPY_C */
759 
760 #if defined(MBEDTLS_ERROR_C)
761     if( use_ret == -(MBEDTLS_ERR_ERROR_GENERIC_ERROR) )
762         mbedtls_snprintf( buf, buflen, "ERROR - Generic error" );
763     if( use_ret == -(MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED) )
764         mbedtls_snprintf( buf, buflen, "ERROR - This is a bug in the library" );
765 #endif /* MBEDTLS_ERROR_C */
766 
767 #if defined(MBEDTLS_GCM_C)
768     if( use_ret == -(MBEDTLS_ERR_GCM_AUTH_FAILED) )
769         mbedtls_snprintf( buf, buflen, "GCM - Authenticated decryption failed" );
770     if( use_ret == -(MBEDTLS_ERR_GCM_HW_ACCEL_FAILED) )
771         mbedtls_snprintf( buf, buflen, "GCM - GCM hardware accelerator failed" );
772     if( use_ret == -(MBEDTLS_ERR_GCM_BAD_INPUT) )
773         mbedtls_snprintf( buf, buflen, "GCM - Bad input parameters to function" );
774 #endif /* MBEDTLS_GCM_C */
775 
776 #if defined(MBEDTLS_HKDF_C)
777     if( use_ret == -(MBEDTLS_ERR_HKDF_BAD_INPUT_DATA) )
778         mbedtls_snprintf( buf, buflen, "HKDF - Bad input parameters to function" );
779 #endif /* MBEDTLS_HKDF_C */
780 
781 #if defined(MBEDTLS_HMAC_DRBG_C)
782     if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG) )
783         mbedtls_snprintf( buf, buflen, "HMAC_DRBG - Too many random requested in single call" );
784     if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG) )
785         mbedtls_snprintf( buf, buflen, "HMAC_DRBG - Input too large (Entropy + additional)" );
786     if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR) )
787         mbedtls_snprintf( buf, buflen, "HMAC_DRBG - Read/write error in file" );
788     if( use_ret == -(MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED) )
789         mbedtls_snprintf( buf, buflen, "HMAC_DRBG - The entropy source failed" );
790 #endif /* MBEDTLS_HMAC_DRBG_C */
791 
792 #if defined(MBEDTLS_MD2_C)
793     if( use_ret == -(MBEDTLS_ERR_MD2_HW_ACCEL_FAILED) )
794         mbedtls_snprintf( buf, buflen, "MD2 - MD2 hardware accelerator failed" );
795 #endif /* MBEDTLS_MD2_C */
796 
797 #if defined(MBEDTLS_MD4_C)
798     if( use_ret == -(MBEDTLS_ERR_MD4_HW_ACCEL_FAILED) )
799         mbedtls_snprintf( buf, buflen, "MD4 - MD4 hardware accelerator failed" );
800 #endif /* MBEDTLS_MD4_C */
801 
802 #if defined(MBEDTLS_MD5_C)
803     if( use_ret == -(MBEDTLS_ERR_MD5_HW_ACCEL_FAILED) )
804         mbedtls_snprintf( buf, buflen, "MD5 - MD5 hardware accelerator failed" );
805 #endif /* MBEDTLS_MD5_C */
806 
807 #if defined(MBEDTLS_NET_C)
808     if( use_ret == -(MBEDTLS_ERR_NET_SOCKET_FAILED) )
809         mbedtls_snprintf( buf, buflen, "NET - Failed to open a socket" );
810     if( use_ret == -(MBEDTLS_ERR_NET_CONNECT_FAILED) )
811         mbedtls_snprintf( buf, buflen, "NET - The connection to the given server / port failed" );
812     if( use_ret == -(MBEDTLS_ERR_NET_BIND_FAILED) )
813         mbedtls_snprintf( buf, buflen, "NET - Binding of the socket failed" );
814     if( use_ret == -(MBEDTLS_ERR_NET_LISTEN_FAILED) )
815         mbedtls_snprintf( buf, buflen, "NET - Could not listen on the socket" );
816     if( use_ret == -(MBEDTLS_ERR_NET_ACCEPT_FAILED) )
817         mbedtls_snprintf( buf, buflen, "NET - Could not accept the incoming connection" );
818     if( use_ret == -(MBEDTLS_ERR_NET_RECV_FAILED) )
819         mbedtls_snprintf( buf, buflen, "NET - Reading information from the socket failed" );
820     if( use_ret == -(MBEDTLS_ERR_NET_SEND_FAILED) )
821         mbedtls_snprintf( buf, buflen, "NET - Sending information through the socket failed" );
822     if( use_ret == -(MBEDTLS_ERR_NET_CONN_RESET) )
823         mbedtls_snprintf( buf, buflen, "NET - Connection was reset by peer" );
824     if( use_ret == -(MBEDTLS_ERR_NET_UNKNOWN_HOST) )
825         mbedtls_snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
826     if( use_ret == -(MBEDTLS_ERR_NET_BUFFER_TOO_SMALL) )
827         mbedtls_snprintf( buf, buflen, "NET - Buffer is too small to hold the data" );
828     if( use_ret == -(MBEDTLS_ERR_NET_INVALID_CONTEXT) )
829         mbedtls_snprintf( buf, buflen, "NET - The context is invalid, eg because it was free()ed" );
830     if( use_ret == -(MBEDTLS_ERR_NET_POLL_FAILED) )
831         mbedtls_snprintf( buf, buflen, "NET - Polling the net context failed" );
832     if( use_ret == -(MBEDTLS_ERR_NET_BAD_INPUT_DATA) )
833         mbedtls_snprintf( buf, buflen, "NET - Input invalid" );
834 #endif /* MBEDTLS_NET_C */
835 
836 #if defined(MBEDTLS_OID_C)
837     if( use_ret == -(MBEDTLS_ERR_OID_NOT_FOUND) )
838         mbedtls_snprintf( buf, buflen, "OID - OID is not found" );
839     if( use_ret == -(MBEDTLS_ERR_OID_BUF_TOO_SMALL) )
840         mbedtls_snprintf( buf, buflen, "OID - output buffer is too small" );
841 #endif /* MBEDTLS_OID_C */
842 
843 #if defined(MBEDTLS_PADLOCK_C)
844     if( use_ret == -(MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED) )
845         mbedtls_snprintf( buf, buflen, "PADLOCK - Input data should be aligned" );
846 #endif /* MBEDTLS_PADLOCK_C */
847 
848 #if defined(MBEDTLS_PLATFORM_C)
849     if( use_ret == -(MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED) )
850         mbedtls_snprintf( buf, buflen, "PLATFORM - Hardware accelerator failed" );
851     if( use_ret == -(MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED) )
852         mbedtls_snprintf( buf, buflen, "PLATFORM - The requested feature is not supported by the platform" );
853 #endif /* MBEDTLS_PLATFORM_C */
854 
855 #if defined(MBEDTLS_POLY1305_C)
856     if( use_ret == -(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA) )
857         mbedtls_snprintf( buf, buflen, "POLY1305 - Invalid input parameter(s)" );
858     if( use_ret == -(MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE) )
859         mbedtls_snprintf( buf, buflen, "POLY1305 - Feature not available. For example, s part of the API is not implemented" );
860     if( use_ret == -(MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED) )
861         mbedtls_snprintf( buf, buflen, "POLY1305 - Poly1305 hardware accelerator failed" );
862 #endif /* MBEDTLS_POLY1305_C */
863 
864 #if defined(MBEDTLS_RIPEMD160_C)
865     if( use_ret == -(MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED) )
866         mbedtls_snprintf( buf, buflen, "RIPEMD160 - RIPEMD160 hardware accelerator failed" );
867 #endif /* MBEDTLS_RIPEMD160_C */
868 
869 #if defined(MBEDTLS_SHA1_C)
870     if( use_ret == -(MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED) )
871         mbedtls_snprintf( buf, buflen, "SHA1 - SHA-1 hardware accelerator failed" );
872     if( use_ret == -(MBEDTLS_ERR_SHA1_BAD_INPUT_DATA) )
873         mbedtls_snprintf( buf, buflen, "SHA1 - SHA-1 input data was malformed" );
874 #endif /* MBEDTLS_SHA1_C */
875 
876 #if defined(MBEDTLS_SHA256_C)
877     if( use_ret == -(MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED) )
878         mbedtls_snprintf( buf, buflen, "SHA256 - SHA-256 hardware accelerator failed" );
879     if( use_ret == -(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA) )
880         mbedtls_snprintf( buf, buflen, "SHA256 - SHA-256 input data was malformed" );
881 #endif /* MBEDTLS_SHA256_C */
882 
883 #if defined(MBEDTLS_SHA512_C)
884     if( use_ret == -(MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED) )
885         mbedtls_snprintf( buf, buflen, "SHA512 - SHA-512 hardware accelerator failed" );
886     if( use_ret == -(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA) )
887         mbedtls_snprintf( buf, buflen, "SHA512 - SHA-512 input data was malformed" );
888 #endif /* MBEDTLS_SHA512_C */
889 
890 #if defined(MBEDTLS_THREADING_C)
891     if( use_ret == -(MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE) )
892         mbedtls_snprintf( buf, buflen, "THREADING - The selected feature is not available" );
893     if( use_ret == -(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA) )
894         mbedtls_snprintf( buf, buflen, "THREADING - Bad input parameters to function" );
895     if( use_ret == -(MBEDTLS_ERR_THREADING_MUTEX_ERROR) )
896         mbedtls_snprintf( buf, buflen, "THREADING - Locking / unlocking / free failed with error code" );
897 #endif /* MBEDTLS_THREADING_C */
898 
899 #if defined(MBEDTLS_XTEA_C)
900     if( use_ret == -(MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH) )
901         mbedtls_snprintf( buf, buflen, "XTEA - The data input has an invalid length" );
902     if( use_ret == -(MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED) )
903         mbedtls_snprintf( buf, buflen, "XTEA - XTEA hardware accelerator failed" );
904 #endif /* MBEDTLS_XTEA_C */
905     // END generated code
906 
907     if( strlen( buf ) != 0 )
908         return;
909 
910     mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
911 }
912 
913 #else /* MBEDTLS_ERROR_C */
914 
915 #if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
916 
917 /*
918  * Provide an non-function in case MBEDTLS_ERROR_C is not defined
919  */
920 void mbedtls_strerror( int ret, char *buf, size_t buflen )
921 {
922     ((void) ret);
923 
924     if( buflen > 0 )
925         buf[0] = '\0';
926 }
927 
928 #endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
929 
930 #endif /* MBEDTLS_ERROR_C */
931