Debian Codegraph @

crt/s2n/tests/testlib/s2n_nist_kats.h

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 *
15 * Modified from PQCgenKAT_kem.c
16 * Created by Bassham, Lawrence E (Fed) on 8/29/17.
17 * Copyright © 2017 Bassham, Lawrence E (Fed). All rights reserved.
18 */
19
20 #pragma once
21
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #define MAX_MARKER_LEN 50
27 #define NUM_OF_KATS 100
28
29 /*
30 * ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
31 */
32
33 static inline int FindMarker(FILE *infile, const char *marker)
34 {
35 int8_t line[MAX_MARKER_LEN];
36 uint32_t i, len;
37
38 len = (int) strlen(marker);
39 if (len > (MAX_MARKER_LEN - 1)) {
40 len = MAX_MARKER_LEN - 1;
41 }
42
43 for (i = 0; i < len; i++) {
44 if ((line[i] = fgetc(infile)) == EOF) {
45 return -1;
46 }
47 }
48 line[len] = '\0';
49
50 while (1) {
51 if (!strncmp((char *) line, marker, len)) {
52 return 0;
53 }
54
55 for (i = 0; i < len - 1; i++) {
56 line[i] = line[i + 1];
57 }
58 if ((line[len - 1] = fgetc(infile)) == EOF) {
59 return -1;
60 }
61 line[len] = '\0';
62 }
63 }
64
65 /*
66 * ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
67 */
68
69 static inline int ReadHex(FILE *infile, uint8_t *buf, uint32_t len, const char *str)
70 {
71 int ch;
72 int started = 0;
73 uint8_t ich;
74
75 if (0 == len) {
76 buf[0] = 0x00;
77 return 0;
78 }
79
80 memset(buf, 0x00, len);
81
82 if (FindMarker(infile, str) == -1) {
83 return -1;
84 }
85
86 while ((ch = fgetc(infile)) != EOF) {
87 if (!isxdigit(ch)) {
88 if (!started) {
89 if (ch == '\n')
90 break;
91 else
92 continue;
93 } else
94 break;
95 }
96 started = 1;
97
98 if ((ch >= '0') && (ch <= '9')) {
99 ich = ch - '0';
100 } else if ((ch >= 'A') && (ch <= 'F')) {
101 ich = ch - 'A' + 10;
102 } else if ((ch >= 'a') && (ch <= 'f')) {
103 ich = ch - 'a' + 10;
104 } else {
105 /* shouldn't ever get here */
106 ich = 0;
107 }
108
109 for (uint32_t i = 0; i < len - 1; i++) {
110 buf[i] = (buf[i] << 4) | (buf[i + 1] >> 4);
111 }
112 buf[len - 1] = (buf[len - 1] << 4) | ich;
113 }
114
115 return 0;
116 }