Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame^] | 1 | /* |
| 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 "tee_internal_api.h" |
| 28 | #include "testframework.h" |
| 29 | |
| 30 | /* |
| 31 | * TEE_BigIntConvertFromString |
| 32 | * |
| 33 | * !! Not part of the spec !! |
| 34 | * |
| 35 | * Assigns dest the value of the src, where src is a zero-terminated character |
| 36 | * string. If the src starts with a valid number, the valid part will be |
| 37 | * converted and the rest of the src will not be parsed further. src is assumed |
| 38 | * to be in base 16. Returns -1 if the src was malformed, and the number of base |
| 39 | * digits converted (not including leading zeros) if the conversion was OK. If |
| 40 | * the src is a null-ptr we return -1. If the src is empty, we don't touch dest |
| 41 | * and just returns 0. If the src only consists of white spaces, we set dest to |
| 42 | * zero returns 0. |
| 43 | */ |
| 44 | int TEE_BigIntConvertFromString(TEE_BigInt *dest, const char *src) |
| 45 | { |
| 46 | mpanum mpa_dest = (mpa_num_base *) dest; |
| 47 | |
| 48 | return mpa_set_str(mpa_dest, src); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * TEE_BigIntConvertToString |
| 53 | * |
| 54 | * !! Not part of the spec !! |
| 55 | * |
| 56 | * Prints a zero-terminated string representation of src into dest. The need |
| 57 | * length of dest is the space needed to print src plus additional chars for the |
| 58 | * minus sign and the terminating '\0' char. If grouping is used (!= 0), we pad |
| 59 | * the number string with zeros to the left, up to the current group size. A |
| 60 | * pointer to str is returned. If something went wrong, we return 0. |
| 61 | * |
| 62 | * mode is one of the following: |
| 63 | * TEE_MATHAPI_PRINT_MODE_HEX_LC : output in lower case hex |
| 64 | * TEE_MATHAPI_PRINT_MODE_DEC : output in decimal |
| 65 | */ |
| 66 | char *TEE_BigIntConvertToString(char *dest, int mode, int groupsize, |
| 67 | const TEE_BigInt *src) |
| 68 | { |
| 69 | mpanum mpa_src = (mpa_num_base *) src; |
| 70 | |
| 71 | if (dest == 0) { |
| 72 | dest = TEE_Malloc(mpa_get_str_size(), 0); |
| 73 | if (dest == 0) |
| 74 | return 0; |
| 75 | } |
| 76 | return mpa_get_str(dest, mode, groupsize, mpa_src); |
| 77 | } |
| 78 | |
| 79 | static uint8_t myrand(void) |
| 80 | { |
| 81 | static uint32_t lcg_state = 17; |
| 82 | static const uint32_t a = 1664525; |
| 83 | static const uint32_t c = 1013904223; |
| 84 | |
| 85 | lcg_state = (a * lcg_state + c); |
| 86 | return (uint8_t) (lcg_state >> 24); |
| 87 | } |
| 88 | |
| 89 | static int getrand(int min, int max) |
| 90 | { |
| 91 | return (myrand() % (max - min)) + min; |
| 92 | } |
| 93 | |
| 94 | static char nibble_to_char(int c) |
| 95 | { |
| 96 | if (c < 10) |
| 97 | return '0' + (char)c; |
| 98 | c -= 10; |
| 99 | return 'A' + (char)c; |
| 100 | } |
| 101 | |
| 102 | static char getrandchar(int base) |
| 103 | { |
| 104 | return nibble_to_char(getrand(0, base == 16 ? 15 : 9)); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Function: tb_get_random_str |
| 109 | * |
| 110 | * Sets str to a random number in base 16 of max length |
| 111 | * MAX_RAND_DIGITS. str must point to a memory arear which is |
| 112 | * at least MAX_RAND_STR_SIZE big. |
| 113 | * if allow_neg is 1 we can generate negative numbers |
| 114 | */ |
| 115 | void tb_get_random_str(char *str, int allow_neg) |
| 116 | { |
| 117 | char *ptr; |
| 118 | int r; |
| 119 | int neg = 0; |
| 120 | int j; |
| 121 | char c; |
| 122 | |
| 123 | ptr = str; |
| 124 | if (allow_neg) { |
| 125 | neg = getrand(0, 1); |
| 126 | if (neg) |
| 127 | *ptr++ = '-'; |
| 128 | } |
| 129 | r = getrand(0, MAX_RAND_DIGITS); |
| 130 | if (r == 0 && neg) { |
| 131 | *str++ = '0'; |
| 132 | *str = '\0'; |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | for (j = 0; j < r; j++) { |
| 137 | c = getrandchar(16); |
| 138 | |
| 139 | /* avoid leading zeros since that is difficult to test later */ |
| 140 | if (j == 0 && c == '0') { |
| 141 | while ((c = getrandchar(16)) == '0') |
| 142 | ; |
| 143 | } |
| 144 | *ptr++ = c; |
| 145 | } |
| 146 | *ptr++ = '\0'; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Function: tb_set_random_value |
| 151 | * |
| 152 | * Sets a to a random value and returns the representation in str (in base |
| 153 | * "base") |
| 154 | */ |
| 155 | void tb_set_random_value(TEE_BigInt *a, char *str, int allow_neg) |
| 156 | { |
| 157 | do { |
| 158 | tb_get_random_str(str, allow_neg); |
| 159 | } while (*str == '\0'); |
| 160 | |
| 161 | TEE_BigIntConvertFromString(a, str); |
| 162 | } |
| 163 | |
| 164 | static uint32_t mempool_u32[mpa_scratch_mem_size_in_U32(10, 2048)]; |
| 165 | mpa_scratch_mem mempool = (void *)&mempool_u32; |
| 166 | |
| 167 | void tb_main(void) |
| 168 | { |
| 169 | mpa_init_scratch_mem(mempool, 10, 2048); |
| 170 | |
| 171 | tb_var(); |
| 172 | tb_conv(); |
| 173 | tb_cmp(); |
| 174 | tb_addsub(); |
| 175 | tb_mul(); |
| 176 | tb_div(); |
| 177 | tb_modulus(); |
| 178 | tb_prime(); |
| 179 | |
| 180 | ALL_PASSED; |
| 181 | } |