blob: 15e6c55210699181ce3805122431b51b06986a85 [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27#include <stdint.h>
28#include "testframework.h"
29
30#define getsettest(bigint, _short) \
31do { \
32 int32_t bi = 0; \
33 TEE_Result result; \
34 TEE_BigIntConvertFromS32((bigint), (_short)); \
35 TB_ASSERT_EQ_SHORT((bigint), (_short)); \
36 result = TEE_BigIntConvertToS32(&bi, (bigint)); \
37 TB_ASSERT_MSG(result == TEE_SUCCESS, "Failed getting short\n"); \
38 TB_ASSERT_INT_EQ(bi, (_short)); \
39} while (0)
40
41static void test_BigInt_getsetShort(void)
42{
43 int32_t b;
44 TEE_Result res;
45
46 TB_INFO("Testing GetShort and SetShort");
47
48 DEF_BIGINT(a, 512);
49
50 getsettest(a, 1);
51 getsettest(a, -1);
52 getsettest(a, 123);
53 getsettest(a, -123);
54 getsettest(a, 0x7FFFFFFF);
55 getsettest(a, (int)0x80000000);
56 getsettest(a, (int)0xFFFFFFFF);
57 getsettest(a, 0);
58
59 /* Testing too large BigInt */
60 TEE_BigIntConvertFromString(a, "0x1FFFFFFFFF");
61 res = TEE_BigIntConvertToS32(&b, a);
62 TB_ASSERT(res == TEE_ERROR_OVERFLOW);
63
64 mpa_wipe((mpanum) a);
65
66 TEE_BigIntConvertFromString(a, "-0x1FFFFFFFFF");
67 res = TEE_BigIntConvertToS32(&b, a);
68 TB_ASSERT(res == TEE_ERROR_OVERFLOW);
69
70 DEL_BIGINT(a);
71
72}
73
74static void test_BigInt_getsetOctetString(void)
75{
76 const uint8_t os1[] = { 1, 2, 3, 4 };
77 const uint8_t os2[] = { 1, 2, 3, 4, 5 };
78 const uint8_t os3[] = { 0, 1, 2, 3, 4 };
79 const uint8_t os4[] = { 0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88 };
80 uint8_t os_res[10];
81 TEE_Result res;
82 size_t os_len;
83
84 DEF_BIGINT(a, 512);
85 DEF_BIGINT(b, 512);
86
87 TB_INFO("Testing Convert to and from OctetString");
88
89 /* Test with 0x0102030405 */
90 TEE_BigIntConvertFromString(a, "0x0102030405");
91 os_len = sizeof(os_res);
92 res = TEE_BigIntConvertToOctetString(os_res, &os_len, a);
93 TB_ASSERT(res == TEE_SUCCESS);
94 TB_ASSERT(sizeof(os2) == os_len
95 && TEE_MemCompare(os2, os_res, sizeof(os2)) == 0);
96
97 res = TEE_BigIntConvertFromOctetString(b, os_res, os_len, 1);
98 TB_ASSERT(res == TEE_SUCCESS);
99 TB_ASSERT(TEE_BigIntCmp(a, b) == 0);
100
101 /* Test with 0x11224455667788 */
102 TEE_BigIntConvertFromString(a, "0x11224455667788");
103 os_len = sizeof(os_res);
104 res = TEE_BigIntConvertToOctetString(os_res, &os_len, a);
105 TB_ASSERT(res == TEE_SUCCESS);
106 TB_ASSERT(sizeof(os4) == os_len
107 && TEE_MemCompare(os4, os_res, sizeof(os4)) == 0);
108
109 res = TEE_BigIntConvertFromOctetString(b, os_res, os_len, 1);
110 TB_ASSERT(res == TEE_SUCCESS);
111 TB_ASSERT(TEE_BigIntCmp(a, b) == 0);
112
113 /* Test with static octet strings */
114 res = TEE_BigIntConvertFromOctetString(a, os1, sizeof(os1), 1);
115 TB_ASSERT(res == TEE_SUCCESS);
116
117 os_len = sizeof(os_res);
118 res = TEE_BigIntConvertToOctetString(os_res, &os_len, a);
119 TB_ASSERT(res == TEE_SUCCESS);
120 TB_ASSERT(sizeof(os1) == os_len
121 && TEE_MemCompare(os1, os_res, sizeof(os1)) == 0);
122
123 res = TEE_BigIntConvertFromOctetString(b, os3, sizeof(os3), 1);
124 TB_ASSERT(res == TEE_SUCCESS);
125 TB_ASSERT(TEE_BigIntCmp(a, b) == 0);
126
127 DEL_BIGINT(a);
128 DEL_BIGINT(b);
129}
130
131void tb_conv(void)
132{
133 const char *TEST_NAME = "Conversion functions";
134
135 TB_HEADER(TEST_NAME);
136
137 test_BigInt_getsetShort();
138 test_BigInt_getsetOctetString();
139
140 TB_FOOTER(TEST_NAME);
141
142}