1 /*******************************************************************************
2   Copyright (c) 2020, Intel Corporation
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions are met:
6 
7       * Redistributions of source code must retain the above copyright notice,
8         this list of conditions and the following disclaimer.
9       * Redistributions in binary form must reproduce the above copyright
10         notice, this list of conditions and the following disclaimer in the
11         documentation and/or other materials provided with the distribution.
12       * Neither the name of Intel Corporation nor the names of its contributors
13         may be used to endorse or promote products derived from this software
14         without specific prior written permission.
15 
16   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27 
28 /* Use XSI-compliant portable version of strerror_r() */
29 #undef _GNU_SOURCE
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 
35 #include "intel-ipsec-mb.h"
36 #include "error.h"
37 
38 #ifndef LINUX
39 #pragma warning(disable : 4996)
40 #endif
41 
42 IMB_DLL_LOCAL int imb_errno;
43 
imb_get_errno(IMB_MGR * mb_mgr)44 int imb_get_errno(IMB_MGR *mb_mgr)
45 {
46         /* try get IMB_MGR error status first */
47         if (mb_mgr != NULL)
48                 return mb_mgr->imb_errno;
49 
50         /* otherwise return global error status */
51         return imb_errno;
52 }
53 
54 const char *
imb_get_strerror(int errnum)55 imb_get_strerror(int errnum)
56 {
57 	if (errnum >= IMB_ERR_MAX)
58 		return "Unknown error";
59 
60         switch (errnum){
61         case 0:
62                 return "No error";
63         case IMB_ERR_NULL_MBMGR:
64                 return "Null IMB_MGR pointer";
65         case IMB_ERR_JOB_NULL_SRC:
66                 return "Null source pointer";
67         case IMB_ERR_JOB_NULL_DST:
68                 return "Null destination pointer";
69         case IMB_ERR_JOB_NULL_KEY:
70                 return "Null key pointer";
71         case IMB_ERR_JOB_NULL_IV:
72                 return "Null Initialization Vector (IV) pointer";
73         case IMB_ERR_JOB_NULL_AUTH:
74                 return "Null authentication tag output pointer";
75         case IMB_ERR_JOB_NULL_AAD:
76                 return "Null Additional Authenticated Data (AAD) pointer";
77         case IMB_ERR_JOB_CIPH_LEN:
78                 return "Invalid cipher message length";
79         case IMB_ERR_JOB_AUTH_LEN:
80                 return "Invalid authentication message length";
81         case IMB_ERR_JOB_IV_LEN:
82                 return "Invalid Initialization Vector (IV) length";
83         case IMB_ERR_JOB_KEY_LEN:
84                 return "Invalid key length";
85         case IMB_ERR_JOB_AUTH_TAG_LEN:
86                 return "Invalid authentication tag length";
87         case IMB_ERR_JOB_AAD_LEN:
88                 return "Invalid Additional Authenticated Data (AAD) length";
89         case IMB_ERR_JOB_SRC_OFFSET:
90                 return "Invalid source offset";
91         case IMB_ERR_JOB_CHAIN_ORDER:
92                 return "Invalid chain order";
93         case IMB_ERR_CIPH_MODE:
94                 return "Invalid cipher mode";
95         case IMB_ERR_HASH_ALGO:
96                 return "Invalid hash algorithm";
97         case IMB_ERR_JOB_NULL_AUTH_KEY:
98                 return "Null pointer to authentication key";
99         default:
100                 return strerror(errnum);
101         }
102 }
103