Skip to content

Commit 5061bfd

Browse files
committed
some ideas related to ltc_ecc_set_type
1 parent 0499f30 commit 5061bfd

File tree

2 files changed

+282
-0
lines changed

2 files changed

+282
-0
lines changed

src/pk/ecc/ecc_dp_utils1.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt.h"
11+
12+
#ifdef LTC_MECC
13+
14+
ltc_ecc_set_type *ecc_dp_copy_set(const ltc_ecc_set_type *set)
15+
{
16+
ltc_ecc_set_type *new;
17+
size_t len;
18+
unsigned long i;
19+
20+
if (set == NULL || set->size == 0) return NULL;
21+
22+
new = XMALLOC(sizeof(ltc_ecc_set_type));
23+
if (new == NULL) return NULL;
24+
25+
/* A */
26+
len = strlen(set->A) + 1;
27+
if ((new->A = XMALLOC(len)) == NULL) goto cleanup1;
28+
strncpy(new->A, set->A, len);
29+
/* B */
30+
len = strlen(set->B) + 1;
31+
if ((new->B = XMALLOC(len)) == NULL) goto cleanup2;
32+
strncpy(new->B, set->B, len);
33+
/* order */
34+
len = strlen(set->order) + 1;
35+
if ((new->order = XMALLOC(len)) == NULL) goto cleanup3;
36+
strncpy(new->order, set->order, len);
37+
/* prime */
38+
len = strlen(set->prime) + 1;
39+
if ((new->prime = XMALLOC(len)) == NULL) goto cleanup4;
40+
strncpy(new->prime, set->prime, len);
41+
/* Gx */
42+
len = strlen(set->Gx) + 1;
43+
if ((new->Gx = XMALLOC(len)) == NULL) goto cleanup5;
44+
strncpy(new->Gx, set->Gx, len);
45+
/* Gy */
46+
len = strlen(set->Gy) + 1;
47+
if ((new->Gy = XMALLOC(len)) == NULL) goto cleanup6;
48+
strncpy(new->Gy, set->Gy, len);
49+
/* name */
50+
len = strlen(set->name) + 1;
51+
if ((new->name = XMALLOC(len)) == NULL) goto cleanup7;
52+
strncpy(new->name, set->name, len);
53+
/* cofactor & size */
54+
new->cofactor = set->cofactor;
55+
new->size = set->size;
56+
/* oid */
57+
new->oid.OIDlen = set->oid.OIDlen;
58+
for (i = 0; i < new->oid.OIDlen; i++) new->oid.OID[i] = set->oid.OID[i];
59+
return new;
60+
61+
cleanup7:
62+
XFREE(new->Gy);
63+
cleanup6:
64+
XFREE(new->Gx);
65+
cleanup5:
66+
XFREE(new->prime);
67+
cleanup4:
68+
XFREE(new->order);
69+
cleanup3:
70+
XFREE(new->B);
71+
cleanup2:
72+
XFREE(new->A);
73+
cleanup1:
74+
XFREE(new);
75+
return NULL;
76+
}
77+
78+
ltc_ecc_set_type *ecc_dp_new_by_name(char *curve_name)
79+
{
80+
int i;
81+
for (i = 0; ltc_ecc_sets[i].size != 0; i++) {
82+
if (ltc_ecc_sets[i].name != NULL && XSTRCMP(ltc_ecc_sets[i].name, curve_name) == 0) {
83+
break;
84+
}
85+
}
86+
return ecc_dp_copy_set(&ltc_ecc_sets[i]);
87+
}
88+
89+
ltc_ecc_set_type *ecc_dp_new_by_oid(unsigned long *oid, unsigned long oidsize)
90+
{
91+
int i;
92+
for(i = 0; ltc_ecc_sets[i].size != 0; i++) {
93+
if ((oidsize == ltc_ecc_sets[i].oid.OIDlen) &&
94+
(XMEM_NEQ(oid, ltc_ecc_sets[i].oid.OID, sizeof(unsigned long) * ltc_ecc_sets[i].oid.OIDlen) == 0)) {
95+
break;
96+
}
97+
}
98+
return ecc_dp_copy_set(&ltc_ecc_sets[i]);
99+
}
100+
101+
ltc_ecc_set_type *ecc_dp_new_by_size(int size)
102+
{
103+
/* for compatibility with libtomcrypt-1.17 the sizes below must match the specific curves */
104+
if (size <= 14) {
105+
return ecc_dp_new_by_name("SECP112R1");
106+
}
107+
else if (size <= 16) {
108+
return ecc_dp_new_by_name("SECP128R1");
109+
}
110+
else if (size <= 20) {
111+
return ecc_dp_new_by_name("SECP160R1");
112+
}
113+
else if (size <= 24) {
114+
return ecc_dp_new_by_name("SECP192R1");
115+
}
116+
else if (size <= 28) {
117+
return ecc_dp_new_by_name("SECP224R1");
118+
}
119+
else if (size <= 32) {
120+
return ecc_dp_new_by_name("SECP256R1");
121+
}
122+
else if (size <= 48) {
123+
return ecc_dp_new_by_name("SECP384R1");
124+
}
125+
else if (size <= 66) {
126+
return ecc_dp_new_by_name("SECP521R1");
127+
}
128+
return NULL;
129+
}
130+
131+
void ecc_dp_free(ltc_ecc_set_type *dp)
132+
{
133+
if (dp == NULL) return;
134+
if (dp->name != NULL) XFREE(dp->name);
135+
if (dp->prime != NULL) XFREE(dp->prime);
136+
if (dp->A != NULL) XFREE(dp->A);
137+
if (dp->B != NULL) XFREE(dp->B);
138+
if (dp->order != NULL) XFREE(dp->order);
139+
if (dp->Gx != NULL) XFREE(dp->Gx);
140+
if (dp->Gy != NULL) XFREE(dp->Gy);
141+
XFREE(dp);
142+
return;
143+
}
144+
145+
#endif
146+
147+
/* ref: $Format:%D$ */
148+
/* git commit: $Format:%H$ */
149+
/* commit time: $Format:%ai$ */

src/pk/ecc/ecc_dp_utils2.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2+
*
3+
* LibTomCrypt is a library that provides various cryptographic
4+
* algorithms in a highly modular and flexible manner.
5+
*
6+
* The library is free for all purposes without any express
7+
* guarantee it works.
8+
*/
9+
10+
#include "tomcrypt.h"
11+
12+
#ifdef LTC_MECC
13+
14+
typedef struct {
15+
int size; /* The size of the curve in octets */
16+
void *prime; /* The prime that defines the field the curve is in */
17+
void *A; /* The fields A param */
18+
void *B; /* The fields B param */
19+
void *order; /* The order of the curve */
20+
void *Gx; /* The x co-ordinate of the base point on the curve */
21+
void *Gy; /* The y co-ordinate of the base point on the curve */
22+
unsigned long cofactor; /* The co-factor */
23+
oid_st oid; /* The OID stucture */
24+
} ltc_ecc_dp;
25+
26+
ltc_ecc_dp *ecc_dp_new_set(const ltc_ecc_set_type *set)
27+
{
28+
ltc_ecc_dp *new;
29+
unsigned long i;
30+
int err;
31+
32+
if (set == NULL || set->size == 0) return NULL;
33+
34+
new = XMALLOC(sizeof(ltc_ecc_dp));
35+
if (new == NULL) return NULL;
36+
37+
if ((err = mp_init_multi(&new->prime, &new->A, &new->B, &new->order, &new->Gx, &new->Gy, NULL)) != CRYPT_OK) {
38+
goto cleanup1;
39+
}
40+
41+
/* A, B, order, prime, Gx, Gy */
42+
if ((err = mp_read_radix(new->A, set->A, 16)) != CRYPT_OK) { goto cleanup2; }
43+
if ((err = mp_read_radix(new->B, set->B, 16)) != CRYPT_OK) { goto cleanup2; }
44+
if ((err = mp_read_radix(new->order, set->order, 16)) != CRYPT_OK) { goto cleanup2; }
45+
if ((err = mp_read_radix(new->prime, set->prime, 16)) != CRYPT_OK) { goto cleanup2; }
46+
if ((err = mp_read_radix(new->Gx, set->Gx, 16)) != CRYPT_OK) { goto cleanup2; }
47+
if ((err = mp_read_radix(new->Gy, set->Gy, 16)) != CRYPT_OK) { goto cleanup2; }
48+
/* cofactor & size */
49+
new->cofactor = set->cofactor;
50+
new->size = set->size;
51+
/* OID */
52+
new->oid.OIDlen = set->oid.OIDlen;
53+
for (i = 0; i < new->oid.OIDlen; i++) new->oid.OID[i] = set->oid.OID[i];
54+
return new;
55+
56+
cleanup2:
57+
mp_clear_multi(new->prime, new->A, new->B, new->order, new->Gx, new->Gy, NULL);
58+
cleanup1:
59+
XFREE(new);
60+
return NULL;
61+
}
62+
63+
ltc_ecc_dp *ecc_dp_new_name(char *curve_name)
64+
{
65+
int i;
66+
for (i = 0; ltc_ecc_sets[i].size != 0; i++) {
67+
if (ltc_ecc_sets[i].name != NULL && XSTRCMP(ltc_ecc_sets[i].name, curve_name) == 0) {
68+
break;
69+
}
70+
}
71+
return ecc_dp_new_set(&ltc_ecc_sets[i]);
72+
}
73+
74+
ltc_ecc_dp *ecc_dp_new_oid(unsigned long *oid, unsigned long oidsize)
75+
{
76+
int i;
77+
for(i = 0; ltc_ecc_sets[i].size != 0; i++) {
78+
if ((oidsize == ltc_ecc_sets[i].oid.OIDlen) &&
79+
(XMEM_NEQ(oid, ltc_ecc_sets[i].oid.OID, sizeof(unsigned long) * ltc_ecc_sets[i].oid.OIDlen) == 0)) {
80+
break;
81+
}
82+
}
83+
return ecc_dp_new_set(&ltc_ecc_sets[i]);
84+
}
85+
86+
ltc_ecc_dp *ecc_dp_new_size(int size)
87+
{
88+
/* for compatibility with libtomcrypt-1.17 the sizes below must match the specific curves */
89+
if (size <= 14) {
90+
return ecc_dp_new_name("SECP112R1");
91+
}
92+
else if (size <= 16) {
93+
return ecc_dp_new_name("SECP128R1");
94+
}
95+
else if (size <= 20) {
96+
return ecc_dp_new_name("SECP160R1");
97+
}
98+
else if (size <= 24) {
99+
return ecc_dp_new_name("SECP192R1");
100+
}
101+
else if (size <= 28) {
102+
return ecc_dp_new_name("SECP224R1");
103+
}
104+
else if (size <= 32) {
105+
return ecc_dp_new_name("SECP256R1");
106+
}
107+
else if (size <= 48) {
108+
return ecc_dp_new_name("SECP384R1");
109+
}
110+
else if (size <= 66) {
111+
return ecc_dp_new_name("SECP521R1");
112+
}
113+
return NULL;
114+
}
115+
116+
void ecc_dp_free(ltc_ecc_dp *dp)
117+
{
118+
if (dp == NULL) return;
119+
if (dp->prime != NULL) XFREE(dp->prime);
120+
if (dp->A != NULL) XFREE(dp->A);
121+
if (dp->B != NULL) XFREE(dp->B);
122+
if (dp->order != NULL) XFREE(dp->order);
123+
if (dp->Gx != NULL) XFREE(dp->Gx);
124+
if (dp->Gy != NULL) XFREE(dp->Gy);
125+
XFREE(dp);
126+
return;
127+
}
128+
129+
#endif
130+
131+
/* ref: $Format:%D$ */
132+
/* git commit: $Format:%H$ */
133+
/* commit time: $Format:%ai$ */

0 commit comments

Comments
 (0)