Class COSEKeyBuilder

java.lang.Object
com.authlete.cose.COSEKeyBuilder

public class COSEKeyBuilder extends Object
A utility to build an instance of COSEKey or its subclass (e.g. COSEOKPKey and COSEEC2Key).
Since:
1.1
See Also:
  • Constructor Details

    • COSEKeyBuilder

      public COSEKeyBuilder()
  • Method Details

    • put

      public COSEKeyBuilder put(Object key, Object value)
      Set a parameter.
      Parameters:
      key - The key of the parameter.
      value - The value of the parameter.
      Returns:
      this object.
    • kty

      public COSEKeyBuilder kty(String kty)
      Set the "kty (1)" parameter.
      Parameters:
      kty - The key type.
      Returns:
      this object.
      See Also:
    • kty

      public COSEKeyBuilder kty(int kty)
      Set the "kty (1)" parameter.
      Parameters:
      kty - The key type.
      Returns:
      this object.
      See Also:
    • ktyOKP

      public COSEKeyBuilder ktyOKP()
      Set "OKP (1)" to the "kty (1)" parameter.
      Returns:
      this object.
    • ktyEC2

      public COSEKeyBuilder ktyEC2()
      Set "EC2 (2)" to the "kty (1)" parameter.
      Returns:
      this object.
    • ktyRSA

      public COSEKeyBuilder ktyRSA()
      Set "RSA (3)" to the "kty (1)" parameter.
      Returns:
      this object.
    • ktySymmetric

      public COSEKeyBuilder ktySymmetric()
      Set "Symmetric (4)" to the "kty (1)" parameter.
      Returns:
      this object.
    • ktyHSSLMS

      public COSEKeyBuilder ktyHSSLMS()
      Set "HSS-LMS (5)" to the "kty (1)" parameter.
      Returns:
      this object.
    • ktyWalnutDSA

      public COSEKeyBuilder ktyWalnutDSA()
      Set "WalnutDSA (6)" to the "kty (1)" parameter.
      Returns:
      this object.
    • kid

      public COSEKeyBuilder kid(byte[] kid)
      Set the "kid (2)" parameter.
      Parameters:
      kid - The key ID.
      Returns:
      this object.
    • kid

      public COSEKeyBuilder kid(String kid)
      Set the "kid (2)" parameter.
      Parameters:
      kid - The key ID. Its UTF-8 byte sequence is used.
      Returns:
      this object.
    • alg

      public COSEKeyBuilder alg(String alg)
      Set the "alg (3)" parameter.
      Parameters:
      alg - The algorithm.
      Returns:
      this object.
      See Also:
    • alg

      public COSEKeyBuilder alg(int alg)
      Set the "alg (3)" parameter.
      Parameters:
      alg - The algorithm.
      Returns:
      this object.
      See Also:
    • keyOps

      public COSEKeyBuilder keyOps(List<Object> operations)
      Set the "key_ops (4)" parameter.
      Parameters:
      operations - Key operations.
      Returns:
      this object.
    • baseIv

      public COSEKeyBuilder baseIv(byte[] baseIv)
      Set the "Base IV (5)" parameter.
      Parameters:
      baseIv - The base IV.
      Returns:
      this object.
    • baseIvInBase64Url

      public COSEKeyBuilder baseIvInBase64Url(String baseIv)
      Set the "Base IV (5)" parameter.
      Parameters:
      baseIv - The base IV, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • okpCrv

      public COSEKeyBuilder okpCrv(int crv)
      Set the "crv (-1)" parameter for the key type "OKP".
      Parameters:
      crv - The curve.
      Returns:
      this object.
    • okpCrv

      public COSEKeyBuilder okpCrv(String crv)
      Set the "crv (-1)" parameter for the key type "OKP".
      Parameters:
      crv - The curve.
      Returns:
      this object.
    • okpCrvX25519

      public COSEKeyBuilder okpCrvX25519()
      Set "X25519 (4)" to the "crv (-1)" parameter for the key type "OKP".
      Returns:
      this object.
    • okpCrvX448

      public COSEKeyBuilder okpCrvX448()
      Set "X448 (5)" to the "crv (-1)" parameter for the key type "OKP".
      Returns:
      this object.
    • okpCrvEd25519

      public COSEKeyBuilder okpCrvEd25519()
      Set "Ed25519 (6)" to the "crv (-1)" parameter for the key type "OKP".
      Returns:
      this object.
    • okpCrvEd448

      public COSEKeyBuilder okpCrvEd448()
      Set "Ed448 (7)" to the "crv (-1)" parameter for the key type "OKP".
      Returns:
      this object.
    • okpX

      public COSEKeyBuilder okpX(byte[] x)
      Set the "x (-2)" parameter for the key type "OKP".
      Parameters:
      x - The public key.
      Returns:
      this object.
    • okpXInBase64Url

      public COSEKeyBuilder okpXInBase64Url(String x)
      Set the "x (-2)" parameter for the key type "OKP".
      Parameters:
      x - The public key, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • okpD

      public COSEKeyBuilder okpD(byte[] d)
      Set the "d (-4)" parameter for the key type "OKP".
      Parameters:
      d - The private key.
      Returns:
      this object.
    • okpDInBase64Url

      public COSEKeyBuilder okpDInBase64Url(String d)
      Set the "d (-4)" parameter for the key type "OKP".
      Parameters:
      d - The private key, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • ec2Crv

      public COSEKeyBuilder ec2Crv(int crv)
      Set the "crv (-1)" parameter for the key type "EC2".
      Parameters:
      crv - The curve.
      Returns:
      this object.
    • ec2Crv

      public COSEKeyBuilder ec2Crv(String crv)
      Set the "crv (-1)" parameter for the key type "EC2".
      Parameters:
      crv - The curve.
      Returns:
      this object.
    • ec2CrvP256

      public COSEKeyBuilder ec2CrvP256()
      Set "P-256 (1)" to the "crv (-1)" parameter for the key type "EC2".
      Returns:
      this object.
    • ec2CrvP384

      public COSEKeyBuilder ec2CrvP384()
      Set "P-384 (2)" to the "crv (-1)" parameter for the key type "EC2".
      Returns:
      this object.
    • ec2CrvP521

      public COSEKeyBuilder ec2CrvP521()
      Set "P-521 (3)" to the "crv (-1)" parameter for the key type "EC2".
      Returns:
      this object.
    • ec2CrvSecp256k1

      public COSEKeyBuilder ec2CrvSecp256k1()
      Set "secp256k1 (8)" to the "crv (-1)" parameter for the key type "EC2".
      Returns:
      this object.
    • ec2X

      public COSEKeyBuilder ec2X(byte[] x)
      Set the "x (-2)" parameter for the key type "EC2".
      Parameters:
      x - The x-coordinate.
      Returns:
      this object.
    • ec2XInBase64Url

      public COSEKeyBuilder ec2XInBase64Url(String x)
      Set the "x (-2)" parameter for the key type "EC2".
      Parameters:
      x - The x-coordinate, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • ec2Y

      public COSEKeyBuilder ec2Y(byte[] y)
      Set the "y (-3)" parameter for the key type "EC2".
      Parameters:
      y - The y-coordinate.
      Returns:
      this object.
    • ec2YInBase64Url

      public COSEKeyBuilder ec2YInBase64Url(String y)
      Set the "y (-3)" parameter for the key type "EC2".
      Parameters:
      y - The y-coordinate, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • ec2Y

      public COSEKeyBuilder ec2Y(boolean y)
      Set the "y (-3)" parameter for the key type "EC2".
      Parameters:
      y - The y-coordinate.
      Returns:
      this object.
    • ec2D

      public COSEKeyBuilder ec2D(byte[] d)
      Set the "d (-4)" parameter for the key type "EC2".
      Parameters:
      d - The private key.
      Returns:
      this object.
    • ec2DInBase64Url

      public COSEKeyBuilder ec2DInBase64Url(String d)
      Set the "d (-4)" parameter for the key type "EC2".
      Parameters:
      d - The private key, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaN

      public COSEKeyBuilder rsaN(byte[] n)
      Set the "n (-1)" parameter for the key type "RSA".
      Parameters:
      n - The RSA modulus n.
      Returns:
      this object.
    • rsaNInBase64Url

      public COSEKeyBuilder rsaNInBase64Url(String n)
      Set the "n (-1)" parameter for the key type "RSA".
      Parameters:
      n - The RSA modulus n, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaE

      public COSEKeyBuilder rsaE(byte[] e)
      Set the "e (-2)" parameter for the key type "RSA".
      Parameters:
      e - The RSA public exponent e.
      Returns:
      this object.
    • rsaEInBase64Url

      public COSEKeyBuilder rsaEInBase64Url(String e)
      Set the "e (-2)" parameter for the key type "RSA".
      Parameters:
      e - The RSA public exponent e, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaD

      public COSEKeyBuilder rsaD(byte[] d)
      Set the "d (-3)" parameter for the key type "RSA".
      Parameters:
      d - The RSA private exponent d.
      Returns:
      this object.
    • rsaDInBase64Url

      public COSEKeyBuilder rsaDInBase64Url(String d)
      Set the "d (-3)" parameter for the key type "RSA".
      Parameters:
      d - The RSA private exponent d, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaP

      public COSEKeyBuilder rsaP(byte[] p)
      Set the "p (-4)" parameter for the key type "RSA".
      Parameters:
      p - The prime factor p of n.
      Returns:
      this object.
    • rsaPInBase64Url

      public COSEKeyBuilder rsaPInBase64Url(String p)
      Set the "p (-4)" parameter for the key type "RSA".
      Parameters:
      p - The prime factor p of n, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaQ

      public COSEKeyBuilder rsaQ(byte[] q)
      Set the "q (-5)" parameter for the key type "RSA".
      Parameters:
      q - The prime factor q of n.
      Returns:
      this object.
    • rsaQInBase64url

      public COSEKeyBuilder rsaQInBase64url(String q)
      Set the "q (-5)" parameter for the key type "RSA".
      Parameters:
      q - The prime factor q of n, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaDP

      public COSEKeyBuilder rsaDP(byte[] dP)
      Set the "dP (-6)" parameter for the key type "RSA".
      Parameters:
      dP - d mod (p -1)
      Returns:
      this object.
    • rsaDPInBase64Url

      public COSEKeyBuilder rsaDPInBase64Url(String dP)
      Set the "dP (-6)" parameter for the key type "RSA".
      Parameters:
      dP - d mod (p -1), expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaDQ

      public COSEKeyBuilder rsaDQ(byte[] dQ)
      Set the "dQ (-7)" parameter for the key type "RSA".
      Parameters:
      dQ - d mod (q -1)
      Returns:
      this object.
    • rsaDQInBase64Url

      public COSEKeyBuilder rsaDQInBase64Url(String dQ)
      Set the "dQ (-7)" parameter for the key type "RSA".
      Parameters:
      dQ - d mod (q -1), expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaQInv

      public COSEKeyBuilder rsaQInv(byte[] qInv)
      Set the "qInv (-8)" parameter for the key type "RSA".
      Parameters:
      qInv - The CRT coefficient q^(-1) mod p.
      Returns:
      this object.
    • rsaQInvInBase64Url

      public COSEKeyBuilder rsaQInvInBase64Url(String qInv)
      Set the "qInv (-8)" parameter for the key type "RSA".
      Parameters:
      qInv - The CRT coefficient q^(-1) mod p, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaOther

      public COSEKeyBuilder rsaOther(List<Object> other)
      Set the "other (-9)" parameter for the key type "RSA".
      Parameters:
      other - Other prime infos, an array.
      Returns:
      this object.
    • rsaRI

      public COSEKeyBuilder rsaRI(byte[] r_i)
      Set the "r_i (-10)" parameter for the key type "RSA".
      Parameters:
      r_i - A prime factor r_i of n, where i >= 3.
      Returns:
      this object.
    • rsaRIInBase64Url

      public COSEKeyBuilder rsaRIInBase64Url(String r_i)
      Set the "r_i (-10)" parameter for the key type "RSA".
      Parameters:
      r_i - A prime factor r_i of n, where i >= 3. Expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaDI

      public COSEKeyBuilder rsaDI(byte[] d_i)
      Set the "d_i (-11)" parameter for the key type "RSA".
      Parameters:
      d_i - d mod (r_i - 1).
      Returns:
      this object.
    • rsaDIInBase64Url

      public COSEKeyBuilder rsaDIInBase64Url(String d_i)
      Set the "d_i (-11)" parameter for the key type "RSA".
      Parameters:
      d_i - d mod (r_i - 1). Expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • rsaTI

      public COSEKeyBuilder rsaTI(byte[] t_i)
      Set the "t_i (-12)" parameter for the key type "RSA".
      Parameters:
      t_i - The CRT coefficient t_i = (r_1 * r_2 * ... * r_(i-1))^(-1) mod r_i.
      Returns:
      this object.
    • rsaTIInBase64Url

      public COSEKeyBuilder rsaTIInBase64Url(String t_i)
      Set the "t_i (-12)" parameter for the key type "RSA".
      Parameters:
      t_i - The CRT coefficient t_i = (r_1 * r_2 * ... * r_(i-1))^(-1) mod r_i. Expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • symmetricK

      public COSEKeyBuilder symmetricK(byte[] k)
      Set the "k (-1)" parameter for the key type "Symmetric".
      Parameters:
      k - The key value.
      Returns:
      this object.
    • symmetricKInBase64Url

      public COSEKeyBuilder symmetricKInBase64Url(String k)
      Set the "k (-1)" parameter for the key type "Symmetric".
      Parameters:
      k - The key value, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • hssLmsPub

      public COSEKeyBuilder hssLmsPub(byte[] pub)
      Set the "pub (-1)" parameter for the key type "HSS-LMS".
      Parameters:
      pub - The public key.
      Returns:
      this object.
    • hssLmsPubInBase64Url

      public COSEKeyBuilder hssLmsPubInBase64Url(String pub)
      Set the "pub (-1)" parameter for the key type "HSS-LMS".
      Parameters:
      pub - The public key, expressed in the base64url format.
      Returns:
      this object.
      Since:
      1.5
    • walnutDsaN

      public COSEKeyBuilder walnutDsaN(Number N)
      Set the "N (-1)" parameter for the key type "WalnutDSA".
      Parameters:
      N - Group and Matrix (NxN) size.
      Returns:
      this object.
    • walnutDsaQ

      public COSEKeyBuilder walnutDsaQ(Number q)
      Set the "q (-2)" parameter for the key type "WalnutDSA".
      Parameters:
      q - Finite field F_q.
      Returns:
      this object.
    • walnutDsaTValues

      public COSEKeyBuilder walnutDsaTValues(List<Number> tValues)
      Set the "t-values (-3)" parameter for the key type "WalnutDSA".
      Parameters:
      tValues - List of T-values, entries in F_q.
      Returns:
      this object.
    • walnutDsaMatrix1

      public COSEKeyBuilder walnutDsaMatrix1(List<List<Number>> matrix1)
      Set the "matrix 1 (-4)" parameter for the key type "WalnutDSA".
      Parameters:
      matrix1 - NxN Matrix of entries in F_q in column-major form.
      Returns:
      this object.
    • walnutDsaPermutation1

      public COSEKeyBuilder walnutDsaPermutation1(List<Number> permutation1)
      Set the "permutation 1 (-5)" parameter for the key type "WalnutDSA".
      Parameters:
      permutation1 - Permutation associated with matrix 1.
      Returns:
      this object.
    • walnutDsaMatrix2

      public COSEKeyBuilder walnutDsaMatrix2(List<List<Number>> matrix2)
      Set the "matrix 2 (-6)" parameter for the key type "WalnutDSA".
      Parameters:
      matrix2 - NxN Matrix of entries in F_q in column-major form.
      Returns:
      this object.
    • buildOKPKey

      public COSEOKPKey buildOKPKey()
      Build a COSEOKPKey instance.
      Returns:
      A COSEOKPKey instance.
    • buildEC2Key

      public COSEEC2Key buildEC2Key()
      Build a COSEEC2Key instance.
      Returns:
      A COSEEC2Key instance.
    • build

      public COSEKey build() throws COSEException
      Build an instance of COSEKey or its subclass when possible (e.g. COSEOKPKey and COSEEC2Key).
      Returns:
      this object.
      Throws:
      COSEException