Package com.zaranux.client.crypto.util

Examples of com.zaranux.client.crypto.util.BigInteger


    /**
     * RSA public key ops and non-CRT private key ops. Simple modPow().
     */
    private static byte[] crypt(byte[] msg, BigInteger n, BigInteger exp)
      throws BadPaddingException {
        BigInteger m = parseMsg(msg, n);
        BigInteger c = m.modPow(exp, n);
        return toByteArray(c, getByteLength(n));
    }
View Full Code Here


     * We do not generate new blinding parameters for each operation but reuse
     * them BLINDING_MAX_REUSE times (see definition below).
     */
    private static byte[] crtCrypt(byte[] msg, RSAPrivateCrtKey key)
            throws BadPaddingException {
        BigInteger n = key.getModulus();
        BigInteger c = parseMsg(msg, n);
        BigInteger p = key.getPrimeP();
        BigInteger q = key.getPrimeQ();
        BigInteger dP = key.getPrimeExponentP();
        BigInteger dQ = key.getPrimeExponentQ();
        BigInteger qInv = key.getCrtCoefficient();

        BlindingParameters params;
        if (ENABLE_BLINDING) {
            params = getBlindingParameters(key);
            c = c.multiply(params.re).mod(n);
        } else {
            params = null;
        }

        // m1 = c ^ dP mod p
        BigInteger m1 = c.modPow(dP, p);
   
        // m2 = c ^ dQ mod q
        BigInteger m2 = c.modPow(dQ, q);
       
        // h = (m1 - m2) * qInv mod p
        BigInteger mtmp = m1.subtract(m2);
        if (mtmp.signum() < 0) {
            mtmp = mtmp.add(p);
        }
        BigInteger h = mtmp.multiply(qInv).mod(p);

        // m = m2 + q * h
        BigInteger m = h.multiply(q).add(m2);

        if (params != null) {
            m = m.multiply(params.rInv).mod(n);
        }

        return toByteArray(m, getByteLength(n));
    }
View Full Code Here

   
    private static BigInteger parseMsg(byte[] msg, BigInteger n)
           throws BadPaddingException {
        BigInteger2 m_ = new BigInteger2(1, msg);
       
        BigInteger m = BigInteger.getBigInteger(m_.toString());
       if (m.compareTo(n) >= 0) {
            throw new BadPaddingException("Message is larger than modulus");
       }
        return m;
    }
View Full Code Here

     * Use cached parameters if available. If not, generate new parameters
     * and cache.
     */
    private static BlindingParameters getBlindingParameters
            (RSAPrivateCrtKey key) {
        BigInteger modulus = key.getModulus();
        BigInteger e = key.getPublicExponent();
        BlindingParameters params;
        // we release the lock between get() and put()
        // that means threads might concurrently generate new blinding
        // parameters for the same modulus. this is only a slight waste
        // of cycles and seems preferable in terms of scalability
        // to locking out all threads while generating new parameters
        synchronized (blindingCache) {
            params = blindingCache.get(modulus);
        }
        if ((params != null) && params.valid(e)) {
            return params;
        }
        int len = modulus.bitLength();
//        SecureRandom random = JCAUtil.getSecureRandom();
        BigInteger r = BigInteger.getRandomOddInteger(len);//new BigInteger(len, random).mod(modulus);
        BigInteger re = r.modPow(e, modulus);
        BigInteger rInv = r.modInverse(modulus);
        params = new BlindingParameters(e, re, rInv);
        synchronized (blindingCache) {
            blindingCache.put(modulus, params);
        }
        return params;
View Full Code Here

        }
    } */

    public void generateKeyPair(final KeyPairCallback kp) {
      
      BigInteger x = BigInteger.getBigInteger();

         // accomodate odd key sizes in case anybody wants to use them
      final int  lp = (keySize + 1) >> 1;
      final int  lq = keySize - lp;

        final BigInteger e = BigInteger.getBigInteger(publicExponent.toString());
      com.allen_sauer.gwt.log.client.Log.debug("e : " + e);

              // generate two random primes of size lp/lq
            BigInteger.getProbablePrime(lp,new NumberGeneratedCallback(){
            public void onGenerated(final BigInteger p_)
            {
              //final BigInteger p_ = new BigInteger(nbInteger.toString());
              com.allen_sauer.gwt.log.client.Log.debug("p : " + p_);
              BigInteger.getProbablePrime(lq,
                new NumberGeneratedCallback(){
                public void onGenerated(BigInteger q)
                {
                  com.allen_sauer.gwt.log.client.Log.debug("q : " + q);
                  BigInteger p = p_;
                         if (p_.compareTo(q) < 0) {
                           BigInteger tmp = p;
                              p = q;
                              q = tmp;
                          }
                         
                        BigInteger n = p.multiply(q);

                        // phi = (p - 1) * (q - 1) must be relative prime to e
                        // otherwise RSA just won't work ;-)
                        BigInteger p1 = p.subtract(BigInteger.ONE);
                        BigInteger q1 = q.subtract(BigInteger.ONE);
                        BigInteger phi = p1.multiply(q1);
                        // generate new p and q until they work. typically
                        // the first try will succeed when using F4                       
                        if (e.gcd(phi).compareTo(BigInteger.ONE) != 0) {
                          //repeat
                          generateKeyPair(kp);
                          return;
                        }

                        // private exponent d is the inverse of e mod phi
                        BigInteger d = e.modInverse(phi);

                        // 1st prime exponent pe = d mod (p - 1)
                        BigInteger pe = d.mod(p1);
                        // 2nd prime exponent qe = d mod (q - 1)
                        BigInteger qe = d.mod(q1);

                        // crt coefficient coeff is the inverse of q mod p
                        BigInteger coeff = q.modInverse(p);

                        try {
                            PublicKey publicKey = new RSAPublicKeyImpl(n, e);
                            PrivateKey privateKey =
                                    new RSAPrivateCrtKeyImpl(n, e,d,p, q, pe, qe, coeff);
View Full Code Here

        try {
            val = new DerValue (in);
            if (val.tag != DerValue.tag_Sequence)
                throw new InvalidKeyException ("invalid key format");

            BigInteger version = val.data.getBigInteger();

            if (!version.equals(this.version)) {
                throw new IOException("version mismatch: (supported: " //+
                                     // Debug.toHexString(this.version) +
                                     // ", parsed: " +
                                     // Debug.toHexString(version)
                    );
View Full Code Here

    /**
     * Read a BigInteger from the DerInputStream.
     */
   
    static BigInteger getBigInteger(DerInputStream data) throws IOException {
        BigInteger b = data.getBigInteger();

        /*
         * Some implementations do not correctly encode ASN.1 INTEGER values
         * in 2's complement format, resulting in a negative integer when
         * decoded. Correct the error by converting it to a positive integer.
View Full Code Here

     * @param len the number of bytes to use.
     * @return the integer.
     */
    public int getInteger(int len) throws IOException {

        BigInteger result = getBigInteger(len, false);
//        if (result.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {
        if (result.compareTo(BigInteger.getBigInteger(Integer.toString((Integer.MIN_VALUE)))) < 0) {
            throw new IOException("Integer below minimum valid value");
        }
//        if (result.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
        if (result.compareTo(BigInteger.getBigInteger(Integer.toString((Integer.MAX_VALUE)))) > 0) {
            throw new IOException("Integer exceeds maximum valid value");
        }
//        return result.intValue();
        return Integer.parseInt(result.toString());
    }
View Full Code Here

TOP

Related Classes of com.zaranux.client.crypto.util.BigInteger

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.