java.lang.Object
no.uio.ifi.crypt4gh.util.KeyUtils
A bunch of methods for generating/constructing/reading/writing/deriving keys.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Header line text for private key file in Crypt4GH formatstatic final String
Header line text for public key file in Crypt4GH formatstatic final String
Header line text for private key file in OpenSSL formatstatic final String
Header line text for public key file in OpenSSL formatstatic final String
A constant string with the value "ChaCha20"static final String
Magic number string used at the beginning of private key files in Crypt4GH formatstatic final String
Footer line text for private key file in Crypt4GH formatstatic final String
Footer line text for public key file in Crypt4GH formatstatic final String
Footer line text for private key file in OpenSSL formatstatic final String
Footer line text for public key file in OpenSSL formatstatic final String
A constant string with the value "X25519" -
Method Summary
Modifier and TypeMethodDescriptionconstructPrivateKey
(byte[] scalar) Constructs X25519 private key from scalar.constructPublicKey
(byte[] u) Constructs X25519 PUBLIC key from U.byte[]
Decodes Base64 key string, surrounded by header and footer.derivePublicKey
(PrivateKey privateKey) Derives X25519 public key from the given X25519 private key.byte[]
Extracts either scalar from the X25519 private key, or U from the X25519 public key.byte[]
generateDiffieHellmanSharedKey
(PrivateKey privateKey, PublicKey publicKey) Generates Diffie Hellman shared key from sender's X25519 private and recipient's X25519 public keys.Generates X25519 key pair.Generates X25519 private key.generateReaderSharedKey
(PrivateKey readerPrivateKey, PublicKey writerPublicKey) Generates reader's shared key based on recipient's X25519 private and sender's X25519 public keys.Generates ChaCha20 secret key.generateWriterSharedKey
(PrivateKey writerPrivateKey, PublicKey readerPublicKey) Generates writer's shared key based on sender's X25519 private and recipient's X25519 public keys.static KeyUtils
byte[]
getScalar
(PrivateKey privateKey) Extracts scalar from the X25519 private key as a byte array.byte[]
Extracts U from the X25519 public key as a byte array.readCrypt4GHPrivateKey
(byte[] keyMaterial, char[] password) Reads Crypt4GH private key.readPrivateKey
(File keyFile, char[] password) Reads private key (OpenSSL or Crypt4GH format) file.readPrivateKey
(String keyMaterial, char[] password) Reads private key (OpenSSL or Crypt4GH format) file.readPublicKey
(File keyFile) Reads public key (OpenSSL or Crypt4GH format) file.readPublicKey
(String keyMaterial) Reads public key (OpenSSL or Crypt4GH format).void
writeCrypt4GHKey
(File keyFile, Key key, char[] password) Writes the key to a file in Crypt4GH format.void
writeCrypt4GHKey
(Writer writer, Key key, char[] password) Writes the key using a supplied writer in Crypt4GH format.void
writeOpenSSLKey
(File keyFile, Key key) Writes the key to a file in OpenSSL format.void
writeOpenSSLKey
(Writer writer, Key key) Writes the key using a supplied writer in OpenSSL format.
-
Field Details
-
CHA_CHA_20
A constant string with the value "ChaCha20"- See Also:
-
X25519
A constant string with the value "X25519"- See Also:
-
BEGIN_PUBLIC_KEY
Header line text for public key file in OpenSSL format- See Also:
-
END_PUBLIC_KEY
Footer line text for public key file in OpenSSL format- See Also:
-
BEGIN_PRIVATE_KEY
Header line text for private key file in OpenSSL format- See Also:
-
END_PRIVATE_KEY
Footer line text for private key file in OpenSSL format- See Also:
-
BEGIN_CRYPT4GH_PUBLIC_KEY
Header line text for public key file in Crypt4GH format- See Also:
-
END_CRYPT4GH_PUBLIC_KEY
Footer line text for public key file in Crypt4GH format- See Also:
-
BEGIN_CRYPT4GH_ENCRYPTED_PRIVATE_KEY
Header line text for private key file in Crypt4GH format- See Also:
-
END_CRYPT4GH_ENCRYPTED_PRIVATE_KEY
Footer line text for private key file in Crypt4GH format- See Also:
-
CRYPT4GH_AUTH_MAGIC
Magic number string used at the beginning of private key files in Crypt4GH format- See Also:
-
-
Method Details
-
getInstance
-
generateKeyPair
Generates X25519 key pair.- Returns:
- X25519 key pair
- Throws:
NoSuchAlgorithmException
- If the X25519 algorithm is not found.
-
encodeKey
Extracts either scalar from the X25519 private key, or U from the X25519 public key.- Parameters:
key
- Key to extract data from.- Returns:
- Scalar or U.
- Throws:
GeneralSecurityException
- If the key was not XECPublicKey or XECPrivateKey.
-
getU
Extracts U from the X25519 public key as a byte array.- Parameters:
publicKey
- X25519 public key to extract U from.- Returns:
- U as byte array.
- Throws:
GeneralSecurityException
- In case U can't be extracted.
-
getScalar
Extracts scalar from the X25519 private key as a byte array.- Parameters:
privateKey
- X25519 private key to extract scalar from.- Returns:
- Scalar as a byte array.
- Throws:
GeneralSecurityException
- In case scalar can't be extracted.
-
generatePrivateKey
Generates X25519 private key.- Returns:
- X25519 private key.
- Throws:
GeneralSecurityException
- In case key can't be generated.
-
constructPrivateKey
Constructs X25519 private key from scalar.- Parameters:
scalar
- Scalar to build X25519 private key upon.- Returns:
- X25519 private key.
- Throws:
GeneralSecurityException
- In case the X25519 private key can't be constructed from the given scalar.
-
constructPublicKey
Constructs X25519 PUBLIC key from U.- Parameters:
u
- U to build X25519 public key upon.- Returns:
- X25519 public key.
- Throws:
GeneralSecurityException
- In case the X25519 public key can't be constructed from the given U.
-
derivePublicKey
Derives X25519 public key from the given X25519 private key.- Parameters:
privateKey
- X25519 private key to derive public key from.- Returns:
- Derived X25519 public key.
- Throws:
GeneralSecurityException
- In case X25519 public key can't be derived.
-
generateSessionKey
Generates ChaCha20 secret key.- Returns:
- ChaCha20 secret key.
- Throws:
NoSuchAlgorithmException
- If ChaCha20 algorithm can't be found.
-
readPublicKey
Reads public key (OpenSSL or Crypt4GH format) file.- Parameters:
keyFile
- Public key file.- Returns:
- Public key.
- Throws:
IOException
- If the file can't be read.GeneralSecurityException
- If the key can't be constructed from the given file.
-
readPublicKey
Reads public key (OpenSSL or Crypt4GH format).- Parameters:
keyMaterial
- Content of the key file.- Returns:
- Public key.
- Throws:
GeneralSecurityException
- If the key can't be constructed from the given content.
-
readPrivateKey
public PrivateKey readPrivateKey(File keyFile, char[] password) throws IOException, GeneralSecurityException Reads private key (OpenSSL or Crypt4GH format) file.- Parameters:
keyFile
- Private key file.password
- Optional password (if private key is password-protected). Can be null for unencrypted key.- Returns:
- Private key.
- Throws:
IOException
- If the file can't be read.GeneralSecurityException
- If the key can't be constructed from the given file.
-
readPrivateKey
public PrivateKey readPrivateKey(String keyMaterial, char[] password) throws GeneralSecurityException, IllegalArgumentException Reads private key (OpenSSL or Crypt4GH format) file.- Parameters:
keyMaterial
- Content of the key file.password
- Optional password (if private key is encrypted).- Returns:
- Private key.
- Throws:
GeneralSecurityException
- If the key can't be constructed from the given content.IllegalArgumentException
- If the key is password-protected, but the password wasnull
.
-
readCrypt4GHPrivateKey
public PrivateKey readCrypt4GHPrivateKey(byte[] keyMaterial, char[] password) throws GeneralSecurityException, IllegalArgumentException Reads Crypt4GH private key.- Parameters:
keyMaterial
- Decoded key file content.password
- Optional password (if private key is password-protected). Can be null for unencrypted key.- Returns:
- Private key.
- Throws:
GeneralSecurityException
- If the key can't be constructed from the given content.IllegalArgumentException
- If the key is password-protected, but the password wasnull
.
-
decodeKey
Decodes Base64 key string, surrounded by header and footer.- Parameters:
keyMaterial
- Base64 key string, surrounded by header and footer.- Returns:
- Decoded key as byte array.
-
writeOpenSSLKey
Writes the key using a supplied writer in OpenSSL format.- Parameters:
writer
- Writer to use.key
- Key to write.- Throws:
IOException
- If the file can't be written.
-
writeCrypt4GHKey
public void writeCrypt4GHKey(Writer writer, Key key, char[] password) throws IOException, GeneralSecurityException Writes the key using a supplied writer in Crypt4GH format.- Parameters:
writer
- Writer to use.key
- Key to write.password
- Password to lock private key.- Throws:
IOException
- If the file can't be written.GeneralSecurityException
- If the key was not XECPublicKey or XECPrivateKey.
-
writeOpenSSLKey
Writes the key to a file in OpenSSL format.- Parameters:
keyFile
- Key file to create.key
- Key to write.- Throws:
IOException
- If the file can't be written.
-
writeCrypt4GHKey
public void writeCrypt4GHKey(File keyFile, Key key, char[] password) throws IOException, GeneralSecurityException Writes the key to a file in Crypt4GH format.- Parameters:
keyFile
- Key file to create.key
- Key to write.password
- Password to lock private key.- Throws:
IOException
- If the file can't be written.GeneralSecurityException
- If the key was not XECPublicKey or XECPrivateKey.
-