|
1 | 1 | package org.bouncycastle.crypto.digests; |
2 | 2 |
|
3 | | -import org.bouncycastle.crypto.DataLengthException; |
4 | | -import org.bouncycastle.crypto.ExtendedDigest; |
5 | 3 | import org.bouncycastle.crypto.OutputLengthException; |
6 | 4 | import org.bouncycastle.util.Arrays; |
7 | 5 | import org.bouncycastle.util.Longs; |
8 | 6 |
|
9 | 7 | abstract class AsconBaseDigest |
10 | | - implements ExtendedDigest |
| 8 | + extends BufferBaseDigest |
11 | 9 | { |
12 | 10 | protected long x0; |
13 | 11 | protected long x1; |
14 | 12 | protected long x2; |
15 | 13 | protected long x3; |
16 | 14 | protected long x4; |
17 | | - protected final int DigestSize = 32; |
18 | | - protected final int BlockSize = 8; |
19 | 15 | protected int ASCON_PB_ROUNDS = 12; |
20 | | - protected final byte[] m_buf = new byte[BlockSize]; |
21 | | - protected int m_bufPos = 0; |
| 16 | + |
| 17 | + protected AsconBaseDigest() |
| 18 | + { |
| 19 | + DigestSize = 32; |
| 20 | + BlockSize = 8; |
| 21 | + m_buf = new byte[BlockSize]; |
| 22 | + } |
22 | 23 |
|
23 | 24 | private void round(long C) |
24 | 25 | { |
@@ -66,67 +67,17 @@ protected void p(int nr) |
66 | 67 |
|
67 | 68 | protected abstract void setBytes(long w, byte[] bytes, int inOff, int n); |
68 | 69 |
|
69 | | - @Override |
70 | | - public int getDigestSize() |
71 | | - { |
72 | | - return DigestSize; |
73 | | - } |
74 | | - |
75 | | - @Override |
76 | | - public int getByteLength() |
77 | | - { |
78 | | - return BlockSize; |
79 | | - } |
80 | | - |
81 | | - @Override |
82 | | - public void update(byte in) |
| 70 | + protected void processBytes(byte[] input, int inOff) |
83 | 71 | { |
84 | | - m_buf[m_bufPos] = in; |
85 | | - if (++m_bufPos == BlockSize) |
86 | | - { |
87 | | - x0 ^= loadBytes(m_buf, 0); |
88 | | - p(ASCON_PB_ROUNDS); |
89 | | - m_bufPos = 0; |
90 | | - } |
| 72 | + x0 ^= loadBytes(input, inOff); |
| 73 | + p(ASCON_PB_ROUNDS); |
91 | 74 | } |
92 | 75 |
|
93 | | - @Override |
94 | | - public void update(byte[] input, int inOff, int len) |
| 76 | + protected void finish(byte[] output, int outOff) |
95 | 77 | { |
96 | | - if ((inOff + len) > input.length) |
97 | | - { |
98 | | - throw new DataLengthException("input buffer too short"); |
99 | | - } |
100 | | - int available = 8 - m_bufPos; |
101 | | - if (len < available) |
102 | | - { |
103 | | - System.arraycopy(input, inOff, m_buf, m_bufPos, len); |
104 | | - m_bufPos += len; |
105 | | - return; |
106 | | - } |
107 | | - int inPos = 0; |
108 | | - if (m_bufPos > 0) |
109 | | - { |
110 | | - System.arraycopy(input, inOff, m_buf, m_bufPos, available); |
111 | | - inPos += available; |
112 | | - x0 ^= loadBytes(m_buf, 0); |
113 | | - p(ASCON_PB_ROUNDS); |
114 | | - } |
115 | | - int remaining; |
116 | | - while ((remaining = len - inPos) >= 8) |
117 | | - { |
118 | | - x0 ^= loadBytes(input, inOff + inPos); |
119 | | - p(ASCON_PB_ROUNDS); |
120 | | - inPos += 8; |
121 | | - } |
122 | | - System.arraycopy(input, inOff + inPos, m_buf, 0, remaining); |
123 | | - m_bufPos = remaining; |
124 | | - } |
125 | | - |
126 | | - @Override |
127 | | - public int doFinal(byte[] output, int outOff) |
128 | | - { |
129 | | - return hash(output, outOff, DigestSize); |
| 78 | + padAndAbsorb(); |
| 79 | + /* squeeze full output blocks */ |
| 80 | + squeeze(output, outOff, DigestSize); |
130 | 81 | } |
131 | 82 |
|
132 | 83 | protected void padAndAbsorb() |
|
0 commit comments