examples/fips_validation: implement JSON CMAC test
[dpdk.git] / examples / fips_validation / fips_validation.c
index 4c3ed80..e8520f5 100644 (file)
@@ -270,12 +270,14 @@ parse_file_type(const char *path)
 {
        const char *tmp = path + strlen(path) - 3;
 
-       if (strstr(tmp, REQ_FILE_PERFIX))
+       if (strstr(tmp, REQ_FILE_PREFIX))
                info.file_type = FIPS_TYPE_REQ;
-       else if (strstr(tmp, RSP_FILE_PERFIX))
+       else if (strstr(tmp, RSP_FILE_PREFIX))
                info.file_type = FIPS_TYPE_RSP;
-       else if (strstr(path, FAX_FILE_PERFIX))
+       else if (strstr(path, FAX_FILE_PREFIX))
                info.file_type = FIPS_TYPE_FAX;
+       else if (strstr(path, JSON_FILE_PREFIX))
+               info.file_type = FIPS_TYPE_JSON;
        else
                return -EINVAL;
 
@@ -311,6 +313,21 @@ fips_test_init(const char *req_file_path, const char *rsp_file_path,
                return -EINVAL;
        }
 
+       if (info.file_type == FIPS_TYPE_JSON) {
+#ifdef RTE_HAS_JANSSON
+               json_error_t error;
+               json_info.json_root = json_loadf(info.fp_rd, 0, &error);
+               if (!json_info.json_root) {
+                       RTE_LOG(ERR, USER1, "Cannot parse json file %s (line %d, column %d)\n",
+                               req_file_path, error.line, error.column);
+                       return -EINVAL;
+               }
+#else /* RTE_HAS_JANSSON */
+               RTE_LOG(ERR, USER1, "No json library configured.\n");
+               return -EINVAL;
+#endif /* RTE_HAS_JANSSON */
+       }
+
        info.fp_wr = fopen(rsp_file_path, "w");
        if (!info.fp_wr) {
                RTE_LOG(ERR, USER1, "Cannot open file %s\n", rsp_file_path);
@@ -329,6 +346,9 @@ fips_test_init(const char *req_file_path, const char *rsp_file_path,
                return -EINVAL;
        }
 
+       if (info.file_type == FIPS_TYPE_JSON)
+               return 0;
+
        if (fips_test_parse_header() < 0) {
                RTE_LOG(ERR, USER1, "Failed parsing header\n");
                return -1;
@@ -344,8 +364,7 @@ fips_test_clear(void)
                fclose(info.fp_rd);
        if (info.fp_wr)
                fclose(info.fp_wr);
-       if (info.one_line_text)
-               free(info.one_line_text);
+       free(info.one_line_text);
        if (info.nb_vec_lines) {
                uint32_t i;
 
@@ -429,6 +448,80 @@ fips_test_write_one_case(void)
                fprintf(info.fp_wr, "%s\n", info.vec[i]);
 }
 
+#ifdef RTE_HAS_JANSSON
+int
+fips_test_parse_one_json_vector_set(void)
+{
+       json_t *algo_obj = json_object_get(json_info.json_vector_set, "algorithm");
+       const char *algo_str = json_string_value(algo_obj);
+
+       /* Vector sets contain the algorithm type, and nothing else we need. */
+       if (strstr(algo_str, "AES-GCM"))
+               info.algo = FIPS_TEST_ALGO_AES_GCM;
+       if (strstr(algo_str, "HMAC"))
+               info.algo = FIPS_TEST_ALGO_HMAC;
+       else
+               return -EINVAL;
+
+       return 0;
+}
+
+int
+fips_test_parse_one_json_group(void)
+{
+       int ret, i;
+       json_int_t val;
+       json_t *param;
+
+       if (info.interim_callbacks) {
+               char json_value[256];
+               for (i = 0; info.interim_callbacks[i].key != NULL; i++) {
+                       param = json_object_get(json_info.json_test_group,
+                                       info.interim_callbacks[i].key);
+                       val = json_integer_value(param);
+                       snprintf(json_value, 255, "%"JSON_INTEGER_FORMAT, val);
+                       /* First argument is blank because the key
+                        * is not included in the string being parsed.
+                        */
+                       ret = info.interim_callbacks[i].cb(
+                               "", json_value,
+                               info.interim_callbacks[i].val
+                       );
+                       if (ret < 0)
+                               return ret;
+               }
+       }
+
+       return 0;
+}
+
+int
+fips_test_parse_one_json_case(void)
+{
+       uint32_t i;
+       int ret = 0;
+       json_t *param;
+
+       for (i = 0; info.callbacks[i].key != NULL; i++) {
+               param = json_object_get(json_info.json_test_case, info.callbacks[i].key);
+               if (param) {
+                       strcpy(info.one_line_text, json_string_value(param));
+                       /* First argument is blank because the key
+                        * is not included in the string being parsed.
+                        */
+                       ret = info.callbacks[i].cb(
+                               "", info.one_line_text,
+                               info.callbacks[i].val
+                       );
+                       if (ret < 0)
+                               return ret;
+               }
+       }
+
+       return 0;
+}
+#endif /* RTE_HAS_JANSSON */
+
 static int
 parser_read_uint64_hex(uint64_t *value, const char *p)
 {
@@ -650,6 +743,22 @@ parser_read_uint32(uint32_t *value, char *p)
        return 0;
 }
 
+int
+parser_read_uint16(uint16_t *value, const char *p)
+{
+       uint64_t val = 0;
+       int ret = parser_read_uint64(&val, p);
+
+       if (ret < 0)
+               return ret;
+
+       if (val > UINT16_MAX)
+               return -ERANGE;
+
+       *value = val;
+       return 0;
+}
+
 void
 parse_write_hex_str(struct fips_val *src)
 {