crypto_hashblocks_hl static method

int crypto_hashblocks_hl(
  1. List<Int32> hh,
  2. List<Int32> hl,
  3. Uint8List m,
  4. int moff,
  5. int n,
)

Implementation

static int crypto_hashblocks_hl(
    List<Int32> hh, List<Int32> hl, Uint8List m, final int moff, int n) {
  ///String dbgt = "";
  ///for (int dbg = 0; dbg < n; dbg ++) dbgt += " "+m[dbg+moff];
  ///Log.d(TAG, "crypto_hashblocks_hl m/"+n + "-> "+dbgt);

  int i, j;

  List<Int32> wh = List<Int32>(16), wl = List<Int32>(16);
  Int32 bh0,
      bh1,
      bh2,
      bh3,
      bh4,
      bh5,
      bh6,
      bh7,
      bl0,
      bl1,
      bl2,
      bl3,
      bl4,
      bl5,
      bl6,
      bl7,
      th,
      tl,
      h,
      l,
      a,
      b,
      c,
      d;

  Int32 ah0 = hh[0],
      ah1 = hh[1],
      ah2 = hh[2],
      ah3 = hh[3],
      ah4 = hh[4],
      ah5 = hh[5],
      ah6 = hh[6],
      ah7 = hh[7],
      al0 = hl[0],
      al1 = hl[1],
      al2 = hl[2],
      al3 = hl[3],
      al4 = hl[4],
      al5 = hl[5],
      al6 = hl[6],
      al7 = hl[7];

  int pos = 0;
  while (n >= 128) {
    for (i = 0; i < 16; i++) {
      j = 8 * i + pos;
      wh[i] = Int32((m[j + 0 + moff] & 0xff) << 24) |
          ((m[j + 1 + moff] & 0xff) << 16) |
          ((m[j + 2 + moff] & 0xff) << 8) |
          ((m[j + 3 + moff] & 0xff) << 0);
      wl[i] = Int32((m[j + 4 + moff] & 0xff) << 24) |
          ((m[j + 5 + moff] & 0xff) << 16) |
          ((m[j + 6 + moff] & 0xff) << 8) |
          ((m[j + 7 + moff] & 0xff) << 0);
    }
    for (i = 0; i < 80; i++) {
      bh0 = ah0;
      bh1 = ah1;
      bh2 = ah2;
      bh3 = ah3;
      bh4 = ah4;
      bh5 = ah5;
      bh6 = ah6;
      bh7 = ah7;

      bl0 = al0;
      bl1 = al1;
      bl2 = al2;
      bl3 = al3;
      bl4 = al4;
      bl5 = al5;
      bl6 = al6;
      bl7 = al7;

      // add
      h = ah7;
      l = al7;

      a = l & 0xffff;
      b = l.shiftRightUnsigned(16);
      c = h & 0xffff;
      d = h.shiftRightUnsigned(16);

      // Sigma1
      h = ((ah4.shiftRightUnsigned(14)) | (al4 << (32 - 14))) ^
          ((ah4.shiftRightUnsigned(18)) | (al4 << (32 - 18))) ^
          ((al4.shiftRightUnsigned((41 - 32))) | (ah4 << (32 - (41 - 32))));
      l = ((al4.shiftRightUnsigned(14)) | (ah4 << (32 - 14))) ^
          ((al4.shiftRightUnsigned(18)) | (ah4 << (32 - 18))) ^
          ((ah4.shiftRightUnsigned((41 - 32))) | (al4 << (32 - (41 - 32))));

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      // Ch
      h = (ah4 & ah5) ^ (~ah4 & ah6);
      l = (al4 & al5) ^ (~al4 & al6);

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      // K
      ///h = K[i*2];
      ///l = K[i*2+1];
      h = Int32((K[i].shiftRightUnsigned(32) & 0xffffffff).toInt());
      l = Int32((K[i].shiftRightUnsigned(0) & 0xffffffff).toInt());

      ///Log.d(TAG, "i"+i + ",h:0x"+Integer.toHexString(h) + ",l:0x"+Integer.toHexString(l));

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      // w
      h = wh[i % 16];
      l = wl[i % 16];

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      b += a.shiftRightUnsigned(16);
      c += b.shiftRightUnsigned(16);
      d += c.shiftRightUnsigned(16);

      th = c & 0xffff | d << 16;
      tl = a & 0xffff | b << 16;

      // add
      h = th;
      l = tl;

      a = l & 0xffff;
      b = l.shiftRightUnsigned(16);
      c = h & 0xffff;
      d = h.shiftRightUnsigned(16);

      // Sigma0
      h = ((ah0.shiftRightUnsigned(28)) | (al0 << (32 - 28))) ^
          ((al0.shiftRightUnsigned((34 - 32))) | (ah0 << (32 - (34 - 32)))) ^
          ((al0.shiftRightUnsigned((39 - 32))) | (ah0 << (32 - (39 - 32))));
      l = ((al0.shiftRightUnsigned(28)) | (ah0 << (32 - 28))) ^
          ((ah0.shiftRightUnsigned((34 - 32))) | (al0 << (32 - (34 - 32)))) ^
          ((ah0.shiftRightUnsigned((39 - 32))) | (al0 << (32 - (39 - 32))));

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      // Maj
      h = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2);
      l = (al0 & al1) ^ (al0 & al2) ^ (al1 & al2);

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      b += a.shiftRightUnsigned(16);
      c += b.shiftRightUnsigned(16);
      d += c.shiftRightUnsigned(16);

      bh7 = (c & 0xffff) | (d << 16);
      bl7 = (a & 0xffff) | (b << 16);

      // add
      h = bh3;
      l = bl3;

      a = l & 0xffff;
      b = l.shiftRightUnsigned(16);
      c = h & 0xffff;
      d = h.shiftRightUnsigned(16);

      h = th;
      l = tl;

      a += l & 0xffff;
      b += l.shiftRightUnsigned(16);
      c += h & 0xffff;
      d += h.shiftRightUnsigned(16);

      b += a.shiftRightUnsigned(16);
      c += b.shiftRightUnsigned(16);
      d += c.shiftRightUnsigned(16);

      bh3 = (c & 0xffff) | (d << 16);
      bl3 = (a & 0xffff) | (b << 16);

      ah1 = bh0;
      ah2 = bh1;
      ah3 = bh2;
      ah4 = bh3;
      ah5 = bh4;
      ah6 = bh5;
      ah7 = bh6;
      ah0 = bh7;

      al1 = bl0;
      al2 = bl1;
      al3 = bl2;
      al4 = bl3;
      al5 = bl4;
      al6 = bl5;
      al7 = bl6;
      al0 = bl7;

      if (i % 16 == 15) {
        for (j = 0; j < 16; j++) {
          // add
          h = wh[j];
          l = wl[j];

          a = l & 0xffff;
          b = l.shiftRightUnsigned(16);
          c = h & 0xffff;
          d = h.shiftRightUnsigned(16);

          h = wh[(j + 9) % 16];
          l = wl[(j + 9) % 16];

          a += l & 0xffff;
          b += l.shiftRightUnsigned(16);
          c += h & 0xffff;
          d += h.shiftRightUnsigned(16);

          // sigma0
          th = wh[(j + 1) % 16];
          tl = wl[(j + 1) % 16];
          h = ((th.shiftRightUnsigned(1)) | (tl << (32 - 1))) ^
              ((th.shiftRightUnsigned(8)) | (tl << (32 - 8))) ^
              (th.shiftRightUnsigned(7));
          l = ((tl.shiftRightUnsigned(1)) | (th << (32 - 1))) ^
              ((tl.shiftRightUnsigned(8)) | (th << (32 - 8))) ^
              ((tl.shiftRightUnsigned(7)) | (th << (32 - 7)));

          a += l & 0xffff;
          b += l.shiftRightUnsigned(16);
          c += h & 0xffff;
          d += h.shiftRightUnsigned(16);

          // sigma1
          th = wh[(j + 14) % 16];
          tl = wl[(j + 14) % 16];
          h = ((th.shiftRightUnsigned(19)) | (tl << (32 - 19))) ^
              ((tl.shiftRightUnsigned((61 - 32))) |
                  (th << (32 - (61 - 32)))) ^
              (th.shiftRightUnsigned(6));
          l = ((tl.shiftRightUnsigned(19)) | (th << (32 - 19))) ^
              ((th.shiftRightUnsigned((61 - 32))) |
                  (tl << (32 - (61 - 32)))) ^
              ((tl.shiftRightUnsigned(6)) | (th << (32 - 6)));

          a += l & 0xffff;
          b += l.shiftRightUnsigned(16);
          c += h & 0xffff;
          d += h.shiftRightUnsigned(16);

          b += a.shiftRightUnsigned(16);
          c += b.shiftRightUnsigned(16);
          d += c.shiftRightUnsigned(16);

          wh[j] = ((c & 0xffff) | (d << 16));
          wl[j] = ((a & 0xffff) | (b << 16));
        }
      }
    }

    // add
    h = ah0;
    l = al0;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[0];
    l = hl[0];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[0] = ah0 = (c & 0xffff) | (d << 16);
    hl[0] = al0 = (a & 0xffff) | (b << 16);

    h = ah1;
    l = al1;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[1];
    l = hl[1];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[1] = ah1 = (c & 0xffff) | (d << 16);
    hl[1] = al1 = (a & 0xffff) | (b << 16);

    h = ah2;
    l = al2;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[2];
    l = hl[2];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[2] = ah2 = (c & 0xffff) | (d << 16);
    hl[2] = al2 = (a & 0xffff) | (b << 16);

    h = ah3;
    l = al3;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[3];
    l = hl[3];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[3] = ah3 = (c & 0xffff) | (d << 16);
    hl[3] = al3 = (a & 0xffff) | (b << 16);

    h = ah4;
    l = al4;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[4];
    l = hl[4];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[4] = ah4 = (c & 0xffff) | (d << 16);
    hl[4] = al4 = (a & 0xffff) | (b << 16);

    h = ah5;
    l = al5;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[5];
    l = hl[5];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[5] = ah5 = (c & 0xffff) | (d << 16);
    hl[5] = al5 = (a & 0xffff) | (b << 16);

    h = ah6;
    l = al6;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[6];
    l = hl[6];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[6] = ah6 = (c & 0xffff) | (d << 16);
    hl[6] = al6 = (a & 0xffff) | (b << 16);

    h = ah7;
    l = al7;

    a = l & 0xffff;
    b = l.shiftRightUnsigned(16);
    c = h & 0xffff;
    d = h.shiftRightUnsigned(16);

    h = hh[7];
    l = hl[7];

    a += l & 0xffff;
    b += l.shiftRightUnsigned(16);
    c += h & 0xffff;
    d += h.shiftRightUnsigned(16);

    b += a.shiftRightUnsigned(16);
    c += b.shiftRightUnsigned(16);
    d += c.shiftRightUnsigned(16);

    hh[7] = ah7 = (c & 0xffff) | (d << 16);
    hl[7] = al7 = (a & 0xffff) | (b << 16);

    pos += 128;
    n -= 128;
  }

  return n;
}