Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bsize bug #165

Merged
merged 2 commits into from
Jan 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/main/java/net/schmizz/sshj/transport/mac/BaseMAC.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public BaseMAC(String algorithm, int bsize, int defbsize) {

@Override
public byte[] doFinal() {
return mac.doFinal();
return resizeToHashSize(mac.doFinal());
}

@Override
public byte[] doFinal(byte[] input) {
return mac.doFinal(input);
return resizeToHashSize(mac.doFinal(input));
}

@Override
Expand All @@ -62,6 +62,15 @@ public void doFinal(byte[] buf, int offset) {
}
}

private byte[] resizeToHashSize(byte[] buf) {
if (bsize == defbsize)
return buf;

byte[] result = new byte[bsize];
System.arraycopy(buf, 0, result, 0, bsize);
return result;
}

@Override
public int getBlockSize() {
return bsize;
Expand Down
90 changes: 90 additions & 0 deletions src/test/java/net/schmizz/sshj/transport/mac/BaseMacTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Copyright 2009 sshj contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.transport.mac;

import net.schmizz.sshj.common.SSHRuntimeException;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

import java.nio.charset.Charset;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class BaseMacTest {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce74ef73778bfb0cb9";
private static final String KEY = "et1Quo5ooCie6theel8i";

@Test
public void testResizeTooBigKeys() {
BaseMAC hmac = new HMACSHA1();
hmac.init((KEY + "foo").getBytes(CHARSET));
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test(expected = SSHRuntimeException.class)
public void testUnknownAlgorithm() {
// hopefully a algorithm with that name won't be created :-)
BaseMAC hmac = new BaseMAC("AlgorithmThatDoesNotExist", 20, 20);
hmac.init((KEY + "foo").getBytes(CHARSET));
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithDoFinal() {
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithRange() {
BaseMAC hmac = initHmac();

// a leading and trailing byte to the plaintext
byte[] plainText = new byte[PLAIN_TEXT.length + 2];
System.arraycopy(PLAIN_TEXT, 0, plainText, 1, PLAIN_TEXT.length);

// update with the range from the second to penultimate byte
hmac.update(plainText, 1, PLAIN_TEXT.length);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithDoFinalWithResultBuffer() {
BaseMAC hmac = initHmac();
byte[] resultBuf = new byte[20];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}

private BaseMAC initHmac() {
BaseMAC hmac = new HMACSHA1();
hmac.init(KEY.getBytes(CHARSET));
return hmac;
}
}
60 changes: 60 additions & 0 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACMD596Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2009 sshj contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.transport.mac;

import org.bouncycastle.util.encoders.Hex;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.charset.Charset;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class HMACMD596Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "dff33c507463f9cf088a5ce8";

@Test
public void testUpdateWithDoFinal() {
HMACMD596 hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
HMACMD596 hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)),
is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACMD596 hmac = initHmac();
byte[] resultBuf = new byte[12];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}

private HMACMD596 initHmac() {
HMACMD596 hmac = new HMACMD596();
hmac.init("ohBahfei6pee5dai".getBytes(CHARSET));
return hmac;
}
}
59 changes: 59 additions & 0 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACMD5Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2009 sshj contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.transport.mac;

import org.bouncycastle.util.encoders.Hex;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.charset.Charset;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class HMACMD5Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "dff33c507463f9cf088a5ce8d969c386";

@Test
public void testUpdateWithDoFinal() {
HMACMD5 hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
HMACMD5 hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACMD5 hmac = initHmac();
byte[] resultBuf = new byte[16];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}

private HMACMD5 initHmac() {
HMACMD5 hmac = new HMACMD5();
hmac.init("ohBahfei6pee5dai".getBytes(CHARSET));
return hmac;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright 2009 sshj contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.transport.mac;

import org.bouncycastle.util.encoders.Hex;
import org.junit.Ignore;
import org.junit.Test;

import java.nio.charset.Charset;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class HMACSHA196Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce";

@Test
public void testUpdateWithDoFinal() {
HMACSHA196 hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
HMACSHA196 hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACSHA196 hmac = initHmac();
byte[] resultBuf = new byte[12];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}

private HMACSHA196 initHmac() {
HMACSHA196 hmac = new HMACSHA196();
hmac.init("et1Quo5ooCie6theel8i".getBytes(CHARSET));
return hmac;
}
}
58 changes: 58 additions & 0 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACSHA1Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright 2009 sshj contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.schmizz.sshj.transport.mac;

import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;

import java.nio.charset.Charset;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class HMACSHA1Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce74ef73778bfb0cb9";

@Test
public void testUpdateWithDoFinal() {
HMACSHA1 hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
HMACSHA1 hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
public void testUpdateWithDoFinalWithResultBuffer() {
HMACSHA1 hmac = initHmac();
byte[] resultBuf = new byte[20];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
}

private HMACSHA1 initHmac() {
HMACSHA1 hmac = new HMACSHA1();
hmac.init("et1Quo5ooCie6theel8i".getBytes(CHARSET));
return hmac;
}
}
Loading