|
| 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(<c_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(<c_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