Class SM9EncUserPrivateKeySpec
- All Implemented Interfaces:
KeySpec
KeyFactory.SM9. The
encoding alone does not determine a usable key - decryption also needs the
encryption master public key, the user's identity (part of the decryption KDF
input) and the hid the KGC derived the key under, none of which are part of it -
so the spec carries all four, letting a stored user key be reconstituted without
access to the master private key.
This extends EncodedKeySpec rather than PKCS8EncodedKeySpec:
the encoded bytes are a real PKCS#8 encoding (reflected in getFormat()), but the
spec is not self-sufficient the way a plain PKCS8EncodedKeySpec is meant to be, and
subclassing the concrete JDK type would let generic code treat it as one.
The matching spec is returned by the factory's getKeySpec method, so a
user key round-trips: store getEncoded() (or ask for this spec), rebuild
with generatePrivate. A key-exchange user key rebuilds the same way with
exchangeKey set - the encoding does not record which usage the KGC
derived the key for, so the flag is the importer's claim, and the consumers
enforce whichever usage the rebuilt key carries (KeyAgreement.SM9
accepts only exchange keys; the KEM and cipher only non-exchange keys). The same
goes for a key declared for the KEM or for decryption: the encoding does not record
that either, so a key rebuilt from getEncoded() is declared for neither
unless the spec is built with the usage again, and is then held only to the first
of the two it is used for.
KeyFactory.SM9 checks the point against the other three, by the KGC's own
relation e([H1(ID || hid, N)]P1 + P_pub-e, de) = e(P_pub-e, P2), and refuses the spec
with an InvalidKeySpecException unless the point is the key
the KGC derives for this identity and hid under this master public key.
The claim is not taken entirely on its own, though: the hid says which of the
KGC's generation functions formed the point, so the one combination that names two
keys at once - the KEM / decryption usage claimed for a point derived under
SM9EncUserKeyGenerator.HID_EXCHANGE - is
refused where both halves are first in hand, by this spec's constructor, as well as
by KeyFactory.SM9 behind it.
What the two cannot separate is a KGC that publishes one hid for both functions,
where the exchange key and the decryption key of an identity are the same point;
such a master key has to serve one function only, as the GM/T 0044.5 worked
examples themselves arrange.
A KEM / decryption key can also be declared, through the constructor taking a usage, for one
of the SM9 KEM and Cipher.SM9; it is then refused for the other on every use, its first
included. A key rebuilt without a declaration is held to the first of the two it is used for -
see SM9EncPrivateKeyParameters.recordUsage(int). The spec getKeySpec hands back
carries the usage the key was rebuilt with.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe usage of a key declared for SM9 public-key decryption (Cipher.SM9), which the KEM refuses.static final intThe usage of a key declared for the SM9 KEM (KeyGenerator.SM9-KEM,KEM.SM9-KEM), whichCipher.SM9refuses.static final intThe usage of a KEM / decryption key declared for neither of the KEM andCipher.SM9: it is held to the first of them it is used for. -
Constructor Summary
ConstructorsConstructorDescriptionSM9EncUserPrivateKeySpec(byte[] pkcs8Encoding, SM9EncMasterPublicKey masterPublicKey, byte[] identity, byte hid) Base constructor, for a KEM / decryption user key.SM9EncUserPrivateKeySpec(byte[] pkcs8Encoding, SM9EncMasterPublicKey masterPublicKey, byte[] identity, byte hid, boolean exchangeKey) SM9EncUserPrivateKeySpec(byte[] pkcs8Encoding, SM9EncMasterPublicKey masterPublicKey, byte[] identity, byte hid, int usage) -
Method Summary
Modifier and TypeMethodDescriptionbytegetHid()The private-key generation function identifier hid the key was derived under - the KGC's published choice, not sensitive.byte[]intgetUsage()The usage the key rebuilds for:USAGE_KEMorUSAGE_DECRYPTIONfor a KEM / decryption key declared for one of the two,USAGE_UNSPECIFIEDfor one declared for neither, andSM9EncPrivateKeyParameters.USAGE_KEY_EXCHANGEfor a key-exchange key, whichisExchangeKey()reports as well.booleanWhether the key rebuilds as a key-exchange user key rather than a KEM / decryption one.Methods inherited from class EncodedKeySpec
getAlgorithm, getEncoded
-
Field Details
-
USAGE_UNSPECIFIED
public static final int USAGE_UNSPECIFIEDThe usage of a KEM / decryption key declared for neither of the KEM andCipher.SM9: it is held to the first of them it is used for. The value ofSM9EncPrivateKeyParameters.USAGE_UNSPECIFIED.- See Also:
-
USAGE_KEM
public static final int USAGE_KEMThe usage of a key declared for the SM9 KEM (KeyGenerator.SM9-KEM,KEM.SM9-KEM), whichCipher.SM9refuses. The value ofSM9EncPrivateKeyParameters.USAGE_KEM.- See Also:
-
USAGE_DECRYPTION
public static final int USAGE_DECRYPTIONThe usage of a key declared for SM9 public-key decryption (Cipher.SM9), which the KEM refuses. The value ofSM9EncPrivateKeyParameters.USAGE_DECRYPTION.- See Also:
-
-
Constructor Details
-
SM9EncUserPrivateKeySpec
public SM9EncUserPrivateKeySpec(byte[] pkcs8Encoding, SM9EncMasterPublicKey masterPublicKey, byte[] identity, byte hid) Base constructor, for a KEM / decryption user key.- Parameters:
pkcs8Encoding- the user private key's PKCS#8 encoding, as returned by the key'sgetEncoded().masterPublicKey- the encryption master public key the user key was derived under.identity- the user's identity.hid- the private-key generation function identifier the KGC derived the key under - its published choice,SM9EncUserKeyGenerator.HIDin the published examples, and neverSM9EncUserKeyGenerator.HID_EXCHANGE, which names the key exchange.
-
SM9EncUserPrivateKeySpec
public SM9EncUserPrivateKeySpec(byte[] pkcs8Encoding, SM9EncMasterPublicKey masterPublicKey, byte[] identity, byte hid, boolean exchangeKey) - Parameters:
pkcs8Encoding- the user private key's PKCS#8 encoding, as returned by the key'sgetEncoded().masterPublicKey- the encryption master public key the user key was derived under.identity- the user's identity.hid- the private-key generation function identifier the KGC derived the key under.exchangeKey- whether the KGC derived the key for the key exchange (fromgenerateExchangeKeyPair) rather than for KEM / decryption.
-
SM9EncUserPrivateKeySpec
public SM9EncUserPrivateKeySpec(byte[] pkcs8Encoding, SM9EncMasterPublicKey masterPublicKey, byte[] identity, byte hid, int usage) - Parameters:
pkcs8Encoding- the user private key's PKCS#8 encoding, as returned by the key'sgetEncoded().masterPublicKey- the encryption master public key the user key was derived under.identity- the user's identity.hid- the private-key generation function identifier the KGC derived the key under.usage- the usage the KEM / decryption key rebuilds for:USAGE_KEMorUSAGE_DECRYPTIONfor a key declared for one of the two, which the other then refuses on every use, orUSAGE_UNSPECIFIEDfor one declared for neither. A key-exchange key is named through the constructor takingexchangeKey, asSM9EncPrivateKeyParameters.fromEncodedExchangeKey(byte[], SM9EncMasterPublicKeyParameters, byte[])names one in the light-weight API.
-
-
Method Details
-
getMasterPublicKey
-
getIdentity
public byte[] getIdentity() -
getHid
public byte getHid()The private-key generation function identifier hid the key was derived under - the KGC's published choice, not sensitive. -
isExchangeKey
public boolean isExchangeKey()Whether the key rebuilds as a key-exchange user key rather than a KEM / decryption one. -
getUsage
public int getUsage()The usage the key rebuilds for:USAGE_KEMorUSAGE_DECRYPTIONfor a KEM / decryption key declared for one of the two,USAGE_UNSPECIFIEDfor one declared for neither, andSM9EncPrivateKeyParameters.USAGE_KEY_EXCHANGEfor a key-exchange key, whichisExchangeKey()reports as well. The constructor taking a usage takes the first three. -
getFormat
- Specified by:
getFormatin classEncodedKeySpec
-