use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class UnikeyFactory method rsaOaepPKFromUnikey.
/**
* Given the .unikey-format byte array with the private key, create the {@link RSAOAEPPrivateKey}.
*/
@Nullable
static RSAOAEPPrivateKey rsaOaepPKFromUnikey(@NonNull byte[] bytes) {
assert bytes != null;
try {
final ArrayList unpackedFromBoss = Boss.load(bytes);
assert ((Integer) unpackedFromBoss.get(0)) == 0;
final byte[] e = ((Bytes) unpackedFromBoss.get(1)).toArray(), p = ((Bytes) unpackedFromBoss.get(2)).toArray(), q = ((Bytes) unpackedFromBoss.get(3)).toArray();
return new RSAOAEPPrivateKey(e, p, q, RSAOAEPPrivateKey.DEFAULT_OAEP_HASH, RSAOAEPPrivateKey.DEFAULT_MGF1_HASH, new SecureRandom());
} catch (Throwable e) {
return null;
}
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class BitrustedConnector method decryptBlock.
private Object decryptBlock(Command command) throws EncryptionError {
try {
synchronized (remoteSessionKey) {
Bytes ciphertext = command.getParam(0);
if (ciphertext == null) {
throw new IllegalStateException("missing block data");
}
Binder plain = Boss.unpack(remoteSessionKey.etaDecrypt(ciphertext.toArray()));
inputQueue.put(plain);
}
} catch (SymmetricKey.AuthenticationFailed authenticationFailed) {
throw new EncryptionError("authentication failed on bitrusted block");
} catch (InterruptedException e) {
Thread.interrupted();
} catch (Exception e) {
log.wtf("failed to process block", e);
e.printStackTrace();
}
return null;
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class TransactionPack method deserialize.
@Override
public void deserialize(Binder data, BiDeserializer deserializer) throws IOException {
synchronized (this) {
// It is local quantiser that should throw exception
// if limit is got while deserializing TransactionPack.
Quantiser quantiser = new Quantiser();
quantiser.reset(Contract.getTestQuantaLimit());
List<Object> keysList = deserializer.deserializeCollection(data.getList("keys", new ArrayList<>()));
keysForPack = new HashSet<>();
if (keysList != null) {
for (Object x : keysList) {
if (x instanceof Bytes)
x = ((Bytes) x).toArray();
if (x instanceof byte[]) {
keysForPack.add(new PublicKey((byte[]) x));
} else {
throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName());
}
}
}
List<Bytes> foreignReferenceBytesList = deserializer.deserializeCollection(data.getList("referencedItems", new ArrayList<>()));
if (foreignReferenceBytesList != null) {
for (Bytes b : foreignReferenceBytesList) {
Contract frc = new Contract(b.toArray(), this);
quantiser.addWorkCostFrom(frc.getQuantiser());
referencedItems.put(frc.getId(), frc);
}
}
List<Bytes> subItemsBytesList = deserializer.deserializeCollection(data.getListOrThrow("subItems"));
HashMap<ContractDependencies, Bytes> allContractsTrees = new HashMap<>();
List<HashId> allContractsHids = new ArrayList<>();
ArrayList<Bytes> sortedSubItemsBytesList = new ArrayList<>();
if (subItemsBytesList != null) {
// First of all extract contracts dependencies from subItems
for (Bytes b : subItemsBytesList) {
ContractDependencies ct = new ContractDependencies(b.toArray());
allContractsTrees.put(ct, b);
allContractsHids.add(ct.id);
}
// and add items to subItems on the each level of tree's hierarchy
do {
// first add contract from ends of trees, means without own subitems
sortedSubItemsBytesList = new ArrayList<>();
List<ContractDependencies> removingContractDependencies = new ArrayList<>();
for (ContractDependencies ct : allContractsTrees.keySet()) {
if (ct.dependencies.size() == 0) {
sortedSubItemsBytesList.add(allContractsTrees.get(ct));
removingContractDependencies.add(ct);
}
}
// remove found items from tree's list
for (ContractDependencies ct : removingContractDependencies) {
allContractsTrees.remove(ct);
}
// then add contract with already exist subitems in the subItems or will never find in the tree
removingContractDependencies = new ArrayList<>();
for (ContractDependencies ct : allContractsTrees.keySet()) {
boolean allDependenciesSafe = true;
for (HashId hid : ct.dependencies) {
if (!subItems.containsKey(hid) && allContractsHids.contains(hid)) {
allDependenciesSafe = false;
}
}
if (allDependenciesSafe) {
sortedSubItemsBytesList.add(allContractsTrees.get(ct));
removingContractDependencies.add(ct);
}
}
// remove found items from tree's list
for (ContractDependencies ct : removingContractDependencies) {
allContractsTrees.remove(ct);
}
// add found binaries on the hierarchy level to subItems
for (int i = 0; i < sortedSubItemsBytesList.size(); i++) {
Contract c = new Contract(sortedSubItemsBytesList.get(i).toArray(), this);
quantiser.addWorkCostFrom(c.getQuantiser());
subItems.put(c.getId(), c);
}
// then repeat until we can find hierarchy
} while (sortedSubItemsBytesList.size() != 0);
// finally add not found binaries on the hierarchy levels to subItems
for (Bytes b : allContractsTrees.values()) {
Contract c = new Contract(b.toArray(), this);
quantiser.addWorkCostFrom(c.getQuantiser());
subItems.put(c.getId(), c);
}
}
byte[] bb = data.getBinaryOrThrow("contract");
contract = new Contract(bb, this);
quantiser.addWorkCostFrom(contract.getQuantiser());
}
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class Contract method findSignatureInSeal.
public boolean findSignatureInSeal(PublicKey publicKey) throws Quantiser.QuantiserException {
if (sealedBinary == null)
throw new IllegalStateException("failed to create revision");
Binder data = Boss.unpack(sealedBinary);
byte[] contractBytes = data.getBinaryOrThrow("data");
List<Bytes> signatures = data.getListOrThrow("signatures");
for (Bytes s : signatures) {
verifySignatureQuantized(publicKey);
if (ExtendedSignature.verify(publicKey, s.getData(), contractBytes) != null)
return true;
}
return false;
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class ExtendedSignature method verify.
/**
* Unpack and the extended signature. On success, returns instance of the {@link ExtendedSignature} with a decoded
* timestamp, {@link #getCreatedAt()}
*
* @param key to verify signature with
* @param signature the binary extended signature
* @param data signed data
*
* @return null if the signature is invalud, {@link ExtendedSignature} instance on success.
*/
public static ExtendedSignature verify(PublicKey key, byte[] signature, byte[] data) {
try {
Binder src = Boss.unpack(signature);
ExtendedSignature es = new ExtendedSignature();
byte[] exts = src.getBinaryOrThrow("exts");
boolean isSignValid = key.verify(exts, src.getBinaryOrThrow("sign"), HashType.SHA512);
boolean isSign2Valid = true;
byte[] sign2bin = null;
try {
sign2bin = src.getBinaryOrThrow("sign2");
} catch (IllegalArgumentException e) {
sign2bin = null;
}
if (sign2bin != null)
isSign2Valid = key.verify(exts, sign2bin, HashType.SHA3_384);
if (isSignValid && isSign2Valid) {
Binder b = Boss.unpack(exts);
es.keyId = b.getBytesOrThrow("key");
es.createdAt = b.getZonedDateTimeOrThrow("created_at");
es.signature = signature;
es.publicKey = null;
try {
byte[] publicKeyBytes = b.getBinaryOrThrow("pub_key");
es.publicKey = new PublicKey(publicKeyBytes);
} catch (IllegalArgumentException e) {
es.publicKey = null;
}
Bytes hash = b.getBytesOrThrow("sha512");
Bytes dataHash = new Bytes(new Sha512().digest(data));
boolean isHashValid = hash.equals(dataHash);
Bytes hash2 = null;
boolean isHash2Valid = true;
try {
hash2 = b.getBytesOrThrow("sha3_384");
} catch (IllegalArgumentException e) {
hash2 = null;
}
if (hash2 != null) {
Bytes dataHash2 = new Bytes(new Sha3_384().digest(data));
isHash2Valid = hash2.equals(dataHash2);
}
if (isHashValid && isHash2Valid)
return es;
}
} catch (EncryptionError encryptionError) {
encryptionError.printStackTrace();
}
return null;
}
Aggregations