|
25 | 25 | // local copy of function from mariadb interface |
26 | 26 | static int32_t convert_hexstring_to_byte_array(char *source_str, uint8_t *dest_buffer, uint16_t max_len) |
27 | 27 | { // https://stackoverflow.com/questions/3408706/hexadecimal-string-to-byte-array-in-c/56247335#56247335 |
28 | | - int offset; |
29 | | - unsigned int read_byte; |
30 | | - uint32_t data_len = 0; |
31 | | - |
32 | | - if (dest_buffer == NULL || source_str == NULL) |
33 | | - { |
34 | | - return CRYPTO_LIB_ERROR; |
35 | | - } |
36 | | - |
37 | | - uint32_t source_len = (strlen(source_str) / 2); |
38 | | - if (source_len > max_len) |
39 | | - { |
40 | | - return CRYPTO_LIB_ERROR; |
41 | | - } |
42 | | - |
43 | | - while (sscanf(source_str, " %02x%n", &read_byte, &offset) == 1) |
44 | | - { |
45 | | - dest_buffer[data_len++] = read_byte; |
46 | | - source_str += offset; |
47 | | - } |
48 | | - |
49 | | - return CRYPTO_LIB_SUCCESS; |
| 28 | + int offset; |
| 29 | + unsigned int read_byte; |
| 30 | + uint32_t data_len = 0; |
| 31 | + |
| 32 | + if (dest_buffer == NULL || source_str == NULL) |
| 33 | + { |
| 34 | + return CRYPTO_LIB_ERROR; |
| 35 | + } |
| 36 | + |
| 37 | + uint32_t source_len = (strlen(source_str) / 2); |
| 38 | + if (source_len > max_len) |
| 39 | + { |
| 40 | + return CRYPTO_LIB_ERROR; |
| 41 | + } |
| 42 | + |
| 43 | + while (sscanf(source_str, " %02x%n", &read_byte, &offset) == 1) |
| 44 | + { |
| 45 | + dest_buffer[data_len++] = read_byte; |
| 46 | + source_str += offset; |
| 47 | + } |
| 48 | + |
| 49 | + return CRYPTO_LIB_SUCCESS; |
50 | 50 | } |
51 | 51 |
|
52 | 52 | UTEST(CRYPTO_MDB, HEXSTRING_TO_BYTE_ARRAY) |
53 | 53 | { |
54 | | - int32_t status = CRYPTO_LIB_SUCCESS; |
55 | | - uint16_t max_len = IV_SIZE; |
56 | | - uint8_t *dest_buffer = malloc(IV_SIZE); |
57 | | - uint8_t *dest_buffer_null = NULL; |
58 | | - |
59 | | - // Failure Case, wrong source length |
60 | | - char *source_str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
61 | | - status = convert_hexstring_to_byte_array(source_str, dest_buffer, max_len); |
62 | | - printf("Status: %d\n", status); |
63 | | - ASSERT_EQ(status, CRYPTO_LIB_ERROR); |
64 | | - |
65 | | - // Failure Case, null dest_buffer |
66 | | - source_str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
67 | | - status = convert_hexstring_to_byte_array(source_str, dest_buffer_null, max_len); |
68 | | - printf("Status: %d\n", status); |
69 | | - ASSERT_EQ(status, CRYPTO_LIB_ERROR); |
70 | | - |
71 | | - // Failure Case, null source_str |
72 | | - source_str = NULL; |
73 | | - status = convert_hexstring_to_byte_array(source_str, dest_buffer, max_len); |
74 | | - printf("Status: %d\n", status); |
75 | | - ASSERT_EQ(status, CRYPTO_LIB_ERROR); |
76 | | - |
77 | | - // Success case, correct length source string and max |
78 | | - source_str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
79 | | - status = convert_hexstring_to_byte_array(source_str, dest_buffer, max_len); |
80 | | - printf("Status: %d\n", status); |
81 | | - ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); |
82 | | - |
83 | | - free(dest_buffer); |
| 54 | + int32_t status = CRYPTO_LIB_SUCCESS; |
| 55 | + uint16_t max_len = IV_SIZE; |
| 56 | + uint8_t *dest_buffer = malloc(IV_SIZE); |
| 57 | + uint8_t *dest_buffer_null = NULL; |
| 58 | + |
| 59 | + // Failure Case, wrong source length |
| 60 | + char *source_str = |
| 61 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 62 | + status = convert_hexstring_to_byte_array(source_str, dest_buffer, max_len); |
| 63 | + printf("Status: %d\n", status); |
| 64 | + ASSERT_EQ(status, CRYPTO_LIB_ERROR); |
| 65 | + |
| 66 | + // Failure Case, null dest_buffer |
| 67 | + source_str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 68 | + status = convert_hexstring_to_byte_array(source_str, dest_buffer_null, max_len); |
| 69 | + printf("Status: %d\n", status); |
| 70 | + ASSERT_EQ(status, CRYPTO_LIB_ERROR); |
| 71 | + |
| 72 | + // Failure Case, null source_str |
| 73 | + source_str = NULL; |
| 74 | + status = convert_hexstring_to_byte_array(source_str, dest_buffer, max_len); |
| 75 | + printf("Status: %d\n", status); |
| 76 | + ASSERT_EQ(status, CRYPTO_LIB_ERROR); |
| 77 | + |
| 78 | + // Success case, correct length source string and max |
| 79 | + source_str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
| 80 | + status = convert_hexstring_to_byte_array(source_str, dest_buffer, max_len); |
| 81 | + printf("Status: %d\n", status); |
| 82 | + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); |
| 83 | + |
| 84 | + free(dest_buffer); |
84 | 85 | } |
85 | 86 |
|
86 | 87 | // #ifdef KMC_MDB_RH |
|
0 commit comments