Commit 2a4d2172 authored by Davide Galassi's avatar Davide Galassi

Critical MPI square bugfix

Unhandled possible integer overflow
parent b56cb2a6
#include "mpi_pvt.h"
#include <stdio.h>
int cry_mpi_sqr(cry_mpi *r, const cry_mpi *a)
{
......@@ -35,6 +36,8 @@ int cry_mpi_sqr(cry_mpi *r, const cry_mpi *a)
>> (CRY_MPI_DIGIT_BITS - 1);
dd <<= 1; /* mul by 2 */
dd += ((cry_mpi_dword)*tmpt + c);
if (ch == 0 && dd < ((cry_mpi_dword)*tmpt + c))
ch = (cry_mpi_dword)1U << CRY_MPI_DIGIT_BITS;
/* store lower part */
*tmpt++ = (cry_mpi_digit)dd;
/* get the new carry */
......
......@@ -131,6 +131,17 @@ static void mpi_abs(int argc, char *argv[])
#define ERROR_FLAG 'Z'
static void check(int res, cry_mpi *num, char *res_str)
{
if (*res_str != ERROR_FLAG) {
ASSERT(res == 0);
ASSERT(cry_mpi_store_str(num, 16, (char *)g_buf) == 0);
ASSERT(strcmp((char *)g_buf, res_str) == 0);
} else {
ASSERT(atoi(res_str + 1) == res);
}
}
typedef int (* binary_op_f)(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
static void mpi_binary_op(int argc, char *argv[], binary_op_f op)
......@@ -144,13 +155,7 @@ static void mpi_binary_op(int argc, char *argv[], binary_op_f op)
res = op(g_mpi2, g_mpi0, g_mpi1);
if (*argv[2] != ERROR_FLAG) {
ASSERT(res == 0);
ASSERT(cry_mpi_store_str(g_mpi2, 16, (char *)g_buf) == 0);
ASSERT(strcmp((char *)g_buf, argv[2]) == 0);
} else {
ASSERT(atoi(argv[2] + 1) == res);
}
check(res, g_mpi2, argv[2]);
}
typedef int (* binary_mod_op_f)(cry_mpi *r, const cry_mpi *a,
......@@ -168,13 +173,7 @@ static void mpi_binary_mod_op(int argc, char *argv[], binary_mod_op_f op)
res = op(g_mpi3, g_mpi0, g_mpi1, g_mpi2);
if (*argv[3] != ERROR_FLAG) {
ASSERT(res == 0);
ASSERT(cry_mpi_store_str(g_mpi3, 16, (char *)g_buf) == 0);
ASSERT(strcmp((char *)g_buf, argv[3]) == 0);
} else {
ASSERT(atoi(argv[3] + 1) == res);
}
check(res, g_mpi3, argv[3]);
}
static void mpi_dispatch(int argc, char *argv[])
......@@ -225,8 +224,6 @@ static void mpi_dispatch(int argc, char *argv[])
mpi_teardown();
}
void mpi_test(void)
{
printf("* MPI\n");
......
......@@ -436,6 +436,17 @@ c92e20b990846a1037594736fd02ce34f1776d4c019b0d31c4b5bdb0df568ea2292223e70d6dad39
2b6cc14a427b833f583b1f5fab69855ed61a0ae3518a7e51743d9568f05a90797edafaeae6fdd2c6eb401b743b8c5ad434bf4189e2de8dedabdcdf9379ab71692d0f3f9658e92dfe07d4a05f11224c35c14bc83b0344b8fe352943a082ee06ce888489bf6fc0256dcbec020b717c369e6ea872562fa8736dee1413d6c6dd9531c7364b994c27b26480a4328a9bb9a4404fbe7726c854068e108f86338bd5f1a4534fadd1d15972effccb0b90e575fd8153480d1991395f490c01e8b6dad9f69a17d1a310170b15f75759a3d21c209b15c390478e053987323ee022d7804852e17bb1a6798d21976c3c5ee4312644b28b6f974eac6ff1d1db834426d0aa8618ac95054c1c1b085769a8c298d1e0abb499e761d11e56ff07bad43c4ae78c53a95a8dc2a730dfc7bbb9d7c759170c83b831b1640737081badc6f66874842d21858b412b05d62e3ac566d37bd8770195e34826fb4c351fd1075472f4e743255c4b1b9ec1c9d3d6aedfbd25c3fb7772592c3d2935e41c2807913ca9575c9c9c155de2c70debf1166cc6b06cadfbbe707d7ea3eb804359db08d78be557c5bbffe75c084be5a019bb282acdaa201ab8f44bed03da090bf83e39fa0567331d0afe0d31aa1ccf22fb407fc229584f81442b56fc83181af65a1488a5d6a232b4777f0203100385e7236d5e3cc456cee6397ca6420636a9848b33ba6092a2f2a45f68b88b8ffb4c9db8baa12dc53018aa74eacf61c8266bfb9433fe31be22c21caa6a16583c1dc15de08e82e4b73413794ee950b93938063e502641243e0bf794c5d2c4690c3ff78b295dc2dd40a446a74671b093b110f18e6eb5d2a40b545c6a5a5a60494e93ecde73d63730b1b34d534c9ab0aaa25c58a07a138bae78849b8edcb1ac360e544ae5c0880b5fc8adda26255862fc0365dd676affd379cb0b46a01550270010b611affd035d775cf81165c05edae63379dc0b4dbd25a7a4a1820e3ee18199051273c8fa17ab85301013d6b7354f34eccbe49b94174530aad38d59066bebbde95786e6266e574f38d077e162e5d36a20e99b34fbc60e4f3bfb0861a23d68d4386a8b988c56a4e727e10ad6c84774adc76d6ba6cde16032ccd30b24ec6baaa628
222036db1bcf8603053a222926586ff562cf5759e1d165383ee41b505fc2998a6731dd7cb8ac391040026bf0e8178540ddb9ec30cb988da176cd33b2eeb5c61006b535a488e88948d0b845166fdff383073b11d68c529dc89a06a250f4b3bc7119b05f4cd5c95e19fbb04ee7ce1ae003743a428897b5c5768617ddf2662e198984167c669a32474e14964c1c75b034d007a978a4ea4518c52657408a142b60aa52fe3ed21636701485e82e8753337f942ff060c42429c6a8ab3a9fcdea6a27f7ef76f2d1809a18bfd43b1f5da74a273f51c06507f5945d6fa9a273db883a8636f5feb52e353db15f4418bf94c2ae733999e7edf4ef5ea7d9b8f00cc67574e9d9860412cd6b5ca85ee1bc00ffda5c2d2f74aec2e0363b7509cbf22594e008120599d7f8d4a938e5fbc6d813fde2b650278c439aa8556fd5d6560a274c976e2b79bca16fab4d1ca96a6cbbd8d6cf67ca0a79a1280f1471da2e3ee8b7e63b61babec83493ff4425c1c434cd3926e18baf03962d446d440507c6a49f17cebabb5774a833588991ead6ed9da9645fafd8d70bac704ddfbfc4133310c8a6eedbe89b8fcd1ec518c73dc2262f0a6f6bc5f6d1d04fa350aad1f0d92accdcf1464732060a6a4ac7c920bf5f73dbb843eeb829904b9e46977d0fc54923747cc0debb7000f0b0c1c679bf92a80e441b4bf4a0ae21a64b60d3b7f44f607413e5b30833a5b75c9ebc014c7b981c1acff1b0bafcc16bd0c590c09f245cab89c625bc03fb4843a597d25b878613f575853fc4c58ce68a8d3041306d717952df3fbb3bd2e23c604ddfc5afe632ba39bcda0f322555d84806c52c35831909901b0642897e8ab71267304e78be30bb60e32ea16645f7a04f2dc0e6cd6a98b4dfa4103d26c4248c458f4db9f6a015c5bc784b667b6eb473ae232e3174879d17f66e5fccfac905b262810ce49602a7c07851938fd500f1d8376b0ea537d643460129aba2b0fb8de862428b0c6cc49129118673e0cb57e9ef685326d5e4b31498599446b1653ffa4726964b56861e2c561f7184343ba353c4673d92a85c6fa5201fcb60be6d4c5fc8d60c8ab75fb49183e81fee77df86fd7a3d68bdeea1f210b2cf8c278de2704205218744caaaa580343856579bbe93a2235281bf9998e9fd45117dc25f2b33b89cc36ea2a6143c0b16a5a71055dd8e942913429b20facebfde81102caa232410ef52c4c23b79c9292862adf3e2810dfae324e65334ca74f7fe569893af2e91aa8c512788f2ad985e5e6b7ef5932c389b7f72ee5a75dd6f9756437fd00ee6b29b3f93dc4898190591abf65ac1557e71019e37c5dfa8f6d60378c941ab2427d9c2a7a3355dd808ba41ff861bdba0308f1f7932d67fd2ce1784b6b2b777e8226688ac607c11f97097a90ce50239d1c43b69c2cbabb0083624f63e4848e295eb49e6a4d2442b849ce53984a8274bce38d22ff544e02f5da0d03c42f3591003f8c1536204350b829ec06602351e95d74a20d416e766a07d7a31b7c75c12cabf74be61fac0ae21d97052a61d034ad372a58b0ad8bfeb9c24f03a3cb4e59741c42c951b6bf3c1fe4c212b0959b095d62794560a4d754ea8b57789bc791a20b2d5f8b6915f3b83166eb8e66400a3e28ab679c256899f821016e8701851cd0095aa625dcf0a4ef028035d095c6f75f7dc7f60f06974cb57091b706ef28da05646f4eba1c1df4fee4e0c0585a2024c11288c7acd19e38bd78dd7692b9fb826e992042efbd7faba78084d18d83f81ac86bd4907ca1a4574ae9456f19d6a5a8845e15bf7cfefe7a92b20eb8d666c4e1a18e809e6d9f7fc611678ed67f84ff7aac987522aaf76a947ca6745133c5fa1e86b183c83ca7aeff86e854c78e36d960b3a0ad1390d47b92d3b16e08b36e7ed35160dbfbf44f1bbcc583405fdffcf0f59cea599b31adbd5eccdb74e8753290cd7adea2d1716031dcebecc32db55c6a47e2c926ef3848e3d8c72beb03c9096d35470f9610fb4e4a0293b7f99af165967c90bc7372032c2edb133a19fa97c05b3368c3c302058fc059706d0c3e17ab338a53b10606972307ef8401da23ad1672b4e4d596bf45728ed12eb8268d4f297e78416ba394ad3aff037dd510769caf4bd501be168d1fdf1457cbc7b84b69329c0da70e60dfcdbd85d32d6500f67ce1b9947b29c6cfa75794303eab5a7c9ff0629036aa3e745857ca5769e4f093f41b77aa6fdfb83e2438080483f7f2b79d4530638c991521785e73226c8
################################################################################
# Square
# p0 : input value as hex string
# p2 : square value
################################################################################
Sqr
mpi_sqr
24652d5a2646a8fd9b92c281708d3ddb
52c9cbe2beee47c72af113101cb4adb7bf95ad3ddd78ce8716026c2d2301959
################################################################################
# Exponentiation (Binary)
# p0 : base
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment