diff options
author | Joachim Strömbergson <joachim@secworks.se> | 2015-04-27 11:17:08 +0200 |
---|---|---|
committer | Joachim Strömbergson <joachim@secworks.se> | 2015-04-27 11:17:08 +0200 |
commit | a315223f98fa6f1fdea2b1080c5f3e33352ebb13 (patch) | |
tree | 0fb3f6c458df78f58017e9475ff3b0c5cb3b52d0 /src/model | |
parent | 502f0f429a261628fe5e43582280012541c40804 (diff) |
Updating modexp core to v 0.50. This version contains a working core that can perform sign and verify with big keys/values. The core builds ok in Altera and Xilinx FPGA tools. This commit also includes a new testgenerator capable of generating testbench for modexp with autgenerated test data of different lengths. The README has been updated with status and implementation results in for different FPGA devices.
Diffstat (limited to 'src/model')
-rw-r--r-- | src/model/c/src/autogenerated_tests.c | 154 | ||||
-rw-r--r-- | src/model/c/src/montgomery_array.c | 23 | ||||
-rw-r--r-- | src/model/c/src/montgomery_array.h | 2 | ||||
-rwxr-xr-x | src/model/python/modexp.py | 203 |
4 files changed, 366 insertions, 16 deletions
diff --git a/src/model/c/src/autogenerated_tests.c b/src/model/c/src/autogenerated_tests.c index e9ec0b7..2a80c42 100644 --- a/src/model/c/src/autogenerated_tests.c +++ b/src/model/c/src/autogenerated_tests.c @@ -202,6 +202,146 @@ void autogenerated_BASIC_M7037130911981370263(void) { mod_exp_array(1, X, E, M, Z); assertArrayEquals(1, expected, Z); } +void autogenerated_BASIC_5073338267670769216(void) { + printf("=== autogenerated_BASIC_5073338267670769216 ===\n"); + uint32_t X[] = { 0x3028983f, 0xdc9bdc25, 0xa3fdfeda, 0x283f4463 }; + uint32_t E[] = { 0x29493211, 0xc4252db0, 0x7775443d, 0x13e1d929 }; + uint32_t M[] = { 0x2fb9ba2f, 0xa485d5f7, 0x3c6652c9, 0x670fdbfd }; + uint32_t expected[] = { 0x2f45cdd6, 0x23f0e6f2, 0xaa4bd5d8, 0x6297da06 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M1841989679506188752(void) { + printf("=== autogenerated_BASIC_M1841989679506188752 ===\n"); + uint32_t X[] = { 0x29462882, 0x12caa2d5, 0xb80e1c66, 0x1006807f }; + uint32_t E[] = { 0x3285c343, 0x2acbcb0f, 0x4d023228, 0x2ecc73db }; + uint32_t M[] = { 0x267d2f2e, 0x51c216a7, 0xda752ead, 0x48d22d89 }; + uint32_t expected[] = { 0x0ddc404d, 0x91600596, 0x7425a8d8, 0xa066ca56 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M3339729654500648482(void) { + printf("=== autogenerated_BASIC_M3339729654500648482 ===\n"); + uint32_t X[] = { 0x2963efb9, 0xc6f5d260, 0xa2d0fe74, 0x49726b57 }; + uint32_t E[] = { 0x2f55c103, 0xbace4bf1, 0x2ab9fac2, 0x30aec7d3 }; + uint32_t M[] = { 0x376cf9ae, 0xd9e988e8, 0xbd995f5c, 0xdeec42f5 }; + uint32_t expected[] = { 0x0ce8cff7, 0x9f564e2c, 0x1b61e3d9, 0x717db9ef }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M6837928193394880512(void) { + printf("=== autogenerated_BASIC_M6837928193394880512 ===\n"); + uint32_t X[] = { 0x2a9283cc, 0x5999f49d, 0xf8cf6ab2, 0x5f47bf25 }; + uint32_t E[] = { 0x2c7564a0, 0x2d1fcda1, 0x2825318a, 0xae23c271 }; + uint32_t M[] = { 0x32b892f9, 0x096c5ada, 0x43918370, 0x8398c7e3 }; + uint32_t expected[] = { 0x27cf8839, 0x94004d82, 0xa0d69d86, 0x3f61a929 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M7333111649825079555(void) { + printf("=== autogenerated_BASIC_M7333111649825079555 ===\n"); + uint32_t X[] = { 0x246fa2ec, 0x405f234d, 0x39b93e77, 0xf16bcc91 }; + uint32_t E[] = { 0x2807eb7a, 0x646df633, 0xeaa95a21, 0x85252adf }; + uint32_t M[] = { 0x2cdd3307, 0x782e5711, 0x584f179b, 0x011087df }; + uint32_t expected[] = { 0x02d6e42e, 0x46e2d304, 0x435170c1, 0x9f344f83 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_480186175131589607(void) { + printf("=== autogenerated_BASIC_480186175131589607 ===\n"); + uint32_t X[] = { 0x300a5cf7, 0x269f6369, 0x02e025cb, 0xaf16fcfd }; + uint32_t E[] = { 0x2cc4b1c0, 0x9205a8b4, 0xbc130ee2, 0x923f1f3f }; + uint32_t M[] = { 0x2cd376d5, 0xd9e3b080, 0x2533288a, 0xd4b9bb37 }; + uint32_t expected[] = { 0x0ae9c475, 0xf4fb4c63, 0xcf2c4f56, 0x902eba0c }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M5239159917778665002(void) { + printf("=== autogenerated_BASIC_M5239159917778665002 ===\n"); + uint32_t X[] = { 0x3eaed5af, 0xa287db7e, 0x4ff07fee, 0x9bbda80b }; + uint32_t E[] = { 0x3c077d49, 0xf3a131ab, 0x6289042a, 0xc15083cb }; + uint32_t M[] = { 0x344b8538, 0xcf4f2576, 0xd28c1c52, 0xc83a8199 }; + uint32_t expected[] = { 0x15028046, 0x5c12d235, 0x7580fd1b, 0x6bc76b24 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_228752064885223799(void) { + printf("=== autogenerated_BASIC_228752064885223799 ===\n"); + uint32_t X[] = { 0x3904d7ab, 0x13937a4f, 0x926856d1, 0x6bdda621 }; + uint32_t E[] = { 0x3d360083, 0xa50eaf0e, 0xffce2df2, 0xb1f51cef }; + uint32_t M[] = { 0x2d32376f, 0x205555b3, 0x2c9daf8c, 0xe2b7cf81 }; + uint32_t expected[] = { 0x08836692, 0xe6398828, 0x1eeccd57, 0x2c231153 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_856940511857911599(void) { + printf("=== autogenerated_BASIC_856940511857911599 ===\n"); + uint32_t X[] = { 0x23e80223, 0x52b700ee, 0x6cb8a294, 0x47c6fac9 }; + uint32_t E[] = { 0x253cebdb, 0xcc78dcb4, 0x925682b3, 0x490c424b }; + uint32_t M[] = { 0x2f2885eb, 0x67987cee, 0x717298bd, 0x7a1baf7b }; + uint32_t expected[] = { 0x2bb0b86c, 0xc854652b, 0x2b2bce27, 0xd2595a8e }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M6501553661140603953(void) { + printf("=== autogenerated_BASIC_M6501553661140603953 ===\n"); + uint32_t X[] = { 0x287e9711, 0x4d346dab, 0x3ff5e6da, 0xa93edebd }; + uint32_t E[] = { 0x3658192a, 0x4b5fac3f, 0x9a78bc81, 0x5ac8c667 }; + uint32_t M[] = { 0x238cd95d, 0x298ee5e1, 0x320323da, 0x0a46ec21 }; + uint32_t expected[] = { 0x0415fef6, 0x5fc81702, 0x8be83fd4, 0x4c7fbf24 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(4, X, E, M, Z); + assertArrayEquals(4, expected, Z); +} +void autogenerated_BASIC_M8496483018338900149(void) { + printf("=== autogenerated_BASIC_M8496483018338900149 ===\n"); + uint32_t X[] = { 0x3b4511d5, 0x5f61da31, 0x4bf252e0, 0x3962f93c, 0x590171d0, 0xda31097f, 0x0f73fee3, 0x0ba1b379, 0x514b7d8f, 0x1e337cf9, 0x733512ac, 0x4f5b0b52, 0x40762697, 0xb3a30c84, 0x5563b4db, 0x59f7cef1 }; + uint32_t E[] = { 0x271cb7c4, 0x11f07a63, 0x1df850e7, 0x8bf6df66, 0x7bc8fa0e, 0xa51002ce, 0xf16946c5, 0x96916dc7, 0xba1681b1, 0x5ca395ab, 0x7839780d, 0xc5e760c3, 0x578af4f9, 0xffbbbd8c, 0x8576c8fc, 0x518012a7 }; + uint32_t M[] = { 0x3c0f154d, 0x7fc7750a, 0x03eb8968, 0xfbde501a, 0x63848fe5, 0xdcd7d883, 0x5131c9f9, 0xa9ca3399, 0xba581205, 0x4cf86f2a, 0xed928b92, 0x13a0e90f, 0x5b24c81a, 0xf4ac077c, 0x68b8ac70, 0xc58961fd }; + uint32_t expected[] = { 0x30d0a0c1, 0x1be7b00f, 0x9ed921c6, 0x7a7f9a87, 0xb8ed7f88, 0xd81f20d5, 0xb3ba9839, 0x38dfdfb4, 0x70b1927c, 0xa4b9487b, 0x8d8d7e2c, 0xc706e737, 0x17e9c280, 0xbec0006a, 0xeea7dc97, 0x4f5badc9 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(16, X, E, M, Z); + assertArrayEquals(16, expected, Z); +} +void autogenerated_BASIC_6145567102166328515(void) { + printf("=== autogenerated_BASIC_6145567102166328515 ===\n"); + uint32_t X[] = { 0x23446522, 0x9185c81e, 0x09283a50, 0x82c1f517, 0xd00d3159, 0x846c2c99, 0x261d1dcb, 0xde183d66, 0x98f8a990, 0xd295bd50, 0x09ef8644, 0xadcf9cdb, 0x5eec13a3, 0x92baa627, 0x18caa215, 0x8836480f }; + uint32_t E[] = { 0x397f2b38, 0xa95cc0bc, 0xc13b26cf, 0xa20dda3c, 0xf8801c39, 0x00731abe, 0x2ad0afc4, 0xdb247141, 0xc29b5a2d, 0x9e51a3ed, 0xcf364a51, 0x90b761d5, 0xfa0624d3, 0x3a0b27c7, 0xa36bc66c, 0x6423efd3 }; + uint32_t M[] = { 0x3ad2464f, 0x75da362c, 0x6e5c37b4, 0x432cc6b7, 0x6f48b57c, 0xebb87e14, 0x0a3d3f4d, 0xfa4c32c0, 0x165a5892, 0x742f720d, 0x8b4e1b43, 0x281d5390, 0xff2f77dc, 0x698dbc05, 0xdee97a68, 0xde2c176d }; + uint32_t expected[] = { 0x083aa822, 0x7c70341a, 0xa2cbc9a8, 0xd116bc41, 0x79a81ba7, 0x584c91c8, 0x5b87c314, 0xc5e6f4f9, 0x601ebead, 0x054bf14b, 0x65b48a4a, 0xf8aa2700, 0xa765ee1d, 0xb6c638cd, 0x7e529f23, 0x0d4adfff }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(16, X, E, M, Z); + assertArrayEquals(16, expected, Z); +} +void autogenerated_BASIC_7216348574014690328(void) { + printf("=== autogenerated_BASIC_7216348574014690328 ===\n"); + uint32_t X[] = { 0x35baa860, 0x4e47ad49, 0xc6c4a7c0, 0x7857335b, 0x9b81d24f, 0x7be86e34, 0xf84f7560, 0x484b20db, 0xb83b4f9e, 0x694c6987, 0x7d3232f5, 0x18ee8603, 0x94eca5ef, 0x5179ef69, 0xf6600efb, 0xfc71deab, 0xdb939552, 0x642db1e0, 0x78e11e39, 0x924f0dbd, 0xdb225803, 0x449bbb35, 0xfc40ee05, 0x9b19931c, 0x8b8af884, 0xb5f96476, 0xf97ad419, 0xcc7543f9, 0xce25ed83, 0x94da3499, 0x4f37f331, 0xe64e7799 }; + uint32_t E[] = { 0x3a7570f2, 0x38cb0f2f, 0x2e6c8989, 0xcf7c2665, 0xa0fce3d0, 0x12c7a8eb, 0x40fab1ea, 0x39eb4809, 0x822fa6cc, 0x4ef9d604, 0x2ca1cd3b, 0xa9b23cdb, 0x17e823ce, 0x5fea5198, 0x1ab12946, 0xcec748b5, 0x752a3a6f, 0x73421a9a, 0x7138d7a4, 0xa47327c0, 0x17475543, 0xe841c19a, 0x3085410a, 0x06438b4c, 0xe0d4b918, 0xfeccca17, 0x9ed86072, 0x86db4a93, 0x60c7d437, 0xcdfe77e3, 0x2631f264, 0x80c9b645 }; + uint32_t M[] = { 0x34d90901, 0xf192009c, 0xc34f345f, 0x63f592b2, 0xaba32d7a, 0x161d1510, 0x2c264dec, 0x07306f1d, 0x3e61c031, 0xacd4eba0, 0xff1318ff, 0x09a78cf4, 0x97bace67, 0xc8fcecf4, 0x3b3901a3, 0x5d447957, 0xc0397708, 0x7e7e48f9, 0x571db58a, 0x80d65921, 0x68a025e0, 0x4f85f776, 0xaa8450c7, 0x15c42f52, 0xe65507f2, 0xdfeed660, 0x0db8eddb, 0xb1e48d93, 0x7e314a2f, 0xea81ccb1, 0xbe22cc03, 0xf2928621 }; + uint32_t expected[] = { 0x14c07087, 0x3e92a437, 0xbfd5a5b3, 0xec017ed5, 0xacf23e12, 0x6e48a51f, 0xe1fdbde9, 0x43fade6b, 0x98935c7a, 0xaff9b1e5, 0x3adaa120, 0xcbaa5af5, 0x344fabb2, 0x8d2987c7, 0xfb83d342, 0x3f85bbfc, 0xc30e755a, 0x37f20fa4, 0x7fb5621b, 0xcd1cef03, 0x664ccb56, 0xce0a28b9, 0xa9cbdd51, 0xad12eb24, 0xacc08c8d, 0x5d9fe7f5, 0x018c1e1e, 0x2115bba8, 0x22b52262, 0xaee3bf8a, 0x91824a22, 0xde48a1f3 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(32, X, E, M, Z); + assertArrayEquals(32, expected, Z); +} +void autogenerated_BASIC_M5663191947183200100(void) { + printf("=== autogenerated_BASIC_M5663191947183200100 ===\n"); + uint32_t X[] = { 0x21558179, 0x3e2914b1, 0xefe95957, 0x965fdead, 0xe766d8fc, 0x136eadf4, 0xa6106a2a, 0x88b2df7e, 0xe0b0eaae, 0x2c17946a, 0x6f5b5563, 0x228052ae, 0x7fc40d80, 0xf81354db, 0xfceecd1a, 0xa5e4c97d, 0x433ecfcd, 0xc20d1e4d, 0x2a748fe3, 0x1d9e63f0, 0xdc6c25d6, 0xdae5c8be, 0x1d8c5431, 0xb1d7d270, 0xed5b2566, 0x1463b0fd, 0xa9e26cf7, 0x3dd6fbd7, 0x1347c8f7, 0x76c2cc37, 0xf382b786, 0x1d5ac517, 0x26b96692, 0x2c1fe6f8, 0x5852dbf8, 0x4bcabda2, 0xbedb2f5f, 0xbfe58158, 0x8cd5d15f, 0xac7c7f4c, 0xf8ba47d2, 0x86c6571d, 0x06a4760b, 0xa6afa0e1, 0x7a819f62, 0x5cdbfe15, 0x9b2d10b5, 0xf508b1fd, 0xb3f0462a, 0x92f45a64, 0x69b6ec58, 0xbfad8fab, 0x6799260f, 0x27415db5, 0xf6ac7832, 0xe547826d, 0x6a9806a5, 0x36c62a88, 0x98bee14d, 0x9b8c2648, 0xabdbbd3d, 0xaf59eea1, 0x164eacb5, 0x3a18e427 }; + uint32_t E[] = { 0x2519837b, 0xe73a9031, 0xe241606d, 0x21e70fa2, 0x7881f254, 0x4e60831d, 0x266f408e, 0x4a83e6ed, 0xa7741995, 0x32b477ba, 0x91bdf5d0, 0x4acd7a06, 0x51e344b9, 0xdf376e4e, 0x8494e625, 0xa0cc9697, 0x817a0c93, 0x3b68cefb, 0x46de14c1, 0x52229965, 0x329645bd, 0xf4176adc, 0x29a8bc50, 0x44900fec, 0x1558d492, 0xf838a8e7, 0xea207abd, 0xcd21a28c, 0x91e6b02f, 0x2a490ea8, 0x5d99663b, 0x87c92fb6, 0x0a185325, 0x5256a7a3, 0x496b7288, 0x6688b6c8, 0x650e1776, 0x54cd429f, 0x90ea3b18, 0x0b72ae61, 0xcc8651b3, 0xa488742d, 0x93c401ef, 0x5a2220ff, 0xaee1f257, 0xf9d1e29a, 0xd47151fe, 0x4978342b, 0x0927048a, 0x404b0689, 0xdc9df8cc, 0xfba9845f, 0xeb8a39b0, 0xd3f24ae2, 0x5ea9ca0a, 0x0c064f94, 0x35368ae2, 0xeab6c035, 0x9baa39c6, 0x2ef6259d, 0xa2577555, 0x514c7d98, 0x0890d44f, 0xf416fbdd }; + uint32_t M[] = { 0x2c5337a9, 0x3f2e1ca6, 0x91de65ea, 0xc3f9a3c2, 0xdc9099e0, 0x64ebe412, 0xf4583fae, 0x1fc8e8dd, 0x92dcbbfb, 0x9159239e, 0xdbbec456, 0x8735a660, 0x8248dbbc, 0x76f01415, 0x3cb8a897, 0x7cc09280, 0x6cc6db51, 0x9c2544da, 0x316564ce, 0x4b6d9b3b, 0x3e0e123f, 0x942a4a3c, 0x1f128873, 0x5ad14862, 0xdde8e6dd, 0x73da31fb, 0x1a8a2046, 0xc3ff18c6, 0x24e31d54, 0x7d8a1796, 0x88ab346c, 0x262bb321, 0x2cada5dc, 0x1fb2284c, 0x042375fd, 0xba10d309, 0xcda978ec, 0x229ee156, 0x8470728a, 0xa58017fd, 0x65727801, 0x1ea396a6, 0xbd9a4bc1, 0x8e97c08f, 0xd7529796, 0x2c8339e9, 0xc5340a83, 0x6f7d1f9c, 0xd6014fec, 0xdffa2265, 0xfa9906a9, 0xafbd424a, 0x631994ae, 0x73a9b3f1, 0x2284f999, 0x6f8c87f6, 0x93136a66, 0x47c81e45, 0xd35f0e41, 0x238d6960, 0x96cf337d, 0x8865e4cc, 0x15039c40, 0x65ee7211 }; + uint32_t expected[] = { 0x24665860, 0x4b150493, 0xc0834602, 0xc0b99ab5, 0xbe649545, 0xa7d8b1ca, 0x55c1b98a, 0x1dce374b, 0x65750415, 0x573dfed7, 0x95df9943, 0x58a4aea0, 0x5fb40a92, 0x1408d9c2, 0xb5e23fc9, 0x225eb60b, 0x41d33a41, 0xbf958f7f, 0x619f5ac1, 0x207647f3, 0x223e56f8, 0x26afd4ae, 0x6a297840, 0x830947db, 0xbc5af940, 0x4c97ebb1, 0xca38b220, 0x04c9a26d, 0x49a16b72, 0x0882c658, 0x2dbc50e0, 0x67e2d057, 0x4b8ef356, 0x4ba5eac3, 0x17237d9f, 0x27c111a8, 0xc1b1944e, 0xe91fd6b6, 0xa78d9747, 0x61e946d3, 0x0078fe23, 0x7770a088, 0x6d5762af, 0x435ac5f9, 0x36cde9d5, 0xc313804d, 0xa4623760, 0xb1c37572, 0x2b22486d, 0x8af131e3, 0x3e5fc3ea, 0x0d9c9ba0, 0x218bcc8f, 0x8bcdfea2, 0xcf55a599, 0x57b9fcbc, 0x5c087f62, 0xec130a15, 0x7e8bd1f5, 0x60eaaa51, 0x020dd89b, 0x890cc6ea, 0x042d0054, 0x74055863 }; + uint32_t Z[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 }; + mod_exp_array(64, X, E, M, Z); + assertArrayEquals(64, expected, Z); +} void autogenerated_tests(void) { autogenerated_BASIC_M4962768465676381896(); autogenerated_BASIC_8982867242010371843(); @@ -223,5 +363,19 @@ void autogenerated_tests(void) { autogenerated_BASIC_M2453278165832221565(); autogenerated_BASIC_M1847183855567461116(); autogenerated_BASIC_M7037130911981370263(); + autogenerated_BASIC_5073338267670769216(); + autogenerated_BASIC_M1841989679506188752(); + autogenerated_BASIC_M3339729654500648482(); + autogenerated_BASIC_M6837928193394880512(); + autogenerated_BASIC_M7333111649825079555(); + autogenerated_BASIC_480186175131589607(); + autogenerated_BASIC_M5239159917778665002(); + autogenerated_BASIC_228752064885223799(); + autogenerated_BASIC_856940511857911599(); + autogenerated_BASIC_M6501553661140603953(); + autogenerated_BASIC_M8496483018338900149(); + autogenerated_BASIC_6145567102166328515(); + autogenerated_BASIC_7216348574014690328(); + autogenerated_BASIC_M5663191947183200100(); } diff --git a/src/model/c/src/montgomery_array.c b/src/model/c/src/montgomery_array.c index 0ddc742..c3579a9 100644 --- a/src/model/c/src/montgomery_array.c +++ b/src/model/c/src/montgomery_array.c @@ -3,22 +3,15 @@ #include "bignum_uint32_t.h" #include "montgomery_array.h" -void mont_prod_array(uint32_t length, uint32_t *A, uint32_t *B, uint32_t *M, - uint32_t *temp, uint32_t *s) { +void mont_prod_array(uint32_t length, uint32_t *A, uint32_t *B, uint32_t *M, uint32_t *s) { zero_array(length, s); for (int32_t wordIndex = ((int32_t) length) - 1; wordIndex >= 0; wordIndex--) { for (int i = 0; i < 32; i++) { - int b = (B[wordIndex] >> i) & 1; + uint32_t b = (B[wordIndex] >> i) & 1; //q = (s - b * A) & 1; - sub_array(length, s, A, temp); - int q; - if (b == 1) { - q = temp[length - 1] & 1; - } else { - q = s[length - 1] & 1; - } + uint32_t q = (s[length-1] ^ (A[length-1] & b)) & 1; // int q = (s - b * A) & 1; // s = (s + q*M + b*A) >>> 1; if (q == 1) { @@ -77,11 +70,11 @@ void mont_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, // 2. Z0 := MontProd( 1, Nr, M ) zero_array(length, ONE); ONE[length - 1] = 1; - mont_prod_array(length, ONE, Nr, M, temp, Z); + mont_prod_array(length, ONE, Nr, M, Z); //debugArray("Z0", length, Z); // 3. P0 := MontProd( X, Nr, M ); - mont_prod_array(length, X, Nr, M, temp, P); + mont_prod_array(length, X, Nr, M, P); //debugArray("P0", length, P); // 4. for i = 0 to n-1 loop @@ -91,18 +84,18 @@ void mont_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, uint32_t ei = (ei_ >> (i % 32)) & 1; // 6. if (ei = 1) then Zi+1 := MontProd ( Zi, Pi, M) else Zi+1 := Zi if (ei == 1) { - mont_prod_array(length, Z, P, M, temp, temp2); + mont_prod_array(length, Z, P, M, temp2); copy_array(length, temp2, Z); //debugArray("Z ", length, Z); } // 5. Pi+1 := MontProd( Pi, Pi, M ); - mont_prod_array(length, P, P, M, temp, temp2); + mont_prod_array(length, P, P, M, temp2); copy_array(length, temp2, P); //debugArray("P ", length, P); // 7. end for } // 8. Zn := MontProd( 1, Zn, M ); - mont_prod_array(length, ONE, Z, M, temp, temp2); + mont_prod_array(length, ONE, Z, M, temp2); copy_array(length, temp2, Z); //debugArray("Z ", length, Z); // 9. RETURN Zn diff --git a/src/model/c/src/montgomery_array.h b/src/model/c/src/montgomery_array.h index 82ef9de..984bc07 100644 --- a/src/model/c/src/montgomery_array.h +++ b/src/model/c/src/montgomery_array.h @@ -9,7 +9,7 @@ #define MONTGOMERY_ARRAY_H_ void mont_prod_array(uint32_t length, uint32_t *A, uint32_t *B, uint32_t *M, - uint32_t *temp, uint32_t *s); + uint32_t *s); void mod_exp_array(uint32_t length, uint32_t *X, uint32_t *E, uint32_t *M, uint32_t *Z); #endif /* MONTGOMERY_ARRAY_H_ */ diff --git a/src/model/python/modexp.py b/src/model/python/modexp.py new file mode 100755 index 0000000..97aab0b --- /dev/null +++ b/src/model/python/modexp.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +#======================================================================= +# +# modexp.py +# --------- +# A python model for doing modular exponention. +# +# +# Author: Joachim Strömbergson +# Copyright (c) 2014, Secworks Sweden AB +# +# Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +#======================================================================= + +#------------------------------------------------------------------- +# Python module imports. +#------------------------------------------------------------------- +import sys + + +#------------------------------------------------------------------- +# Defines. +#------------------------------------------------------------------- +VERBOSE = False + + +#------------------------------------------------------------------- +# iter_mult() +# +# Iterative multiplier (i*j) with operands that are bitlen +# number of bits. +#------------------------------------------------------------------- +def iter_mult(i, j, bitlen): + print("Mult of 0x%08x and 0x%08x of max 0x%08x bits" % + (i, j, bitlen)) + + r = 0 + max = 2**bitlen - 1 + + for bit in range(bitlen): + mask = ((j & (1 << bit))) + r = (r + (i * mask)) & max + print("bit: 0x%08x, mask = 0x%01x, r = 0x%08x" % + (bit, mask, r)) + return r + + +#------------------------------------------------------------------- +# iter_exp() +# +# Iterative exponentiator (i ** j) with operands that are +# bitlen number of bits. +#------------------------------------------------------------------- +def iter_exp(i, j, bitlen): + print("Exp of 0x%08x and 0x%08x of max 0x%08x bits" % + (i, j, bitlen)) + + n = i + for bit in range(j): + n = iter_mult(n, n, bitlen) + return n + + +#------------------------------------------------------------------- +# gen_keypair() +# +# Generate a keypair (and exponent) with n bits in length. +#------------------------------------------------------------------- +def gen_keypair(bitlen): + print("Generating keys with %d bits" % (bitlen)) + print("") + + e = 3 + pub = 2**bitlen - 1 + priv = pub - 2 + + return (pub, priv, e) + + +#------------------------------------------------------------------- +# keytest() +#------------------------------------------------------------------- +def keytest(): + print("key encryption and decryption") + print("-----------------------------") + + p = 11 + q = 13 + n = p * q + tiotent = (p - 1) * (q - 1) + + print("p = %d, q = %d, n = %d, tiotent = %d" % (p, q, n, tiotent)) + + e = 7 + d = 103 + + print("e = %d, d = %d" % (e, d)) + + print("Public key: e, n = %d, %d" % (e, n)) + print("private key: d = %d" % (d)) + + m = 9 + cm = modexp(m, e, n) + m2 = modexp(cm, d, n) + print("Encryption of message m = %d -> cm = %d" % (m, cm)) + print("Decryption of message cm = %d -> m = %d" % (cm, m2)) + + +#------------------------------------------------------------------- +# modtest() +#------------------------------------------------------------------- +def modtest(): + print("modular exponentition") + print("---------------------") + + M = 12345 + e = 3 + N = 12347 + + print("M = %d, e = %d, N = %d" % (M, e, N)) + print(modexp(M, e, N)) + print("") + + M = 2**8192 - 37 + e = 3 + N = 2**8192 - 1 + + print("M = %d, e = %d, N = %d" % (M, e, N)) + print(modexp(M, e, N)) + print("") + + +#------------------------------------------------------------------- +# modexp() +# +# Perform generic modular exponention of the given message M +# using the exponent e and modulus N. +#------------------------------------------------------------------- +def modexp(M, e, N): + return (M ** e) % N + + +#------------------------------------------------------------------- +# main() +# +# Parse any arguments and run the tests. +#------------------------------------------------------------------- +def main(): +# my_keypair = gen_keypair(12) +# print(my_keypair) +# modtest() +# keytest() + + # test of iterative multiply. + print(iter_mult(2, 3, 4)) + print(iter_mult(2, 3, 5)) + print(iter_mult(2543, 1201, 12)) + print(iter_mult(2543, 1201, 16)) + print(iter_mult(2543, 1201, 23)) + + # test of iterative exponentiation. + print(iter_exp(2, 3, 12)) + print(iter_exp(8, 8, 4)) + + +#------------------------------------------------------------------- +# __name__ +# Python thingy which allows the file to be run standalone as +# well as parsed from within a Python interpreter. +#------------------------------------------------------------------- +if __name__=="__main__": + # Run the main function. + sys.exit(main()) + + +#======================================================================= +# EOF modexp.py +#======================================================================= |