Class InitializedMessageDigest.OneShot
java.lang.Object
javacard.security.MessageDigest
javacard.security.InitializedMessageDigest
javacard.security.InitializedMessageDigest.OneShot
- Enclosing class:
InitializedMessageDigest
The
OneShot class is a specialization of the
InitializedMessageDigest class intended to support efficient one-shot
hash operations that may avoid persistent memory writes entirely. The
OneShot class uses a delegation model where
calls are delegated to an instance of a
InitializedMessageDigest-implementing class configured for one-shot
use.
Note:
- Instances of
OneShotare JCRE owned temporary Entry Point Object instances and references to these temporary objects cannot be stored in class variables or instance variables or array components. See Runtime Environment Specification, Java Card Platform, Classic Edition, section 6.2.1 for details. - The platform must support at least one instance of
OneShot. Support for severalOneShotinstances is platform dependent. To guarantee application code portability, acquiring/opening and then releasing/closingOneShotinstances should be performed within tighttry-catch-finallyblocks (as illustrated in the code sample below) in order to avoid unnecessarily keeping hold of instances and to prevent interleaving invocations - hence enforcing the One-Shot usage pattern. Additionally, any local variable holding a reference to aOneShotinstance should be set tonullonce the instance is closed in order to prevent further use attempts. - Upon return from any
Appletentry point method, back to the JCRE, and on tear or card reset events anyOneShotinstances in use are released back to the JCRE. - The internal state associated with an instance of
OneShotmust be bound to the initial calling context (owner context) as to preclude use/calls on that instance from other contexts. - Unless otherwise specified, after an instance of
OneShotis released back to the JCRE, calls to any of the instance methods of theOneShotclass results in anCryptoExceptionbeing thrown with reason codeCryptoException.ILLEGAL_USE.
The following code shows a typical usage pattern for the
OneShot class.
...
InitializedMessageDigest.OneShot dig = null;
try {
dig = InitializedMessageDigest.OneShot.open(MessageDigest.ALG_SHA);
dig.setInitialDigest(initialDigData, (short) 2, (short) initialDigData.length, initialDigData, (short) 0, (short) 2);
dig.doFinal(someInData, (short) 0, (short) someInData.length, digData, (short) 0);
} catch (CryptoException ce) {
// Handle exception
} finally {
if (dig != null) {
dig.close();
dig = null;
}
}
...
- Since:
- 3.0.5
-
Nested Class Summary
Nested classes/interfaces inherited from class InitializedMessageDigest
InitializedMessageDigest.OneShotModifier and TypeClassDescriptionstatic final classTheOneShotclass is a specialization of theInitializedMessageDigestclass intended to support efficient one-shot hash operations that may avoid persistent memory writes entirely. -
Field Summary
Fields inherited from class MessageDigest
ALG_MD5, ALG_NULL, ALG_RIPEMD160, ALG_SHA, ALG_SHA_224, ALG_SHA_256, ALG_SHA_384, ALG_SHA_512, ALG_SHA3_224, ALG_SHA3_256, ALG_SHA3_384, ALG_SHA3_512, ALG_SM3, LENGTH_MD5, LENGTH_RIPEMD160, LENGTH_SHA, LENGTH_SHA_224, LENGTH_SHA_256, LENGTH_SHA_384, LENGTH_SHA_512, LENGTH_SHA3_224, LENGTH_SHA3_256, LENGTH_SHA3_384, LENGTH_SHA3_512, LENGTH_SM3Modifier and TypeFieldDescriptionstatic final byteMessage Digest algorithm MD5.static final byteThis constant indicates that there is no discrete message digest algorithm.static final byteMessage Digest algorithm RIPE MD-160.static final byteMessage Digest algorithm SHA-1.static final byteMessage Digest algorithm SHA-224.static final byteMessage Digest algorithm SHA-256.static final byteMessage Digest algorithm SHA-384.static final byteMessage Digest algorithm SHA-512.static final byteMessage Digest algorithm SHA3-224.static final byteMessage Digest algorithm SHA3-256.static final byteMessage Digest algorithm SHA3-384.static final byteMessage Digest algorithm SHA3-512.static final byteMessage Digest algorithm SM3.static final byteLength of digest in bytes for MD5static final byteLength of digest in bytes for RIPE MD-160static final byteLength of digest in bytes for SHA-1static final byteLength of digest in bytes for SHA-224static final byteLength of digest in bytes for SHA-256static final byteLength of digest in bytes for SHA-384static final byteLength of digest in bytes for SHA-512static final byteLength of digest in bytes for SHA3-224static final byteLength of digest in bytes for SHA3-256static final byteLength of digest in bytes for SHA3-384static final byteLength of digest in bytes for SHA3-512static final byteLength of digest in bytes for SM3 -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes and releases this JCRE owned temporary instance of theOneShotobject for reuse.shortdoFinal(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset) Generates a hash of all/last input data.shortdoIntermediateMessageDigest(byte[] outBuff, short outOffset) Always throws aCryptoException.This method is not supported byOneShot.byteGets the Message digest algorithm.byteReturns the byte length of the hash.static final InitializedMessageDigest.OneShotopen(byte algorithm) Closes and acquires a JCRE owned temporary Entry Point Object instance ofOneShotwith the selected algorithm.voidreset()Resets theMessageDigestobject to the initial state for further use.voidsetInitialDigest(byte[] state, short stateOffset, short stateLength, byte[] digestedMsgLenBuf, short digestedMsgLenOffset, short digestedMsgLenLength) This method sets the state of the hash engine in place of the default value used by theMessageDigestsuperclass.voidupdate(byte[] inBuff, short inOffset, short inLength) Always throws aCryptoException.This method is not supported byOneShot.Methods inherited from class MessageDigest
getInitializedMessageDigestInstance, getInstance, isIntermediateMessageDigestSupportedModifier and TypeMethodDescriptionstatic final InitializedMessageDigestgetInitializedMessageDigestInstance(byte algorithm, boolean externalAccess) Creates aInitializedMessageDigestobject instance of the selected algorithm.static final MessageDigestgetInstance(byte algorithm, boolean externalAccess) Creates aMessageDigestobject instance of the selected algorithm.static final booleanisIntermediateMessageDigestSupported(byte algorithm) This method is used to determine if instances of MessageDigest or InitializedMessageDigest classes support intermediate hash calculation for a given algorithm.
-
Method Details
-
open
Closes and acquires a JCRE owned temporary Entry Point Object instance ofOneShotwith the selected algorithm.- Parameters:
algorithm- the desired message digest algorithm. Valid codes listed inALG_*constants above, for example,ALG_SHA.- Returns:
- the
InitializedMessageDigestobject instance of the requested algorithm. - Throws:
CryptoException- with the following reason codes:CryptoException.NO_SUCH_ALGORITHMif the requested algorithm or shared access mode is not supported.
SystemException- with the following reason codes:SystemException.NO_RESOURCEif sufficient resources are not available.
-
close
public void close()Closes and releases this JCRE owned temporary instance of theOneShotobject for reuse. If this method is called again this method does nothing.- Throws:
SecurityException- if this JCRE owned temporary instance of theOneShotobject was opened in a context different from that of the caller.
-
setInitialDigest
public void setInitialDigest(byte[] state, short stateOffset, short stateLength, byte[] digestedMsgLenBuf, short digestedMsgLenOffset, short digestedMsgLenLength) throws CryptoException This method sets the state of the hash engine in place of the default value used by theMessageDigestsuperclass. This state is the intermediate state of the hash engine when a block-aligned portion of the message has been previously processed. The remaining bytes of the message must be presented to thisInitializedMessageDigestobject via theupdateanddoFinalmethods to generate the final message digest.Note:
- The maximum allowed value of the byte length of the first part of the message is algorithm specific
- Specified by:
setInitialDigestin classInitializedMessageDigest- Parameters:
state- input buffer containing the intermediate state of the hash engine when a block-aligned portion of the message has been previously processed (using the same algorithm).stateOffset- offset intostatearray where the state data beginsstateLength- the length of data instatearray.digestedMsgLenBuf- the byte array containing the number of bytes in the first part of the message that has previously been processed to obtain the specified intermediate state. The value must be a multiple of the block size defined by the hash algorithm.digestedMsgLenOffset- the offset withindigestedMsgLenBufwhere the digested length begins (the bytes starting at this offset for digestedMsgLenLength bytes are concatenated to form the actual length of the message portion already processed)digestedMsgLenLength- byte length of the value stored indigestedMsgLenBuf- Throws:
SecurityException- if this JCRE owned temporary instance of theOneShotobject was opened in a context different from that of the caller.CryptoException- with the following reason codes:CryptoException.ILLEGAL_VALUEif the parameterstateLengthis not equal to the intermediate hash value size of the algorithm or if the number of bytes in the first part of the message that has previously been hashed is 0 or not a multiple of the algorithm's block size or greater than the maximum length supported by the algorithm (seeALG_*algorithm descriptionsALG_SHA).
-
getAlgorithm
public byte getAlgorithm()Gets the Message digest algorithm.- Specified by:
getAlgorithmin classMessageDigest- Returns:
- the algorithm code defined above
- Throws:
SecurityException- if this JCRE owned temporary instance of theOneShotobject was opened in a context different from that of the caller.
-
getLength
public byte getLength()Returns the byte length of the hash.- Specified by:
getLengthin classMessageDigest- Returns:
- hash length
- Throws:
SecurityException- if this JCRE owned temporary instance of theOneShotobject was opened in a context different from that of the caller.
-
doFinal
public short doFinal(byte[] inBuff, short inOffset, short inLength, byte[] outBuff, short outOffset) throws CryptoException Generates a hash of all/last input data. Completes and returns the hash computation after performing final operations such as padding. TheMessageDigestobject is reset to the initial state after this call is made. The data format is big-endian.The input and output buffer data may overlap.
In addition to returning a
shortresult, this method sets the result in an internal state which can be rechecked using assertion methods of theSensitiveResultclass, if supported by the platform.- Specified by:
doFinalin classMessageDigest- Parameters:
inBuff- the input buffer of data to be hashedinOffset- the offset into the input buffer at which to begin hash generationinLength- the byte length to hashoutBuff- the output buffer, may be the same as the input bufferoutOffset- the offset into the output buffer where the resulting hash value begins- Returns:
- number of bytes of hash output in
outBuff - Throws:
SecurityException- if this JCRE owned temporary instance of theOneShotobject was opened in a context different from that of the caller.CryptoException- with the following reason codes:CryptoException.ILLEGAL_USEif the accumulated message length is greater than the maximum length supported by the algorithm.
-
doIntermediateMessageDigest
Always throws aCryptoException.This method is not supported byOneShot.- Specified by:
doIntermediateMessageDigestin classMessageDigest- Parameters:
outBuff- the output buffer, may be the same as the input bufferoutOffset- the offset into the output buffer where the resulting hash value begins- Returns:
- number of bytes of hash output in
outBuff - Throws:
CryptoException- with the following reason codes:CryptoException.ILLEGAL_USEalways.
- See Also:
-
reset
public void reset()Resets theMessageDigestobject to the initial state for further use.- Specified by:
resetin classMessageDigest- Throws:
SecurityException- if this JCRE owned temporary instance of theOneShotobject was opened in a context different from that of the caller.
-
update
Always throws aCryptoException.This method is not supported byOneShot.- Specified by:
updatein classMessageDigest- Parameters:
inBuff- the input buffer of data to be hashedinOffset- the offset into the input buffer at which to begin hash generationinLength- the byte length to hash- Throws:
CryptoException- with the following reason codes:CryptoException.ILLEGAL_USEalways.
- See Also:
-