@Override public byte[] getEncoded() { return this.getKey().getEncoded(); }
static byte[] encodePublicKey(ECPublicKey key) { if (key == null) { return new byte[0]; } return key.getEncoded(); }
@Override public byte[] getEncoded() { return this.getKey().getEncoded(); }
/** Return the public key as a byte[] in network format */ public byte[] getPublicKeyNetworkFormat() { byte[] ret = getPublicKey().getEncoded(); if(ret.length == curve.modulusSize) { return ret; } else if(ret.length > curve.modulusSize) { throw new IllegalStateException("Encoded public key too long: should be "+curve.modulusSize+" bytes but is "+ret.length); } else { Logger.warning(this, "Padding public key from "+ret.length+" to "+curve.modulusSize+" bytes"); byte[] out = new byte[curve.modulusSize]; System.arraycopy(ret, 0, out, 0, ret.length); return ret; } } }
/** * Encodes a given {@link ECPublicKey} to base64. * * @param publicKey key to encode * @return encoded key * @throws IllegalArgumentException if publicKey is null * @see #getPublicKey(String) */ public static @NonNull String base64Encode(@NonNull ECPublicKey publicKey) { Sanity.nullCheck(publicKey, "Public key cannot be null"); return Base64.getEncoder().encodeToString(publicKey.getEncoded()); }
private byte[] encodeEcPublicKeyAsBitString(ECPublicKey ecPublicKey) { SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(ecPublicKey.getEncoded()); byte[] bytes = publicKeyInfo.getPublicKeyData().getBytes(); return bytes; }
public static byte[] encode(final ECPublicKey key) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); int bitLength = key.getW().getAffineX().bitLength(); String curveName = null; int qLen; if (bitLength <= 256) { curveName = "nistp256"; qLen = 65; } else if (bitLength <= 384) { curveName = "nistp384"; qLen = 97; } else if (bitLength <= 521) { curveName = "nistp521"; qLen = 133; } else { throw new CryptoException("ECDSA bit length unsupported: " + bitLength); } byte[] name = ("ecdsa-sha2-" + curveName).getBytes(StandardCharsets.US_ASCII); byte[] curve = curveName.getBytes(StandardCharsets.US_ASCII); writeArray(name, buf); writeArray(curve, buf); byte[] javaEncoding = key.getEncoded(); byte[] q = new byte[qLen]; System.arraycopy(javaEncoding, javaEncoding.length - qLen, q, 0, qLen); writeArray(q, buf); return buf.toByteArray(); }
rpk.addProperty("pkcs8", Base64.encodeBase64String(ecPublicKey.getEncoded())); } else { throw new JsonParseException("Unsupported Public Key Format (only ECPublicKey supported).");
rpk.addProperty("pkcs8", Base64.encodeBase64String(ecPublicKey.getEncoded())); } else { throw new JsonParseException("Unsupported Public Key Format (only ECPublicKey supported).");
public byte[] getHMACKey(ECPublicKey peerExponential) { synchronized(this) { lastUsedTime = System.currentTimeMillis(); } byte[] sharedKey = ecdh.getAgreedSecret(peerExponential); if (logMINOR) { Logger.minor(this, "Curve in use: " + ecdh.curve.toString()); if(logDEBUG) { Logger.debug(this, "My exponential: " + HexUtil.bytesToHex(ecdh.getPublicKey().getEncoded())); Logger.debug( this, "Peer's exponential: " + HexUtil.bytesToHex(peerExponential.getEncoded())); Logger.debug(this, "SharedSecret = " + HexUtil.bytesToHex(sharedKey)); } } return sharedKey; }
@Override public JsonObject jSerialize(PublicKey publicKey) { if (!(publicKey instanceof ECPublicKey)) throw new IllegalStateException("Unsupported Public Key Format (only ECPublicKey supported)."); ECPublicKey ecPublicKey = (ECPublicKey) publicKey; JsonObject o = new JsonObject(); // Get x coordinate byte[] x = ecPublicKey.getW().getAffineX().toByteArray(); if (x[0] == 0) x = Arrays.copyOfRange(x, 1, x.length); o.add("x", Hex.encodeHexString(x)); // Get Y coordinate byte[] y = ecPublicKey.getW().getAffineY().toByteArray(); if (y[0] == 0) y = Arrays.copyOfRange(y, 1, y.length); o.add("y", Hex.encodeHexString(y)); // Get Curves params o.add("params", ecPublicKey.getParams().toString()); // Get raw public key in format SubjectPublicKeyInfo (DER encoding) o.add("b64Der", Base64.encodeBase64String(ecPublicKey.getEncoded())); return o; }
@Override public JsonObject jSerialize(PublicKey publicKey) { if (!(publicKey instanceof ECPublicKey)) throw new IllegalStateException("Unsupported Public Key Format (only ECPublicKey supported)."); ECPublicKey ecPublicKey = (ECPublicKey) publicKey; JsonObject o = new JsonObject(); // Get x coordinate byte[] x = ecPublicKey.getW().getAffineX().toByteArray(); if (x[0] == 0) x = Arrays.copyOfRange(x, 1, x.length); o.add("x", Hex.encodeHexString(x)); // Get Y coordinate byte[] y = ecPublicKey.getW().getAffineY().toByteArray(); if (y[0] == 0) y = Arrays.copyOfRange(y, 1, y.length); o.add("y", Hex.encodeHexString(y)); // Get Curves params o.add("params", ecPublicKey.getParams().toString()); // Get raw public key in format SubjectPublicKeyInfo (DER encoding) o.add("b64Der", Base64.encodeBase64String(ecPublicKey.getEncoded())); return o; }
public SimpleFieldSet getSFS(ECPublicKey pub) { SimpleFieldSet ecdsaSFS = new SimpleFieldSet(true); SimpleFieldSet curveSFS = new SimpleFieldSet(true); curveSFS.putSingle("pub", Base64.encode(pub.getEncoded())); ecdsaSFS.put(name(), curveSFS); return ecdsaSFS; }
/** * Returns a digest of the provided (message, purpose) pair which is meant * to be signed by the private counterpart of key. * * @param vHashAlgorithm name of the Vanadium hash algorithm to use * @param message message that is part of the digest * @param purpose purpose that is part of the digest * @param key public key of the signer of the returned digest * @return digest for the specified message and digest * @throws VException if there was an error creating a digest */ static byte[] messageDigest(String vHashAlgorithm, byte[] message, byte[] purpose, ECPublicKey key) throws VException { if (message == null) { throw new VException("Empty message."); } if (purpose == null) { throw new VException("Empty purpose."); } if (key == null) { throw new VException("PublicKey of signer not provided."); } // TODO(ashankar): Remove this if once https://github.com/vanadium/issues/issues/619 is resolved. String purposeStr = purpose.toString(); byte[] keyBytes = hash(vHashAlgorithm, key.getEncoded()); message = hash(vHashAlgorithm, message); purpose = hash(vHashAlgorithm, purpose); return join(join(keyBytes, message), purpose); }
/** * Create the cryptographic keys etc from scratch */ public void initCrypto() { ecdsaP256 = new ECDSA(ECDSA.Curves.P256); ecdsaPubKeyHash = SHA256.digest(ecdsaP256.getPublicKey().getEncoded()); myARK = InsertableClientSSK.createRandom(random, "ark"); myARKNumber = 0; clientNonce = new byte[32]; node.random.nextBytes(clientNonce); myIdentity = new byte[IDENTITY_LENGTH]; node.random.nextBytes(myIdentity); identityHash = SHA256.digest(myIdentity); identityHashHash = SHA256.digest(identityHash); anonSetupCipher.initialize(identityHash); }
ecdsaP256 = new ECDSA(Curves.P256); ecdsaPubKeyHash = SHA256.digest(ecdsaP256.getPublicKey().getEncoded());
throw new FSParseException("ecdsa.P256.pub is invalid!"); this.peerECDSAPubKey = key; peerECDSAPubKeyHash = SHA256.digest(peerECDSAPubKey.getEncoded());