Example #1
0
 private void link() {
   synchronized (linkStatus) {
     try {
       if (linkStatus.attemptLoad()) {
         linkStatus.checkVersion(getLibMajorVersion(), getLibMinorVersion());
         linkStatus.check(native_clinit());
       }
       if (linkStatus.useNative()) {
         linkStatus.check(native_init());
         native_lock = new Object();
       }
     } catch (UnsatisfiedLinkError e) {
       linkStatus.fail(e);
       if (DEBUG && debuglevel > 2) debug(e.getMessage());
     }
     if (DEBUG && debuglevel > 2) debug("Using native library? " + (native_lock != null));
   }
 }
Example #2
0
  /**
   * Expands a user-key to a working key schedule.
   *
   * @param key the user-key object to use.
   * @exception InvalidKeyException if one of the following occurs:
   *     <ul>
   *       <li>key.getEncoded() == null;
   *       <li>The length of the user key array is not ...
   *     </ul>
   */
  private void makeKey(Key key) throws InvalidKeyException {

    byte[] userkey = key.getEncoded();
    if (userkey == null) throw new InvalidKeyException(getAlgorithm() + ": Null user key");

    int len = userkey.length;
    if (len < MIN_USER_KEY_LENGTH || len > MAX_USER_KEY_LENGTH)
      throw new InvalidKeyException(getAlgorithm() + ": Invalid user key length");

    // If native library available then use it. If not or if
    // native method returned error then revert to 100% Java.

    if (native_lock != null) {
      synchronized (native_lock) {
        try {
          linkStatus.check(native_ks(native_cookie, userkey));
          return;
        } catch (Error error) {
          native_finalize();
          native_lock = null;
          if (DEBUG && debuglevel > 0) debug(error + ". Will use 100% Java.");
        }
      }
    }

    set_constants(userkey.length);

    kb = new int[kb_bits]; // scheduling temp buffer
    round_key = new int[rounds]; // real internal key

    //
    // Copy the key into the double-byte temporary buffer
    //
    for (int i = 0; i < key_len_dbyte; i++) kb[i] = userkey[2 * i] | userkey[2 * i + 1] << 8;

    //
    // Fill out the buffer from earlier parts of the key
    //
    for (int i = key_len_dbyte; i < kb_bits; i++) {
      int t = (s2 & s1) ^ (s1 & s0) ^ (s0 & s2);
      t = (t << 5) | (t >>> 11);
      t += s2 + kb[i % key_len_dbyte];
      t &= 0xFFFF;
      s2 = s1;
      s1 = s0;
      s0 = kb[i] = t;
    }

    if (DEBUG && debuglevel >= 5)
      debug(
          "kb_bits="
              + kb_bits
              + ", kb.length="
              + kb.length
              + ", round_key.length="
              + round_key.length);

    //
    // Transfer the double-byte temporary key into the real key
    //
    switch (data_bits) {
      case 256:
        for (int i = 0; i < kb_bits / 2; i++) round_key[i] = kb[2 * i] | (kb[2 * i + 1] << 16);
        break;

      case 128:
        for (int i = 0; i < kb_bits; i++) round_key[i] = kb[i];
        break;

      case 64:
        for (int i = 0; i < kb_bits; i++) {
          round_key[2 * i] = kb[i] & 0xFF;
          round_key[2 * i + 1] = (kb[i] >>> 8) & 0xFF;
        }
        break;

      default:
        throw new CryptixException("SPEED: " + data_bits + " illegal in key_schedule?");
    }
  }