Exemplo n.º 1
0
  // ------------------------------------------------
  public final void diversify(byte[] MxK, byte[] factor, byte[] DxK) {
    Util.arrayCopyNonAtomic(factor, (short) 0, tbuf2, (short) 0, (short) 8);

    tripledes(MxK, tbuf2, (short) 0, (short) 8, tbuf1, (short) 0, Cipher.MODE_ENCRYPT);
    Util.arrayCopy(tbuf1, (short) 0, DxK, (short) 0, (short) 8);
    notblock8(tbuf2);
    tripledes(MxK, tbuf2, (short) 0, (short) 8, tbuf1, (short) 0, Cipher.MODE_ENCRYPT);
    Util.arrayCopyNonAtomic(tbuf1, (short) 0, DxK, (short) 8, (short) 8);
  }
Exemplo n.º 2
0
  // ------------------------------------------------
  public final void gmac4(byte alg, byte[] key, byte[] data, short dl, byte[] mac) {
    dl = pbocpadding(data, dl);

    Util.arrayFillNonAtomic(tbuf1, (short) 0, (short) 8, (byte) 0);

    for (short i = (short) 0; i < dl; i += (short) 8) {
      xorblock8(tbuf1, data, i);
      cdes(key, (short) 0, tbuf1, (short) 0, (short) 8, tbuf2, (short) 0, Cipher.MODE_ENCRYPT);
      Util.arrayCopyNonAtomic(tbuf2, (short) 0, tbuf1, (short) 0, (short) 8);
    }

    if (alg == ALG_3DES) {
      cdes(key, (short) 8, tbuf1, (short) 0, (short) 8, tbuf2, (short) 0, Cipher.MODE_DECRYPT);
      cdes(key, (short) 0, tbuf2, (short) 0, (short) 8, tbuf1, (short) 0, Cipher.MODE_ENCRYPT);
    }

    Util.arrayCopyNonAtomic(tbuf1, (short) 0, mac, (short) 0, (short) 4);
  }
Exemplo n.º 3
0
  // ------------------------------------------------
  public final short PBDecrypt(
      byte alg, byte[] key, byte[] data, short doff, short len, byte[] res) {
    if (alg == ALG_3DES) tripledes(key, data, doff, len, res, (short) 0, Cipher.MODE_DECRYPT);
    else cdes(key, (short) 0, data, doff, len, res, (short) 0, Cipher.MODE_DECRYPT);

    len = res[0];
    Util.arrayCopyNonAtomic(res, (short) 1, res, (short) 0, len);
    return len;
  }
Exemplo n.º 4
0
  // ------------------------------------------------
  public final short pbocpadding(byte[] data, short len) {
    short f;

    data[len] = (byte) 0x80;
    len++;

    f = (short) (len % 8);
    f = (short) (8 - f);

    if (f != (short) 8) Util.arrayFillNonAtomic(data, len, f, (byte) 0);
    else f = (short) 0;

    return (short) (len + f);
  }
Exemplo n.º 5
0
  // ------------------------------------------------
  public final short PBEncrypt(byte alg, byte[] key, byte[] data, short len, byte[] res) {
    Util.arrayCopyNonAtomic(data, (short) 0, res, (short) 1, len);
    res[0] = (byte) len;

    if (((short) (len + 1) % (short) 8) > (short) 0) len = pbocpadding(res, (short) (len + 1));
    else len = (short) (len + 1);

    if (alg == ALG_3DES) {
      tripledes(key, data, (short) 0, len, res, (short) 0, Cipher.MODE_ENCRYPT);
    } else {
      cdes(key, (short) 0, data, (short) 0, len, res, (short) 0, Cipher.MODE_ENCRYPT);
    }

    return len;
  }