2015-12-22 22:31:15 +00:00
|
|
|
--------------
|
|
|
|
The HMAC class
|
|
|
|
--------------
|
2015-12-17 09:36:09 +00:00
|
|
|
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
.. cpp:class:: emsha::HMAC
|
2015-12-17 09:36:09 +00:00
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
HMAC is an implementation of the :cpp:class:`emsha::Hash` interface
|
|
|
|
implementing the HMAC keyed-hash message authentication code as
|
|
|
|
defined in FIPS 198-1, using SHA-256 internally.
|
|
|
|
|
|
|
|
.. cpp:function:: HMAC::HMAC(const uint8_t *key, uint32_t keylen)
|
2015-12-17 09:36:09 +00:00
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
An HMAC context must be initialised with a key.
|
2015-12-17 09:36:09 +00:00
|
|
|
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
.. cpp:function:: HMAc::~HMAC()
|
2015-12-17 09:36:09 +00:00
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
The HMAC destructor will attempt to wipe the key and reset the
|
|
|
|
underlying SHA-256 context.
|
2015-12-17 09:36:09 +00:00
|
|
|
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
.. cpp:function:: emsha::EMSHA_RESULT HMAC::reset(void)
|
2015-12-17 09:36:09 +00:00
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
reset clears the internal state of the `HMAC` context and returns
|
2015-12-17 09:36:09 +00:00
|
|
|
it to its initial state. It should always return ``EMSHA_ROK``.
|
2015-12-22 22:31:15 +00:00
|
|
|
This function will **not** wipe the key; an `HMAC` object that has
|
|
|
|
`reset` called it can be used immediately after.
|
|
|
|
|
2015-12-17 09:36:09 +00:00
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
.. cpp:function:: emsha::EMSHA_RESULT HMAC::update(const uint8_t *m, uint32_t ml)
|
2015-12-17 09:36:09 +00:00
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
update writes data into the context. While there is an upper limit on
|
|
|
|
the size of data that the underlying SHA-256 context can operate on,
|
|
|
|
this package is designed for small systems that will not approach
|
|
|
|
that level of data (which is on the order of 2 exabytes), so it is
|
|
|
|
not thought to be a concern.
|
2015-12-17 09:36:09 +00:00
|
|
|
|
|
|
|
**Inputs**:
|
|
|
|
|
|
|
|
+ ``m``: a byte array containing the message to be written. It must
|
|
|
|
not be NULL (unless the message length is zero).
|
|
|
|
|
|
|
|
+ ``ml``: the message length, in bytes.
|
|
|
|
|
|
|
|
**Return values**:
|
|
|
|
|
|
|
|
* ``EMSHA_NULLPTR`` is returned if ``m`` is NULL and ``ml`` is nonzero.
|
|
|
|
|
|
|
|
* ``EMSHA_INVALID_STATE`` is returned if the `update` is called
|
|
|
|
after a call to `finalize`.
|
|
|
|
|
|
|
|
* ``SHA256_INPUT_TOO_LONG`` is returned if too much data has been
|
|
|
|
written to the context.
|
|
|
|
|
|
|
|
+ ``EMSHA_ROK`` is returned if the data was successfully added to
|
2015-12-22 22:31:15 +00:00
|
|
|
the HMAC context.
|
2015-12-17 09:36:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
.. cpp:function:: emsha::EMSHA_RESULT SHA256::finalize(uint8_t *d)
|
|
|
|
|
|
|
|
``finalize`` completes the digest. Once this method is called, the
|
|
|
|
context cannot be updated unless the context is reset.
|
|
|
|
|
|
|
|
**Inputs**:
|
|
|
|
|
|
|
|
* d: a byte buffer that must be at least ``SHA256.size()`` in
|
|
|
|
length.
|
|
|
|
|
|
|
|
**Outputs**:
|
|
|
|
|
|
|
|
* ``EMSHA_NULLPTR`` is returned if ``d`` is the null pointer.
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
* ``EMSHA_INVALID_STATE`` is returned if the HMAC context is in
|
2015-12-17 09:36:09 +00:00
|
|
|
an invalid state, such as if there were errors in previous
|
|
|
|
updates.
|
|
|
|
|
|
|
|
* ``EMSHA_ROK`` is returned if the context was successfully
|
|
|
|
finalised and the digest copied to ``d``.
|
|
|
|
|
|
|
|
|
|
|
|
.. cpp:function:: emsha::EMSHA_RESULT SHA256::result(uint8_t *d)
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
``result`` copies the result from the HMAC context into the
|
2015-12-17 09:36:09 +00:00
|
|
|
buffer pointed to by ``d``, running finalize if needed. Once
|
|
|
|
called, the context cannot be updated until the context is reset.
|
|
|
|
|
|
|
|
**Inputs**:
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
* ``d``: a byte buffer that must be at least ``HMAC.size()`` in
|
2015-12-17 09:36:09 +00:00
|
|
|
length.
|
|
|
|
|
|
|
|
**Outputs**:
|
|
|
|
|
|
|
|
* ``EMSHA_NULLPTR`` is returned if ``d`` is the null pointer.
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
* ``EMSHA_INVALID_STATE`` is returned if the HMAC context is in
|
2015-12-17 09:36:09 +00:00
|
|
|
an invalid state, such as if there were errors in previous
|
|
|
|
updates.
|
|
|
|
|
|
|
|
* ``EMSHA_ROK`` is returned if the context was successfully
|
|
|
|
finalised and the digest copied to ``d``.
|
|
|
|
|
|
|
|
.. cpp:function:: uint32_t SHA256::size(void)
|
|
|
|
|
2015-12-22 22:31:15 +00:00
|
|
|
``size`` returns the output size of HMAC, e.g. the size that the
|
|
|
|
buffers passed to ``finalize`` and ``result`` should be.
|
2015-12-17 09:36:09 +00:00
|
|
|
|
|
|
|
**Outputs**:
|
|
|
|
|
|
|
|
* a ``uint32_t`` representing the expected size of buffers passed
|
|
|
|
to ``result`` and ``finalize``.
|
|
|
|
|