@@ -24,7 +24,7 @@ public class Argon2BytesGenerator
2424
2525 /* Minimum and maximum number of lanes (degree of parallelism) */
2626 private static final int MIN_PARALLELISM = 1 ;
27- private static final int MAX_PARALLELISM = 16777216 ;
27+ private static final int MAX_PARALLELISM = ( 1 << 24 ) - 1 ;
2828
2929 /* Minimum and maximum digest size in bytes */
3030 private static final int MIN_OUTLEN = 4 ;
@@ -52,26 +52,49 @@ public Argon2BytesGenerator()
5252 */
5353 public void init (Argon2Parameters parameters )
5454 {
55- this .parameters = parameters ;
56-
57- if (parameters .getLanes () < Argon2BytesGenerator .MIN_PARALLELISM )
55+ if (parameters .getVersion () != Argon2Parameters .ARGON2_VERSION_10 &&
56+ parameters .getVersion () != Argon2Parameters .ARGON2_VERSION_13 )
5857 {
59- throw new IllegalStateException ( "lanes must be greater than " + Argon2BytesGenerator . MIN_PARALLELISM );
58+ throw new UnsupportedOperationException ( "unknown Argon2 version" );
6059 }
61- else if (parameters .getLanes () > Argon2BytesGenerator .MAX_PARALLELISM )
60+ if (parameters .getType () != Argon2Parameters .ARGON2_d &&
61+ parameters .getType () != Argon2Parameters .ARGON2_i &&
62+ parameters .getType () != Argon2Parameters .ARGON2_id )
63+ {
64+ throw new UnsupportedOperationException ("unknown Argon2 type" );
65+ }
66+
67+ if (parameters .getLanes () < MIN_PARALLELISM )
6268 {
63- throw new IllegalStateException ("lanes must be less than " + Argon2BytesGenerator . MAX_PARALLELISM );
69+ throw new IllegalStateException ("lanes must be at least " + MIN_PARALLELISM );
6470 }
65- else if (parameters .getMemory () < 2 * parameters . getLanes () )
71+ else if (parameters .getLanes () > MAX_PARALLELISM )
6672 {
67- throw new IllegalStateException ("memory is less than: " + ( 2 * parameters . getLanes ()) + " expected " + ( 2 * parameters . getLanes ()) );
73+ throw new IllegalStateException ("lanes must be at most " + MAX_PARALLELISM );
6874 }
69- else if (parameters .getIterations () < Argon2BytesGenerator . MIN_ITERATIONS )
75+ else if (parameters .getIterations () < MIN_ITERATIONS )
7076 {
71- throw new IllegalStateException ("iterations is less than: " + Argon2BytesGenerator . MIN_ITERATIONS );
77+ throw new IllegalStateException ("iterations is less than: " + MIN_ITERATIONS );
7278 }
7379
74- doInit (parameters );
80+ this .parameters = parameters ;
81+
82+ // 2. Align memory size
83+ // Minimum memoryBlocks = 8L blocks, where L is the number of lanes
84+ int memoryBlocks = Math .max (parameters .getMemory (), 2 * ARGON2_SYNC_POINTS * parameters .getLanes ());
85+
86+ this .segmentLength = memoryBlocks / (ARGON2_SYNC_POINTS * parameters .getLanes ());
87+ this .laneLength = segmentLength * ARGON2_SYNC_POINTS ;
88+
89+ // Ensure that all segments have equal length
90+ memoryBlocks = parameters .getLanes () * laneLength ;
91+
92+ this .memory = new Block [memoryBlocks ];
93+
94+ for (int i = 0 ; i < memory .length ; i ++)
95+ {
96+ memory [i ] = new Block ();
97+ }
7598 }
7699
77100 public int generateBytes (char [] password , byte [] out )
@@ -91,9 +114,9 @@ public int generateBytes(byte[] password, byte[] out)
91114
92115 public int generateBytes (byte [] password , byte [] out , int outOff , int outLen )
93116 {
94- if (outLen < Argon2BytesGenerator . MIN_OUTLEN )
117+ if (outLen < MIN_OUTLEN )
95118 {
96- throw new IllegalStateException ("output length less than " + Argon2BytesGenerator . MIN_OUTLEN );
119+ throw new IllegalStateException ("output length less than " + MIN_OUTLEN );
97120 }
98121
99122 byte [] tmpBlockBytes = new byte [ARGON2_BLOCK_SIZE ];
@@ -124,36 +147,6 @@ private void reset()
124147 }
125148 }
126149
127- private void doInit (Argon2Parameters parameters )
128- {
129- /* 2. Align memory size */
130- /* Minimum memoryBlocks = 8L blocks, where L is the number of lanes */
131- int memoryBlocks = parameters .getMemory ();
132-
133- if (memoryBlocks < 2 * Argon2BytesGenerator .ARGON2_SYNC_POINTS * parameters .getLanes ())
134- {
135- memoryBlocks = 2 * Argon2BytesGenerator .ARGON2_SYNC_POINTS * parameters .getLanes ();
136- }
137-
138- this .segmentLength = memoryBlocks / (parameters .getLanes () * Argon2BytesGenerator .ARGON2_SYNC_POINTS );
139- this .laneLength = segmentLength * Argon2BytesGenerator .ARGON2_SYNC_POINTS ;
140-
141- /* Ensure that all segments have equal length */
142- memoryBlocks = segmentLength * (parameters .getLanes () * Argon2BytesGenerator .ARGON2_SYNC_POINTS );
143-
144- initMemory (memoryBlocks );
145- }
146-
147- private void initMemory (int memoryBlocks )
148- {
149- this .memory = new Block [memoryBlocks ];
150-
151- for (int i = 0 ; i < memory .length ; i ++)
152- {
153- memory [i ] = new Block ();
154- }
155- }
156-
157150 private void fillMemoryBlocks ()
158151 {
159152 FillBlock filler = new FillBlock ();
0 commit comments