Skip to content

Commit

Permalink
Changed name of signature functions and added constants for future va…
Browse files Browse the repository at this point in the history
…riable padding support.
  • Loading branch information
dfaranha committed Aug 20, 2012
1 parent 8e81829 commit 880d20b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 59 deletions.
36 changes: 18 additions & 18 deletions bench/bench_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,29 @@ static void rsa(void) {

BENCH_ONCE("cp_rsa_gen", cp_rsa_gen(pub, prv, BN_BITS));

BENCH_BEGIN("cp_rsa_sign (h = 0)") {
BENCH_BEGIN("cp_rsa_sig (h = 0)") {
bn_size_bin(&in_len, pub->n);
in_len -= (2 * MD_LEN + 2);
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
BENCH_ADD(cp_rsa_sign(out, &out_len, in, in_len, 0, prv));
BENCH_ADD(cp_rsa_sig(out, &out_len, in, in_len, 0, prv));
} BENCH_END;

BENCH_BEGIN("cp_rsa_sign (h = 1)") {
BENCH_BEGIN("cp_rsa_sig (h = 1)") {
bn_size_bin(&in_len, pub->n);
in_len -= (2 * MD_LEN + 2);
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
md_map(h, in, in_len);
BENCH_ADD(cp_rsa_sign(out, &out_len, h, MD_LEN, 1, prv));
BENCH_ADD(cp_rsa_sig(out, &out_len, h, MD_LEN, 1, prv));
} BENCH_END;

BENCH_BEGIN("cp_rsa_ver (h = 0)") {
bn_size_bin(&in_len, pub->n);
in_len -= (2 * MD_LEN + 2);
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
cp_rsa_sign(out, &out_len, in, in_len, 0, prv);
cp_rsa_sig(out, &out_len, in, in_len, 0, prv);
BENCH_ADD(cp_rsa_ver(out, out_len, in, in_len, 0, pub));
} BENCH_END;

Expand All @@ -131,49 +131,49 @@ static void rsa(void) {
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
md_map(h, in, in_len);
cp_rsa_sign(out, &out_len, h, MD_LEN, 1, prv);
cp_rsa_sig(out, &out_len, h, MD_LEN, 1, prv);
BENCH_ADD(cp_rsa_ver(out, out_len, h, MD_LEN, 1, pub));
} BENCH_END;

#if CP_RSA == BASIC || !defined(STRIP)
BENCH_ONCE("cp_rsa_gen_basic", cp_rsa_gen_basic(pub, prv, BN_BITS));

BENCH_BEGIN("cp_rsa_sign_basic (h = 0)") {
BENCH_BEGIN("cp_rsa_sig_basic (h = 0)") {
bn_size_bin(&in_len, pub->n);
in_len -= (2 * MD_LEN + 2);
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
BENCH_ADD(cp_rsa_sign_basic(out, &out_len, in, in_len, 0, prv));
BENCH_ADD(cp_rsa_sig_basic(out, &out_len, in, in_len, 0, prv));
} BENCH_END;

BENCH_BEGIN("cp_rsa_sign_basic (h = 1)") {
BENCH_BEGIN("cp_rsa_sig_basic (h = 1)") {
bn_size_bin(&in_len, pub->n);
in_len -= (2 * MD_LEN + 2);
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
md_map(h, in, in_len);
BENCH_ADD(cp_rsa_sign_basic(out, &out_len, h, MD_LEN, 1, prv));
BENCH_ADD(cp_rsa_sig_basic(out, &out_len, h, MD_LEN, 1, prv));
} BENCH_END;
#endif

#if CP_RSA == QUICK || !defined(STRIP)
BENCH_ONCE("cp_rsa_gen_quick", cp_rsa_gen_quick(pub, prv, BN_BITS));

BENCH_BEGIN("cp_rsa_sign_quick (h = 0)") {
BENCH_BEGIN("cp_rsa_sig_quick (h = 0)") {
bn_size_bin(&in_len, pub->n);
in_len -= 11;
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
BENCH_ADD(cp_rsa_sign_quick(out, &out_len, in, in_len, 0, prv));
BENCH_ADD(cp_rsa_sig_quick(out, &out_len, in, in_len, 0, prv));
} BENCH_END;

BENCH_BEGIN("cp_rsa_sign_quick (h = 1)") {
BENCH_BEGIN("cp_rsa_sig_quick (h = 1)") {
bn_size_bin(&in_len, pub->n);
in_len -= 11;
out_len = BN_BITS / 8 + 1;
rand_bytes(in, in_len);
md_map(h, in, in_len);
BENCH_ADD(cp_rsa_sign_quick(out, &out_len, in, in_len, 1, prv));
BENCH_ADD(cp_rsa_sig_quick(out, &out_len, in, in_len, 1, prv));
} BENCH_END;
#endif

Expand Down Expand Up @@ -301,7 +301,7 @@ static void ecdsa(void) {
BENCH_END;

BENCH_BEGIN("cp_ecdsa_sign") {
BENCH_ADD(cp_ecdsa_sign(r, s, msg, 5, d));
BENCH_ADD(cp_ecdsa_sig(r, s, msg, 5, d));
}
BENCH_END;

Expand Down Expand Up @@ -337,7 +337,7 @@ static void ecss(void) {
BENCH_END;

BENCH_BEGIN("cp_ecss_sign") {
BENCH_ADD(cp_ecss_sign(r, s, msg, 5, d));
BENCH_ADD(cp_ecss_sig(r, s, msg, 5, d));
}
BENCH_END;

Expand Down Expand Up @@ -418,7 +418,7 @@ static void bls(void) {
BENCH_END;

BENCH_BEGIN("cp_bls_sign") {
BENCH_ADD(cp_bls_sign(s, msg, 5, d));
BENCH_ADD(cp_bls_sig(s, msg, 5, d));
}
BENCH_END;

Expand Down Expand Up @@ -456,7 +456,7 @@ static void bbs(void) {
BENCH_END;

BENCH_BEGIN("cp_bbs_sign") {
BENCH_ADD(cp_bbs_sign(&b, s, msg, 5, d));
BENCH_ADD(cp_bbs_sig(&b, s, msg, 5, d));
}
BENCH_END;

Expand Down
75 changes: 51 additions & 24 deletions include/relic_cp.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@
/* Constant definitions */
/*============================================================================*/

/**
* Flag used to indicate that the message being signed is plaintext.
*/
#define CP_TEXT 0

/**
* Flag used to indicate that the message being signed is a hash value.
*/
#define CP_HASH 1

/**
* Flag used to indicate that no padding should be used.
*/
#define CP_EMPTY 0

/**
* Flag used to indicate that PKCS#1 v1.5 padding should be used.
*/
#define CP_PKCS1 1

/**
* Flag used to indicate that PKCS#1 v2.5 padding should be used.
*/
#define CP_PKCS2 2

/*============================================================================*/
/* Type definitions. */
/*============================================================================*/
Expand Down Expand Up @@ -135,7 +160,7 @@ typedef sokaka_st *sokaka_t;
* @param[out] A - the key pair to initialize.
*/
#if ALLOC == AUTO
#define rsa_null(A) /* empty */
#define rsa_null(A) /* empty */
#else
#define rsa_null(A) A = NULL;
#endif
Expand Down Expand Up @@ -235,7 +260,7 @@ typedef sokaka_st *sokaka_t;
} \

#elif ALLOC == AUTO
#define rsa_free(A) /* empty */
#define rsa_free(A) /* empty */

#elif ALLOC == STACK
#define rsa_free(A) \
Expand All @@ -257,7 +282,7 @@ typedef sokaka_st *sokaka_t;
* @param[out] A - the key pair to initialize.
*/
#if ALLOC == AUTO
#define rabin_null(A) /* empty */
#define rabin_null(A) /* empty */
#else
#define rabin_null(A) A = NULL;
#endif
Expand Down Expand Up @@ -358,7 +383,7 @@ typedef sokaka_st *sokaka_t;
* @param[out] A - the key pair to initialize.
*/
#if ALLOC == AUTO
#define sokaka_null(A) /* empty */
#define sokaka_null(A) /* empty */
#else
#define sokaka_null(A) A = NULL;
#endif
Expand Down Expand Up @@ -451,13 +476,13 @@ typedef sokaka_st *sokaka_t;
* @param[out] OL - the number of bytes written in the output buffer.
* @param[in] I - the input buffer.
* @param[in] IL - the number of bytes to encrypt.
* @param[in] P - the private key.
* @param[in] K - the private key.
* @return STS_OK if no errors occurred, STS_ERR otherwise.
*/
#if CP_RSA == BASIC
#define cp_rsa_dec(O, OL, I, IL, P) cp_rsa_dec_basic(O, OL, I, IL, P)
#define cp_rsa_dec(O, OL, I, IL, K) cp_rsa_dec_basic(O, OL, I, IL, K)
#elif CP_RSA == QUICK
#define cp_rsa_dec(O, OL, I, IL, P) cp_rsa_dec_quick(O, OL, I, IL, P)
#define cp_rsa_dec(O, OL, I, IL, K) cp_rsa_dec_quick(O, OL, I, IL, K)
#endif

/**
Expand All @@ -466,15 +491,15 @@ typedef sokaka_st *sokaka_t;
* @param[out] O - the output buffer.
* @param[out] OL - the number of bytes written in the output buffer.
* @param[in] I - the input buffer.
* @param[in] L - the number of bytes to sign.
* @param[in] H - the flag to indicate message format.
* @param[in] P - the private key.
* @param[in] IL - the number of bytes to sign.
* @param[in] H - the flag to indicate the message format.
* @param[in] K - the private key.
* @return STS_OK if no errors occurred, STS_ERR otherwise.
*/
#if CP_RSA == BASIC
#define cp_rsa_sign(O, OL, I, L, H, P) cp_rsa_sign_basic(O, OL, I, L, H, P)
#define cp_rsa_sig(O, OL, I, IL, H, K) cp_rsa_sig_basic(O, OL, I, IL, H, K)
#elif CP_RSA == QUICK
#define cp_rsa_sign(O, OL, I, L, H, P) cp_rsa_sign_quick(O, OL, I, L, H, P)
#define cp_rsa_sig(O, OL, I, IL, H, K) cp_rsa_sig_quick(O, OL, I, IL, H, K)
#endif

/*============================================================================*/
Expand Down Expand Up @@ -552,7 +577,7 @@ int cp_rsa_dec_quick(unsigned char *out, int *out_len, unsigned char *in,
* @param[in] prv - the private key.
* @return STS_OK if no errors occurred, STS_ERR otherwise.
*/
int cp_rsa_sign_basic(unsigned char *sig, int *sig_len, unsigned char *msg,
int cp_rsa_sig_basic(unsigned char *sig, int *sig_len, unsigned char *msg,
int msg_len, int hash, rsa_t prv);

/**
Expand All @@ -567,7 +592,7 @@ int cp_rsa_sign_basic(unsigned char *sig, int *sig_len, unsigned char *msg,
* @param[in] prv - the private key.
* @return STS_OK if no errors occurred, STS_ERR otherwise.
*/
int cp_rsa_sign_quick(unsigned char *sig, int *sig_len, unsigned char *msg,
int cp_rsa_sig_quick(unsigned char *sig, int *sig_len, unsigned char *msg,
int msg_len, int hash, rsa_t prv);

/**
Expand Down Expand Up @@ -605,8 +630,8 @@ int cp_rabin_gen(rabin_t pub, rabin_t prv, int bits);
* @param[in] pub - the public key.
* @return STS_OK if no errors occurred, STS_ERR otherwise.
*/
int cp_rabin_enc(unsigned char *out, int *out_len, unsigned char *in, int in_len,
rabin_t pub);
int cp_rabin_enc(unsigned char *out, int *out_len, unsigned char *in,
int in_len, rabin_t pub);

/**
* Decrypts using the Rabin cryptosystem.
Expand All @@ -618,8 +643,8 @@ int cp_rabin_enc(unsigned char *out, int *out_len, unsigned char *in, int in_len
* @param[in] prv - the private key.
* @return STS_OK if no errors occurred, STS_ERR otherwise.
*/
int cp_rabin_dec(unsigned char *out, int *out_len, unsigned char *in, int in_len,
rabin_t prv);
int cp_rabin_dec(unsigned char *out, int *out_len, unsigned char *in,
int in_len, rabin_t prv);

/**
* Generates an ECDH key pair.
Expand Down Expand Up @@ -660,7 +685,8 @@ void cp_ecmqv_gen(bn_t d, ec_t q);
* @param[in] q1v - the point received from the other party.
* @param[in] q2v - the ephemeral point received from the other party.
*/
void cp_ecmqv_key(unsigned char *key, int key_len, bn_t d1, bn_t d2, ec_t q2u, ec_t q1v, ec_t q2v);
void cp_ecmqv_key(unsigned char *key, int key_len, bn_t d1, bn_t d2, ec_t q2u,
ec_t q1v, ec_t q2v);

/**
* Generates an ECDSA key pair.
Expand All @@ -679,7 +705,7 @@ void cp_ecdsa_gen(bn_t d, ec_t q);
* @param[in] len - the message length in bytes.
* @param[in] d - the private key.
*/
void cp_ecdsa_sign(bn_t r, bn_t s, unsigned char *msg, int len, bn_t d);
void cp_ecdsa_sig(bn_t r, bn_t s, unsigned char *msg, int len, bn_t d);

/**
* Verifies a message signed with ECDSA using the basic method.
Expand Down Expand Up @@ -709,7 +735,7 @@ void cp_ecss_gen(bn_t d, ec_t q);
* @param[in] len - the message length in bytes.
* @param[in] d - the private key.
*/
void cp_ecss_sign(bn_t e, bn_t s, unsigned char *msg, int len, bn_t d);
void cp_ecss_sig(bn_t e, bn_t s, unsigned char *msg, int len, bn_t d);

/**
* Verifies a message signed with the Elliptic Curve Schnorr Signature using the
Expand Down Expand Up @@ -752,7 +778,8 @@ void cp_sokaka_gen_prv(sokaka_t k, char *id, int len, bn_t master);
* @param[in] id2 - the second identity.
* @param[in] len2 - the length of the second identity in bytes.
*/
void cp_sokaka_key(unsigned char *key, unsigned int key_len, char *id1, int len1, sokaka_t k, char *id2, int len2);
void cp_sokaka_key(unsigned char *key, unsigned int key_len, char *id1,
int len1, sokaka_t k, char *id2, int len2);

/**
* Generates a BLS key pair.
Expand All @@ -770,7 +797,7 @@ void cp_bls_gen(bn_t d, g2_t q);
* @param[in] len - the message length in bytes.
* @param[in] d - the private key.
*/
void cp_bls_sign(g1_t s, unsigned char *msg, int len, bn_t d);
void cp_bls_sig(g1_t s, unsigned char *msg, int len, bn_t d);

/**
* Verifies a message signed with BLS using the basic method.
Expand Down Expand Up @@ -801,7 +828,7 @@ void cp_bbs_gen(bn_t d, g2_t q, gt_t z);
* @param[in] len - the message length in bytes.
* @param[in] d - the private key.
*/
void cp_bbs_sign(int *b, g1_t s, unsigned char *msg, int len, bn_t d);
void cp_bbs_sig(int *b, g1_t s, unsigned char *msg, int len, bn_t d);

/**
* Verifies a message signed with BLS using the basic method.
Expand Down
2 changes: 1 addition & 1 deletion src/cp/relic_cp_bbs.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void cp_bbs_gen(bn_t d, g2_t q, gt_t z) {
}
}

void cp_bbs_sign(int *b, g1_t s, unsigned char *msg, int len, bn_t d) {
void cp_bbs_sig(int *b, g1_t s, unsigned char *msg, int len, bn_t d) {
bn_t m, n, r;
unsigned char hash[MD_LEN];
unsigned char key[len + PC_BYTES];
Expand Down
2 changes: 1 addition & 1 deletion src/cp/relic_cp_bls.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void cp_bls_gen(bn_t d, g2_t q) {
}
}

void cp_bls_sign(g1_t s, unsigned char *msg, int len, bn_t d) {
void cp_bls_sig(g1_t s, unsigned char *msg, int len, bn_t d) {
g1_t p;

g1_null(p);
Expand Down
3 changes: 1 addition & 2 deletions src/cp/relic_cp_ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void cp_ecdsa_gen(bn_t d, ec_t q) {
}
}

void cp_ecdsa_sign(bn_t r, bn_t s, unsigned char *msg, int len, bn_t d) {
void cp_ecdsa_sig(bn_t r, bn_t s, unsigned char *msg, int len, bn_t d) {
bn_t n, k, x, e;
ec_t p;
unsigned char hash[MD_LEN];
Expand Down Expand Up @@ -191,4 +191,3 @@ int cp_ecdsa_ver(bn_t r, bn_t s, unsigned char *msg, int len, ec_t q) {
}
return result;
}

2 changes: 1 addition & 1 deletion src/cp/relic_cp_ecss.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void cp_ecss_gen(bn_t d, ec_t q) {
}
}

void cp_ecss_sign(bn_t e, bn_t s, unsigned char *msg, int len, bn_t d) {
void cp_ecss_sig(bn_t e, bn_t s, unsigned char *msg, int len, bn_t d) {
bn_t n, k, x, r;
ec_t p;
unsigned char hash[MD_LEN];
Expand Down
4 changes: 2 additions & 2 deletions src/cp/relic_cp_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ int cp_rsa_dec_quick(unsigned char *out, int *out_len, unsigned char *in,

#if CP_RSA == BASIC || !defined(STRIP)

int cp_rsa_sign_basic(unsigned char *sig, int *sig_len, unsigned char *msg,
int cp_rsa_sig_basic(unsigned char *sig, int *sig_len, unsigned char *msg,
int msg_len, int hash, rsa_t prv) {
bn_t m, eb;
int size, pad_len, result = STS_OK;
Expand Down Expand Up @@ -998,7 +998,7 @@ int cp_rsa_sign_basic(unsigned char *sig, int *sig_len, unsigned char *msg,

#if CP_RSA == QUICK || !defined(STRIP)

int cp_rsa_sign_quick(unsigned char *sig, int *sig_len, unsigned char *msg,
int cp_rsa_sig_quick(unsigned char *sig, int *sig_len, unsigned char *msg,
int msg_len, int hash, rsa_t prv) {
bn_t m, eb;
int pad_len, size, result = STS_OK;
Expand Down
Loading

0 comments on commit 880d20b

Please sign in to comment.