mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-01-27 06:35:22 +00:00
Move bignum_core tests into separate files
Test cases for mpi_core_add_if, mpi_core_mla, mpi_core_sub, mpi_core_montmul are not copied into new files, these are generated in subsequent commits. Signed-off-by: Werner Lewis <werner.lewis@arm.com>
This commit is contained in:
parent
149c1516e7
commit
c6004a2ab3
264
tests/suites/test_suite_bignum_core.data
Normal file
264
tests/suites/test_suite_bignum_core.data
Normal file
@ -0,0 +1,264 @@
|
||||
Test mbedtls_mpi_core_io functions with null pointers
|
||||
mpi_core_io_null
|
||||
|
||||
Test mbedtls_mpi_core_io_be #1 (Buffer and limbs just fit, input limb-aligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #2 (Buffer and limbs just fit, input unaligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #3 (Buffer just fits, extra limbs, input limb-aligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #4 (Buffer just fits, extra limbs, input unaligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #5 (Extra limbs, buffer aligned to extra limbs, input limb-aligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":112:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #6 (Extra limbs, buffer aligned to extra limbs, input unaligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":112:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #7 (Buffer and limbs just fit, input limb-aligned with leading zeroes)
|
||||
mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":88:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #8 (Buffer and limbs just fit, input unaligned with leading zeroes)
|
||||
mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":86:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #9 (Buffer just fits, extra limbs, input limb-aligned with leading zeroes)
|
||||
mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":88:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #10 (Buffer just fits, extra limbs, input unaligned with leading zeroes)
|
||||
mpi_core_io_be:"00000000000000001fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":86:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #11 (Zero)
|
||||
mpi_core_io_be:"00":1:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #12 (Zero, empty output)
|
||||
mpi_core_io_be:"00":0:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #13 (Zero, empty input)
|
||||
mpi_core_io_be:"":1:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #14 (One)
|
||||
mpi_core_io_be:"01":1:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #15 (One limb, 32 bit)
|
||||
depends_on:MBEDTLS_HAVE_INT32
|
||||
mpi_core_io_be:"ff000000":4:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #16 (One limb, 64 bit)
|
||||
depends_on:MBEDTLS_HAVE_INT64
|
||||
mpi_core_io_be:"ff00000000000000":8:2:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #17 (not enough limbs, input limb-aligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:22:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #18 (not enough limbs, input unaligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:22:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:0
|
||||
|
||||
Test mbedtls_mpi_core_io_be #19 (buffer too small, input limb-aligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":95:24:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
|
||||
|
||||
Test mbedtls_mpi_core_io_be #20 (buffer too small, input unaligned)
|
||||
mpi_core_io_be:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":93:24:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
|
||||
|
||||
Test mbedtls_mpi_core_io_be #21 (Buffer and limbs fit, input unaligned, odd number of limbs)
|
||||
mpi_core_io_be:"00de4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":82:21:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #1 (Buffer and limbs just fit, input limb-aligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #2 (Buffer and limbs just fit, input unaligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #3 (Buffer just fits, extra limbs, input limb-aligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #4 (Buffer just fits, extra limbs, input unaligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #5 (Extra limbs, buffer aligned to extra limbs, input limb-aligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":112:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #6 (Extra limbs, buffer aligned to extra limbs, input unaligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":112:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #7 (Buffer and limbs just fit, input limb-aligned with leading zeroes)
|
||||
mpi_core_io_le:"1fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b44240000000000000000":88:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #8 (Buffer and limbs just fit, input unaligned with leading zeroes)
|
||||
mpi_core_io_le:"1fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b0000000000000000":86:24:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #9 (Buffer just fits, extra limbs, input limb-aligned with leading zeroes)
|
||||
mpi_core_io_le:"1fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b44240000000000000000":88:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #10 (Buffer just fits, extra limbs, input unaligned with leading zeroes)
|
||||
mpi_core_io_le:"1fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b0000000000000000":86:28:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #11 (Zero)
|
||||
mpi_core_io_le:"00":1:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #12 (Zero, empty output)
|
||||
mpi_core_io_le:"00":0:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #13 (Zero, empty input)
|
||||
mpi_core_io_le:"":1:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #14 (One)
|
||||
mpi_core_io_le:"01":1:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #15 (One limb)
|
||||
depends_on:MBEDTLS_HAVE_INT32
|
||||
mpi_core_io_le:"000000ff":4:1:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #16 (One limb)
|
||||
depends_on:MBEDTLS_HAVE_INT64
|
||||
mpi_core_io_le:"00000000000000ff":8:2:0:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #17 (not enough limbs, input limb-aligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":96:22:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #18 (not enough limbs, input unaligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":94:22:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:0
|
||||
|
||||
Test mbedtls_mpi_core_io_le #19 (buffer too small, input limb-aligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":95:24:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
|
||||
|
||||
Test mbedtls_mpi_core_io_le #20 (buffer too small, input unaligned)
|
||||
mpi_core_io_le:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b":93:24:0:MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL
|
||||
|
||||
Test mbedtls_mpi_core_io_le #21 (Buffer and limbs fit, input unaligned, odd number of limbs)
|
||||
mpi_core_io_le:"de4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b442400":82:21:0:0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x=y (1 limb)
|
||||
mpi_core_lt_ct:"2B5":"2B5":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (1 limb)
|
||||
mpi_core_lt_ct:"2B5":"2B4":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (1 limb)
|
||||
mpi_core_lt_ct:"2B5":"2B6":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x=y (0 limbs)
|
||||
mpi_core_lt_ct:"":"":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (63 bit x, y first byte greater)
|
||||
mpi_core_lt_ct:"7FFFFFFFFFFFFFFF":"00000000000000FF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (63 bit y, x first byte greater)
|
||||
mpi_core_lt_ct:"00000000000000FF":"7FFFFFFFFFFFFFFF":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (64 bit x, y=x-1)
|
||||
mpi_core_lt_ct:"8000000000000000":"7FFFFFFFFFFFFFFF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (64 bit y, x=y-1)
|
||||
mpi_core_lt_ct:"7FFFFFFFFFFFFFFF":"8000000000000000":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (64 bit x, y=1)
|
||||
mpi_core_lt_ct:"8000000000000000":"0000000000000001":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (64 bit y, x=1)
|
||||
mpi_core_lt_ct:"0000000000000001":"8000000000000000":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (64 bit x, y=0)
|
||||
mpi_core_lt_ct:"8000000000000000":"0000000000000000":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (64 bit y, x=0)
|
||||
mpi_core_lt_ct:"0000000000000000":"8000000000000000":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (64 bit x, first bytes equal)
|
||||
mpi_core_lt_ct:"FFFFFFFFFFFFFFFF":"00000000000000FF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (64 bit y, first bytes equal)
|
||||
mpi_core_lt_ct:"00000000000000FF":"FFFFFFFFFFFFFFFF":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (31 bit x, y first byte greater)
|
||||
mpi_core_lt_ct:"7FFFFFFF":"000000FF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (31 bit y, x first byte greater)
|
||||
mpi_core_lt_ct:"000000FF":"7FFFFFFF":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (32 bit x, y=x-1)
|
||||
mpi_core_lt_ct:"80000000":"7FFFFFFF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (32 bit y, x=y-1)
|
||||
mpi_core_lt_ct:"7FFFFFFF":"80000000":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (32 bit x, y=1)
|
||||
mpi_core_lt_ct:"80000000":"00000001":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (32 bit y, x=1)
|
||||
mpi_core_lt_ct:"00000001":"80000000":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (32 bit x, y=0)
|
||||
mpi_core_lt_ct:"80000000":"00000000":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (32 bit y, x=0)
|
||||
mpi_core_lt_ct:"00000000":"80000000":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (32 bit x, first bytes equal)
|
||||
mpi_core_lt_ct:"FFFFFFFF":"000000FF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (32 bit y, first bytes equal)
|
||||
mpi_core_lt_ct:"000000FF":"FFFFFFFF":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y, zero vs non-zero MS limb
|
||||
mpi_core_lt_ct:"0FFFFFFFFFFFFFFFF":"1FFFFFFFFFFFFFFFF":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y, equal MS limbs
|
||||
mpi_core_lt_ct:"EEFFFFFFFFFFFFFFFF":"EEFFFFFFFFFFFFFFF1":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x=y (multi-limb)
|
||||
mpi_core_lt_ct:"EEFFFFFFFFFFFFFFFF":"EEFFFFFFFFFFFFFFFF":0
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x<y (alternating limbs)
|
||||
mpi_core_lt_ct:"11FFFFFFFFFFFFFFFF":"FF1111111111111111":1
|
||||
|
||||
mbedtls_mpi_core_lt_ct: x>y (alternating limbs)
|
||||
mpi_core_lt_ct:"FF1111111111111111":"11FFFFFFFFFFFFFFFF":0
|
||||
|
||||
mbedtls_mpi_montg_init #1
|
||||
mpi_montg_init:"000000000000001d":"cb08d3dcb08d3dcb"
|
||||
|
||||
mbedtls_mpi_montg_init #2
|
||||
mpi_montg_init:"0000000000000009":"71c71c71c71c71c7"
|
||||
|
||||
mbedtls_mpi_montg_init #3
|
||||
mpi_montg_init:"000000000001869f":"34d76bc8e5e3eaa1"
|
||||
|
||||
mbedtls_mpi_montg_init #4
|
||||
mpi_montg_init:"00000000000080000000000000000001":"ffffffffffffffff"
|
||||
|
||||
mbedtls_mpi_montg_init #5
|
||||
mpi_montg_init:"0000000000a1ffffffffffffffffffff":"0000000000000001"
|
||||
|
||||
mbedtls_mpi_montg_init #6
|
||||
mpi_montg_init:"00000000000257ffffffffffffffffff":"0000000000000001"
|
||||
|
||||
mbedtls_mpi_montg_init #7
|
||||
mpi_montg_init:"b91ba63180c726fbd57786f27f1ede97a3b40c59a7fcfb5898f076e9af57028d":"32edc7e1ac2e6fbb"
|
||||
|
||||
mbedtls_mpi_montg_init #8
|
||||
mpi_montg_init:"b3a119602ee213cde28581ecd892e0f592a338655dce4ca88054b3d124d0e561":"e41cfb909805815f"
|
||||
|
||||
mbedtls_mpi_montg_init #9
|
||||
mpi_montg_init:"0284139ea19c139ebe09a8111926aaa39a2c2be12ed487a809d3cb5bc55854725b4cdcb5734c58f90b2f60d99cc1950cdbc8d651793e93c9c6f0ead752500a32c56c62082912b66132b2a6aa42ada923e1ad22ceb7ba0123":"c02e2164b293c975"
|
||||
|
||||
mbedtls_mpi_montg_init #10
|
||||
mpi_montg_init:"00000000000000011a9351d2d32ccd568e75bf8b4ebbb2a36be691b55832edac662ff79803df8af525fba453068be16ac3920bcc1b468f8f7fe786e0fa4ecbabcad31e5e3b05def802eb8600deaf11ef452487db878df20a80606e4bb6a163b83895d034cc8b53dbcd005be42ffdd2ce99bed06089a0b79d":"ffec8978c055794b"
|
||||
|
||||
mbedtls_mpi_montg_init #11
|
||||
mpi_montg_init:"eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3":"7b07a0b0379b9135"
|
||||
|
||||
mbedtls_mpi_montg_init #12
|
||||
mpi_montg_init:"00000007a364ab3de755f924642bd5273524234f78395da1ed9098f39af4fe248288b0cb7f1c27214588969479d7dc9f0d327b5544dd4c095aa1fa271df421fe9ee460855cc8423d223e2c85dc793f6babdca7fc804ea1f408f867db053bfd98c45085ea5d805c78d2863bacdfcaf4c6147ebb74a9056045074785714c0b84ed":"8f54b233c070871b"
|
||||
|
||||
mbedtls_mpi_montg_init #13
|
||||
mpi_montg_init:"e2df85c83ee8463b3af26805791cc0b1ba1af89564e887a63d5ba18ea72fb593b664cf8ace78241ea3109b7644510e02324a5c1e9a85daada3c383759d7678ce8d8886b51a3237dc84b543de4f843c77fc77ba08ef90e7e96ba622478f6b96daa3e9b8511f36279fb0120ef93bad2090e7878346fe4ae29ad61be48b6835e8407d0849422e05c7a4d1e02322f2675056d73d4c5a1ab376bfaccfd61ff7d64b715c9525a7ed8dcda1144f8722c30d12ba3d95221d897edc825a1598a645e2c457":"b777a905d9239899"
|
||||
|
||||
mbedtls_mpi_montg_init #14
|
||||
mpi_montg_init:"baea2d65939296fc2536f18f2a4042a741f33088ecd5000e76c67a466e7a1e696f8ee9a15497168b3a2b597799dc9475909ebbc64b96f233430c6aa3e4a86e9352b0230081502da09ef41dc0a164a1c6a31bd1338e359a28c78ef50c89f06a46b46a27d7245bba7468334625687201d62ef084de4c5190dfe70c14a318204492de6edd138e14e9337fda739dcadd0212302db7770de28d8c5c79b6a6b5f927e656e157cd7e41204ec39731fe3608ecd4b885a194647fe7f02b74639cc76cdf03":"827ef0810f71fc55"
|
||||
|
||||
mbedtls_mpi_montg_init #15
|
||||
mpi_montg_init:"bf741f75e28a44e271cf43e68dbadd23c72d2f2e1fc78a6d6aaaadf2ccbf26c9a232aff5b3f3f29323b114f3018144ed9438943e07820e222137d3bb229b61671e61f75f6021a26436df9e669929fa392df021f105d2fce0717468a522018721ccde541b9a7b558128419f457ef33a5753f00c20c2d709727eef6278c55b278b10abe1d13e538514128b5dcb7bfd015e0fdcb081555071813974135d5ab5000630a94f5b0f4021a504ab4f3df2403e6140b9939f8bbe714635f5cff10744be03":"aab901da57bba355"
|
635
tests/suites/test_suite_bignum_core.function
Normal file
635
tests/suites/test_suite_bignum_core.function
Normal file
@ -0,0 +1,635 @@
|
||||
/* BEGIN_HEADER */
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "bignum_core.h"
|
||||
#include "constant_time_internal.h"
|
||||
#include "test/constant_flow.h"
|
||||
|
||||
/* END_HEADER */
|
||||
|
||||
/* BEGIN_DEPENDENCIES
|
||||
* depends_on:MBEDTLS_BIGNUM_C
|
||||
* END_DEPENDENCIES
|
||||
*/
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_io_null()
|
||||
{
|
||||
mbedtls_mpi_uint X = 0;
|
||||
int ret;
|
||||
|
||||
ret = mbedtls_mpi_core_read_be( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_be( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
ret = mbedtls_mpi_core_read_be( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_be( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
ret = mbedtls_mpi_core_read_le( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_le( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
ret = mbedtls_mpi_core_read_le( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_le( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
exit:
|
||||
;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_io_be( data_t *input, int nb_int, int nx_32_int, int iret,
|
||||
int oret )
|
||||
{
|
||||
if( iret != 0 )
|
||||
TEST_ASSERT( oret == 0 );
|
||||
|
||||
TEST_LE_S( 0, nb_int );
|
||||
size_t nb = nb_int;
|
||||
|
||||
unsigned char buf[1024];
|
||||
TEST_LE_U( nb, sizeof( buf ) );
|
||||
|
||||
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
||||
* to halve the number of limbs to have the same size. */
|
||||
size_t nx;
|
||||
TEST_LE_S( 0, nx_32_int );
|
||||
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
||||
nx = nx_32_int / 2 + nx_32_int % 2;
|
||||
else
|
||||
nx = nx_32_int;
|
||||
|
||||
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
||||
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
||||
|
||||
int ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
|
||||
TEST_EQUAL( ret, iret );
|
||||
|
||||
if( iret == 0 )
|
||||
{
|
||||
ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
|
||||
TEST_EQUAL( ret, oret );
|
||||
}
|
||||
|
||||
if( ( iret == 0 ) && ( oret == 0 ) )
|
||||
{
|
||||
if( nb > input->len )
|
||||
{
|
||||
size_t leading_zeroes = nb - input->len;
|
||||
TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
|
||||
for( size_t i = 0; i < leading_zeroes; i++ )
|
||||
TEST_EQUAL( buf[i], 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t leading_zeroes = input->len - nb;
|
||||
TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
|
||||
for( size_t i = 0; i < leading_zeroes; i++ )
|
||||
TEST_EQUAL( input->x[i], 0 );
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_io_le( data_t *input, int nb_int, int nx_32_int, int iret,
|
||||
int oret )
|
||||
{
|
||||
if( iret != 0 )
|
||||
TEST_ASSERT( oret == 0 );
|
||||
|
||||
TEST_LE_S( 0, nb_int );
|
||||
size_t nb = nb_int;
|
||||
|
||||
unsigned char buf[1024];
|
||||
TEST_LE_U( nb, sizeof( buf ) );
|
||||
|
||||
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
||||
* to halve the number of limbs to have the same size. */
|
||||
size_t nx;
|
||||
TEST_LE_S( 0, nx_32_int );
|
||||
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
||||
nx = nx_32_int / 2 + nx_32_int % 2;
|
||||
else
|
||||
nx = nx_32_int;
|
||||
|
||||
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
||||
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
||||
|
||||
int ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len );
|
||||
TEST_EQUAL( ret, iret );
|
||||
|
||||
if( iret == 0 )
|
||||
{
|
||||
ret = mbedtls_mpi_core_write_le( X, nx, buf, nb );
|
||||
TEST_EQUAL( ret, oret );
|
||||
}
|
||||
|
||||
if( ( iret == 0 ) && ( oret == 0 ) )
|
||||
{
|
||||
if( nb > input->len )
|
||||
{
|
||||
TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
|
||||
for( size_t i = input->len; i < nb; i++ )
|
||||
TEST_EQUAL( buf[i], 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
|
||||
for( size_t i = nb; i < input->len; i++ )
|
||||
TEST_EQUAL( input->x[i], 0 );
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_lt_ct( char *input_X, char *input_Y, int exp_ret )
|
||||
{
|
||||
mbedtls_mpi_uint *X = NULL;
|
||||
size_t X_limbs;
|
||||
mbedtls_mpi_uint *Y = NULL;
|
||||
size_t Y_limbs;
|
||||
int ret;
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &X, &X_limbs, input_X ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &Y, &Y_limbs, input_Y ) );
|
||||
|
||||
/* We need two same-length limb arrays */
|
||||
TEST_EQUAL( X_limbs, Y_limbs );
|
||||
|
||||
TEST_CF_SECRET( X, X_limbs * sizeof( mbedtls_mpi_uint ) );
|
||||
TEST_CF_SECRET( Y, X_limbs * sizeof( mbedtls_mpi_uint ) );
|
||||
|
||||
ret = mbedtls_mpi_core_lt_ct( X, Y, X_limbs );
|
||||
TEST_EQUAL( ret, exp_ret );
|
||||
|
||||
exit:
|
||||
mbedtls_free( X );
|
||||
mbedtls_free( Y );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_add_if( char * input_A, char * input_B,
|
||||
char * input_S4, int carry4,
|
||||
char * input_S8, int carry8 )
|
||||
{
|
||||
mbedtls_mpi S4, S8, A, B;
|
||||
mbedtls_mpi_uint *a = NULL; /* first value to add */
|
||||
mbedtls_mpi_uint *b = NULL; /* second value to add */
|
||||
mbedtls_mpi_uint *sum = NULL;
|
||||
mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &S4 );
|
||||
mbedtls_mpi_init( &S8 );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S4, input_S4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) );
|
||||
|
||||
/* We only need to work with one of (S4, carry4) or (S8, carry8) depending
|
||||
* on sizeof(mbedtls_mpi_uint)
|
||||
*/
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8;
|
||||
mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, X->s );
|
||||
|
||||
/* Test cases are such that A <= B, so #limbs should be <= */
|
||||
TEST_LE_U( A.n, B.n );
|
||||
TEST_LE_U( X->n, B.n );
|
||||
|
||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
||||
|
||||
/* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which
|
||||
* must be the same size. The MPIs we've read in will only have arrays
|
||||
* large enough for the number they represent. Therefore we create new
|
||||
* raw arrays of mbedtls_mpi_uints and populate them from the MPIs we've
|
||||
* just read in.
|
||||
*
|
||||
* We generated test data such that B was always >= A, so that's how many
|
||||
* limbs each of these need.
|
||||
*/
|
||||
size_t limbs = B.n;
|
||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||
|
||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
||||
ASSERT_ALLOC( a, bytes );
|
||||
ASSERT_ALLOC( b, bytes );
|
||||
ASSERT_ALLOC( sum, bytes );
|
||||
ASSERT_ALLOC( d, bytes );
|
||||
|
||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
||||
* processed by mbedtls_mpi_core_add_if()) are little endian, we can just
|
||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
||||
*/
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( sum, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
||||
|
||||
/* The test cases have a <= b to avoid repetition, so we test a + b then,
|
||||
* if a != b, b + a. If a == b, we can test when a and b are aliased */
|
||||
|
||||
/* a + b */
|
||||
|
||||
/* cond = 0 => d unchanged, no carry */
|
||||
memcpy( d, a, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) );
|
||||
ASSERT_COMPARE( d, bytes, a, bytes );
|
||||
|
||||
/* cond = 1 => correct result and carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) );
|
||||
ASSERT_COMPARE( d, bytes, sum, bytes );
|
||||
|
||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
/* a == b, so test where a and b are aliased */
|
||||
|
||||
/* cond = 0 => d unchanged, no carry */
|
||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) );
|
||||
ASSERT_COMPARE( b, bytes, B.p, bytes );
|
||||
|
||||
/* cond = 1 => correct result and carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) );
|
||||
ASSERT_COMPARE( b, bytes, sum, bytes );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* a != b, so test b + a */
|
||||
|
||||
/* cond = 0 => d unchanged, no carry */
|
||||
memcpy( d, b, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) );
|
||||
ASSERT_COMPARE( d, bytes, b, bytes );
|
||||
|
||||
/* cond = 1 => correct result and carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) );
|
||||
ASSERT_COMPARE( d, bytes, sum, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( a );
|
||||
mbedtls_free( b );
|
||||
mbedtls_free( sum );
|
||||
mbedtls_free( d );
|
||||
|
||||
mbedtls_mpi_free( &S4 );
|
||||
mbedtls_mpi_free( &S8 );
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_sub( char * input_A, char * input_B,
|
||||
char * input_X4, char * input_X8,
|
||||
int carry )
|
||||
{
|
||||
mbedtls_mpi A, B, X4, X8;
|
||||
mbedtls_mpi_uint *a = NULL;
|
||||
mbedtls_mpi_uint *b = NULL;
|
||||
mbedtls_mpi_uint *x = NULL; /* expected */
|
||||
mbedtls_mpi_uint *r = NULL; /* result */
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &X4 );
|
||||
mbedtls_mpi_init( &X8 );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, X4.s );
|
||||
TEST_EQUAL( 1, X8.s );
|
||||
|
||||
/* Get the number of limbs we will need */
|
||||
size_t limbs = MAX( A.n, B.n );
|
||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||
|
||||
/* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
||||
|
||||
/* The result shouldn't have more limbs than the longest input */
|
||||
TEST_LE_U( X->n, limbs );
|
||||
|
||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
||||
|
||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
||||
ASSERT_ALLOC( a, bytes );
|
||||
ASSERT_ALLOC( b, bytes );
|
||||
ASSERT_ALLOC( x, bytes );
|
||||
ASSERT_ALLOC( r, bytes );
|
||||
|
||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
||||
* processed by mbedtls_mpi_core_sub()) are little endian, we can just
|
||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
||||
*/
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
||||
|
||||
/* 1a) r = a - b => we should get the correct carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) );
|
||||
|
||||
/* 1b) r = a - b => we should get the correct result */
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
|
||||
/* 2 and 3 test "r may be aliased to a or b" */
|
||||
/* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */
|
||||
memcpy( r, a, bytes );
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, b, limbs ) );
|
||||
|
||||
/* 2b) r -= b => we should get the correct result */
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
|
||||
/* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */
|
||||
memcpy( r, b, bytes );
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, r, limbs ) );
|
||||
|
||||
/* 3b) r = a - b => we should get the correct result */
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
|
||||
/* 4 tests "r may be aliased to [...] both" */
|
||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
memcpy( r, b, bytes );
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, r, limbs ) );
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( a );
|
||||
mbedtls_free( b );
|
||||
mbedtls_free( x );
|
||||
mbedtls_free( r );
|
||||
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
mbedtls_mpi_free( &X4 );
|
||||
mbedtls_mpi_free( &X8 );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_mla( char * input_A, char * input_B, char * input_S,
|
||||
char * input_X4, char * input_cy4,
|
||||
char * input_X8, char * input_cy8 )
|
||||
{
|
||||
/* We are testing A += B * s; A, B are MPIs, s is a scalar.
|
||||
*
|
||||
* However, we encode s as an MPI in the .data file as the test framework
|
||||
* currently only supports `int`-typed scalars, and that doesn't cover the
|
||||
* full range of `mbedtls_mpi_uint`.
|
||||
*
|
||||
* We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8.
|
||||
*/
|
||||
mbedtls_mpi A, B, S, X4, X8, cy4, cy8;
|
||||
mbedtls_mpi_uint *a = NULL;
|
||||
mbedtls_mpi_uint *x = NULL;
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &S );
|
||||
mbedtls_mpi_init( &X4 );
|
||||
mbedtls_mpi_init( &X8 );
|
||||
mbedtls_mpi_init( &cy4 );
|
||||
mbedtls_mpi_init( &cy8 );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy4, input_cy4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy8, input_cy8 ) );
|
||||
|
||||
/* The MPI encoding of scalar s must be only 1 limb */
|
||||
TEST_EQUAL( 1, S.n );
|
||||
|
||||
/* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
||||
mbedtls_mpi *cy = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &cy4 : &cy8;
|
||||
|
||||
/* The carry should only have one limb */
|
||||
TEST_EQUAL( 1, cy->n );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, S.s );
|
||||
TEST_EQUAL( 1, X->s );
|
||||
TEST_EQUAL( 1, cy->s );
|
||||
|
||||
/* Get the (max) number of limbs we will need */
|
||||
size_t limbs = MAX( A.n, B.n );
|
||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||
|
||||
/* The result shouldn't have more limbs than the longest input */
|
||||
TEST_LE_U( X->n, limbs );
|
||||
|
||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
||||
|
||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
||||
ASSERT_ALLOC( a, bytes );
|
||||
ASSERT_ALLOC( x, bytes );
|
||||
|
||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
||||
* processed by mbedtls_mpi_core_mla()) are little endian, we can just
|
||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()).
|
||||
*/
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
||||
|
||||
/* 1a) A += B * s => we should get the correct carry */
|
||||
TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, B.p, B.n, *S.p ), *cy->p );
|
||||
|
||||
/* 1b) A += B * s => we should get the correct result */
|
||||
ASSERT_COMPARE( a, bytes, x, bytes );
|
||||
|
||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
/* Check when A and B are aliased */
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, a, limbs, *S.p ), *cy->p );
|
||||
ASSERT_COMPARE( a, bytes, x, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( a );
|
||||
mbedtls_free( x );
|
||||
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
mbedtls_mpi_free( &S );
|
||||
mbedtls_mpi_free( &X4 );
|
||||
mbedtls_mpi_free( &X8 );
|
||||
mbedtls_mpi_free( &cy4 );
|
||||
mbedtls_mpi_free( &cy8 );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_montg_init( char * input_N, char * input_mm )
|
||||
{
|
||||
mbedtls_mpi N, mm;
|
||||
|
||||
mbedtls_mpi_init( &N );
|
||||
mbedtls_mpi_init( &mm );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &mm, input_mm ) );
|
||||
|
||||
/* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or
|
||||
* 2 limbs (sizeof(mbedtls_mpi_uint) == 4).
|
||||
*
|
||||
* The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8;
|
||||
* for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this.
|
||||
*/
|
||||
TEST_ASSERT( mm.n == 1 || mm.n == 2 );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, N.s );
|
||||
TEST_EQUAL( 1, mm.s );
|
||||
|
||||
/* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */
|
||||
mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init( N.p );
|
||||
|
||||
/* Check we got the correct result */
|
||||
TEST_EQUAL( result, mm.p[0] );
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &mm );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_montmul( int limbs_AN4, int limbs_B4,
|
||||
int limbs_AN8, int limbs_B8,
|
||||
char * input_A,
|
||||
char * input_B,
|
||||
char * input_N,
|
||||
char * input_X4,
|
||||
char * input_X8 )
|
||||
{
|
||||
mbedtls_mpi A, B, N, X4, X8, T, R;
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &N );
|
||||
mbedtls_mpi_init( &X4 ); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */
|
||||
mbedtls_mpi_init( &X8 ); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */
|
||||
mbedtls_mpi_init( &T );
|
||||
mbedtls_mpi_init( &R ); /* for the result */
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
||||
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
||||
|
||||
int limbs_AN = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_AN4 : limbs_AN8;
|
||||
int limbs_B = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_B4 : limbs_B8;
|
||||
|
||||
TEST_LE_U( A.n, (size_t)limbs_AN );
|
||||
TEST_LE_U( X->n, (size_t)limbs_AN );
|
||||
TEST_LE_U( B.n, (size_t)limbs_B );
|
||||
TEST_LE_U( limbs_B, limbs_AN );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, N.s );
|
||||
TEST_EQUAL( 1, X->s );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &A, limbs_AN ) );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &N, limbs_AN ) );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) );
|
||||
|
||||
/* Calculate the Montgomery constant (this is unit tested separately) */
|
||||
mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &R, limbs_AN ) ); /* ensure it's got the right number of limbs */
|
||||
|
||||
mbedtls_mpi_core_montmul( R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
size_t bytes = N.n * sizeof(mbedtls_mpi_uint);
|
||||
ASSERT_COMPARE( R.p, bytes, X->p, bytes );
|
||||
|
||||
/* The output (R, above) may be aliased to A - use R to save the value of A */
|
||||
|
||||
memcpy( R.p, A.p, bytes );
|
||||
|
||||
mbedtls_mpi_core_montmul( A.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( A.p, bytes, X->p, bytes );
|
||||
|
||||
memcpy( A.p, R.p, bytes ); /* restore A */
|
||||
|
||||
/* The output may be aliased to N - use R to save the value of N */
|
||||
|
||||
memcpy( R.p, N.p, bytes );
|
||||
|
||||
mbedtls_mpi_core_montmul( N.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( N.p, bytes, X->p, bytes );
|
||||
|
||||
memcpy( N.p, R.p, bytes );
|
||||
|
||||
if (limbs_AN == limbs_B)
|
||||
{
|
||||
/* Test when A aliased to B (requires A == B on input values) */
|
||||
if ( memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
/* Test with A aliased to B and output, since this is permitted -
|
||||
* don't bother with yet another test with only A and B aliased */
|
||||
|
||||
mbedtls_mpi_core_montmul( B.p, B.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( B.p, bytes, X->p, bytes );
|
||||
|
||||
memcpy( B.p, A.p, bytes ); /* restore B from equal value A */
|
||||
}
|
||||
|
||||
/* The output may be aliased to B - last test, so we don't save B */
|
||||
|
||||
mbedtls_mpi_core_montmul( B.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( B.p, bytes, X->p, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &X4 );
|
||||
mbedtls_mpi_free( &X8 );
|
||||
mbedtls_mpi_free( &T );
|
||||
mbedtls_mpi_free( &R );
|
||||
}
|
||||
/* END_CASE */
|
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,6 @@
|
||||
/* BEGIN_HEADER */
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "bignum_core.h"
|
||||
#include "bignum_mod.h"
|
||||
#include "bignum_mod_raw.h"
|
||||
#include "constant_time_internal.h"
|
||||
@ -201,149 +200,6 @@ exit:
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_io_null()
|
||||
{
|
||||
mbedtls_mpi_uint X = 0;
|
||||
int ret;
|
||||
|
||||
ret = mbedtls_mpi_core_read_be( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_be( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
ret = mbedtls_mpi_core_read_be( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_be( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
ret = mbedtls_mpi_core_read_le( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_le( &X, 1, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
ret = mbedtls_mpi_core_read_le( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
ret = mbedtls_mpi_core_write_le( NULL, 0, NULL, 0 );
|
||||
TEST_EQUAL( ret, 0 );
|
||||
|
||||
exit:
|
||||
;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_io_be( data_t *input, int nb_int, int nx_32_int, int iret,
|
||||
int oret )
|
||||
{
|
||||
if( iret != 0 )
|
||||
TEST_ASSERT( oret == 0 );
|
||||
|
||||
TEST_LE_S( 0, nb_int );
|
||||
size_t nb = nb_int;
|
||||
|
||||
unsigned char buf[1024];
|
||||
TEST_LE_U( nb, sizeof( buf ) );
|
||||
|
||||
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
||||
* to halve the number of limbs to have the same size. */
|
||||
size_t nx;
|
||||
TEST_LE_S( 0, nx_32_int );
|
||||
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
||||
nx = nx_32_int / 2 + nx_32_int % 2;
|
||||
else
|
||||
nx = nx_32_int;
|
||||
|
||||
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
||||
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
||||
|
||||
int ret = mbedtls_mpi_core_read_be( X, nx, input->x, input->len );
|
||||
TEST_EQUAL( ret, iret );
|
||||
|
||||
if( iret == 0 )
|
||||
{
|
||||
ret = mbedtls_mpi_core_write_be( X, nx, buf, nb );
|
||||
TEST_EQUAL( ret, oret );
|
||||
}
|
||||
|
||||
if( ( iret == 0 ) && ( oret == 0 ) )
|
||||
{
|
||||
if( nb > input->len )
|
||||
{
|
||||
size_t leading_zeroes = nb - input->len;
|
||||
TEST_ASSERT( memcmp( buf + nb - input->len, input->x, input->len ) == 0 );
|
||||
for( size_t i = 0; i < leading_zeroes; i++ )
|
||||
TEST_EQUAL( buf[i], 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t leading_zeroes = input->len - nb;
|
||||
TEST_ASSERT( memcmp( input->x + input->len - nb, buf, nb ) == 0 );
|
||||
for( size_t i = 0; i < leading_zeroes; i++ )
|
||||
TEST_EQUAL( input->x[i], 0 );
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_io_le( data_t *input, int nb_int, int nx_32_int, int iret,
|
||||
int oret )
|
||||
{
|
||||
if( iret != 0 )
|
||||
TEST_ASSERT( oret == 0 );
|
||||
|
||||
TEST_LE_S( 0, nb_int );
|
||||
size_t nb = nb_int;
|
||||
|
||||
unsigned char buf[1024];
|
||||
TEST_LE_U( nb, sizeof( buf ) );
|
||||
|
||||
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
||||
* to halve the number of limbs to have the same size. */
|
||||
size_t nx;
|
||||
TEST_LE_S( 0, nx_32_int );
|
||||
if( sizeof( mbedtls_mpi_uint ) == 8 )
|
||||
nx = nx_32_int / 2 + nx_32_int % 2;
|
||||
else
|
||||
nx = nx_32_int;
|
||||
|
||||
mbedtls_mpi_uint X[sizeof( buf ) / sizeof( mbedtls_mpi_uint )];
|
||||
TEST_LE_U( nx, sizeof( X ) / sizeof( X[0] ) );
|
||||
|
||||
int ret = mbedtls_mpi_core_read_le( X, nx, input->x, input->len );
|
||||
TEST_EQUAL( ret, iret );
|
||||
|
||||
if( iret == 0 )
|
||||
{
|
||||
ret = mbedtls_mpi_core_write_le( X, nx, buf, nb );
|
||||
TEST_EQUAL( ret, oret );
|
||||
}
|
||||
|
||||
if( ( iret == 0 ) && ( oret == 0 ) )
|
||||
{
|
||||
if( nb > input->len )
|
||||
{
|
||||
TEST_ASSERT( memcmp( buf, input->x, input->len ) == 0 );
|
||||
for( size_t i = input->len; i < nb; i++ )
|
||||
TEST_EQUAL( buf[i], 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
TEST_ASSERT( memcmp( input->x, buf, nb ) == 0 );
|
||||
for( size_t i = nb; i < input->len; i++ )
|
||||
TEST_EQUAL( input->x[i], 0 );
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
;
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_mod_setup( int ext_rep, int int_rep, int iret )
|
||||
{
|
||||
@ -741,33 +597,6 @@ exit:
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_lt_ct( char *input_X, char *input_Y, int exp_ret )
|
||||
{
|
||||
mbedtls_mpi_uint *X = NULL;
|
||||
size_t X_limbs;
|
||||
mbedtls_mpi_uint *Y = NULL;
|
||||
size_t Y_limbs;
|
||||
int ret;
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &X, &X_limbs, input_X ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi_core( &Y, &Y_limbs, input_Y ) );
|
||||
|
||||
/* We need two same-length limb arrays */
|
||||
TEST_EQUAL( X_limbs, Y_limbs );
|
||||
|
||||
TEST_CF_SECRET( X, X_limbs * sizeof( mbedtls_mpi_uint ) );
|
||||
TEST_CF_SECRET( Y, X_limbs * sizeof( mbedtls_mpi_uint ) );
|
||||
|
||||
ret = mbedtls_mpi_core_lt_ct( X, Y, X_limbs );
|
||||
TEST_EQUAL( ret, exp_ret );
|
||||
|
||||
exit:
|
||||
mbedtls_free( X );
|
||||
mbedtls_free( Y );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_cond_assign( data_t * input_X,
|
||||
data_t * input_Y,
|
||||
@ -2035,457 +1864,6 @@ exit:
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_add_if( char * input_A, char * input_B,
|
||||
char * input_S4, int carry4,
|
||||
char * input_S8, int carry8 )
|
||||
{
|
||||
mbedtls_mpi S4, S8, A, B;
|
||||
mbedtls_mpi_uint *a = NULL; /* first value to add */
|
||||
mbedtls_mpi_uint *b = NULL; /* second value to add */
|
||||
mbedtls_mpi_uint *sum = NULL;
|
||||
mbedtls_mpi_uint *d = NULL; /* destination - the in/out first operand */
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &S4 );
|
||||
mbedtls_mpi_init( &S8 );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S4, input_S4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S8, input_S8 ) );
|
||||
|
||||
/* We only need to work with one of (S4, carry4) or (S8, carry8) depending
|
||||
* on sizeof(mbedtls_mpi_uint)
|
||||
*/
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &S4 : &S8;
|
||||
mbedtls_mpi_uint carry = ( sizeof(mbedtls_mpi_uint) == 4 ) ? carry4 : carry8;
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, X->s );
|
||||
|
||||
/* Test cases are such that A <= B, so #limbs should be <= */
|
||||
TEST_LE_U( A.n, B.n );
|
||||
TEST_LE_U( X->n, B.n );
|
||||
|
||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
||||
|
||||
/* mbedtls_mpi_core_add_if() uses input arrays of mbedtls_mpi_uints which
|
||||
* must be the same size. The MPIs we've read in will only have arrays
|
||||
* large enough for the number they represent. Therefore we create new
|
||||
* raw arrays of mbedtls_mpi_uints and populate them from the MPIs we've
|
||||
* just read in.
|
||||
*
|
||||
* We generated test data such that B was always >= A, so that's how many
|
||||
* limbs each of these need.
|
||||
*/
|
||||
size_t limbs = B.n;
|
||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||
|
||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
||||
ASSERT_ALLOC( a, bytes );
|
||||
ASSERT_ALLOC( b, bytes );
|
||||
ASSERT_ALLOC( sum, bytes );
|
||||
ASSERT_ALLOC( d, bytes );
|
||||
|
||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
||||
* processed by mbedtls_mpi_core_add_if()) are little endian, we can just
|
||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
||||
*/
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( sum, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
||||
|
||||
/* The test cases have a <= b to avoid repetition, so we test a + b then,
|
||||
* if a != b, b + a. If a == b, we can test when a and b are aliased */
|
||||
|
||||
/* a + b */
|
||||
|
||||
/* cond = 0 => d unchanged, no carry */
|
||||
memcpy( d, a, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, b, limbs, 0 ) );
|
||||
ASSERT_COMPARE( d, bytes, a, bytes );
|
||||
|
||||
/* cond = 1 => correct result and carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, b, limbs, 1 ) );
|
||||
ASSERT_COMPARE( d, bytes, sum, bytes );
|
||||
|
||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
/* a == b, so test where a and b are aliased */
|
||||
|
||||
/* cond = 0 => d unchanged, no carry */
|
||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( b, b, limbs, 0 ) );
|
||||
ASSERT_COMPARE( b, bytes, B.p, bytes );
|
||||
|
||||
/* cond = 1 => correct result and carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( b, b, limbs, 1 ) );
|
||||
ASSERT_COMPARE( b, bytes, sum, bytes );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* a != b, so test b + a */
|
||||
|
||||
/* cond = 0 => d unchanged, no carry */
|
||||
memcpy( d, b, bytes );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_core_add_if( d, a, limbs, 0 ) );
|
||||
ASSERT_COMPARE( d, bytes, b, bytes );
|
||||
|
||||
/* cond = 1 => correct result and carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_add_if( d, a, limbs, 1 ) );
|
||||
ASSERT_COMPARE( d, bytes, sum, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( a );
|
||||
mbedtls_free( b );
|
||||
mbedtls_free( sum );
|
||||
mbedtls_free( d );
|
||||
|
||||
mbedtls_mpi_free( &S4 );
|
||||
mbedtls_mpi_free( &S8 );
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_sub( char * input_A, char * input_B,
|
||||
char * input_X4, char * input_X8,
|
||||
int carry )
|
||||
{
|
||||
mbedtls_mpi A, B, X4, X8;
|
||||
mbedtls_mpi_uint *a = NULL;
|
||||
mbedtls_mpi_uint *b = NULL;
|
||||
mbedtls_mpi_uint *x = NULL; /* expected */
|
||||
mbedtls_mpi_uint *r = NULL; /* result */
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &X4 );
|
||||
mbedtls_mpi_init( &X8 );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, X4.s );
|
||||
TEST_EQUAL( 1, X8.s );
|
||||
|
||||
/* Get the number of limbs we will need */
|
||||
size_t limbs = MAX( A.n, B.n );
|
||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||
|
||||
/* We only need to work with X4 or X8, depending on sizeof(mbedtls_mpi_uint) */
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
||||
|
||||
/* The result shouldn't have more limbs than the longest input */
|
||||
TEST_LE_U( X->n, limbs );
|
||||
|
||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
||||
|
||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
||||
ASSERT_ALLOC( a, bytes );
|
||||
ASSERT_ALLOC( b, bytes );
|
||||
ASSERT_ALLOC( x, bytes );
|
||||
ASSERT_ALLOC( r, bytes );
|
||||
|
||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
||||
* processed by mbedtls_mpi_core_sub()) are little endian, we can just
|
||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
||||
*/
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( b, B.p, B.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
||||
|
||||
/* 1a) r = a - b => we should get the correct carry */
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, b, limbs ) );
|
||||
|
||||
/* 1b) r = a - b => we should get the correct result */
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
|
||||
/* 2 and 3 test "r may be aliased to a or b" */
|
||||
/* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */
|
||||
memcpy( r, a, bytes );
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, b, limbs ) );
|
||||
|
||||
/* 2b) r -= b => we should get the correct result */
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
|
||||
/* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */
|
||||
memcpy( r, b, bytes );
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, a, r, limbs ) );
|
||||
|
||||
/* 3b) r = a - b => we should get the correct result */
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
|
||||
/* 4 tests "r may be aliased to [...] both" */
|
||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
memcpy( r, b, bytes );
|
||||
TEST_EQUAL( carry, mbedtls_mpi_core_sub( r, r, r, limbs ) );
|
||||
ASSERT_COMPARE( r, bytes, x, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( a );
|
||||
mbedtls_free( b );
|
||||
mbedtls_free( x );
|
||||
mbedtls_free( r );
|
||||
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
mbedtls_mpi_free( &X4 );
|
||||
mbedtls_mpi_free( &X8 );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_mla( char * input_A, char * input_B, char * input_S,
|
||||
char * input_X4, char * input_cy4,
|
||||
char * input_X8, char * input_cy8 )
|
||||
{
|
||||
/* We are testing A += B * s; A, B are MPIs, s is a scalar.
|
||||
*
|
||||
* However, we encode s as an MPI in the .data file as the test framework
|
||||
* currently only supports `int`-typed scalars, and that doesn't cover the
|
||||
* full range of `mbedtls_mpi_uint`.
|
||||
*
|
||||
* We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8.
|
||||
*/
|
||||
mbedtls_mpi A, B, S, X4, X8, cy4, cy8;
|
||||
mbedtls_mpi_uint *a = NULL;
|
||||
mbedtls_mpi_uint *x = NULL;
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &S );
|
||||
mbedtls_mpi_init( &X4 );
|
||||
mbedtls_mpi_init( &X8 );
|
||||
mbedtls_mpi_init( &cy4 );
|
||||
mbedtls_mpi_init( &cy8 );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &S, input_S ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy4, input_cy4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &cy8, input_cy8 ) );
|
||||
|
||||
/* The MPI encoding of scalar s must be only 1 limb */
|
||||
TEST_EQUAL( 1, S.n );
|
||||
|
||||
/* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
||||
mbedtls_mpi *cy = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &cy4 : &cy8;
|
||||
|
||||
/* The carry should only have one limb */
|
||||
TEST_EQUAL( 1, cy->n );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, S.s );
|
||||
TEST_EQUAL( 1, X->s );
|
||||
TEST_EQUAL( 1, cy->s );
|
||||
|
||||
/* Get the (max) number of limbs we will need */
|
||||
size_t limbs = MAX( A.n, B.n );
|
||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||
|
||||
/* The result shouldn't have more limbs than the longest input */
|
||||
TEST_LE_U( X->n, limbs );
|
||||
|
||||
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
||||
|
||||
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
||||
ASSERT_ALLOC( a, bytes );
|
||||
ASSERT_ALLOC( x, bytes );
|
||||
|
||||
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
||||
* processed by mbedtls_mpi_core_mla()) are little endian, we can just
|
||||
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()).
|
||||
*/
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
memcpy( x, X->p, X->n * sizeof(mbedtls_mpi_uint) );
|
||||
|
||||
/* 1a) A += B * s => we should get the correct carry */
|
||||
TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, B.p, B.n, *S.p ), *cy->p );
|
||||
|
||||
/* 1b) A += B * s => we should get the correct result */
|
||||
ASSERT_COMPARE( a, bytes, x, bytes );
|
||||
|
||||
if ( A.n == B.n && memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
/* Check when A and B are aliased */
|
||||
memcpy( a, A.p, A.n * sizeof(mbedtls_mpi_uint) );
|
||||
TEST_EQUAL( mbedtls_mpi_core_mla( a, limbs, a, limbs, *S.p ), *cy->p );
|
||||
ASSERT_COMPARE( a, bytes, x, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_free( a );
|
||||
mbedtls_free( x );
|
||||
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
mbedtls_mpi_free( &S );
|
||||
mbedtls_mpi_free( &X4 );
|
||||
mbedtls_mpi_free( &X8 );
|
||||
mbedtls_mpi_free( &cy4 );
|
||||
mbedtls_mpi_free( &cy8 );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_montg_init( char * input_N, char * input_mm )
|
||||
{
|
||||
mbedtls_mpi N, mm;
|
||||
|
||||
mbedtls_mpi_init( &N );
|
||||
mbedtls_mpi_init( &mm );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &mm, input_mm ) );
|
||||
|
||||
/* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or
|
||||
* 2 limbs (sizeof(mbedtls_mpi_uint) == 4).
|
||||
*
|
||||
* The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8;
|
||||
* for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this.
|
||||
*/
|
||||
TEST_ASSERT( mm.n == 1 || mm.n == 2 );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, N.s );
|
||||
TEST_EQUAL( 1, mm.s );
|
||||
|
||||
/* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */
|
||||
mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init( N.p );
|
||||
|
||||
/* Check we got the correct result */
|
||||
TEST_EQUAL( result, mm.p[0] );
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &mm );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void mpi_core_montmul( int limbs_AN4, int limbs_B4,
|
||||
int limbs_AN8, int limbs_B8,
|
||||
char * input_A,
|
||||
char * input_B,
|
||||
char * input_N,
|
||||
char * input_X4,
|
||||
char * input_X8 )
|
||||
{
|
||||
mbedtls_mpi A, B, N, X4, X8, T, R;
|
||||
|
||||
mbedtls_mpi_init( &A );
|
||||
mbedtls_mpi_init( &B );
|
||||
mbedtls_mpi_init( &N );
|
||||
mbedtls_mpi_init( &X4 ); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */
|
||||
mbedtls_mpi_init( &X8 ); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */
|
||||
mbedtls_mpi_init( &T );
|
||||
mbedtls_mpi_init( &R ); /* for the result */
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &A, input_A ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &B, input_B ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &N, input_N ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X4, input_X4 ) );
|
||||
TEST_EQUAL( 0, mbedtls_test_read_mpi( &X8, input_X8 ) );
|
||||
|
||||
mbedtls_mpi *X = ( sizeof(mbedtls_mpi_uint) == 4 ) ? &X4 : &X8;
|
||||
|
||||
int limbs_AN = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_AN4 : limbs_AN8;
|
||||
int limbs_B = ( sizeof(mbedtls_mpi_uint) == 4 ) ? limbs_B4 : limbs_B8;
|
||||
|
||||
TEST_LE_U( A.n, (size_t)limbs_AN );
|
||||
TEST_LE_U( X->n, (size_t)limbs_AN );
|
||||
TEST_LE_U( B.n, (size_t)limbs_B );
|
||||
TEST_LE_U( limbs_B, limbs_AN );
|
||||
|
||||
/* All of the inputs are +ve (or zero) */
|
||||
TEST_EQUAL( 1, A.s );
|
||||
TEST_EQUAL( 1, B.s );
|
||||
TEST_EQUAL( 1, N.s );
|
||||
TEST_EQUAL( 1, X->s );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &A, limbs_AN ) );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &N, limbs_AN ) );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( X, limbs_AN ) );
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &B, limbs_B ) );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &T, limbs_AN * 2 + 1 ) );
|
||||
|
||||
/* Calculate the Montgomery constant (this is unit tested separately) */
|
||||
mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init( N.p );
|
||||
|
||||
TEST_EQUAL( 0, mbedtls_mpi_grow( &R, limbs_AN ) ); /* ensure it's got the right number of limbs */
|
||||
|
||||
mbedtls_mpi_core_montmul( R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
size_t bytes = N.n * sizeof(mbedtls_mpi_uint);
|
||||
ASSERT_COMPARE( R.p, bytes, X->p, bytes );
|
||||
|
||||
/* The output (R, above) may be aliased to A - use R to save the value of A */
|
||||
|
||||
memcpy( R.p, A.p, bytes );
|
||||
|
||||
mbedtls_mpi_core_montmul( A.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( A.p, bytes, X->p, bytes );
|
||||
|
||||
memcpy( A.p, R.p, bytes ); /* restore A */
|
||||
|
||||
/* The output may be aliased to N - use R to save the value of N */
|
||||
|
||||
memcpy( R.p, N.p, bytes );
|
||||
|
||||
mbedtls_mpi_core_montmul( N.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( N.p, bytes, X->p, bytes );
|
||||
|
||||
memcpy( N.p, R.p, bytes );
|
||||
|
||||
if (limbs_AN == limbs_B)
|
||||
{
|
||||
/* Test when A aliased to B (requires A == B on input values) */
|
||||
if ( memcmp( A.p, B.p, bytes ) == 0 )
|
||||
{
|
||||
/* Test with A aliased to B and output, since this is permitted -
|
||||
* don't bother with yet another test with only A and B aliased */
|
||||
|
||||
mbedtls_mpi_core_montmul( B.p, B.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( B.p, bytes, X->p, bytes );
|
||||
|
||||
memcpy( B.p, A.p, bytes ); /* restore B from equal value A */
|
||||
}
|
||||
|
||||
/* The output may be aliased to B - last test, so we don't save B */
|
||||
|
||||
mbedtls_mpi_core_montmul( B.p, A.p, B.p, B.n, N.p, N.n, mm, T.p );
|
||||
ASSERT_COMPARE( B.p, bytes, X->p, bytes );
|
||||
}
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &A );
|
||||
mbedtls_mpi_free( &B );
|
||||
mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &X4 );
|
||||
mbedtls_mpi_free( &X8 );
|
||||
mbedtls_mpi_free( &T );
|
||||
mbedtls_mpi_free( &R );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
||||
void mpi_selftest( )
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user