Skip to content

Commit

Permalink
fix expand_message_xmd for dst with size > 255
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Apr 21, 2021
1 parent d32e456 commit b01ef45
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/fp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ void expand_message_xmd(uint8_t out[], size_t outSize, const void *msg, size_t m
const size_t r_in_bytes = 64;
const size_t n = outSize / mdSize;
static const uint8_t Z_pad[r_in_bytes] = {};
assert(dstSize < 256);
uint8_t largeDst[mdSize];
if (dstSize > 255) {
cybozu::Sha256 h;
h.update("H2C-OVERSIZE-DST-", 17);
h.digest(largeDst, mdSize, dst, dstSize);
dst = largeDst;
dstSize = mdSize;
}
/*
Z_apd | msg | BE(outSize, 2) | BE(0, 1) | DST | BE(dstSize, 1)
*/
Expand Down
71 changes: 71 additions & 0 deletions test/mapto_wb19_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,61 @@ void testHashToFp2v7(const T& mapto)
}
}
{
const uint8_t largeMsg[] = {
0xda, 0x56, 0x6f, 0xc9, 0xaf, 0x58, 0x60, 0x1b, 0xf6, 0xe0, 0x98, 0x25, 0x1c, 0x14, 0x03, 0x8e,
0x7f, 0x1b, 0x7d, 0xfe, 0x8f, 0x1c, 0xf4, 0x18, 0xa5, 0x3b, 0x4a, 0x86, 0x35, 0x9f, 0x67, 0xeb,
0x99, 0x6e, 0x30, 0xea, 0x3e, 0xcb, 0x8b, 0x32, 0x8a, 0xe2, 0x61, 0x5e, 0xde, 0xd5, 0x90, 0x20,
0xa7, 0xcc, 0x08, 0x14, 0x45, 0xe7, 0xa7, 0xe6, 0x1e, 0x53, 0x57, 0x34, 0x9f, 0x37, 0xfe, 0xb0,
0x25, 0xb1, 0x80, 0xf7, 0x1d, 0xee, 0xc3, 0xb0, 0xe8, 0x0f, 0xae, 0x85, 0xef, 0x42, 0x2a, 0x19,
0x98,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39,
0x8c, 0xc1, 0xce, 0x12, 0x9c, 0xa0, 0xc6, 0x96, 0xc9, 0xc0, 0xce, 0xa4, 0xb2, 0x7b, 0x07, 0x29,
0x01, 0x41, 0x97, 0x09, 0x60, 0x76, 0xa9, 0x29, 0x6c, 0x7b, 0x5c, 0x6d, 0xd0, 0xb8, 0xdb, 0xc2,
0x32, 0x99, 0x49, 0x05, 0x27, 0x96, 0x73, 0x43, 0x27, 0x41, 0x0a, 0xb4, 0x90, 0x62, 0xd8, 0xcd,
0xc1, 0x20, 0xc7, 0x3a, 0x20, 0xa0, 0xb7, 0x99, 0x1d, 0x0a, 0x90, 0x93, 0x10, 0x84, 0x3f, 0xd2,
0x6c, 0x2d, 0x9e, 0xf8, 0x8f, 0x37, 0x9f, 0xab, 0xe1, 0xbf, 0x1f, 0x91, 0xc8, 0xee, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xce, 0x9d, 0x8f, 0x67, 0xa6,
0xda, 0x19, 0x4e, 0xa8, 0xe6, 0xc1, 0x5a, 0x7e, 0x25, 0xb0, 0x6c, 0xd0, 0x01, 0x0d, 0x6a, 0x44,
0xdc, 0xfd, 0x3d, 0x7a, 0x21, 0xa3, 0xce, 0x1a, 0x32, 0x2c, 0xfa, 0xce, 0x75, 0xb5, 0xaf, 0x3d,
0x55, 0x6d, 0xd0, 0x06, 0x32, 0x72, 0x43, 0xd2, 0x72, 0xf0, 0xed, 0x00, 0x00, 0x00
};

const struct {
const char *msg;
const char *dst;
Expand Down Expand Up @@ -478,12 +533,28 @@ void testHashToFp2v7(const T& mapto)
"0x03a47f8e6d1763ba0cad63d6114c0accbef65707825a511b251a660a9b3994249ae4e63fac38b23da0c398689ee2ab52",
}
},
{
(const char*)largeMsg,
(const char*)largeDst,
{
"1621198997308164855924679271466068736858072552785749054645341480120465596875824494080137207843099766374306872449248",
"1083281626011882732241684404246060395112782337887047566845349523874158040278646833293564936724890128537465803996399",
},
{
"3796472889358084161418545238718592151842923587110768242624300389546030860679313089401746511331063864568256792368175",
"3428585241079663876306492378538920664201248193908204300130152438040549594213769385563811847347660927609220812378737",
}
},
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
const char *msg = tbl[i].msg;
size_t msgSize = strlen(msg);
const char *dst = tbl[i].dst;
size_t dstSize = strlen(dst);
if (msg == (const char*)largeMsg) {
msgSize = sizeof(largeMsg);
dstSize = sizeof(largeDst);
}
G2 P1, P2;
set(P1.x, tbl[i].x);
set(P1.y, tbl[i].y);
Expand Down

0 comments on commit b01ef45

Please sign in to comment.