Code taken from {@link org.bouncycastle.crypto.macs.CMac}
329605
static byte[] dbl(byte[] in) {
		byte[] ret = new byte[in.length];
		int carry = shiftLeft(in, ret);
		int xor = 0xff & DOUBLING_CONST;

		/*
		 * NOTE: This construction is an attempt at a constant-time implementation.
		 */
		int mask = (-carry) & 0xff;
		ret[in.length - 1] ^= xor & mask;

		return ret;
	}
1288215
final byte[] cryptoAsym(final byte[] input, final int offset, final int len, final boolean decrypt)
			throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
			BadPaddingException {
		rsaCipher.init(decrypt ? Cipher.DECRYPT_MODE : Cipher.ENCRYPT_MODE, decrypt ? rsaKeyForDecrypt
				: rsaKeyForEncrypt);
		return rsaCipher.doFinal(input, offset, len);
	}
718945
public static byte[] encipher(byte[] decrypted_bytes, String crypto_algorithm)
                    throws InvalidPasswordCipherException, UnsupportedCryptoAlgorithmException {
        EncryptedInfo info = encipher_internal(decrypted_bytes, crypto_algorithm, (String) null); // TODO check null
        return info.getEncryptedBytes();
    }
591385
function bcryptAsm(stdlib, foreign, heap) {
        "use asm";

        var HEAP32 = new stdlib.Uint32Array(heap);

        var BLOWFISH_NUM_ROUNDS = 16;
        var S_offset = 0x0000;          // length 0x1000 octets
        var S1_offset = 0x0400;
        var S2_offset = 0x0800;
        var S3_offset = 0x0C00;
        var P_offset = 0x1000;          // length 0x0048 (72) octets
        var P_last_offset = 0x1044;
        var crypt_ciphertext_offset = 0x1048;    // length 0x0018 (24) octets
        var LR_offset = 0x01060;        // length 8 octets
        var password_offset = 0x1068;   // length 0x0048 (72) octets
        var salt_offset = 0x10b0;       // length 0x0048 (72) octets, 18 uint32
        var P_LEN = 18;
        var S_LEN = 1024;

        function encrypt(offset) {
            offset = offset|0;
            var i = 0;
            var n = 0;
            var L = 0;
            var R = 0;
            var imax = 0;
            imax = P_offset | BLOWFISH_NUM_ROUNDS << 2;
            
            L = HEAP32[offset >> 2]|0;
            R = HEAP32[offset + 4 >> 2]|0;

            L = L ^ HEAP32[P_offset>>2];
            for (i = P_offset; (i|0) < (imax|0);) {
                // Feistel substitution on left word
                i = (i + 4)>>>0;
                R = R ^ (
                    ((HEAP32[(L >>> 22)>>2] >>> 0) +
                    (HEAP32[(S1_offset | (L >>> 14 & 0x3ff))>>2] >>> 0) ^
                    (HEAP32[(S2_offset | (L >>> 6 & 0x3ff))>>2])) +
                    (HEAP32[(S3_offset | (L << 2 & 0x3ff))>>2] >>> 0)
                ) ^ HEAP32[i>>2];

                // Feistel substitution on right word
                i = (i + 4)>>>0;
                L = L ^ (
                    ((HEAP32[(R >>> 22)>>2] >>> 0) +
                    (HEAP32[(S1_offset | (R >>> 14 & 0x3ff))>>2] >>> 0) ^
                    (HEAP32[(S2_offset | (R >>> 6 & 0x3ff))>>2])) +
                    (HEAP32[(S3_offset | (R << 2 & 0x3ff))>>2] >>> 0)
                ) ^ HEAP32[i>>2];
            }
            HEAP32[offset>>2] = R ^ HEAP32[P_last_offset>>2];
            HEAP32[(offset+4)>>2] = L;
        }

        function expandKey(offset) {
            offset = offset|0;
            var i = 0;
            var off = 0;

            off = P_offset|0;
            for (i = 0; (i|0) < (P_LEN|0); i = (i+1)|0) {
                HEAP32[off >> 2] = HEAP32[off >> 2] ^ HEAP32[offset >> 2];
                offset = (offset + 4)|0;
                off = (off + 4)|0;
            }

            HEAP32[LR_offset >> 2] = 0;
            HEAP32[LR_offset + 4 >> 2] = 0;

            off = P_offset;
            for (i = 0; (i|0) < (P_LEN|0); i = (i+2)|0) {
                encrypt(LR_offset);
                HEAP32[off >> 2] = HEAP32[LR_offset >> 2];
                HEAP32[off + 4 >> 2] = HEAP32[LR_offset + 4 >> 2];
                off = (off + 8)|0;
            }

            off = S_offset;
            for (i = 0; (i|0) < (S_LEN|0); i = (i+2)|0) {
                encrypt(LR_offset);
                HEAP32[off >> 2] = HEAP32[LR_offset >> 2];
                HEAP32[off + 4 >> 2] = HEAP32[LR_offset + 4 >> 2];
                off = (off + 8)|0;
            }
        }

        function expandLoop(i, counterEnd, maxIterations) {
            i = i|0;
            counterEnd = counterEnd|0;
            maxIterations = maxIterations|0;
            var j = 0;

            for (j = 0; (j|0) <= (maxIterations|0); j = (j+1)|0) {
                if ((i>>>0) > (counterEnd>>>0)) break;
                expandKey(password_offset);
                expandKey(salt_offset);
                i = (i+1)>>>0;
            }
            return i|0;
        }

        return {
            encrypt: encrypt,
            expandLoop: expandLoop
        };
    }
792978
@Override
    public X509Certificate[] getCertificateChain(String s) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.entry(tc, "getCertificateChain: " + s);

        X509Certificate[] rc = getX509KeyManager().getCertificateChain(s);

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            Tr.exit(tc, "getCertificateChain", rc);
        return rc;
    }
632523
private byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
        byte[] data = encryptKey.getBytes(ENCODING);
        // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
        SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
        // 生成一个指定 Mac 算法 的 Mac 对象
        Mac mac = Mac.getInstance(MAC_NAME);
        // 用给定密钥初始化 Mac 对象
        mac.init(secretKey);

        byte[] text = encryptText.getBytes(ENCODING);
        // 完成 Mac 操作
        return mac.doFinal(text);
    }
294637
function crypto_sign(sm, m, n, sk) {
  var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
  var i, j, x = new Float64Array(64);
  var p = [gf(), gf(), gf(), gf()];

  crypto_hash(d, sk, 32);
  d[0] &= 248;
  d[31] &= 127;
  d[31] |= 64;

  var smlen = n + 64;
  for (i = 0; i < n; i++) sm[64 + i] = m[i];
  for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];

  crypto_hash(r, sm.subarray(32), n+32);
  reduce(r);
  scalarbase(p, r);
  pack(sm, p);

  for (i = 32; i < 64; i++) sm[i] = sk[i];
  crypto_hash(h, sm, n + 64);
  reduce(h);

  for (i = 0; i < 64; i++) x[i] = 0;
  for (i = 0; i < 32; i++) x[i] = r[i];
  for (i = 0; i < 32; i++) {
    for (j = 0; j < 32; j++) {
      x[i+j] += h[i] * d[j];
    }
  }

  modL(sm.subarray(32), x);
  return smlen;
}
612548
public static String hmac(String string) throws NoSuchAlgorithmException, InvalidKeyException {
        String code = "";
        KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
        SecretKey secretKey = keyGenerator.generateKey();
        SecretKey secretKey2 = new SecretKeySpec(secretKey.getEncoded(), "HmacMD5");
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey2);
        return code;
    }
1073050
public static int crypto_scalarmult(byte[] q,
  byte[] n,
  byte[] p)
{
  byte[] e = new byte[32];
  int i;
  int[] x1 = new int[10];
  int[] x2 = new int[10];
  int[] z2 = new int[10];
  int[] x3 = new int[10];
  int[] z3 = new int[10];
  int[] tmp0 = new int[10];
  int[] tmp1 = new int[10];
  int pos;
  int swap;
  int b;

  for (i = 0;i < 32;++i) e[i] = n[i];
//  e[0] &= 248;
//  e[31] &= 127;
//  e[31] |= 64;
  fe_frombytes.fe_frombytes(x1,p);
  fe_1.fe_1(x2);
  fe_0.fe_0(z2);
  fe_copy.fe_copy(x3,x1);
  fe_1.fe_1(z3);

  swap = 0;
  for (pos = 254;pos >= 0;--pos) {
    b = e[pos / 8] >>> (pos & 7);
    b &= 1;
    swap ^= b;
    fe_cswap.fe_cswap(x2,x3,swap);
    fe_cswap.fe_cswap(z2,z3,swap);
    swap = b;
//CONVERT #include "montgomery.h"

/* qhasm: fe X2 */

/* qhasm: fe Z2 */

/* qhasm: fe X3 */

/* qhasm: fe Z3 */

/* qhasm: fe X4 */

/* qhasm: fe Z4 */

/* qhasm: fe X5 */

/* qhasm: fe Z5 */

/* qhasm: fe A */

/* qhasm: fe B */

/* qhasm: fe C */

/* qhasm: fe D */

/* qhasm: fe E */

/* qhasm: fe AA */

/* qhasm: fe BB */

/* qhasm: fe DA */

/* qhasm: fe CB */

/* qhasm: fe t0 */

/* qhasm: fe t1 */

/* qhasm: fe t2 */

/* qhasm: fe t3 */

/* qhasm: fe t4 */

/* qhasm: enter ladder */

/* qhasm: D = X3-Z3 */
/* asm 1: fe_sub.fe_sub(>D=fe#5,<X3=fe#3,<Z3=fe#4); */
/* asm 2: fe_sub.fe_sub(>D=tmp0,<X3=x3,<Z3=z3); */
fe_sub.fe_sub(tmp0,x3,z3);

/* qhasm: B = X2-Z2 */
/* asm 1: fe_sub.fe_sub(>B=fe#6,<X2=fe#1,<Z2=fe#2); */
/* asm 2: fe_sub.fe_sub(>B=tmp1,<X2=x2,<Z2=z2); */
fe_sub.fe_sub(tmp1,x2,z2);

/* qhasm: A = X2+Z2 */
/* asm 1: fe_add.fe_add(>A=fe#1,<X2=fe#1,<Z2=fe#2); */
/* asm 2: fe_add.fe_add(>A=x2,<X2=x2,<Z2=z2); */
fe_add.fe_add(x2,x2,z2);

/* qhasm: C = X3+Z3 */
/* asm 1: fe_add.fe_add(>C=fe#2,<X3=fe#3,<Z3=fe#4); */
/* asm 2: fe_add.fe_add(>C=z2,<X3=x3,<Z3=z3); */
fe_add.fe_add(z2,x3,z3);

/* qhasm: DA = D*A */
/* asm 1: fe_mul.fe_mul(>DA=fe#4,<D=fe#5,<A=fe#1); */
/* asm 2: fe_mul.fe_mul(>DA=z3,<D=tmp0,<A=x2); */
fe_mul.fe_mul(z3,tmp0,x2);

/* qhasm: CB = C*B */
/* asm 1: fe_mul.fe_mul(>CB=fe#2,<C=fe#2,<B=fe#6); */
/* asm 2: fe_mul.fe_mul(>CB=z2,<C=z2,<B=tmp1); */
fe_mul.fe_mul(z2,z2,tmp1);

/* qhasm: BB = B^2 */
/* asm 1: fe_sq.fe_sq(>BB=fe#5,<B=fe#6); */
/* asm 2: fe_sq.fe_sq(>BB=tmp0,<B=tmp1); */
fe_sq.fe_sq(tmp0,tmp1);

/* qhasm: AA = A^2 */
/* asm 1: fe_sq.fe_sq(>AA=fe#6,<A=fe#1); */
/* asm 2: fe_sq.fe_sq(>AA=tmp1,<A=x2); */
fe_sq.fe_sq(tmp1,x2);

/* qhasm: t0 = DA+CB */
/* asm 1: fe_add.fe_add(>t0=fe#3,<DA=fe#4,<CB=fe#2); */
/* asm 2: fe_add.fe_add(>t0=x3,<DA=z3,<CB=z2); */
fe_add.fe_add(x3,z3,z2);

/* qhasm: assign x3 to t0 */

/* qhasm: t1 = DA-CB */
/* asm 1: fe_sub.fe_sub(>t1=fe#2,<DA=fe#4,<CB=fe#2); */
/* asm 2: fe_sub.fe_sub(>t1=z2,<DA=z3,<CB=z2); */
fe_sub.fe_sub(z2,z3,z2);

/* qhasm: X4 = AA*BB */
/* asm 1: fe_mul.fe_mul(>X4=fe#1,<AA=fe#6,<BB=fe#5); */
/* asm 2: fe_mul.fe_mul(>X4=x2,<AA=tmp1,<BB=tmp0); */
fe_mul.fe_mul(x2,tmp1,tmp0);

/* qhasm: E = AA-BB */
/* asm 1: fe_sub.fe_sub(>E=fe#6,<AA=fe#6,<BB=fe#5); */
/* asm 2: fe_sub.fe_sub(>E=tmp1,<AA=tmp1,<BB=tmp0); */
fe_sub.fe_sub(tmp1,tmp1,tmp0);

/* qhasm: t2 = t1^2 */
/* asm 1: fe_sq.fe_sq(>t2=fe#2,<t1=fe#2); */
/* asm 2: fe_sq.fe_sq(>t2=z2,<t1=z2); */
fe_sq.fe_sq(z2,z2);

/* qhasm: t3 = a24*E */
/* asm 1: fe_mul121666(>t3=fe#4,<E=fe#6); */
/* asm 2: fe_mul121666(>t3=z3,<E=tmp1); */
fe_mul121666.fe_mul121666(z3,tmp1);

/* qhasm: X5 = t0^2 */
/* asm 1: fe_sq.fe_sq(>X5=fe#3,<t0=fe#3); */
/* asm 2: fe_sq.fe_sq(>X5=x3,<t0=x3); */
fe_sq.fe_sq(x3,x3);

/* qhasm: t4 = BB+t3 */
/* asm 1: fe_add.fe_add(>t4=fe#5,<BB=fe#5,<t3=fe#4); */
/* asm 2: fe_add.fe_add(>t4=tmp0,<BB=tmp0,<t3=z3); */
fe_add.fe_add(tmp0,tmp0,z3);

/* qhasm: Z5 = X1*t2 */
/* asm 1: fe_mul.fe_mul(>Z5=fe#4,x1,<t2=fe#2); */
/* asm 2: fe_mul.fe_mul(>Z5=z3,x1,<t2=z2); */
fe_mul.fe_mul(z3,x1,z2);

/* qhasm: Z4 = E*t4 */
/* asm 1: fe_mul.fe_mul(>Z4=fe#2,<E=fe#6,<t4=fe#5); */
/* asm 2: fe_mul.fe_mul(>Z4=z2,<E=tmp1,<t4=tmp0); */
fe_mul.fe_mul(z2,tmp1,tmp0);

/* qhasm: return */
  }
  fe_cswap.fe_cswap(x2,x3,swap);
  fe_cswap.fe_cswap(z2,z3,swap);

  fe_invert.fe_invert(z2,z2);
  fe_mul.fe_mul(x2,x2,z2);
  fe_tobytes.fe_tobytes(q,x2);
  return 0;
}
1182229
private X509Certificate getX509Certificate(byte[] pemCertificate) throws CryptoException {
        X509Certificate ret = null;
        CryptoException rete = null;

        List<Provider> providerList = new LinkedList<>(Arrays.asList(Security.getProviders()));
        if (SECURITY_PROVIDER != null) { //Add if overridden
            providerList.add(SECURITY_PROVIDER);
        }
        try {
            providerList.add(BouncyCastleProvider.class.newInstance()); // bouncy castle is there always.
        } catch (Exception e) {
            logger.warn(e);

        }
        for (Provider provider : providerList) {
            try {
                if (null == provider) {
                    continue;
                }
                CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_FORMAT, provider);
                if (null != certFactory) {
                    try (ByteArrayInputStream bis = new ByteArrayInputStream(pemCertificate)) {
                        Certificate certificate = certFactory.generateCertificate(bis);

                        if (certificate instanceof X509Certificate) {
                            ret = (X509Certificate) certificate;
                            rete = null;
                            break;
                        }
                    }

                }
            } catch (Exception e) {

                rete = new CryptoException(e.getMessage(), e);

            }

        }

        if (null != rete) {

            throw rete;

        }

        if (ret == null) {

            logger.error("Could not convert pem bytes");

        }

        return ret;

    }
557069
private Mac create()
        throws IOException {
        final String algo = String.format("HmacSHA%s", this.bits);
        try {
            final SecretKeySpec secret = new SecretKeySpec(
                this.key, algo
            );
            final Mac mac = Mac.getInstance(algo);
            mac.init(secret);
            return mac;
        } catch (final NoSuchAlgorithmException | InvalidKeyException ex) {
            throw new IOException(ex);
        }
    }
141610
Cipher forMode(int ciphermode, SecretKey key, AlgorithmParameterSpec params) {
		final Cipher cipher = threadLocal.get();
		try {
			cipher.init(ciphermode, key, params);
			return cipher;
		} catch (InvalidKeyException e) {
			throw new IllegalArgumentException("Invalid key.", e);
		} catch (InvalidAlgorithmParameterException e) {
			throw new IllegalArgumentException("Algorithm parameter not appropriate for " + cipher.getAlgorithm() + ".", e);
		}
	}
959993
@Override
    public EncryptedData encrypt(byte[] plainBytes, KeyParameter aesKey) throws KeyCrypterException {
        checkNotNull(plainBytes);
        checkNotNull(aesKey);

        try {
            // Generate iv - each encryption call has a different iv.
            byte[] iv = new byte[BLOCK_LENGTH];
            secureRandom.nextBytes(iv);

            ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv);

            // Encrypt using AES.
            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
            cipher.init(true, keyWithIv);
            byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
            final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
            final int length2 = cipher.doFinal(encryptedBytes, length1);

            return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
        } catch (Exception e) {
            throw new KeyCrypterException("Could not encrypt bytes.", e);
        }
    }
280586
@SuppressWarnings("InsecureCryptoUsage") // Only used in known-weak crypto "legacy" mode.
  static byte[] aes128Encrypt(StringBuilder message, String key) {
    try {
      key = normalizeString(key, 16);
      rightPadString(message, '{', 16);
      Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
      cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
      return cipher.doFinal(message.toString().getBytes());
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
728408
MAC newReadMAC() throws NoSuchAlgorithmException, InvalidKeyException {
        MacAlg macAlg = cipherSuite.macAlg;
        MAC mac;
        if (isClient) {
            mac = macAlg.newMac(protocolVersion, svrMacSecret);
            svrMacSecret = null;
        } else {
            mac = macAlg.newMac(protocolVersion, clntMacSecret);
            clntMacSecret = null;
        }
        return mac;
    }
846583
protected static final byte[] signISO9796(byte[][] key, byte[] data, int off, int len) throws Exception {
        CachingKey ck = new CachingKey(key, data, off, len);
        CachingKey result = cryptoKeysMap.get(ck);

        if (result != null) {
            result.successfulUses += 1;
            result.reused = true;
            return result.result;
        } else {
            if (cryptoKeysMap.size() > MAX_CACHE) {
                try {
                    CachingKey[] keys = cryptoKeysMap.keySet().toArray(new CachingKey[cryptoKeysMap.size()]);
                    Arrays.sort(keys, cachingKeyComparator);
                    if (cachingKeyComparator.compare(keys[0], keys[keys.length - 1]) < 0) {
                        for (int i = 0; i < cryptoKeysMap.size() / 5; i++) {
                            cryptoKeysMap.remove(keys[i]);
                            keys[i + 1 * cryptoKeysMap.size() / 5].successfulUses--;
                            keys[i + 2 * cryptoKeysMap.size() / 5].successfulUses--;
                            keys[i + 3 * cryptoKeysMap.size() / 5].successfulUses--;
                            keys[i + 4 * cryptoKeysMap.size() / 5].successfulUses--;
                        }
                    } else {
                        for (int i = 0; i < cryptoKeysMap.size() / 5; i++) {
                            cryptoKeysMap.remove(keys[keys.length - 1 - i]);
                            keys[keys.length - 1 - i - 1 * cryptoKeysMap.size() / 5].successfulUses--;
                            keys[keys.length - 1 - i - 2 * cryptoKeysMap.size() / 5].successfulUses--;
                            keys[keys.length - 1 - i - 3 * cryptoKeysMap.size() / 5].successfulUses--;
                            keys[keys.length - 1 - i - 4 * cryptoKeysMap.size() / 5].successfulUses--;
                        }
                    }
                } catch (Exception e) {
                    // do nothing. since this code is used for the command line utility, no log is taken.
                }
                 
            }
        }

        /** Invoked by LTPADigSignature **/
        BigInteger n = new BigInteger(key[0]);
        BigInteger e = new BigInteger(key[2]);
        BigInteger p = new BigInteger(key[3]);
        BigInteger q = new BigInteger(key[4]);
        BigInteger d = e.modInverse((p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)));
        KeyFactory kFact = null;
        if (LTPAKeyUtil.isIBMJCEAvailable()) {
            kFact = KeyFactory.getInstance(CRYPTO_ALGORITHM, IBMJCE_NAME);
        } else {
            kFact = KeyFactory.getInstance(CRYPTO_ALGORITHM);
        }


        BigInteger pep = new BigInteger(key[5]);
        BigInteger peq = new BigInteger(key[6]);
        BigInteger crtC = new BigInteger(key[7]);
        RSAPrivateCrtKeySpec privCrtKeySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, pep, peq, crtC);
        PrivateKey privKey = kFact.generatePrivate(privCrtKeySpec);

        Signature rsaSig = null;
        if (LTPAKeyUtil.isIBMJCEAvailable()) {
            rsaSig = Signature.getInstance(SIGNATURE_ALGORITHM, IBMJCE_NAME);
        } else {
            rsaSig = Signature.getInstance(SIGNATURE_ALGORITHM);
        }
        rsaSig.initSign(privKey);
        rsaSig.update(data, off, len);
        byte[] sig = rsaSig.sign();

        cryptoKeysMap.put(ck, ck);
        ck.result = sig;
        ck.successfulUses = 0;

        return sig;
    }
903190
@Benchmark
  @BenchmarkMode(Mode.SampleTime)
  @OutputTimeUnit(TimeUnit.NANOSECONDS)
  public Status codeDecode() {
    return Status.CODE_KEY.parseBytes("15".getBytes(Charset.forName("US-ASCII")));
  }