Class Pack


  • public abstract class Pack
    extends java.lang.Object
    Utility methods for converting byte arrays into ints and longs, and back again.
    • Constructor Detail

      • Pack

        public Pack()
    • Method Detail

      • bigEndianToShort

        public static short bigEndianToShort​(byte[] bs,
                                             int off)
      • bigEndianToInt

        public static int bigEndianToInt​(byte[] bs,
                                         int off)
      • bigEndianToInt

        public static void bigEndianToInt​(byte[] bs,
                                          int off,
                                          int[] ns)
      • bigEndianToInt

        public static void bigEndianToInt​(byte[] bs,
                                          int off,
                                          int[] ns,
                                          int nsOff,
                                          int nsLen)
      • bigEndianToInt_High

        public static int bigEndianToInt_High​(byte[] bs,
                                              int off,
                                              int len)
      • bigEndianToInt_Low

        public static int bigEndianToInt_Low​(byte[] bs,
                                             int off,
                                             int len)
      • intToBigEndian

        public static byte[] intToBigEndian​(int n)
      • intToBigEndian

        public static void intToBigEndian​(int n,
                                          byte[] bs)
      • intToBigEndian

        public static void intToBigEndian​(int n,
                                          byte[] bs,
                                          int off)
      • intToBigEndian

        public static byte[] intToBigEndian​(int[] ns)
      • intToBigEndian

        public static void intToBigEndian​(int[] ns,
                                          byte[] bs,
                                          int off)
      • intToBigEndian

        public static void intToBigEndian​(int[] ns,
                                          int nsOff,
                                          int nsLen,
                                          byte[] bs,
                                          int bsOff)
      • intToBigEndian_High

        public static void intToBigEndian_High​(int n,
                                               byte[] bs,
                                               int off,
                                               int len)
      • intToBigEndian_Low

        public static void intToBigEndian_Low​(int n,
                                              byte[] bs,
                                              int off,
                                              int len)
      • bigEndianToLong

        public static long bigEndianToLong​(byte[] bs,
                                           int off)
      • bigEndianToLong

        public static void bigEndianToLong​(byte[] bs,
                                           int off,
                                           long[] ns)
      • bigEndianToLong

        public static void bigEndianToLong​(byte[] bs,
                                           int bsOff,
                                           long[] ns,
                                           int nsOff,
                                           int nsLen)
      • bigEndianToLong_High

        public static long bigEndianToLong_High​(byte[] bs,
                                                int off,
                                                int len)
      • bigEndianToLong_Low

        public static long bigEndianToLong_Low​(byte[] bs,
                                               int off,
                                               int len)
        Reads the len bytes at bs[off] as a big-endian value and returns it in the low len bytes of the result: a len of 3 returns what bigEndianToLong(byte[], int) would return for those three bytes preceded by five zero ones.

        It is the read side of longToBigEndian_Low(long, byte[], int, int) and carries the same unenforced 1..8 bound, for a related reason: the first byte is read ahead of the loop, so a len of 0 reads one byte anyway and returns it rather than returning zero, and a len above 8 goes on shifting and so yields the last eight bytes read rather than the first. The callers here are the XMSS ones reading back RFC 8391's toByte(x, y): the index field of a stored private key, in XMSSPrivateKeyCodec, which is the one codec both families' keys are decoded through, and the index field of an XMSS^MT signature in XMSSMTSignature. Each passes the constant 4 or ceil(h/8) for a height its parameter class holds to 2..62, so both are inside the bound already, as are the reads the package's own tests make with the same two lengths.

      • longToBigEndian

        public static byte[] longToBigEndian​(long n)
      • longToBigEndian

        public static void longToBigEndian​(long n,
                                           byte[] bs,
                                           int off)
      • longToBigEndian

        public static byte[] longToBigEndian​(long[] ns)
      • longToBigEndian

        public static void longToBigEndian​(long[] ns,
                                           byte[] bs,
                                           int off)
      • longToBigEndian

        public static void longToBigEndian​(long[] ns,
                                           int nsOff,
                                           int nsLen,
                                           byte[] bs,
                                           int bsOff)
      • longToBigEndian_High

        public static void longToBigEndian_High​(long n,
                                                byte[] bs,
                                                int off,
                                                int len)
        Writes the most significant len bytes of n to bs at off, in big-endian order: a len of 3 writes the three bytes longToBigEndian(long, byte[], int) would put at bs[off] to bs[off + 2], and drops the other five.

        It is the write side of bigEndianToLong_High(byte[], int, int). The pair is shaped for the short final block of a big-endian sponge, where the bytes the block has occupy the top of the rate word and the remainder of that word is padding - so both conversions work against the high end of the word rather than the low one. The callers are the Ascon v1.2 classes (AsconEngine, AsconDigest, AsconXof), all three now deprecated: final Ascon is little-endian and goes through longToLittleEndian_Low(long, byte[], int, int).

        len must be 1..8, and nothing enforces it. The first store sits ahead of the loop, so a len of 0 writes one byte anyway - n's top one - and throws ArrayIndexOutOfBoundsException where the array has no room for it; and because Java takes a shift distance mod 64, a len above 8 wraps round and repeats n's bytes rather than running to zero. Callers whose length can reach 0 guard the call themselves.

      • longToBigEndian_Low

        public static void longToBigEndian_Low​(long n,
                                               byte[] bs,
                                               int off,
                                               int len)
        Writes the least significant len bytes of n to bs at off, in big-endian order: a len of 3 writes the three bytes longToBigEndian(long, byte[], int) would put at bs[off + 5] to bs[off + 7], and drops the other five.

        It is longToBigEndian_High(long, byte[], int, int) applied to n shifted up past the bytes being dropped, the write side of bigEndianToLong_Low(byte[], int, int), and it carries the same unenforced 1..8 bound for the same reason. It is the low-end counterpart of the _High pair the Ascon v1.2 classes use; its callers here are the XMSS ones building RFC 8391's toByte(x, y), which pads left of the eight bytes rather than absorbing into the top of a word, so it wants the low end. Two of the five pass a length that is not a constant, and both are inside the bound because they say so: XMSSUtil.toBytesBigEndian, whose size is its caller's argument, and XMSSEngine's H_msg key, whose length is the security parameter, each taking a min() with 8. The other three are inside it by construction - the index field of a stored private key in XMSSPrivateKeyCodec and of an XMSS^MT signature in XMSSMTSignature, both 4 or ceil(h/8), and WOTSPlus's PRF index at the constant 8.

      • littleEndianToShort

        public static short littleEndianToShort​(byte[] bs,
                                                int off)
      • littleEndianToShort

        public static void littleEndianToShort​(byte[] bs,
                                               int bOff,
                                               short[] ns)
      • littleEndianToShort

        public static void littleEndianToShort​(byte[] bs,
                                               int bOff,
                                               short[] ns,
                                               int nOff,
                                               int count)
      • littleEndianToShort

        public static short[] littleEndianToShort​(byte[] bs,
                                                  int off,
                                                  int count)
      • littleEndianToInt24

        public static int littleEndianToInt24​(byte[] bs,
                                              int off)
      • littleEndianToInt

        public static int littleEndianToInt​(byte[] bs,
                                            int off)
      • littleEndianToInt_High

        public static int littleEndianToInt_High​(byte[] bs,
                                                 int off,
                                                 int len)
      • littleEndianToInt_Low

        public static int littleEndianToInt_Low​(byte[] bs,
                                                int off,
                                                int len)
      • littleEndianToInt

        public static void littleEndianToInt​(byte[] bs,
                                             int off,
                                             int[] ns)
      • littleEndianToInt

        public static void littleEndianToInt​(byte[] bs,
                                             int bOff,
                                             int[] ns,
                                             int nOff,
                                             int count)
      • littleEndianToInt

        public static int[] littleEndianToInt​(byte[] bs,
                                              int off,
                                              int count)
      • shortToLittleEndian

        public static byte[] shortToLittleEndian​(short n)
      • shortToLittleEndian

        public static void shortToLittleEndian​(short n,
                                               byte[] bs,
                                               int off)
      • shortToLittleEndian

        public static void shortToLittleEndian​(short[] ns,
                                               byte[] bs,
                                               int bsOff)
      • shortToLittleEndian

        public static void shortToLittleEndian​(short[] ns,
                                               int nsOff,
                                               int nsLen,
                                               byte[] bs,
                                               int bsOff)
      • shortToLittleEndian

        public static byte[] shortToLittleEndian​(short[] ns)
      • shortToBigEndian

        public static byte[] shortToBigEndian​(short n)
      • shortToBigEndian

        public static void shortToBigEndian​(short n,
                                            byte[] bs,
                                            int off)
      • intToLittleEndian

        public static byte[] intToLittleEndian​(int n)
      • intToLittleEndian

        public static void intToLittleEndian​(int n,
                                             byte[] bs,
                                             int off)
      • intToLittleEndian

        public static byte[] intToLittleEndian​(int[] ns)
      • intToLittleEndian

        public static void intToLittleEndian​(int[] ns,
                                             byte[] bs,
                                             int off)
      • intToLittleEndian

        public static void intToLittleEndian​(int[] ns,
                                             int nsOff,
                                             int nsLen,
                                             byte[] bs,
                                             int bsOff)
      • intToLittleEndian_High

        public static void intToLittleEndian_High​(int n,
                                                  byte[] bs,
                                                  int off,
                                                  int len)
      • intToLittleEndian_Low

        public static void intToLittleEndian_Low​(int n,
                                                 byte[] bs,
                                                 int off,
                                                 int len)
      • littleEndianToLong

        public static long littleEndianToLong​(byte[] bs,
                                              int off)
      • littleEndianToLong

        public static void littleEndianToLong​(byte[] bs,
                                              int off,
                                              long[] ns)
      • littleEndianToLong

        public static void littleEndianToLong​(byte[] bs,
                                              int bsOff,
                                              long[] ns,
                                              int nsOff,
                                              int nsLen)
      • littleEndianToLong

        public static long[] littleEndianToLong​(byte[] bs,
                                                int off,
                                                int count)
      • littleEndianToLong_High

        public static long littleEndianToLong_High​(byte[] bs,
                                                   int off,
                                                   int len)
      • littleEndianToLong_Low

        public static long littleEndianToLong_Low​(byte[] bs,
                                                  int off,
                                                  int len)
      • longToLittleEndian

        public static byte[] longToLittleEndian​(long n)
      • longToLittleEndian

        public static void longToLittleEndian​(long n,
                                              byte[] bs,
                                              int off)
      • longToLittleEndian

        public static byte[] longToLittleEndian​(long[] ns)
      • longToLittleEndian

        public static void longToLittleEndian​(long[] ns,
                                              byte[] bs,
                                              int off)
      • longToLittleEndian

        public static void longToLittleEndian​(long[] ns,
                                              int nsOff,
                                              int nsLen,
                                              byte[] bs,
                                              int bsOff)
      • longToLittleEndian_High

        public static void longToLittleEndian_High​(long n,
                                                   byte[] bs,
                                                   int off,
                                                   int len)
      • longToLittleEndian_Low

        public static void longToLittleEndian_Low​(long n,
                                                  byte[] bs,
                                                  int off,
                                                  int len)