michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.jpake; michael@0: michael@0: import java.io.UnsupportedEncodingException; michael@0: import java.math.BigInteger; michael@0: import java.security.GeneralSecurityException; michael@0: import java.security.InvalidKeyException; michael@0: import java.security.MessageDigest; michael@0: import java.security.NoSuchAlgorithmException; michael@0: michael@0: import javax.crypto.Mac; michael@0: import javax.crypto.spec.SecretKeySpec; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.crypto.HKDF; michael@0: import org.mozilla.gecko.sync.crypto.KeyBundle; michael@0: michael@0: public class JPakeCrypto { michael@0: private static final String LOG_TAG = "JPakeCrypto"; michael@0: michael@0: /* michael@0: * Primes P and Q, and generator G - from original Mozilla J-PAKE michael@0: * implementation. michael@0: */ michael@0: public static final BigInteger P = new BigInteger( michael@0: "90066455B5CFC38F9CAA4A48B4281F292C260FEEF01FD61037E56258A7795A1C" + michael@0: "7AD46076982CE6BB956936C6AB4DCFE05E6784586940CA544B9B2140E1EB523F" + michael@0: "009D20A7E7880E4E5BFA690F1B9004A27811CD9904AF70420EEFD6EA11EF7DA1" + michael@0: "29F58835FF56B89FAA637BC9AC2EFAAB903402229F491D8D3485261CD068699B" + michael@0: "6BA58A1DDBBEF6DB51E8FE34E8A78E542D7BA351C21EA8D8F1D29F5D5D159394" + michael@0: "87E27F4416B0CA632C59EFD1B1EB66511A5A0FBF615B766C5862D0BD8A3FE7A0" + michael@0: "E0DA0FB2FE1FCB19E8F9996A8EA0FCCDE538175238FC8B0EE6F29AF7F642773E" + michael@0: "BE8CD5402415A01451A840476B2FCEB0E388D30D4B376C37FE401C2A2C2F941D" + michael@0: "AD179C540C1C8CE030D460C4D983BE9AB0B20F69144C1AE13F9383EA1C08504F" + michael@0: "B0BF321503EFE43488310DD8DC77EC5B8349B8BFE97C2C560EA878DE87C11E3D" + michael@0: "597F1FEA742D73EEC7F37BE43949EF1A0D15C3F3E3FC0A8335617055AC91328E" + michael@0: "C22B50FC15B941D3D1624CD88BC25F3E941FDDC6200689581BFEC416B4B2CB73", michael@0: 16); michael@0: michael@0: public static final BigInteger Q = new BigInteger( michael@0: "CFA0478A54717B08CE64805B76E5B14249A77A4838469DF7F7DC987EFCCFB11D", michael@0: 16); michael@0: michael@0: public static final BigInteger G = new BigInteger( michael@0: "5E5CBA992E0A680D885EB903AEA78E4A45A469103D448EDE3B7ACCC54D521E37" + michael@0: "F84A4BDD5B06B0970CC2D2BBB715F7B82846F9A0C393914C792E6A923E2117AB" + michael@0: "805276A975AADB5261D91673EA9AAFFEECBFA6183DFCB5D3B7332AA19275AFA1" + michael@0: "F8EC0B60FB6F66CC23AE4870791D5982AAD1AA9485FD8F4A60126FEB2CF05DB8" + michael@0: "A7F0F09B3397F3937F2E90B9E5B9C9B6EFEF642BC48351C46FB171B9BFA9EF17" + michael@0: "A961CE96C7E7A7CC3D3D03DFAD1078BA21DA425198F07D2481622BCE45969D9C" + michael@0: "4D6063D72AB7A0F08B2F49A7CC6AF335E08C4720E31476B67299E231F8BD90B3" + michael@0: "9AC3AE3BE0C6B6CACEF8289A2E2873D58E51E029CAFBD55E6841489AB66B5B4B" + michael@0: "9BA6E2F784660896AFF387D92844CCB8B69475496DE19DA2E58259B090489AC8" + michael@0: "E62363CDF82CFD8EF2A427ABCD65750B506F56DDE3B988567A88126B914D7828" + michael@0: "E2B63A6D7ED0747EC59E0E0A23CE7D8A74C1D2C2A7AFB6A29799620F00E11C33" + michael@0: "787F7DED3B30E1A22D09F1FBDA1ABBBFBF25CAE05A13F812E34563F99410E73B", michael@0: 16); michael@0: michael@0: /** michael@0: * michael@0: * Round 1 of J-PAKE protocol. michael@0: * Generate x1, x2, and ZKP for other party. michael@0: */ michael@0: public static void round1(JPakeParty jp, JPakeNumGenerator gen) throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: // Randomly select x1 from [0,q), x2 from [1,q). michael@0: BigInteger x1 = gen.generateFromRange(Q); // [0, q) michael@0: BigInteger x2 = jp.x2 = BigInteger.ONE.add(gen.generateFromRange(Q michael@0: .subtract(BigInteger.ONE))); // [1, q) michael@0: michael@0: BigInteger gx1 = G.modPow(x1, P); michael@0: BigInteger gx2 = G.modPow(x2, P); michael@0: michael@0: jp.gx1 = gx1; michael@0: jp.gx2 = gx2; michael@0: michael@0: // Generate and store zero knowledge proofs. michael@0: jp.zkp1 = createZkp(G, x1, gx1, jp.signerId, gen); michael@0: jp.zkp2 = createZkp(G, x2, gx2, jp.signerId, gen); michael@0: } michael@0: michael@0: /** michael@0: * Round 2 of J-PAKE protocol. michael@0: * Generate A and ZKP for A. michael@0: * Verify ZKP from other party. Does not check for replay ZKP. michael@0: */ michael@0: public static void round2(BigInteger secretValue, JPakeParty jp, JPakeNumGenerator gen) michael@0: throws IncorrectZkpException, NoSuchAlgorithmException, michael@0: Gx3OrGx4IsZeroOrOneException, UnsupportedEncodingException { michael@0: michael@0: Logger.debug(LOG_TAG, "round2 started."); michael@0: michael@0: // checkZkp does some additional checks, but we can throw a more informative exception here. michael@0: if (BigInteger.ZERO.compareTo(jp.gx3) == 0 || BigInteger.ONE.compareTo(jp.gx3) == 0 || michael@0: BigInteger.ZERO.compareTo(jp.gx4) == 0 || BigInteger.ONE.compareTo(jp.gx4) == 0) { michael@0: throw new Gx3OrGx4IsZeroOrOneException(); michael@0: } michael@0: michael@0: // Check ZKP. michael@0: checkZkp(G, jp.gx3, jp.zkp3); michael@0: checkZkp(G, jp.gx4, jp.zkp4); michael@0: michael@0: // Compute a = g^[(x1+x3+x4)*(x2*secret)]. michael@0: BigInteger y1 = jp.gx3.multiply(jp.gx4).mod(P).multiply(jp.gx1).mod(P); michael@0: BigInteger y2 = jp.x2.multiply(secretValue).mod(P); michael@0: michael@0: BigInteger a = y1.modPow(y2, P); michael@0: jp.thisZkpA = createZkp(y1, y2, a, jp.signerId, gen); michael@0: jp.thisA = a; michael@0: michael@0: Logger.debug(LOG_TAG, "round2 finished."); michael@0: } michael@0: michael@0: /** michael@0: * Final round of J-PAKE protocol. michael@0: */ michael@0: public static KeyBundle finalRound(BigInteger secretValue, JPakeParty jp) michael@0: throws IncorrectZkpException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { michael@0: Logger.debug(LOG_TAG, "Final round started."); michael@0: BigInteger gb = jp.gx1.multiply(jp.gx2).mod(P).multiply(jp.gx3) michael@0: .mod(P); michael@0: checkZkp(gb, jp.otherA, jp.otherZkpA); michael@0: michael@0: // Calculate shared key g^(x1+x3)*x2*x4*secret, which is equivalent to michael@0: // (B/g^(x2*x4*s))^x2 = (B*(g^x4)^x2^s^-1)^2. michael@0: BigInteger k = jp.gx4.modPow(jp.x2.multiply(secretValue).negate().mod(Q), P).multiply(jp.otherA) michael@0: .modPow(jp.x2, P); michael@0: michael@0: byte[] enc = new byte[32]; michael@0: byte[] hmac = new byte[32]; michael@0: generateKeyAndHmac(k, enc, hmac); michael@0: michael@0: Logger.debug(LOG_TAG, "Final round finished; returning key."); michael@0: return new KeyBundle(enc, hmac); michael@0: } michael@0: michael@0: // TODO Replace this function with the one in the crypto library michael@0: private static byte[] HMACSHA256(byte[] data, byte[] key) { michael@0: byte[] result = null; michael@0: try { michael@0: Mac hmacSha256; michael@0: hmacSha256 = Mac.getInstance("HmacSHA256"); michael@0: SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256"); michael@0: hmacSha256.init(secret_key); michael@0: result = hmacSha256.doFinal(data); michael@0: } catch (GeneralSecurityException e) { michael@0: Logger.error(LOG_TAG, "Got exception calculating HMAC.", e); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /* Helper Methods */ michael@0: michael@0: /* michael@0: * Generate the ZKP b = r - x*h, and g^r, where h = hash(g, g^r, g^x, id). (We michael@0: * pass in gx to save on an exponentiation of g^x) michael@0: */ michael@0: private static Zkp createZkp(BigInteger g, BigInteger x, BigInteger gx, michael@0: String id, JPakeNumGenerator gen) throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: // Generate random r for exponent. michael@0: BigInteger r = gen.generateFromRange(Q); michael@0: michael@0: // Calculate g^r for ZKP. michael@0: BigInteger gr = g.modPow(r, P); michael@0: michael@0: // Calculate the ZKP b value = (r-x*h) % q. michael@0: BigInteger h = computeBHash(g, gr, gx, id); michael@0: Logger.debug(LOG_TAG, "myhash: " + h.toString(16)); michael@0: michael@0: // ZKP value = b = r-x*h michael@0: BigInteger b = r.subtract(x.multiply(h)).mod(Q); michael@0: michael@0: return new Zkp(gr, b, id); michael@0: } michael@0: michael@0: /* michael@0: * Verify ZKP. michael@0: */ michael@0: private static void checkZkp(BigInteger g, BigInteger gx, Zkp zkp) michael@0: throws IncorrectZkpException, NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: michael@0: BigInteger h = computeBHash(g, zkp.gr, gx, zkp.id); michael@0: michael@0: // Check parameters of zkp, and compare to computed hash. These shouldn't michael@0: // fail. michael@0: if (gx.compareTo(BigInteger.ONE) < 1) { // g^x > 1. michael@0: Logger.error(LOG_TAG, "g^x > 1 fails."); michael@0: throw new IncorrectZkpException(); michael@0: } michael@0: if (gx.compareTo(P.subtract(BigInteger.ONE)) > -1) { // g^x < p-1 michael@0: Logger.error(LOG_TAG, "g^x < p-1 fails."); michael@0: throw new IncorrectZkpException(); michael@0: } michael@0: if (gx.modPow(Q, P).compareTo(BigInteger.ONE) != 0) { michael@0: Logger.error(LOG_TAG, "g^x^q % p = 1 fails."); michael@0: throw new IncorrectZkpException(); michael@0: } michael@0: if (zkp.gr.compareTo(g.modPow(zkp.b, P).multiply(gx.modPow(h, P)).mod(P)) != 0) { michael@0: // b = r-h*x ==> g^r = g^b*g^x^(h) michael@0: Logger.debug(LOG_TAG, "gb*g(xh) = " + g.modPow(zkp.b, P).multiply(gx.modPow(h, P)).mod(P).toString(16)); michael@0: Logger.debug(LOG_TAG, "gr = " + zkp.gr.toString(16)); michael@0: Logger.debug(LOG_TAG, "b = " + zkp.b.toString(16)); michael@0: Logger.debug(LOG_TAG, "g^b = " + g.modPow(zkp.b, P).toString(16)); michael@0: Logger.debug(LOG_TAG, "g^(xh) = " + gx.modPow(h, P).toString(16)); michael@0: Logger.debug(LOG_TAG, "gx = " + gx.toString(16)); michael@0: Logger.debug(LOG_TAG, "h = " + h.toString(16)); michael@0: Logger.error(LOG_TAG, "zkp calculation incorrect."); michael@0: throw new IncorrectZkpException(); michael@0: } michael@0: Logger.debug(LOG_TAG, "*** ZKP SUCCESS ***"); michael@0: } michael@0: michael@0: /* michael@0: * Use SHA-256 to compute a BigInteger hash of g, gr, gx values with michael@0: * mySignerId to prevent replay. Does not make a twos-complement BigInteger michael@0: * form hash. michael@0: */ michael@0: private static BigInteger computeBHash(BigInteger g, BigInteger gr, BigInteger gx, michael@0: String id) throws NoSuchAlgorithmException, UnsupportedEncodingException { michael@0: MessageDigest sha = MessageDigest.getInstance("SHA-256"); michael@0: sha.reset(); michael@0: michael@0: /* michael@0: * Note: you should ensure the items in H(...) have clear boundaries. It michael@0: * is simple if the other party knows sizes of g, gr, gx and signerID and michael@0: * hence the boundary is unambiguous. If not, you'd better prepend each michael@0: * item with its byte length, but I've omitted that here. michael@0: */ michael@0: michael@0: hashByteArrayWithLength(sha, BigIntegerHelper.BigIntegerToByteArrayWithoutSign(g)); michael@0: hashByteArrayWithLength(sha, BigIntegerHelper.BigIntegerToByteArrayWithoutSign(gr)); michael@0: hashByteArrayWithLength(sha, BigIntegerHelper.BigIntegerToByteArrayWithoutSign(gx)); michael@0: hashByteArrayWithLength(sha, id.getBytes("UTF-8")); michael@0: michael@0: byte[] hash = sha.digest(); michael@0: michael@0: return BigIntegerHelper.ByteArrayToBigIntegerWithoutSign(hash); michael@0: } michael@0: michael@0: /* michael@0: * Update a hash with a byte array's length and the byte array. michael@0: */ michael@0: private static void hashByteArrayWithLength(MessageDigest sha, byte[] data) { michael@0: int length = data.length; michael@0: byte[] b = new byte[] { (byte) (length >>> 8), (byte) (length & 0xff) }; michael@0: sha.update(b); michael@0: sha.update(data); michael@0: } michael@0: michael@0: /* michael@0: * Helper function to generate encryption key and HMAC from a byte array. michael@0: */ michael@0: public static void generateKeyAndHmac(BigInteger k, byte[] encOut, byte[] hmacOut) throws NoSuchAlgorithmException, InvalidKeyException { michael@0: // Generate HMAC and Encryption keys from synckey. michael@0: byte[] zerokey = new byte[32]; michael@0: byte[] prk = HMACSHA256(BigIntegerHelper.BigIntegerToByteArrayWithoutSign(k), zerokey); michael@0: michael@0: byte[] okm = HKDF.hkdfExpand(prk, HKDF.HMAC_INPUT, 32 * 2); michael@0: System.arraycopy(okm, 0, encOut, 0, 32); michael@0: System.arraycopy(okm, 32, hmacOut, 0, 32); michael@0: } michael@0: }