You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@milagro.apache.org by km...@apache.org on 2019/11/18 11:24:37 UTC
[incubator-milagro-crypto-c] 01/01: updated BLS to accept non null
terminated input char array
This is an automated email from the ASF dual-hosted git repository.
kmccusker pushed a commit to branch issue53
in repository https://gitbox.apache.org/repos/asf/incubator-milagro-crypto-c.git
commit 3deae8931168de8bb13996743f36e5e20da96dfa
Author: Kealan McCusker <ke...@gmail.com>
AuthorDate: Mon Nov 18 11:24:17 2019 +0000
updated BLS to accept non null terminated input char array
---
examples/example_bls_ZZZ.c.in | 39 +++++++++++++++++++++++----------------
examples/example_bls_sss_ZZZ.c.in | 13 ++++++++-----
include/bls.h.in | 8 ++++----
include/bls192.h.in | 8 ++++----
include/bls256.h.in | 4 ++--
src/bls.c.in | 23 ++++++++++++++---------
src/bls192.c.in | 23 ++++++++++++++---------
src/bls256.c.in | 23 ++++++++++++++---------
test/test_bls_ZZZ.c.in | 34 +++++++++++++++++++---------------
test/test_bls_sss_ZZZ.c.in | 11 ++++++-----
10 files changed, 108 insertions(+), 78 deletions(-)
diff --git a/examples/example_bls_ZZZ.c.in b/examples/example_bls_ZZZ.c.in
index 87074b6..cd3e8ad 100644
--- a/examples/example_bls_ZZZ.c.in
+++ b/examples/example_bls_ZZZ.c.in
@@ -54,12 +54,13 @@ under the License.
#define G2LEN 16*BFS_ZZZ
#endif
-static char message[]="test message";
-
int bls(csprng *RNG)
{
int rc;
+ char m[2000];
+ octet M = {0,sizeof(m),m};
+
char sk1[BGS_ZZZ];
octet SK1 = {0,sizeof(sk1),sk1};
char pktmp[G2LEN];
@@ -113,10 +114,12 @@ int bls(csprng *RNG)
OCT_output(&PK2);
printf("\n");
+ OCT_jstring(&M,"test message");
+
// Sign the message
- BLS_ZZZ_SIGN(&SIG1,message,&SK1);
- BLS_ZZZ_SIGN(&SIG2,message,&SK2);
- BLS_ZZZ_SIGN(&SIG3,message,&SK3);
+ BLS_ZZZ_SIGN(&SIG1,&M,&SK1);
+ BLS_ZZZ_SIGN(&SIG2,&M,&SK2);
+ BLS_ZZZ_SIGN(&SIG3,&M,&SK3);
printf("SIG1: ");
OCT_output(&SIG1);
@@ -127,7 +130,7 @@ int bls(csprng *RNG)
printf("\n");
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIG1,message,&PK1);
+ rc=BLS_ZZZ_VERIFY(&SIG1,&M,&PK1);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
@@ -139,7 +142,7 @@ int bls(csprng *RNG)
}
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIG2,message,&PK2);
+ rc=BLS_ZZZ_VERIFY(&SIG2,&M,&PK2);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
@@ -151,7 +154,7 @@ int bls(csprng *RNG)
}
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIG3,message,&PK3);
+ rc=BLS_ZZZ_VERIFY(&SIG3,&M,&PK3);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
@@ -188,7 +191,7 @@ int bls(csprng *RNG)
}
// Verify aggregated signature. This should fail.
- rc=BLS_ZZZ_VERIFY(&SIG,message,&PK);
+ rc=BLS_ZZZ_VERIFY(&SIG,&M,&PK);
if (rc==BLS_OK)
{
printf("Success: Aggregated signature is valid\n");
@@ -209,7 +212,7 @@ int bls(csprng *RNG)
OCT_output(&SIG);
printf("\n");
// Verify aggregated signature.
- rc=BLS_ZZZ_VERIFY(&SIG,message,&PK);
+ rc=BLS_ZZZ_VERIFY(&SIG,&M,&PK);
if (rc==BLS_OK)
{
printf("Success: Aggregated signature is valid\n");
@@ -222,9 +225,11 @@ int bls(csprng *RNG)
// change the message
- message[0]='Z';
- printf("message %s\n", message);
- rc=BLS_ZZZ_VERIFY(&SIG1,message,&PK1);
+ M.val[0]='Z';
+ printf("message ");
+ OCT_output_string(&M);
+ printf("\n");
+ rc=BLS_ZZZ_VERIFY(&SIG1,&M,&PK1);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
@@ -236,12 +241,14 @@ int bls(csprng *RNG)
}
// Change the signature
- message[0]='t';
+ M.val[0]='t';
SIG1.val[0]=5;
- printf("message %s\n", message);
+ printf("message ");
+ OCT_output_string(&M);
+ printf("\n");
printf("Signature SIG1: ");
OCT_output(&SIG1);
- rc=BLS_ZZZ_VERIFY(&SIG1,message,&PK1);
+ rc=BLS_ZZZ_VERIFY(&SIG1,&M,&PK1);
if (rc==BLS_OK)
{
printf("Success: Signature is valid\n");
diff --git a/examples/example_bls_sss_ZZZ.c.in b/examples/example_bls_sss_ZZZ.c.in
index e2085e9..5bf60ce 100644
--- a/examples/example_bls_sss_ZZZ.c.in
+++ b/examples/example_bls_sss_ZZZ.c.in
@@ -54,14 +54,15 @@ under the License.
#define G2LEN 16*BFS_ZZZ
#endif
-static char message[]="test message";
-
int bls_sss(csprng *RNG)
{
int rc;
int n=4;
int k=3;
+ char m[2000];
+ octet M = {0,sizeof(m),m};
+
char ski[BGS_ZZZ];
octet SKI = {0,sizeof(ski),ski};
char pki[G2LEN];
@@ -104,14 +105,16 @@ int bls_sss(csprng *RNG)
OCT_output(&PKI);
printf("\n");
+ OCT_jstring(&M,"test message");
+
// Sign the message
- BLS_ZZZ_SIGN(&SIGI,message,&SKI);
+ BLS_ZZZ_SIGN(&SIGI,&M,&SKI);
printf("SIGI: ");
OCT_output(&SIGI);
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIGI,message,&PKI);
+ rc=BLS_ZZZ_VERIFY(&SIGI,&M,&PKI);
if (rc!=BLS_OK)
{
printf("Error: Invalid Signature\n");
@@ -176,7 +179,7 @@ int bls_sss(csprng *RNG)
PKS[i].len = G2LEN;
PKS[i].val = pks[i];
BLS_ZZZ_KEY_PAIR_GENERATE(NULL,&Y[i],&PKS[i]);
- BLS_ZZZ_SIGN(&SIGS[i],message,&Y[i]);
+ BLS_ZZZ_SIGN(&SIGS[i],&M,&Y[i]);
}
for(int i=0; i<n; i++)
diff --git a/include/bls.h.in b/include/bls.h.in
index ba79673..29f6525 100644
--- a/include/bls.h.in
+++ b/include/bls.h.in
@@ -58,20 +58,20 @@ int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W);
/** @brief Calculate a signature
*
@param SIG signature
- @param m message to be signed
+ @param M message to be signed
@param S Private key
@return Zero for success or else an error code
*/
-int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S);
+int BLS_ZZZ_SIGN(octet *SIG,octet *m,octet *S);
/** @brief Verify a signature
*
@param SIG signature
- @param m message whose signature is to be verified.
+ @param M message whose signature is to be verified.
@param W Public key
@return Zero for success or else an error code
*/
-int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W);
+int BLS_ZZZ_VERIFY(octet *SIG,octet *m,octet *W);
/** @brief Add two members from the group G1
*
diff --git a/include/bls192.h.in b/include/bls192.h.in
index 143fcaf..208e2d6 100644
--- a/include/bls192.h.in
+++ b/include/bls192.h.in
@@ -58,20 +58,20 @@ int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W);
/** @brief Calculate a signature
*
@param SIG signature
- @param m message to be signed
+ @param M message to be signed
@param S Private key
@return Zero for success or else an error code
*/
-int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S);
+int BLS_ZZZ_SIGN(octet *SIG,octet *M,octet *S);
/** @brief Verify a signature
*
@param SIG signature
- @param m message whose signature is to be verified.
+ @param M message whose signature is to be verified.
@param W Public key
@return Zero for success or else an error code
*/
-int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W);
+int BLS_ZZZ_VERIFY(octet *SIG,octet *M,octet *W);
/** @brief Add two members from the group G1
*
diff --git a/include/bls256.h.in b/include/bls256.h.in
index 87be447..d0d6190 100644
--- a/include/bls256.h.in
+++ b/include/bls256.h.in
@@ -62,7 +62,7 @@ int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W);
@param S Private key
@return Zero for success or else an error code
*/
-int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S);
+int BLS_ZZZ_SIGN(octet *SIG,octet *M,octet *S);
/** @brief Verify a signature
*
@@ -71,7 +71,7 @@ int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S);
@param W Public key
@return Zero for success or else an error code
*/
-int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W);
+int BLS_ZZZ_VERIFY(octet *SIG,octet *M,octet *W);
/** @brief Add two members from the group G1
*
diff --git a/src/bls.c.in b/src/bls.c.in
index 95e7f54..3902c2c 100644
--- a/src/bls.c.in
+++ b/src/bls.c.in
@@ -80,15 +80,20 @@ static int recover_coefficients(int k, octet* X, octet* COEFS)
}
-/* hash a message to an ECP point, using SHA3 */
-static void BLS_HASHIT(ECP_ZZZ *P,char *m)
+/* hash a message, M, to an ECP point, using SHA3 */
+static void BLS_HASHIT(ECP_ZZZ *P,octet *M)
{
int i;
+ int j;
sha3 hs;
char h[MODBYTES_XXX];
octet HM= {0,sizeof(h),h};
SHA3_init(&hs,SHAKE256);
- for (i=0; m[i]!=0; i++) SHA3_process(&hs,m[i]);
+ for (i=0; i<M->len; i++)
+ {
+ j = (unsigned char) M->val[i];
+ SHA3_process(&hs,j);
+ }
SHA3_shake(&hs,HM.val,MODBYTES_XXX);
HM.len=MODBYTES_XXX;
ECP_ZZZ_mapit(P,&HM);
@@ -119,12 +124,12 @@ int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W)
return BLS_OK;
}
-/* Sign message m using private key S to produce signature SIG */
-int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S)
+/* Sign message M using private key S to produce signature SIG */
+int BLS_ZZZ_SIGN(octet *SIG,octet *M,octet *S)
{
BIG_XXX s;
ECP_ZZZ D;
- BLS_HASHIT(&D,m);
+ BLS_HASHIT(&D,M);
BIG_XXX_fromBytes(s,S->val);
PAIR_ZZZ_G1mul(&D,s);
// compress output
@@ -132,13 +137,13 @@ int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S)
return BLS_OK;
}
-/* Verify signature of message m, the signature SIG, and the public key W */
-int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W)
+/* Verify signature of message M, the signature SIG, and the public key W */
+int BLS_ZZZ_VERIFY(octet *SIG,octet *M,octet *W)
{
FP12_YYY v;
ECP2_ZZZ G,PK;
ECP_ZZZ D,HM;
- BLS_HASHIT(&HM,m);
+ BLS_HASHIT(&HM,M);
if (!ECP_ZZZ_fromOctet(&D,SIG))
{
diff --git a/src/bls192.c.in b/src/bls192.c.in
index 88a7056..e2e6e27 100644
--- a/src/bls192.c.in
+++ b/src/bls192.c.in
@@ -80,15 +80,20 @@ static int recover_coefficients(int k, octet* X, octet* COEFS)
}
-/* hash a message to an ECP point, using SHA3 */
-static void BLS_HASHIT(ECP_ZZZ *P,char *m)
+/* hash a message, M, to an ECP point, using SHA3 */
+static void BLS_HASHIT(ECP_ZZZ *P,octet *M)
{
int i;
+ int j;
sha3 hs;
char h[MODBYTES_XXX];
octet HM= {0,sizeof(h),h};
SHA3_init(&hs,SHAKE256);
- for (i=0; m[i]!=0; i++) SHA3_process(&hs,m[i]);
+ for (i=0; i<M->len; i++)
+ {
+ j = (unsigned char) M->val[i];
+ SHA3_process(&hs,j);
+ }
SHA3_shake(&hs,HM.val,MODBYTES_XXX);
HM.len=MODBYTES_XXX;
ECP_ZZZ_mapit(P,&HM);
@@ -119,25 +124,25 @@ int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W)
return BLS_OK;
}
-/* Sign message m using private key S to produce signature SIG */
-int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S)
+/* Sign message M using private key S to produce signature SIG */
+int BLS_ZZZ_SIGN(octet *SIG,octet *M,octet *S)
{
BIG_XXX s;
ECP_ZZZ D;
- BLS_HASHIT(&D,m);
+ BLS_HASHIT(&D,M);
BIG_XXX_fromBytes(s,S->val);
PAIR_ZZZ_G1mul(&D,s);
ECP_ZZZ_toOctet(SIG,&D,true); /* compress output */
return BLS_OK;
}
-/* Verify signature given message m, the signature SIG, and the public key W */
-int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W)
+/* Verify signature given message M, the signature SIG, and the public key W */
+int BLS_ZZZ_VERIFY(octet *SIG,octet *M,octet *W)
{
FP24_YYY v;
ECP4_ZZZ G,PK;
ECP_ZZZ D,HM;
- BLS_HASHIT(&HM,m);
+ BLS_HASHIT(&HM,M);
if (!ECP_ZZZ_fromOctet(&D,SIG))
{
diff --git a/src/bls256.c.in b/src/bls256.c.in
index f03a3bb..34ca082 100644
--- a/src/bls256.c.in
+++ b/src/bls256.c.in
@@ -80,15 +80,20 @@ static int recover_coefficients(int k, octet* X, octet* COEFS)
}
-/* hash a message to an ECP point, using SHA3 */
-static void BLS_HASHIT(ECP_ZZZ *P,char *m)
+/* hash a message, M, to an ECP point, using SHA3 */
+static void BLS_HASHIT(ECP_ZZZ *P,octet *M)
{
int i;
+ int j;
sha3 hs;
char h[MODBYTES_XXX];
octet HM= {0,sizeof(h),h};
SHA3_init(&hs,SHAKE256);
- for (i=0; m[i]!=0; i++) SHA3_process(&hs,m[i]);
+ for (i=0; i<M->len; i++)
+ {
+ j = (unsigned char) M->val[i];
+ SHA3_process(&hs,j);
+ }
SHA3_shake(&hs,HM.val,MODBYTES_XXX);
HM.len=MODBYTES_XXX;
ECP_ZZZ_mapit(P,&HM);
@@ -119,25 +124,25 @@ int BLS_ZZZ_KEY_PAIR_GENERATE(csprng *RNG,octet* S,octet *W)
return BLS_OK;
}
-/* Sign message m using private key S to produce signature SIG */
-int BLS_ZZZ_SIGN(octet *SIG,char *m,octet *S)
+/* Sign message M using private key S to produce signature SIG */
+int BLS_ZZZ_SIGN(octet *SIG,octet *M,octet *S)
{
BIG_XXX s;
ECP_ZZZ D;
- BLS_HASHIT(&D,m);
+ BLS_HASHIT(&D,M);
BIG_XXX_fromBytes(s,S->val);
PAIR_ZZZ_G1mul(&D,s);
ECP_ZZZ_toOctet(SIG,&D,true); /* compress output */
return BLS_OK;
}
-/* Verify signature given message m, the signature SIG, and the public key W */
-int BLS_ZZZ_VERIFY(octet *SIG,char *m,octet *W)
+/* Verify signature given message M, the signature SIG, and the public key W */
+int BLS_ZZZ_VERIFY(octet *SIG,octet *M,octet *W)
{
FP48_YYY v;
ECP8_ZZZ G,PK;
ECP_ZZZ D,HM;
- BLS_HASHIT(&HM,m);
+ BLS_HASHIT(&HM,M);
if (!ECP_ZZZ_fromOctet(&D,SIG))
{
diff --git a/test/test_bls_ZZZ.c.in b/test/test_bls_ZZZ.c.in
index 6a85fa8..4f90682 100644
--- a/test/test_bls_ZZZ.c.in
+++ b/test/test_bls_ZZZ.c.in
@@ -42,12 +42,14 @@
#define G2LEN 16*BFS_ZZZ
#endif
-static char message[]="test message";
int test(csprng *RNG)
{
int rc;
+ char m[2000];
+ octet M = {0,sizeof(m),m};
+
char sk1[BGS_ZZZ];
octet SK1 = {0,sizeof(sk1),sk1};
char pktmp[G2LEN];
@@ -107,10 +109,12 @@ int test(csprng *RNG)
OCT_output(&PK2);
printf("\n");
+ OCT_jstring(&M,"test message");
+
// Sign the message
- BLS_ZZZ_SIGN(&SIG1,message,&SK1);
- BLS_ZZZ_SIGN(&SIG2,message,&SK2);
- BLS_ZZZ_SIGN(&SIG3,message,&SK3);
+ BLS_ZZZ_SIGN(&SIG1,&M,&SK1);
+ BLS_ZZZ_SIGN(&SIG2,&M,&SK2);
+ BLS_ZZZ_SIGN(&SIG3,&M,&SK3);
printf("SIG1: ");
OCT_output(&SIG1);
@@ -121,7 +125,7 @@ int test(csprng *RNG)
printf("\n");
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIG1,message,&PK1);
+ rc=BLS_ZZZ_VERIFY(&SIG1,&M,&PK1);
if (rc==BLS_OK)
{
printf("Test Passed valid Signature / message\n");
@@ -133,7 +137,7 @@ int test(csprng *RNG)
}
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIG2,message,&PK2);
+ rc=BLS_ZZZ_VERIFY(&SIG2,&M,&PK2);
if (rc==BLS_OK)
{
printf("Test Passed valid Signature / message\n");
@@ -145,7 +149,7 @@ int test(csprng *RNG)
}
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIG3,message,&PK3);
+ rc=BLS_ZZZ_VERIFY(&SIG3,&M,&PK3);
if (rc==BLS_OK)
{
printf("Test Passed valid Signature / message\n");
@@ -182,7 +186,7 @@ int test(csprng *RNG)
}
// Verify aggregated signature. This should fail.
- rc=BLS_ZZZ_VERIFY(&SIG,message,&PK);
+ rc=BLS_ZZZ_VERIFY(&SIG,&M,&PK);
if (rc==BLS_FAIL)
{
printf("Test Passed invalid signature SIG = SIG1 + SIG2 \n");
@@ -204,7 +208,7 @@ int test(csprng *RNG)
printf("\n");
// Verify aggregated signature.
- rc=BLS_ZZZ_VERIFY(&SIG,message,&PK);
+ rc=BLS_ZZZ_VERIFY(&SIG,&M,&PK);
if (rc==BLS_OK)
{
printf("Test Passed valid Signature SIG = SIG1 + SIG2 + SIG3\n");
@@ -217,9 +221,9 @@ int test(csprng *RNG)
// change the message
- message[0]='Z';
- printf("message %s\n", message);
- rc=BLS_ZZZ_VERIFY(&SIG1,message,&PK1);
+ M.val[0]='Z';
+ printf("message %s\n", M.val);
+ rc=BLS_ZZZ_VERIFY(&SIG1,&M,&PK1);
if (rc==BLS_FAIL)
{
printf("Test Passed valid signature / invalid message\n");
@@ -232,12 +236,12 @@ int test(csprng *RNG)
// Change the signature
- message[0]='t';
+ M.val[0]='t';
SIG1.val[0]=5;
- printf("message %s\n", message);
+ printf("message %s\n", M.val);
printf("Signature SIG1: ");
OCT_output(&SIG1);
- rc=BLS_ZZZ_VERIFY(&SIG1,message,&PK1);
+ rc=BLS_ZZZ_VERIFY(&SIG1,&M,&PK1);
if (rc==BLS_INVALID_G1)
{
printf("Test Passed invalid signature / valid message\n");
diff --git a/test/test_bls_sss_ZZZ.c.in b/test/test_bls_sss_ZZZ.c.in
index e869c4c..57c8456 100644
--- a/test/test_bls_sss_ZZZ.c.in
+++ b/test/test_bls_sss_ZZZ.c.in
@@ -42,14 +42,15 @@
#define G2LEN 16*BFS_ZZZ
#endif
-static char message[]="test message";
-
int test(csprng *RNG)
{
int rc;
int n=4;
int k=3;
+ char m[2000];
+ octet M = {0,sizeof(m),m};
+
char ski[BGS_ZZZ];
octet SKI = {0,sizeof(ski),ski};
char pki[G2LEN];
@@ -93,13 +94,13 @@ int test(csprng *RNG)
printf("\n");
// Sign the message
- BLS_ZZZ_SIGN(&SIGI,message,&SKI);
+ BLS_ZZZ_SIGN(&SIGI,&M,&SKI);
printf("SIGI: ");
OCT_output(&SIGI);
// Verify signature
- rc=BLS_ZZZ_VERIFY(&SIGI,message,&PKI);
+ rc=BLS_ZZZ_VERIFY(&SIGI,&M,&PKI);
if (rc!=BLS_OK)
{
printf("Test Failed Invalid Signature\n");
@@ -168,7 +169,7 @@ int test(csprng *RNG)
PKS[i].len = G2LEN;
PKS[i].val = pks[i];
BLS_ZZZ_KEY_PAIR_GENERATE(NULL,&Y[i],&PKS[i]);
- BLS_ZZZ_SIGN(&SIGS[i],message,&Y[i]);
+ BLS_ZZZ_SIGN(&SIGS[i],&M,&Y[i]);
}
for(int i=0; i<n; i++)