• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <assert.h>
15 #include <gmssl/hex.h>
16 #include <gmssl/gf128.h>
17 #include <gmssl/error.h>
18 
19 
test_gf128_from_hex(void)20 int test_gf128_from_hex(void)
21 {
22 	char *tests[] = {
23 		"00000000000000000000000000000000",
24 		"00000000000000000000000000000001",
25 		"10000000000000000000000000000000",
26 		"de300f9301a499a965f8bf677e99e80d",
27 		"14b267838ec9ef1bb7b5ce8c19e34bc6",
28 	};
29 	gf128_t a;
30 	int i;
31 
32 	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
33 		a = gf128_from_hex(tests[i]);
34 		if (gf128_equ_hex(a, tests[i]) != 1) {
35 			error_print();
36 			return -1;
37 		}
38 	}
39 
40 	printf("%s() ok\n", __FUNCTION__);
41 	return 1;
42 }
43 
test_gf128_mul2(void)44 int test_gf128_mul2(void)
45 {
46 	char *tests[] = {
47 		"00000000000000000000000000000001",
48 		"de300f9301a499a965f8bf677e99e80d",
49 	};
50 	char *results[] = {
51 		"e1000000000000000000000000000000",
52 		"8e1807c980d24cd4b2fc5fb3bf4cf406",
53 	};
54 	gf128_t a;
55 	int i;
56 
57 	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
58 		a = gf128_from_hex(tests[i]);
59 		a = gf128_mul2(a);
60 		if (gf128_equ_hex(a, results[i]) != 1) {
61 			error_print();
62 			return -1;
63 		}
64 	}
65 
66 	printf("%s() ok\n", __FUNCTION__);
67 	return 1;
68 }
69 
test_gf128_mul(void)70 int test_gf128_mul(void)
71 {
72 	char *hex_a = "de300f9301a499a965f8bf677e99e80d";
73 	char *hex_b = "14b267838ec9ef1bb7b5ce8c19e34bc6";
74 	char *hex_add_a_b = "ca8268108f6d76b2d24d71eb677aa3cb";
75 	char *hex_mul_a_b = "7d87dda57a20b0c51d9743071ab14010";
76 	gf128_t a, b, r;
77 
78 	a = gf128_from_hex(hex_a);
79 	b = gf128_from_hex(hex_b);
80 
81 	r = gf128_add(a, b);
82 	if (gf128_equ_hex(r, hex_add_a_b) != 1) {
83 		error_print();
84 		return -1;
85 	}
86 
87 	r = gf128_mul(a, b);
88 	if (gf128_equ_hex(r, hex_mul_a_b) != 1) {
89 		error_print();
90 		return -1;
91 	}
92 
93 	printf("%s() ok\n", __FUNCTION__);
94 	return 1;
95 }
96 
main(void)97 int main(void)
98 {
99 	if (test_gf128_from_hex() != 1) goto err;
100 	if (test_gf128_mul2() != 1) goto err;
101 	if (test_gf128_mul() != 1) goto err;
102 	printf("%s all tests passed\n", __FILE__);
103 	return 0;
104 err:
105 	error_print();
106 	return -1;
107 
108 }
109