Require a label for hashing

* Add a string label argument to CryptoComponent#hash()
* Convert DoubleDigest from implementing MessageDigest
  to implementing org.spongycastle.crypto.Digest
  (we need to keep DoubleDigest for FortunaGenerator)
* Convert all other uses of MessageDigest to CryptoComponent#hash()
* Remove CryptoComponent#getMessageDigest(), MessageDigest and DigestWrapper
This commit is contained in:
Torsten Grote
2016-12-02 18:44:24 -02:00
parent 9c22ea8434
commit 062ed4ef4b
11 changed files with 59 additions and 162 deletions

View File

@@ -10,8 +10,6 @@ public interface CryptoComponent {
SecretKey generateSecretKey();
MessageDigest getMessageDigest();
PseudoRandom getPseudoRandom(int seed1, int seed2);
SecureRandom getSecureRandom();
@@ -164,8 +162,17 @@ public interface CryptoComponent {
/**
* Returns the hash of the given inputs. The inputs are unambiguously
* combined by prefixing each input with its length.
*
* @param label A label specific to this hash to ensure that hashes
* calculated for distinct purposes don't collide.
*/
byte[] hash(byte[]... inputs);
byte[] hash(String label, byte[]... inputs);
/**
* Returns the length of hashes produced by
* the {@link CryptoComponent#hash(String, byte[]...)} method.
*/
int getHashLength();
/**
* Returns a message authentication code with the given key over the

View File

@@ -1,47 +0,0 @@
package org.briarproject.bramble.api.crypto;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault
public interface MessageDigest {
/**
* @see {@link java.security.MessageDigest#digest()}
*/
byte[] digest();
/**
* @see {@link java.security.MessageDigest#digest(byte[])}
*/
byte[] digest(byte[] input);
/**
* @see {@link java.security.MessageDigest#digest(byte[], int, int)}
*/
int digest(byte[] buf, int offset, int len);
/**
* @see {@link java.security.MessageDigest#getDigestLength()}
*/
int getDigestLength();
/**
* @see {@link java.security.MessageDigest#reset()}
*/
void reset();
/**
* @see {@link java.security.MessageDigest#update(byte)}
*/
void update(byte input);
/**
* @see {@link java.security.MessageDigest#update(byte[])}
*/
void update(byte[] input);
/**
* @see {@link java.security.MessageDigest#update(byte[], int, int)}
*/
void update(byte[] input, int offset, int len);
}

View File

@@ -3,8 +3,6 @@ package org.briarproject.bramble.api.identity;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.nio.charset.Charset;
import javax.annotation.concurrent.ThreadSafe;
/**
@@ -18,8 +16,7 @@ public class AuthorId extends UniqueId {
/**
* Label for hashing authors to calculate their identities.
*/
public static final byte[] LABEL =
"AUTHOR_ID".getBytes(Charset.forName("US-ASCII"));
public static final String LABEL = "org.briarproject.bramble.AUTHOR_ID";
public AuthorId(byte[] id) {
super(id);

View File

@@ -3,8 +3,6 @@ package org.briarproject.bramble.api.sync;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.nio.charset.Charset;
import javax.annotation.concurrent.ThreadSafe;
/**
@@ -17,8 +15,7 @@ public class GroupId extends UniqueId {
/**
* Label for hashing groups to calculate their identifiers.
*/
public static final byte[] LABEL =
"GROUP_ID".getBytes(Charset.forName("US-ASCII"));
public static final String LABEL = "org.briarproject.bramble.GROUP_ID";
public GroupId(byte[] id) {
super(id);

View File

@@ -3,8 +3,6 @@ package org.briarproject.bramble.api.sync;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.nio.charset.Charset;
import javax.annotation.concurrent.ThreadSafe;
/**
@@ -18,8 +16,7 @@ public class MessageId extends UniqueId {
/**
* Label for hashing messages to calculate their identifiers.
*/
public static final byte[] LABEL =
"MESSAGE_ID".getBytes(Charset.forName("US-ASCII"));
public static final String LABEL = "org.briarproject.bramble.MESSAGE_ID";
public MessageId(byte[] id) {
super(id);