ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ltc_ecc_projective_dbl_point.c Source File

ltc_ecc_projective_dbl_point.c

Go to the documentation of this file.
00001 /*
00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices
00003 Author: Donatien Garnier
00004 Copyright (C) 2013-2014 AppNearMe Ltd
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 *//* LibTomCrypt, modular cryptographic library -- Tom St Denis
00020  *
00021  * LibTomCrypt is a library that provides various cryptographic
00022  * algorithms in a highly modular and flexible manner.
00023  *
00024  * The library is free for all purposes without any express
00025  * guarantee it works.
00026  *
00027  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
00028  */
00029 
00030 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
00031  *
00032  * All curves taken from NIST recommendation paper of July 1999
00033  * Available at http://csrc.nist.gov/cryptval/dss.htm
00034  */
00035 #include "ltc.h"
00036 
00037 /**
00038   @file ltc_ecc_projective_dbl_point.c
00039   ECC Crypto, Tom St Denis
00040 */  
00041 
00042 #if defined(LTC_MECC) && (!defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC))
00043 
00044 /**
00045    Double an ECC point
00046    @param P   The point to double
00047    @param R   [out] The destination of the double
00048    @param modulus  The modulus of the field the ECC curve is in
00049    @param mp       The "b" value from montgomery_setup()
00050    @return MINITLS_OK on success
00051 */
00052 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp)
00053 {
00054    fp_int t1, t2;
00055    int   err;
00056 
00057    LTC_ARGCHK(P       != NULL);
00058    LTC_ARGCHK(R       != NULL);
00059    LTC_ARGCHK(modulus != NULL);
00060    LTC_ARGCHK(mp      != NULL);
00061 
00062    if ((err = mp_init_multi(&t1, &t2, NULL)) != MINITLS_OK){
00063       return err;
00064    }
00065 
00066    if (P != R) {
00067       /*if ((err = */mp_copy(&P->x, &R->x);/*) != MINITLS_OK)                             { goto done; }*/
00068       /*if ((err = */mp_copy(&P->y, &R->y);/*) != MINITLS_OK)                             { goto done; }*/
00069       /*if ((err = */mp_copy(&P->z, &R->z);/*) != MINITLS_OK)                             { goto done; }*/
00070    }
00071 
00072    /* &t1 = Z * Z */
00073    /*if ((err = */mp_sqr(&R->z, &t1);/*) != MINITLS_OK)                                   { goto done; }*/
00074    /*if ((err = */mp_montgomery_reduce(&t1, modulus, mp);/*) != MINITLS_OK)                { goto done; }*/
00075    /* Z = Y * Z */
00076    /*if ((err = */mp_mul(&R->z, &R->y, &R->z);/*) != MINITLS_OK)                             { goto done; }*/
00077    /*if ((err = */mp_montgomery_reduce(&R->z, modulus, mp);/*) != MINITLS_OK)            { goto done; }*/
00078    /* Z = 2Z */
00079    /*if ((err = */mp_add(&R->z, &R->z, &R->z);/*) != MINITLS_OK)                             { goto done; }*/
00080    if (mp_cmp(&R->z, modulus) != MP_LT) {
00081       /*if ((err = */mp_sub(&R->z, modulus, &R->z);/*) != MINITLS_OK)                     { goto done; }*/
00082    }
00083    
00084    /* T2 = X - T1 */
00085    /*if ((err = */mp_sub(&R->x, &t1, &t2);/*) != MINITLS_OK)                                 { goto done; }*/
00086    if (mp_cmp_d(&t2, 0) == MP_LT) {
00087       /*if ((err = */mp_add(&t2, modulus, &t2);/*) != MINITLS_OK)                           { goto done; }*/
00088    }
00089    /* T1 = X + T1 */
00090    /*if ((err = */mp_add(&t1, &R->x, &t1);/*) != MINITLS_OK)                                 { goto done; }*/
00091    if (mp_cmp(&t1, modulus) != MP_LT) {
00092       /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK)                           { goto done; }*/
00093    }
00094    /* T2 = T1 * T2 */
00095    /*if ((err = */mp_mul(&t1, &t2, &t2);/*) != MINITLS_OK)                                   { goto done; }*/
00096    /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK)                { goto done; }*/
00097    /* T1 = 2T2 */
00098    /*if ((err = */mp_add(&t2, &t2, &t1);/*) != MINITLS_OK)                                   { goto done; }*/
00099    if (mp_cmp(&t1, modulus) != MP_LT) {
00100       /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK)                           { goto done; }*/
00101    }
00102    /* T1 = T1 + T2 */
00103    /*if ((err = */mp_add(&t1, &t2, &t1);/*) != MINITLS_OK)                                   { goto done; }*/
00104    if (mp_cmp(&t1, modulus) != MP_LT) {
00105       /*if ((err = */mp_sub(&t1, modulus, &t1);/*) != MINITLS_OK)                           { goto done; }*/
00106    }
00107 
00108    /* Y = 2Y */
00109    /*if ((err = */mp_add(&R->y, &R->y, &R->y);/*) != MINITLS_OK)                             { goto done; }*/
00110    if (mp_cmp(&R->y, modulus) != MP_LT) {
00111       /*if ((err = */mp_sub(&R->y, modulus, &R->y);/*) != MINITLS_OK)                     { goto done; }*/
00112    }
00113    /* Y = Y * Y */
00114    /*if ((err = */mp_sqr(&R->y, &R->y);/*) != MINITLS_OK)                                   { goto done; }*/
00115    /*if ((err = */mp_montgomery_reduce(&R->y, modulus, mp);/*) != MINITLS_OK)            { goto done; }*/
00116    /* T2 = Y * Y */
00117    /*if ((err = */mp_sqr(&R->y, &t2);/*) != MINITLS_OK)                                   { goto done; }*/
00118    /*if ((err = */mp_montgomery_reduce(&t2, modulus, mp);/*) != MINITLS_OK)                { goto done; }*/
00119    /* T2 = T2/2 */
00120    if (mp_isodd(&t2)) {
00121       /*if ((err = */mp_add(&t2, modulus, &t2);/*) != MINITLS_OK)                           { goto done; }*/
00122    }
00123    /*if ((err = */mp_div_2(&t2, &t2);/*) != MINITLS_OK)                                   { goto done; }*/
00124    /* Y = Y * X */
00125    /*if ((err = */mp_mul(&R->y, &R->x, &R->y);/*) != MINITLS_OK)                             { goto done; }*/
00126    /*if ((err = */mp_montgomery_reduce(&R->y, modulus, mp);/*) != MINITLS_OK)            { goto done; }*/
00127 
00128    /* X  = T1 * T1 */
00129    /*if ((err = */mp_sqr(&t1, &R->x);/*) != MINITLS_OK)                                   { goto done; }*/
00130    /*if ((err = */mp_montgomery_reduce(&R->x, modulus, mp);/*) != MINITLS_OK)            { goto done; }*/
00131    /* X = X - Y */
00132    /*if ((err = */mp_sub(&R->x, &R->y, &R->x);/*) != MINITLS_OK)                             { goto done; }*/
00133    if (mp_cmp_d(&R->x, 0) == MP_LT) {
00134       /*if ((err = */mp_add(&R->x, modulus, &R->x);/*) != MINITLS_OK)                     { goto done; }*/
00135    }
00136    /* X = X - Y */
00137    /*if ((err = */mp_sub(&R->x, &R->y, &R->x);/*) != MINITLS_OK)                             { goto done; }*/
00138    if (mp_cmp_d(&R->x, 0) == MP_LT) {
00139       /*if ((err = */mp_add(&R->x, modulus, &R->x);/*) != MINITLS_OK)                     { goto done; }*/
00140    }
00141 
00142    /* Y = Y - X */     
00143    /*if ((err = */mp_sub(&R->y, &R->x, &R->y);/*) != MINITLS_OK)                             { goto done; }*/
00144    if (mp_cmp_d(&R->y, 0) == MP_LT) {
00145       /*if ((err = */mp_add(&R->y, modulus, &R->y);/*) != MINITLS_OK)                     { goto done; }*/
00146    }
00147    /* Y = Y * T1 */
00148    /*if ((err = */mp_mul(&R->y, &t1, &R->y);/*) != MINITLS_OK)                             { goto done; }*/
00149    /*if ((err = */mp_montgomery_reduce(&R->y, modulus, mp);/*) != MINITLS_OK)            { goto done; }*/
00150    /* Y = Y - T2 */
00151    /*if ((err = */mp_sub(&R->y, &t2, &R->y);/*) != MINITLS_OK)                             { goto done; }*/
00152    if (mp_cmp_d(&R->y, 0) == MP_LT) {
00153       /*if ((err = */mp_add(&R->y, modulus, &R->y);/*) != MINITLS_OK)                     { goto done; }*/
00154    }
00155  
00156    err = MINITLS_OK;
00157 /*done:*/ //Unused
00158    mp_clear_multi(&t1, &t2, NULL);
00159    return err;
00160 }
00161 #endif
00162 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ltc_ecc_projective_dbl_point.c,v $ */
00163 /* $Revision: 1.11 $ */
00164 /* $Date: 2007/05/12 14:32:35 $ */
00165