]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/ecc/ltc_ecc_map.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / ecc / ltc_ecc_map.c
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 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11  *
12  * All curves taken from NIST recommendation paper of July 1999
13  * Available at http://csrc.nist.gov/cryptval/dss.htm
14  */
15 #include "tomcrypt.h"
16
17 /**
18   @file ltc_ecc_map.c
19   ECC Crypto, Tom St Denis
20 */
21
22 #ifdef LTC_MECC
23
24 /**
25   Map a projective jacbobian point back to affine space
26   @param P        [in/out] The point to map
27   @param modulus  The modulus of the field the ECC curve is in
28   @param mp       The "b" value from montgomery_setup()
29   @return CRYPT_OK on success
30 */
31 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp)
32 {
33    void *t1, *t2;
34    int   err;
35
36    LTC_ARGCHK(P       != NULL);
37    LTC_ARGCHK(modulus != NULL);
38    LTC_ARGCHK(mp      != NULL);
39
40    if ((err = mp_init_multi(&t1, &t2, NULL)) != CRYPT_OK) {
41       return err;
42    }
43
44    /* first map z back to normal */
45    if ((err = mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK)           { goto done; }
46
47    /* get 1/z */
48    if ((err = mp_invmod(P->z, modulus, t1)) != CRYPT_OK)                      { goto done; }
49
50    /* get 1/z^2 and 1/z^3 */
51    if ((err = mp_sqr(t1, t2)) != CRYPT_OK)                                    { goto done; }
52    if ((err = mp_mod(t2, modulus, t2)) != CRYPT_OK)                           { goto done; }
53    if ((err = mp_mul(t1, t2, t1)) != CRYPT_OK)                                { goto done; }
54    if ((err = mp_mod(t1, modulus, t1)) != CRYPT_OK)                           { goto done; }
55
56    /* multiply against x/y */
57    if ((err = mp_mul(P->x, t2, P->x)) != CRYPT_OK)                            { goto done; }
58    if ((err = mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK)           { goto done; }
59    if ((err = mp_mul(P->y, t1, P->y)) != CRYPT_OK)                            { goto done; }
60    if ((err = mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK)           { goto done; }
61    if ((err = mp_set(P->z, 1)) != CRYPT_OK)                                   { goto done; }
62
63    err = CRYPT_OK;
64 done:
65    mp_clear_multi(t1, t2, NULL);
66    return err;
67 }
68
69 #endif
70
71 /* ref:         $Format:%D$ */
72 /* git commit:  $Format:%H$ */
73 /* commit time: $Format:%ai$ */
74