Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/ecp.h> |
| 3 | END_HEADER |
| 4 | |
| 5 | BEGIN_DEPENDENCIES |
| 6 | depends_on:POLARSSL_ECP_C:POLARSSL_BIGNUM_C |
| 7 | END_DEPENDENCIES |
| 8 | |
| 9 | BEGIN_CASE |
| 10 | ecp_small_add:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c |
| 11 | { |
| 12 | ecp_group grp; |
| 13 | ecp_point A, B, C; |
| 14 | |
| 15 | ecp_group_init( &grp ); |
| 16 | ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C ); |
| 17 | |
| 18 | TEST_ASSERT( ecp_group_read_string( &grp, 10, |
| 19 | "47", "4", "17", "42", "13" ) == 0 ); |
| 20 | |
| 21 | if( {a_zero} ) |
| 22 | ecp_set_zero( &A ); |
| 23 | else |
| 24 | TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 ); |
| 25 | |
| 26 | if( {b_zero} ) |
| 27 | ecp_set_zero( &B ); |
| 28 | else |
| 29 | TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 ); |
| 30 | |
| 31 | TEST_ASSERT( ecp_add( &grp, &C, &A, &B ) == 0 ); |
| 32 | |
| 33 | if( {c_zero} ) |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 34 | TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 35 | else |
| 36 | { |
| 37 | TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 ); |
| 38 | TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 ); |
| 39 | } |
| 40 | |
| 41 | TEST_ASSERT( ecp_add( &grp, &C, &B, &A ) == 0 ); |
| 42 | |
| 43 | if( {c_zero} ) |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 44 | TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 45 | else |
| 46 | { |
| 47 | TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 ); |
| 48 | TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 ); |
| 49 | } |
| 50 | |
| 51 | ecp_group_free( &grp ); |
| 52 | ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C ); |
| 53 | } |
| 54 | END_CASE |
| 55 | |
| 56 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 57 | ecp_small_sub:a_zero:x_a:y_a:b_zero:x_b:y_b:c_zero:x_c:y_c |
| 58 | { |
| 59 | ecp_group grp; |
| 60 | ecp_point A, B, C; |
| 61 | |
| 62 | ecp_group_init( &grp ); |
| 63 | ecp_point_init( &A ); ecp_point_init( &B ); ecp_point_init( &C ); |
| 64 | |
| 65 | TEST_ASSERT( ecp_group_read_string( &grp, 10, |
| 66 | "47", "4", "17", "42", "13" ) == 0 ); |
| 67 | |
| 68 | if( {a_zero} ) |
| 69 | ecp_set_zero( &A ); |
| 70 | else |
| 71 | TEST_ASSERT( ecp_point_read_string( &A, 10, {x_a}, {y_a} ) == 0 ); |
| 72 | |
| 73 | if( {b_zero} ) |
| 74 | ecp_set_zero( &B ); |
| 75 | else |
| 76 | TEST_ASSERT( ecp_point_read_string( &B, 10, {x_b}, {y_b} ) == 0 ); |
| 77 | |
| 78 | TEST_ASSERT( ecp_sub( &grp, &C, &A, &B ) == 0 ); |
| 79 | |
| 80 | if( {c_zero} ) |
| 81 | TEST_ASSERT( mpi_cmp_int( &C.Z, 0 ) == 0 ); |
| 82 | else |
| 83 | { |
| 84 | TEST_ASSERT( mpi_cmp_int( &C.X, {x_c} ) == 0 ); |
| 85 | TEST_ASSERT( mpi_cmp_int( &C.Y, {y_c} ) == 0 ); |
| 86 | } |
| 87 | |
| 88 | ecp_group_free( &grp ); |
| 89 | ecp_point_free( &A ); ecp_point_free( &B ); ecp_point_free( &C ); |
| 90 | } |
| 91 | END_CASE |
| 92 | |
| 93 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 94 | ecp_small_mul:m:r_zero:x_r:y_r:ret |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 95 | { |
| 96 | ecp_group grp; |
| 97 | ecp_point R; |
| 98 | mpi m; |
| 99 | |
| 100 | ecp_group_init( &grp ); |
| 101 | ecp_point_init( &R ); |
| 102 | mpi_init( &m ); |
| 103 | |
| 104 | TEST_ASSERT( ecp_group_read_string( &grp, 10, |
| 105 | "47", "4", "17", "42", "13" ) == 0 ); |
| 106 | |
| 107 | TEST_ASSERT( mpi_lset( &m, {m} ) == 0 ); |
| 108 | |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 109 | TEST_ASSERT( ecp_mul( &grp, &R, &m, &grp.G ) == {ret} ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 110 | |
| 111 | if( {r_zero} ) |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 112 | TEST_ASSERT( mpi_cmp_int( &R.Z, 0 ) == 0 ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 113 | else |
| 114 | { |
| 115 | TEST_ASSERT( mpi_cmp_int( &R.X, {x_r} ) == 0 ); |
| 116 | TEST_ASSERT( mpi_cmp_int( &R.Y, {y_r} ) == 0 ); |
| 117 | } |
| 118 | |
| 119 | ecp_group_free( &grp ); |
| 120 | ecp_point_free( &R ); |
| 121 | mpi_free( &m ); |
| 122 | } |
| 123 | END_CASE |
| 124 | |
| 125 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 126 | ecp_small_check_pub:x:y:z:ret |
| 127 | { |
| 128 | ecp_group grp; |
| 129 | ecp_point P; |
| 130 | |
| 131 | ecp_group_init( &grp ); |
| 132 | ecp_point_init( &P ); |
| 133 | |
| 134 | TEST_ASSERT( ecp_group_read_string( &grp, 10, |
| 135 | "47", "4", "17", "42", "13" ) == 0 ); |
| 136 | |
| 137 | TEST_ASSERT( mpi_lset( &P.X, {x} ) == 0 ); |
| 138 | TEST_ASSERT( mpi_lset( &P.Y, {y} ) == 0 ); |
| 139 | TEST_ASSERT( mpi_lset( &P.Z, {z} ) == 0 ); |
| 140 | |
| 141 | TEST_ASSERT( ecp_check_pubkey( &grp, &P ) == {ret} ); |
| 142 | |
| 143 | ecp_group_free( &grp ); |
| 144 | ecp_point_free( &P ); |
| 145 | } |
| 146 | END_CASE |
| 147 | |
| 148 | BEGIN_CASE |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 149 | ecp_test_vect:id:dA:xA:yA:dB:xB:yB:xZ:yZ |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 150 | { |
| 151 | ecp_group grp; |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 152 | ecp_point R; |
| 153 | mpi dA, xA, yA, dB, xB, yB, xZ, yZ; |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 154 | |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 155 | ecp_group_init( &grp ); ecp_point_init( &R ); |
| 156 | mpi_init( &dA ); mpi_init( &xA ); mpi_init( &yA ); mpi_init( &dB ); |
| 157 | mpi_init( &xB ); mpi_init( &yB ); mpi_init( &xZ ); mpi_init( &yZ ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 158 | |
| 159 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 160 | |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 161 | TEST_ASSERT( ecp_check_pubkey( &grp, &grp.G ) == 0 ); |
| 162 | |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 163 | TEST_ASSERT( mpi_read_string( &dA, 16, {dA} ) == 0 ); |
| 164 | TEST_ASSERT( mpi_read_string( &xA, 16, {xA} ) == 0 ); |
| 165 | TEST_ASSERT( mpi_read_string( &yA, 16, {yA} ) == 0 ); |
| 166 | TEST_ASSERT( mpi_read_string( &dB, 16, {dB} ) == 0 ); |
| 167 | TEST_ASSERT( mpi_read_string( &xB, 16, {xB} ) == 0 ); |
| 168 | TEST_ASSERT( mpi_read_string( &yB, 16, {yB} ) == 0 ); |
| 169 | TEST_ASSERT( mpi_read_string( &xZ, 16, {xZ} ) == 0 ); |
| 170 | TEST_ASSERT( mpi_read_string( &yZ, 16, {yZ} ) == 0 ); |
| 171 | |
| 172 | TEST_ASSERT( ecp_mul( &grp, &R, &dA, &grp.G ) == 0 ); |
| 173 | TEST_ASSERT( mpi_cmp_mpi( &R.X, &xA ) == 0 ); |
| 174 | TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yA ) == 0 ); |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 175 | TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 176 | TEST_ASSERT( ecp_mul( &grp, &R, &dB, &R ) == 0 ); |
| 177 | TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 ); |
| 178 | TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 ); |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 179 | TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 180 | |
| 181 | TEST_ASSERT( ecp_mul( &grp, &R, &dB, &grp.G ) == 0 ); |
| 182 | TEST_ASSERT( mpi_cmp_mpi( &R.X, &xB ) == 0 ); |
| 183 | TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yB ) == 0 ); |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 184 | TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 185 | TEST_ASSERT( ecp_mul( &grp, &R, &dA, &R ) == 0 ); |
| 186 | TEST_ASSERT( mpi_cmp_mpi( &R.X, &xZ ) == 0 ); |
| 187 | TEST_ASSERT( mpi_cmp_mpi( &R.Y, &yZ ) == 0 ); |
Manuel Pégourié-Gonnard | 1c33057 | 2012-11-24 12:05:44 +0100 | [diff] [blame] | 188 | TEST_ASSERT( ecp_check_pubkey( &grp, &R ) == 0 ); |
Manuel Pégourié-Gonnard | e739f01 | 2012-11-07 12:24:22 +0100 | [diff] [blame] | 189 | |
| 190 | ecp_group_free( &grp ); ecp_point_free( &R ); |
| 191 | mpi_free( &dA ); mpi_free( &xA ); mpi_free( &yA ); mpi_free( &dB ); |
| 192 | mpi_free( &xB ); mpi_free( &yB ); mpi_free( &xZ ); mpi_free( &yZ ); |
Manuel Pégourié-Gonnard | 4b8c3f2 | 2012-11-07 21:39:45 +0100 | [diff] [blame] | 193 | } |
| 194 | END_CASE |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 195 | |
| 196 | BEGIN_CASE |
| 197 | ecp_fast_mod:id:N |
| 198 | { |
| 199 | ecp_group grp; |
| 200 | mpi N, R; |
| 201 | |
| 202 | mpi_init( &N ); mpi_init( &R ); |
| 203 | ecp_group_init( &grp ); |
| 204 | |
| 205 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 206 | TEST_ASSERT( mpi_read_string( &N, 16, {N} ) == 0 ); |
| 207 | |
| 208 | /* |
| 209 | * Store correct result before we touch N |
| 210 | */ |
| 211 | TEST_ASSERT( mpi_mod_mpi( &R, &N, &grp.P ) == 0 ); |
| 212 | |
| 213 | TEST_ASSERT( grp.modp( &N ) == 0 ); |
| 214 | TEST_ASSERT( mpi_msb( &N ) <= grp.pbits + 3 ); |
| 215 | |
| 216 | /* |
| 217 | * Use mod rather than addition/substraction in case previous test fails |
| 218 | */ |
| 219 | TEST_ASSERT( mpi_mod_mpi( &N, &N, &grp.P ) == 0 ); |
| 220 | TEST_ASSERT( mpi_cmp_mpi( &N, &R ) == 0 ); |
| 221 | |
| 222 | mpi_free( &N ); mpi_free( &R ); |
| 223 | ecp_group_free( &grp ); |
| 224 | } |
| 225 | END_CASE |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 226 | |
| 227 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 228 | ecp_write_binary:id:x:y:z:format:out:blen:ret |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 229 | { |
| 230 | ecp_group grp; |
| 231 | ecp_point P; |
| 232 | unsigned char buf[256], str[512]; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 233 | size_t olen; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 234 | |
| 235 | memset( buf, 0, sizeof( buf ) ); |
| 236 | memset( str, 0, sizeof( str ) ); |
| 237 | |
| 238 | ecp_group_init( &grp ); ecp_point_init( &P ); |
| 239 | |
| 240 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 241 | |
| 242 | TEST_ASSERT( mpi_read_string( &P.X, 16, {x} ) == 0 ); |
| 243 | TEST_ASSERT( mpi_read_string( &P.Y, 16, {y} ) == 0 ); |
| 244 | TEST_ASSERT( mpi_read_string( &P.Z, 16, {z} ) == 0 ); |
| 245 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 246 | #define POLARSSL_ECP_PF_UNKNOWN -1 |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 247 | TEST_ASSERT( ecp_point_write_binary( &grp, &P, POLARSSL_ECP_PF_{format}, |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 248 | &olen, buf, {blen} ) == {ret} ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 249 | |
| 250 | if( {ret} == 0 ) |
| 251 | { |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 252 | hexify( str, buf, olen ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 253 | TEST_ASSERT( strcasecmp( (char *) str, {out} ) == 0 ); |
| 254 | } |
| 255 | |
| 256 | ecp_group_free( &grp ); ecp_point_free( &P ); |
| 257 | } |
| 258 | END_CASE |
| 259 | |
| 260 | BEGIN_CASE |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 261 | ecp_read_binary:id:input:x:y:z:ret |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 262 | { |
| 263 | ecp_group grp; |
| 264 | ecp_point P; |
| 265 | mpi X, Y, Z; |
| 266 | int ilen; |
| 267 | unsigned char buf[256]; |
| 268 | |
| 269 | memset( buf, 0, sizeof( buf ) ); |
| 270 | |
| 271 | ecp_group_init( &grp ); ecp_point_init( &P ); |
| 272 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
| 273 | |
| 274 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 275 | |
| 276 | TEST_ASSERT( mpi_read_string( &X, 16, {x} ) == 0 ); |
| 277 | TEST_ASSERT( mpi_read_string( &Y, 16, {y} ) == 0 ); |
| 278 | TEST_ASSERT( mpi_read_string( &Z, 16, {z} ) == 0 ); |
| 279 | |
| 280 | ilen = unhexify( buf, {input} ); |
| 281 | |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 282 | TEST_ASSERT( ecp_point_read_binary( &grp, &P, buf, ilen ) == {ret} ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 283 | |
| 284 | if( {ret} == 0 ) |
| 285 | { |
| 286 | TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 ); |
| 287 | TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 ); |
| 288 | TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 ); |
| 289 | } |
| 290 | |
| 291 | ecp_group_free( &grp ); ecp_point_free( &P ); |
| 292 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
| 293 | } |
| 294 | END_CASE |
| 295 | |
| 296 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 297 | ecp_tls_read_point:id:input:x:y:z:ret |
| 298 | { |
| 299 | ecp_group grp; |
| 300 | ecp_point P; |
| 301 | mpi X, Y, Z; |
| 302 | size_t ilen; |
| 303 | unsigned char buf[256]; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 304 | const unsigned char *vbuf = buf; |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 305 | |
| 306 | memset( buf, 0, sizeof( buf ) ); |
| 307 | |
| 308 | ecp_group_init( &grp ); ecp_point_init( &P ); |
| 309 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
| 310 | |
| 311 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 312 | |
| 313 | TEST_ASSERT( mpi_read_string( &X, 16, {x} ) == 0 ); |
| 314 | TEST_ASSERT( mpi_read_string( &Y, 16, {y} ) == 0 ); |
| 315 | TEST_ASSERT( mpi_read_string( &Z, 16, {z} ) == 0 ); |
| 316 | |
| 317 | ilen = unhexify( buf, {input} ); |
| 318 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 319 | TEST_ASSERT( ecp_tls_read_point( &grp, &P, &vbuf, ilen ) == {ret} ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 320 | |
| 321 | if( {ret} == 0 ) |
| 322 | { |
| 323 | TEST_ASSERT( mpi_cmp_mpi( &P.X, &X ) == 0 ); |
| 324 | TEST_ASSERT( mpi_cmp_mpi( &P.Y, &Y ) == 0 ); |
| 325 | TEST_ASSERT( mpi_cmp_mpi( &P.Z, &Z ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 326 | TEST_ASSERT( *vbuf == 0x00 ); |
Manuel Pégourié-Gonnard | 8c16f96 | 2013-02-10 13:00:20 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | ecp_group_free( &grp ); ecp_point_free( &P ); |
| 330 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
| 331 | } |
| 332 | END_CASE |
| 333 | |
| 334 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 335 | ecp_tls_write_read_point:id |
| 336 | { |
| 337 | ecp_group grp; |
| 338 | ecp_point pt; |
| 339 | unsigned char buf[256]; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 340 | const unsigned char *vbuf; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 341 | size_t olen; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 342 | |
| 343 | ecp_group_init( &grp ); |
| 344 | ecp_point_init( &pt ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 345 | |
| 346 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 347 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 348 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 349 | TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 350 | POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 351 | TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 352 | == POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 353 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 355 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 356 | TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 357 | POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 358 | TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 359 | TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 ); |
| 360 | TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 ); |
| 361 | TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 362 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 363 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 364 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 365 | TEST_ASSERT( ecp_set_zero( &pt ) == 0 ); |
| 366 | TEST_ASSERT( ecp_tls_write_point( &grp, &pt, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 367 | POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 368 | TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 369 | TEST_ASSERT( ecp_is_zero( &pt ) ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 370 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 371 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 372 | memset( buf, 0x00, sizeof( buf ) ); vbuf = buf; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 373 | TEST_ASSERT( ecp_set_zero( &pt ) == 0 ); |
| 374 | TEST_ASSERT( ecp_tls_write_point( &grp, &pt, |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 375 | POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 376 | TEST_ASSERT( ecp_tls_read_point( &grp, &pt, &vbuf, olen ) == 0 ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 377 | TEST_ASSERT( ecp_is_zero( &pt ) ); |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 378 | TEST_ASSERT( vbuf == buf + olen ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 379 | |
| 380 | ecp_group_free( &grp ); |
| 381 | ecp_point_free( &pt ); |
| 382 | } |
| 383 | END_CASE |
| 384 | |
| 385 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 386 | ecp_tls_read_group:record:ret:bits |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 387 | { |
| 388 | ecp_group grp; |
| 389 | unsigned char buf[10]; |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 390 | const unsigned char *vbuf = buf; |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 391 | int len, ret; |
| 392 | |
| 393 | ecp_group_init( &grp ); |
| 394 | memset( buf, 0x00, sizeof( buf ) ); |
| 395 | |
| 396 | len = unhexify( buf, {record} ); |
| 397 | |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 398 | ret = ecp_tls_read_group( &grp, &vbuf, len ); |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 399 | |
| 400 | TEST_ASSERT( ret == {ret} ); |
| 401 | if( ret == 0) |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 402 | { |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 403 | TEST_ASSERT( mpi_msb( &grp.P ) == {bits} ); |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 404 | TEST_ASSERT( *vbuf == 0x00 ); |
| 405 | } |
Manuel Pégourié-Gonnard | 6282aca | 2013-02-10 11:15:11 +0100 | [diff] [blame] | 406 | |
| 407 | ecp_group_free( &grp ); |
| 408 | } |
| 409 | END_CASE |
| 410 | |
| 411 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 412 | ecp_tls_write_read_group:id |
| 413 | { |
| 414 | ecp_group grp1, grp2; |
| 415 | unsigned char buf[10]; |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 416 | const unsigned char *vbuf = buf; |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 417 | size_t len; |
| 418 | int ret; |
| 419 | |
| 420 | ecp_group_init( &grp1 ); |
| 421 | ecp_group_init( &grp2 ); |
| 422 | memset( buf, 0x00, sizeof( buf ) ); |
| 423 | |
| 424 | TEST_ASSERT( ecp_use_known_dp( &grp1, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 425 | |
| 426 | TEST_ASSERT( ecp_tls_write_group( &grp1, &len, buf, 10 ) == 0 ); |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 427 | TEST_ASSERT( ( ret = ecp_tls_read_group( &grp2, &vbuf, len ) ) == 0 ); |
Manuel Pégourié-Gonnard | 46106a9 | 2013-02-10 12:51:17 +0100 | [diff] [blame] | 428 | |
| 429 | if( ret == 0 ) |
| 430 | { |
| 431 | TEST_ASSERT( mpi_cmp_mpi( &grp1.N, &grp2.N ) == 0 ); |
| 432 | TEST_ASSERT( grp1.id == grp2.id ); |
| 433 | } |
| 434 | |
| 435 | ecp_group_free( &grp1 ); |
| 436 | ecp_group_free( &grp2 ); |
| 437 | } |
| 438 | END_CASE |
| 439 | |
| 440 | BEGIN_CASE |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 441 | ecp_check_prvkey:id |
| 442 | { |
| 443 | ecp_group grp; |
| 444 | mpi d; |
| 445 | |
| 446 | ecp_group_init( &grp ); |
| 447 | mpi_init( &d ); |
| 448 | |
| 449 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 450 | |
| 451 | TEST_ASSERT( mpi_lset( &d, 0 ) == 0 ); |
| 452 | TEST_ASSERT( ecp_check_prvkey( &grp, &d ) == POLARSSL_ERR_ECP_GENERIC ); |
| 453 | |
| 454 | TEST_ASSERT( mpi_copy( &d, &grp.N ) == 0 ); |
| 455 | TEST_ASSERT( ecp_check_prvkey( &grp, &d ) == POLARSSL_ERR_ECP_GENERIC ); |
| 456 | |
| 457 | ecp_group_free( &grp ); |
| 458 | mpi_free( &d ); |
| 459 | } |
| 460 | END_CASE |
| 461 | |
| 462 | BEGIN_CASE |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 463 | ecp_gen_keypair:id |
| 464 | { |
| 465 | ecp_group grp; |
| 466 | ecp_point Q; |
| 467 | mpi d; |
| 468 | rnd_pseudo_info rnd_info; |
| 469 | |
| 470 | ecp_group_init( &grp ); |
| 471 | ecp_point_init( &Q ); |
| 472 | mpi_init( &d ); |
| 473 | memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); |
| 474 | |
| 475 | TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); |
| 476 | |
| 477 | TEST_ASSERT( ecp_gen_keypair( &grp, &d, &Q, &rnd_pseudo_rand, &rnd_info ) |
| 478 | == 0 ); |
| 479 | |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 480 | TEST_ASSERT( ecp_check_pubkey( &grp, &Q ) == 0 ); |
| 481 | TEST_ASSERT( ecp_check_prvkey( &grp, &d ) == 0 ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 482 | |
| 483 | ecp_group_free( &grp ); |
| 484 | ecp_point_free( &Q ); |
| 485 | mpi_free( &d ); |
| 486 | } |
| 487 | END_CASE |
| 488 | |
| 489 | BEGIN_CASE |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 490 | ecp_selftest: |
| 491 | { |
| 492 | TEST_ASSERT( ecp_self_test( 0 ) == 0 ); |
| 493 | } |
| 494 | END_CASE |