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 #678: New hashing functions for crypto4 #691

Merged
merged 4 commits into from
Jan 16, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* PowerAuth Crypto Library
* Copyright 2024 Wultra s.r.o.
*
* 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 io.getlime.security.powerauth.crypto.lib.v4.hash;

import org.bouncycastle.jcajce.provider.digest.SHA3;

/**
* Implementation of SHA-3 algorithms (Keccak).
*
* @author Roman Strobl, [email protected]
*/
public class Sha3 {

/**
* Hash the input data using SHA3-256.
*
* @param originalBytes Input bytes.
* @return Hashed bytes.
*/
public static byte[] hash256(byte[] originalBytes) {
return digest256(originalBytes);
}

/**
* Hash the input data using SHA3-384.
*
* @param originalBytes Input bytes.
* @return Hashed bytes.
*/
public static byte[] hash384(byte[] originalBytes) {
return digest384(originalBytes);
}

private static byte[] digest256(byte[] originalBytes) {
final SHA3.DigestSHA3 sha3_256 = new SHA3.Digest256();
return sha3_256.digest(originalBytes);
}

private static byte[] digest384(byte[] originalBytes) {
final SHA3.DigestSHA3 sha3_384 = new SHA3.Digest384();
return sha3_384.digest(originalBytes);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* PowerAuth Crypto Library
* Copyright 2024 Wultra s.r.o.
*
* 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 io.getlime.security.powerauth.crypto.lib.v4.hash;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

class Sha3Test {

private static final ObjectMapper MAPPER = new ObjectMapper();

static Stream<Map<String, String>> jsonDataSha3_256Provider() throws IOException {
InputStream sha3_256Stream = Sha3Test.class.getResourceAsStream("/io/getlime/security/powerauth/crypto/lib/v4/hash/SHA3_256_TestVectors.json");
Map<String, List<Map<String, String>>> sha3_256Data = MAPPER.readValue(sha3_256Stream, new TypeReference<>() {});
return sha3_256Data.get("sha3_256_test_vectors").stream();
}

static Stream<Map<String, String>> jsonDataSha3_384Provider() throws IOException {
InputStream sha3_384Stream = Sha3Test.class.getResourceAsStream("/io/getlime/security/powerauth/crypto/lib/v4/hash/SHA3_384_TestVectors.json");
Map<String, List<Map<String, String>>> sha3_384Data = MAPPER.readValue(sha3_384Stream, new TypeReference<>() {});
return sha3_384Data.get("sha3_384_test_vectors").stream();
}

@ParameterizedTest
@MethodSource("jsonDataSha3_256Provider")
void testSha3_256(Map<String, String> vector) {
String msgHex = vector.get("msg");
String expectedMdHex = vector.get("digest");
byte[] msgBytes = Hex.decode(msgHex);
byte[] expectedMdBytes = Hex.decode(expectedMdHex);
byte[] computedMdBytes = Sha3.hash256(msgBytes);
assertArrayEquals(expectedMdBytes, computedMdBytes, "SHA3-256 failed for Msg: " + msgHex);
}

@ParameterizedTest
@MethodSource("jsonDataSha3_384Provider")
void testSha3_384(Map<String, String> vector) {
String msgHex = vector.get("msg");
String expectedMdHex = vector.get("digest");
byte[] msgBytes = Hex.decode(msgHex);
byte[] expectedMdBytes = Hex.decode(expectedMdHex);
byte[] computedMdBytes = Sha3.hash384(msgBytes);
assertArrayEquals(expectedMdBytes, computedMdBytes, "SHA3-384 failed for Msg: " + msgHex);
}

}
Loading
Loading