Skip to content

Commit 5546501

Browse files
committed
Add/fix High/Low methods of Pack
1 parent 31a2228 commit 5546501

File tree

11 files changed

+164
-96
lines changed

11 files changed

+164
-96
lines changed

core/src/main/java/org/bouncycastle/crypto/digests/AsconCXof128.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected long loadBytes(final byte[] bytes, int inOff)
5858

5959
protected long loadBytes(final byte[] bytes, int inOff, int n)
6060
{
61-
return Pack.littleEndianToLong(bytes, inOff, n);
61+
return n <= 0 ? 0L : Pack.littleEndianToLong_Low(bytes, inOff, n);
6262
}
6363

6464
protected void setBytes(long w, byte[] bytes, int inOff)
@@ -68,7 +68,10 @@ protected void setBytes(long w, byte[] bytes, int inOff)
6868

6969
protected void setBytes(long w, byte[] bytes, int inOff, int n)
7070
{
71-
Pack.longToLittleEndian(w, bytes, inOff, n);
71+
if (n > 0)
72+
{
73+
Pack.longToLittleEndian_Low(w, bytes, inOff, n);
74+
}
7275
}
7376

7477
@Override

core/src/main/java/org/bouncycastle/crypto/digests/AsconDigest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected long loadBytes(final byte[] bytes, int inOff)
5353

5454
protected long loadBytes(final byte[] bytes, int inOff, int n)
5555
{
56-
return Pack.bigEndianToLong(bytes, inOff, n);
56+
return n <= 0 ? 0L : Pack.bigEndianToLong_High(bytes, inOff, n);
5757
}
5858

5959
protected void setBytes(long w, byte[] bytes, int inOff)
@@ -63,7 +63,10 @@ protected void setBytes(long w, byte[] bytes, int inOff)
6363

6464
protected void setBytes(long w, byte[] bytes, int inOff, int n)
6565
{
66-
Pack.longToBigEndian(w, bytes, inOff, n);
66+
if (n > 0)
67+
{
68+
Pack.longToBigEndian_High(w, bytes, inOff, n);
69+
}
6770
}
6871

6972
@Override

core/src/main/java/org/bouncycastle/crypto/digests/AsconHash256.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected long loadBytes(final byte[] bytes, int inOff)
3434

3535
protected long loadBytes(final byte[] bytes, int inOff, int n)
3636
{
37-
return Pack.littleEndianToLong(bytes, inOff, n);
37+
return n <= 0 ? 0L : Pack.littleEndianToLong_Low(bytes, inOff, n);
3838
}
3939

4040
protected void setBytes(long w, byte[] bytes, int inOff)
@@ -44,7 +44,10 @@ protected void setBytes(long w, byte[] bytes, int inOff)
4444

4545
protected void setBytes(long w, byte[] bytes, int inOff, int n)
4646
{
47-
Pack.longToLittleEndian(w, bytes, inOff, n);
47+
if (n > 0)
48+
{
49+
Pack.longToLittleEndian_Low(w, bytes, inOff, n);
50+
}
4851
}
4952

5053
@Override

core/src/main/java/org/bouncycastle/crypto/digests/AsconXof.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected long loadBytes(final byte[] bytes, int inOff)
5454

5555
protected long loadBytes(final byte[] bytes, int inOff, int n)
5656
{
57-
return Pack.bigEndianToLong(bytes, inOff, n);
57+
return n <= 0 ? 0L : Pack.bigEndianToLong_High(bytes, inOff, n);
5858
}
5959

6060
protected void setBytes(long w, byte[] bytes, int inOff)
@@ -64,7 +64,10 @@ protected void setBytes(long w, byte[] bytes, int inOff)
6464

6565
protected void setBytes(long w, byte[] bytes, int inOff, int n)
6666
{
67-
Pack.longToBigEndian(w, bytes, inOff, n);
67+
if (n > 0)
68+
{
69+
Pack.longToBigEndian_High(w, bytes, inOff, n);
70+
}
6871
}
6972

7073
@Override

core/src/main/java/org/bouncycastle/crypto/digests/AsconXof128.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected long loadBytes(final byte[] bytes, int inOff)
3535

3636
protected long loadBytes(final byte[] bytes, int inOff, int n)
3737
{
38-
return Pack.littleEndianToLong(bytes, inOff, n);
38+
return n <= 0 ? 0L : Pack.littleEndianToLong_Low(bytes, inOff, n);
3939
}
4040

4141
protected void setBytes(long w, byte[] bytes, int inOff)
@@ -45,7 +45,10 @@ protected void setBytes(long w, byte[] bytes, int inOff)
4545

4646
protected void setBytes(long w, byte[] bytes, int inOff, int n)
4747
{
48-
Pack.longToLittleEndian(w, bytes, inOff, n);
48+
if (n > 0)
49+
{
50+
Pack.longToLittleEndian_Low(w, bytes, inOff, n);
51+
}
4952
}
5053

5154
@Override

core/src/main/java/org/bouncycastle/crypto/digests/Blake2bDigest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public int doFinal(byte[] out, int outOffset)
482482
Pack.longToLittleEndian(chainValue, 0, full, out, outOffset);
483483
if (partial > 0)
484484
{
485-
Pack.longToLittleEndian(chainValue[full], out, outOffset + digestLength - partial, partial);
485+
Pack.longToLittleEndian_Low(chainValue[full], out, outOffset + digestLength - partial, partial);
486486
}
487487

488488
reset();

core/src/main/java/org/bouncycastle/crypto/digests/Blake2sDigest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,7 @@ public int doFinal(byte[] out, int outOffset)
502502
Pack.intToLittleEndian(chainValue, 0, full, out, outOffset);
503503
if (partial > 0)
504504
{
505-
byte[] bytes = new byte[4];
506-
Pack.intToLittleEndian(chainValue[full], bytes, 0);
507-
System.arraycopy(bytes, 0, out, outOffset + digestLength - partial, partial);
505+
Pack.intToLittleEndian_Low(chainValue[full], out, outOffset + digestLength - partial, partial);
508506
}
509507

510508
Arrays.fill(chainValue, 0);

core/src/main/java/org/bouncycastle/crypto/engines/AsconAEAD128.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,24 @@ protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int o
8585
{
8686
long c0 = Pack.littleEndianToLong(input, 0);
8787
inLen -= 8;
88-
long c1 = Pack.littleEndianToLong(input, 8, inLen);
8988
Pack.longToLittleEndian(p.x0 ^ c0, output, outOff);
90-
Pack.longToLittleEndian(p.x1 ^ c1, output, outOff + 8, inLen);
9189
p.x0 = c0;
92-
p.x1 &= -(1L << (inLen << 3));
93-
p.x1 |= c1;
90+
91+
if (inLen > 0)
92+
{
93+
long c1 = Pack.littleEndianToLong_Low(input, 8, inLen);
94+
Pack.longToLittleEndian_Low(p.x1 ^ c1, output, outOff + 8, inLen);
95+
p.x1 &= -(1L << (inLen << 3));
96+
p.x1 |= c1;
97+
}
9498
p.x1 ^= pad(inLen);
9599
}
96100
else
97101
{
98-
if (inLen != 0)
102+
if (inLen > 0)
99103
{
100-
long c0 = Pack.littleEndianToLong(input, 0, inLen);
101-
Pack.longToLittleEndian(p.x0 ^ c0, output, outOff, inLen);
104+
long c0 = Pack.littleEndianToLong_Low(input, 0, inLen);
105+
Pack.longToLittleEndian_Low(p.x0 ^ c0, output, outOff, inLen);
102106
p.x0 &= -(1L << (inLen << 3));
103107
p.x0 |= c0;
104108
}
@@ -113,17 +117,21 @@ protected void processFinalEncrypt(byte[] input, int inLen, byte[] output, int o
113117
{
114118
p.x0 ^= Pack.littleEndianToLong(input, 0);
115119
inLen -= 8;
116-
p.x1 ^= Pack.littleEndianToLong(input, 8, inLen);
117120
Pack.longToLittleEndian(p.x0, output, outOff);
118-
Pack.longToLittleEndian(p.x1, output, outOff + 8);
121+
122+
if (inLen > 0)
123+
{
124+
p.x1 ^= Pack.littleEndianToLong_Low(input, 8, inLen);
125+
Pack.longToLittleEndian_Low(p.x1, output, outOff + 8, inLen);
126+
}
119127
p.x1 ^= pad(inLen);
120128
}
121129
else
122130
{
123-
if (inLen != 0)
131+
if (inLen > 0)
124132
{
125-
p.x0 ^= Pack.littleEndianToLong(input, 0, inLen);
126-
Pack.longToLittleEndian(p.x0, output, outOff, inLen);
133+
p.x0 ^= Pack.littleEndianToLong_Low(input, 0, inLen);
134+
Pack.longToLittleEndian_Low(p.x0, output, outOff, inLen);
127135
}
128136
p.x0 ^= pad(inLen);
129137
}

core/src/main/java/org/bouncycastle/crypto/engines/AsconEngine.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,23 +126,23 @@ protected void processFinalDecrypt(byte[] input, int inLen, byte[] output, int o
126126
outOff += 8;
127127
inLen -= 8;
128128
p.x1 ^= pad(inLen);
129-
if (inLen != 0)
129+
if (inLen > 0)
130130
{
131-
long c1 = Pack.littleEndianToLong_High(input, 8, inLen);
131+
long c1 = Pack.bigEndianToLong_High(input, 8, inLen);
132132
p.x1 ^= c1;
133-
Pack.longToLittleEndian_High(p.x1, output, outOff, inLen);
133+
Pack.longToBigEndian_High(p.x1, output, outOff, inLen);
134134
p.x1 &= -1L >>> (inLen << 3);
135135
p.x1 ^= c1;
136136
}
137137
}
138138
else
139139
{
140140
p.x0 ^= pad(inLen);
141-
if (inLen != 0)
141+
if (inLen > 0)
142142
{
143-
long c0 = Pack.littleEndianToLong_High(input, 0, inLen);
143+
long c0 = Pack.bigEndianToLong_High(input, 0, inLen);
144144
p.x0 ^= c0;
145-
Pack.longToLittleEndian_High(p.x0, output, outOff, inLen);
145+
Pack.longToBigEndian_High(p.x0, output, outOff, inLen);
146146
p.x0 &= -1L >>> (inLen << 3);
147147
p.x0 ^= c0;
148148
}
@@ -160,19 +160,19 @@ protected void processFinalEncrypt(byte[] input, int inLen, byte[] output, int o
160160
outOff += 8;
161161
inLen -= 8;
162162
p.x1 ^= pad(inLen);
163-
if (inLen != 0)
163+
if (inLen > 0)
164164
{
165-
p.x1 ^= Pack.littleEndianToLong_High(input, 8, inLen);
166-
Pack.longToLittleEndian_High(p.x1, output, outOff, inLen);
165+
p.x1 ^= Pack.bigEndianToLong_High(input, 8, inLen);
166+
Pack.longToBigEndian_High(p.x1, output, outOff, inLen);
167167
}
168168
}
169169
else
170170
{
171171
p.x0 ^= pad(inLen);
172-
if (inLen != 0)
172+
if (inLen > 0)
173173
{
174-
p.x0 ^= Pack.littleEndianToLong_High(input, 0, inLen);
175-
Pack.longToLittleEndian_High(p.x0, output, outOff, inLen);
174+
p.x0 ^= Pack.bigEndianToLong_High(input, 0, inLen);
175+
Pack.longToBigEndian_High(p.x0, output, outOff, inLen);
176176
}
177177
}
178178
finishData(State.EncFinal);

core/src/main/java/org/bouncycastle/pqc/crypto/mayo/Utils.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void unpackMVecs(byte[] in, int inOff, long[] out, int outOff, int
2626
{
2727
out[outOff + j] = Pack.littleEndianToLong(in, inOff + (j << 3));
2828
}
29-
out[outOff + j] = Pack.littleEndianToLong(in, inOff + (j << 3), lastblockLen);
29+
out[outOff + j] = lastblockLen <= 0 ? 0L : Pack.littleEndianToLong_Low(in, inOff + (j << 3), lastblockLen);
3030
}
3131
}
3232

@@ -52,7 +52,10 @@ public static void packMVecs(long[] in, byte[] out, int outOff, int vecs, int m)
5252
{
5353
Pack.longToLittleEndian(in[inOff + j], out, outOff + (j << 3));
5454
}
55-
Pack.longToLittleEndian(in[inOff + j], out, outOff + (j << 3), lastBlockLen);
55+
if (lastBlockLen > 0)
56+
{
57+
Pack.longToLittleEndian_Low(in[inOff + j], out, outOff + (j << 3), lastBlockLen);
58+
}
5659
}
5760
}
5861

0 commit comments

Comments
 (0)