Ejemplo n.º 1
0
/* Calculate a public/private EC GF(p) key pair W,S where W=S.G mod EC(p),
 * where S is the secret key and W is the public key
 * and G is fixed generator.
 * If RNG is NULL then the private key is provided externally in S
 * otherwise it is generated randomly internally */
    public static int KEY_PAIR_GENERATE(RAND RNG, sbyte[] S, sbyte[] W)
    {
        BIG r, gx, gy, s;
        ECP G, WP;
        int res = 0;

        sbyte[] T = new sbyte[EFS];

        gx = new BIG(ROM.CURVE_Gx);
        if (ROM.CURVETYPE != ROM.MONTGOMERY)
        {
            gy = new BIG(ROM.CURVE_Gy);
            G  = new ECP(gx, gy);
        }
        else
        {
            G = new ECP(gx);
        }

        r = new BIG(ROM.CURVE_Order);

        if (RNG == null)
        {
            s = BIG.fromBytes(S);
        }
        else
        {
            s = BIG.randomnum(r, RNG);

            s.toBytes(T);
            for (int i = 0; i < EGS; i++)
            {
                S[i] = T[i];
            }
        }

        WP = G.mul(s);
        WP.toBytes(W);

        return(res);
    }