Change type names to follow the _t convention, add `deprecated' attribute

This commit is contained in:
Matthias Schiffer 2012-12-22 22:12:03 +01:00
parent c6f33a891f
commit 9c832519c6
3 changed files with 86 additions and 54 deletions

View file

@ -27,12 +27,17 @@
#ifndef _LIBUECC_ECC_H_
#define _LIBUECC_ECC_H_
typedef union _ecc_int_256 {
#ifndef DEPRECATED
#define DEPRECATED __attribute__((deprecated))
#endif
typedef union _ecc_int256 {
unsigned char p[32];
/* old name */
unsigned char s[32];
} ecc_int_256, ecc_secret_key_256, ecc_public_key_256;
unsigned char s[32] DEPRECATED;
} ecc_int256_t;
/* a point on the curve unpacked for efficient calculation */
typedef struct _ecc_25519_work {
@ -40,41 +45,68 @@ typedef struct _ecc_25519_work {
unsigned int Y[32];
unsigned int Z[32];
unsigned int T[32];
} ecc_25519_work;
} ecc_25519_work_t;
void ecc_25519_load_xy(ecc_25519_work *out, const ecc_int_256 *x, const ecc_int_256 *y);
void ecc_25519_store_xy(ecc_int_256 *x, ecc_int_256 *y, const ecc_25519_work *in);
void ecc_25519_load_xy(ecc_25519_work_t *out, const ecc_int256_t *x, const ecc_int256_t *y);
void ecc_25519_store_xy(ecc_int256_t *x, ecc_int256_t *y, const ecc_25519_work_t *in);
void ecc_25519_load_packed(ecc_25519_work *out, const ecc_int_256 *in);
void ecc_25519_store_packed(ecc_int_256 *out, const ecc_25519_work *in);
void ecc_25519_load_packed(ecc_25519_work_t *out, const ecc_int256_t *in);
void ecc_25519_store_packed(ecc_int256_t *out, const ecc_25519_work_t *in);
int ecc_25519_is_identity(const ecc_25519_work *in);
void ecc_25519_add(ecc_25519_work *out, const ecc_25519_work *in1, const ecc_25519_work *in2);
void ecc_25519_double(ecc_25519_work *out, const ecc_25519_work *in);
void ecc_25519_scalarmult(ecc_25519_work *out, const ecc_int_256 *n, const ecc_25519_work *base);
void ecc_25519_scalarmult_base(ecc_25519_work *out, const ecc_int_256 *n);
int ecc_25519_is_identity(const ecc_25519_work_t *in);
void ecc_25519_add(ecc_25519_work_t *out, const ecc_25519_work_t *in1, const ecc_25519_work_t *in2);
void ecc_25519_double(ecc_25519_work_t *out, const ecc_25519_work_t *in);
void ecc_25519_scalarmult(ecc_25519_work_t *out, const ecc_int256_t *n, const ecc_25519_work_t *base);
void ecc_25519_scalarmult_base(ecc_25519_work_t *out, const ecc_int256_t *n);
/* operations on elements of the prime field F_q for q = 2^252 + 27742317777372353535851937790883648493 */
extern const ecc_int_256 ecc_25519_gf_order;
int ecc_25519_gf_is_zero(const ecc_int_256 *in);
void ecc_25519_gf_add(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_256 *in2);
void ecc_25519_gf_sub(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_256 *in2);
void ecc_25519_gf_reduce(ecc_int_256 *out, const ecc_int_256 *in);
void ecc_25519_gf_mult(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_256 *in2);
void ecc_25519_gf_recip(ecc_int_256 *out, const ecc_int_256 *in);
extern const ecc_int256_t ecc_25519_gf_order;
void ecc_25519_gf_sanitize_secret(ecc_int_256 *out, const ecc_int_256 *in);
int ecc_25519_gf_is_zero(const ecc_int256_t *in);
void ecc_25519_gf_add(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2);
void ecc_25519_gf_sub(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2);
void ecc_25519_gf_reduce(ecc_int256_t *out, const ecc_int256_t *in);
void ecc_25519_gf_mult(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2);
void ecc_25519_gf_recip(ecc_int256_t *out, const ecc_int256_t *in);
/* defines for the old names */
#define ecc_25519_load ecc_25519_load_packed
#define ecc_25519_store ecc_25519_store_packed
void ecc_25519_gf_sanitize_secret(ecc_int256_t *out, const ecc_int256_t *in);
#define ecc_25519_secret_is_zero ecc_25519_gf_is_zero
#define ecc_25519_secret_add ecc_25519_gf_add
#define ecc_25519_secret_sub ecc_25519_gf_sub
#define ecc_25519_secret_reduce ecc_25519_gf_reduce
#define ecc_25519_secret_mult ecc_25519_gf_mult
#define ecc_25519_secret_sanitize ecc_25519_gf_sanitize_secret
/* declarations for the old names */
typedef ecc_int256_t ecc_secret_key_256 DEPRECATED;
typedef ecc_int256_t ecc_public_key_256 DEPRECATED;
typedef ecc_25519_work_t ecc_25519_work DEPRECATED;
DEPRECATED static inline void ecc_25519_load(ecc_25519_work_t *out, const ecc_int256_t *in) {
ecc_25519_load_packed(out, in);
}
DEPRECATED static inline void ecc_25519_store(ecc_int256_t *out, const ecc_25519_work_t *in) {
ecc_25519_store_packed(out, in);
}
DEPRECATED static inline int ecc_25519_secret_is_zero(const ecc_int256_t *in) {
return ecc_25519_gf_is_zero(in);
}
DEPRECATED static inline void ecc_25519_secret_add(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2) {
ecc_25519_gf_add(out, in1, in2);
}
DEPRECATED static inline void ecc_25519_secret_sub(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2) {
ecc_25519_gf_sub(out, in1, in2);
}
DEPRECATED static inline void ecc_25519_secret_reduce(ecc_int256_t *out, const ecc_int256_t *in) {
ecc_25519_gf_reduce(out, in);
}
DEPRECATED static inline void ecc_25519_secret_mult(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2) {
ecc_25519_gf_mult(out, in1, in2);
}
DEPRECATED static inline void ecc_25519_secret_sanitize(ecc_int256_t *out, const ecc_int256_t *in) {
ecc_25519_gf_sanitize_secret(out, in);
}
#endif /* _LIBUECC_ECC_H_ */

View file

@ -159,7 +159,7 @@ static int check_zero(const unsigned int x[32]) {
return (check_equal(x, zero) | check_equal(x, p));
}
static void selectw(ecc_25519_work *out, const ecc_25519_work *r, const ecc_25519_work *s, unsigned int b) {
static void selectw(ecc_25519_work_t *out, const ecc_25519_work_t *r, const ecc_25519_work_t *s, unsigned int b) {
unsigned int j;
unsigned int t;
unsigned int bminus1;
@ -347,7 +347,7 @@ static void recip(unsigned int out[32], const unsigned int z[32]) {
/* 2^255 - 21 */ mult(out, t1, z11);
}
void ecc_25519_load_xy(ecc_25519_work *out, const ecc_int_256 *x, const ecc_int_256 *y) {
void ecc_25519_load_xy(ecc_25519_work_t *out, const ecc_int256_t *x, const ecc_int256_t *y) {
int i;
for (i = 0; i < 32; i++) {
@ -359,7 +359,7 @@ void ecc_25519_load_xy(ecc_25519_work *out, const ecc_int_256 *x, const ecc_int_
mult(out->T, out->X, out->Y);
}
void ecc_25519_store_xy(ecc_int_256 *x, ecc_int_256 *y, const ecc_25519_work *in) {
void ecc_25519_store_xy(ecc_int256_t *x, ecc_int256_t *y, const ecc_25519_work_t *in) {
unsigned int X[32], Y[32], Z[32];
int i;
@ -380,7 +380,7 @@ void ecc_25519_store_xy(ecc_int_256 *x, ecc_int_256 *y, const ecc_25519_work *in
}
}
void ecc_25519_load_packed(ecc_25519_work *out, const ecc_int_256 *in) {
void ecc_25519_load_packed(ecc_25519_work_t *out, const ecc_int256_t *in) {
static const unsigned int zero[32] = {0};
static const unsigned int one[32] = {1};
@ -410,16 +410,16 @@ void ecc_25519_load_packed(ecc_25519_work *out, const ecc_int_256 *in) {
mult(out->T, out->X, out->Y);
}
void ecc_25519_store_packed(ecc_int_256 *out, const ecc_25519_work *in) {
ecc_int_256 y;
void ecc_25519_store_packed(ecc_int256_t *out, const ecc_25519_work_t *in) {
ecc_int256_t y;
ecc_25519_store_xy(out, &y, in);
out->p[31] |= (y.p[0] << 7);
}
static const ecc_25519_work id = {{0}, {1}, {1}, {0}};
static const ecc_25519_work_t id = {{0}, {1}, {1}, {0}};
int ecc_25519_is_identity(const ecc_25519_work *in) {
int ecc_25519_is_identity(const ecc_25519_work_t *in) {
unsigned int Y_Z[32];
sub(Y_Z, in->Y, in->Z);
@ -428,7 +428,7 @@ int ecc_25519_is_identity(const ecc_25519_work *in) {
return (check_zero(in->X)&check_zero(Y_Z));
}
void ecc_25519_double(ecc_25519_work *out, const ecc_25519_work *in) {
void ecc_25519_double(ecc_25519_work_t *out, const ecc_25519_work_t *in) {
unsigned int A[32], B[32], C[32], D[32], E[32], F[32], G[32], H[32], t0[32], t1[32], t2[32], t3[32];
square(A, in->X);
@ -449,7 +449,7 @@ void ecc_25519_double(ecc_25519_work *out, const ecc_25519_work *in) {
mult(out->Z, F, G);
}
void ecc_25519_add(ecc_25519_work *out, const ecc_25519_work *in1, const ecc_25519_work *in2) {
void ecc_25519_add(ecc_25519_work_t *out, const ecc_25519_work_t *in1, const ecc_25519_work_t *in2) {
unsigned int A[32], B[32], C[32], D[32], E[32], F[32], G[32], H[32], t0[32], t1[32], t2[32], t3[32], t4[32], t5[32];
mult(A, in1->X, in2->X);
@ -472,9 +472,9 @@ void ecc_25519_add(ecc_25519_work *out, const ecc_25519_work *in1, const ecc_255
mult(out->Z, F, G);
}
void ecc_25519_scalarmult(ecc_25519_work *out, const ecc_int_256 *n, const ecc_25519_work *base) {
ecc_25519_work Q2, Q2p;
ecc_25519_work cur = id;
void ecc_25519_scalarmult(ecc_25519_work_t *out, const ecc_int256_t *n, const ecc_25519_work_t *base) {
ecc_25519_work_t Q2, Q2p;
ecc_25519_work_t cur = id;
int b, pos;
for (pos = 255; pos >= 0; --pos) {
@ -489,7 +489,7 @@ void ecc_25519_scalarmult(ecc_25519_work *out, const ecc_int_256 *n, const ecc_2
*out = cur;
}
static const ecc_25519_work default_base = {
static const ecc_25519_work_t default_base = {
{0xd4, 0x6b, 0xfe, 0x7f, 0x39, 0xfa, 0x8c, 0x22,
0xe1, 0x96, 0x23, 0xeb, 0x26, 0xb7, 0x8e, 0x6a,
0x34, 0x74, 0x8b, 0x66, 0xd6, 0xa3, 0x26, 0xdd,
@ -505,6 +505,6 @@ static const ecc_25519_work default_base = {
0x47, 0x4b, 0x4c, 0x81, 0xa6, 0x02, 0xfd, 0x29}
};
void ecc_25519_scalarmult_base(ecc_25519_work *out, const ecc_int_256 *n) {
void ecc_25519_scalarmult_base(ecc_25519_work_t *out, const ecc_int256_t *n) {
ecc_25519_scalarmult(out, n, &default_base);
}

View file

@ -37,7 +37,7 @@
#define ASR(n,s) (((n) >> s)|(IS_NEGATIVE(n)*((unsigned)-1) << (8*sizeof(n)-s)))
const ecc_int_256 ecc_25519_gf_order = {{
const ecc_int256_t ecc_25519_gf_order = {{
0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -58,9 +58,9 @@ static void select(unsigned char out[32], const unsigned char r[32], const unsig
}
}
int ecc_25519_gf_is_zero(const ecc_int_256 *in) {
int ecc_25519_gf_is_zero(const ecc_int256_t *in) {
int i;
ecc_int_256 r;
ecc_int256_t r;
unsigned int bits = 0;
ecc_25519_gf_reduce(&r, in);
@ -71,7 +71,7 @@ int ecc_25519_gf_is_zero(const ecc_int_256 *in) {
return (((bits-1)>>8) & 1);
}
void ecc_25519_gf_add(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_256 *in2) {
void ecc_25519_gf_add(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2) {
unsigned int j;
unsigned int u;
int nq = 1 - (in1->p[31]>>4) - (in2->p[31]>>4);
@ -85,7 +85,7 @@ void ecc_25519_gf_add(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_25
}
}
void ecc_25519_gf_sub(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_256 *in2) {
void ecc_25519_gf_sub(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2) {
unsigned int j;
unsigned int u;
int nq = 8 - (in1->p[31]>>4) + (in2->p[31]>>4);
@ -121,7 +121,7 @@ static void reduce(unsigned char a[32]) {
select(a, out1, out2, IS_NEGATIVE(u1));
}
void ecc_25519_gf_reduce(ecc_int_256 *out, const ecc_int_256 *in) {
void ecc_25519_gf_reduce(ecc_int256_t *out, const ecc_int256_t *in) {
int i;
for (i = 0; i < 32; i++)
@ -155,7 +155,7 @@ static void montgomery(unsigned char out[32], const unsigned char a[32], const u
}
void ecc_25519_gf_mult(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_256 *in2) {
void ecc_25519_gf_mult(ecc_int256_t *out, const ecc_int256_t *in1, const ecc_int256_t *in2) {
/* 2^512 mod q */
static const unsigned char C[32] = {
0x01, 0x0f, 0x9c, 0x44, 0xe3, 0x11, 0x06, 0xa4,
@ -177,7 +177,7 @@ void ecc_25519_gf_mult(ecc_int_256 *out, const ecc_int_256 *in1, const ecc_int_2
montgomery(out->p, R, C);
}
void ecc_25519_gf_recip(ecc_int_256 *out, const ecc_int_256 *in) {
void ecc_25519_gf_recip(ecc_int256_t *out, const ecc_int256_t *in) {
static const unsigned char C[32] = {
0x01
};
@ -230,7 +230,7 @@ void ecc_25519_gf_recip(ecc_int_256 *out, const ecc_int_256 *in) {
montgomery(out->p, R2, C);
}
void ecc_25519_gf_sanitize_secret(ecc_int_256 *out, const ecc_int_256 *in) {
void ecc_25519_gf_sanitize_secret(ecc_int256_t *out, const ecc_int256_t *in) {
int i;
for (i = 0; i < 32; i++)