forked from cisco/libacvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acvp_util.c
184 lines (160 loc) · 5.07 KB
/
acvp_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*****************************************************************************
* Copyright (c) 2016, Cisco Systems, Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "acvp.h"
#include "acvp_lcl.h"
#ifdef USE_MURL
#include <murl/murl.h>
#else
#include <curl/curl.h>
#endif
static int acvp_char_to_int(char ch);
/*
* This is a rudimentary logging facility for libacvp.
* We will need more when moving beyond the PoC phase.
*/
void acvp_log_msg (ACVP_CTX *ctx, const char *format, ...)
{
va_list arguments;
char tmp[1024];
if (ctx && ctx->test_progress_cb) {
/*
* Pull the arguments from the stack and invoke
* the logger function
*/
va_start(arguments, format);
vsnprintf(tmp, 1023, format, arguments);
ctx->test_progress_cb(tmp);
va_end(arguments);
fflush(stdout);
}
}
/*
* Curl requires a cleanup function to be invoked when done.
* We must extend this to our user, which is done here.
* Our users shouldn't have to include curl.h.
*/
void acvp_cleanup(void)
{
curl_global_cleanup();
}
/*
* This function is used to locate the callback function that's needed
* when a particular crypto operation is needby by libacvp.
*/
ACVP_CAPS_LIST* acvp_locate_cap_entry(ACVP_CTX *ctx, ACVP_SYM_CIPHER cipher)
{
ACVP_CAPS_LIST *cap;
if (!ctx->caps_list) {
return NULL;
}
cap = ctx->caps_list;
while (cap) {
if (cap->cap.sym_cap->cipher == cipher) {
return cap;
}
cap = cap->next;
}
return NULL;
}
//TODO: the next 3 functions could possibly be replaced using OpenSSL bignum,
// which has support for reading/writing hex strings. But do we want
// to include a new dependency on OpenSSL?
/*
* Convert a byte array from source to a hexadecimal string which is
* stored in the destination.
*/
ACVP_RESULT acvp_bin_to_hexstr(const unsigned char *src,
unsigned int src_len,
unsigned char *dest)
{
int i, j;
unsigned char nibb_a, nibb_b;
unsigned char hex_chars[] = "0123456789ABCDEF";
for (i = 0, j = 0; i < src_len; i++, j += 2) {
nibb_a = *src >> 4; /* Get first half of byte */
nibb_b = *src & 0x0f; /* Get second half of byte */
*dest = hex_chars[nibb_a];
*(dest + 1) = hex_chars[nibb_b];
dest += 2;
src++;
}
*dest = '\0';
return ACVP_SUCCESS;
}
/*
* Convert a source hexadecimal string to a byte array which is stored
* in the destination.
* TODO: Enable the function to handle odd number of hex characters
*/
ACVP_RESULT acvp_hexstr_to_bin(const unsigned char *src, unsigned char *dest)
{
int src_len;
int byte_a, byte_b;
int is_odd = 0;
if (!src || !dest) {
return 0;
}
src_len = (int)strlen((char*)src);
if (src_len & 1) {
is_odd = 1;
}
if (!is_odd) {
while (*src && src[1]) {
byte_a = acvp_char_to_int((char)*src) << 4; /* Shift to left half of byte */
byte_b = acvp_char_to_int(*(src + 1));
*dest = byte_a + byte_b; /* Combine left half with right half */
dest++;
src += 2;
}
} else {
return 1;
}
return ACVP_SUCCESS;
}
/*
* Local - helper function for acvp_hexstring_to_bytes
* Used to convert a hexadecimal character to it's byte
* representation.
*/
static int acvp_char_to_int(char ch)
{
int ch_i;
if (ch >= '0' && ch <= '9') {
ch_i = ch - '0';
}
else if (ch >= 'A' && ch <= 'F') {
ch_i = ch - 'A' + 10;
}
else if (ch >= 'a' && ch <= 'f') {
ch_i = ch - 'a' + 10;
}
else {
ch_i = 0;
}
return ch_i;
}