Signing payout requests with HMAC-SHA256
Published
A signature on each payout request lets the receiver check that the instruction arrived exactly as it was sent, and that it came from someone who holds the shared secret. HMAC-SHA256, a signature made with a secret key, provides both, as long as the platform signs the exact bytes it sends, with a timestamp, and looks after the secret.
Integrity and authenticity of a payout request
A payout request is an instruction to move money, sent from one system to another through an application programming interface, or API. Before acting on it, the API that receives it has to establish two things: that nothing in the instruction changed on the way, and that it came from the client it names. The first property is integrity. The second is authenticity. Encryption in transit protects the connection; a signature travels with the request itself.
A message authentication code, or MAC, gives the receiver both properties. It is a fixed-length value computed from the message and a secret key that only the sender and the receiver hold. The glossary of NIST, the U.S. National Institute of Standards and Technology, describes a MAC as a way to detect accidental and intentional changes to data, and notes its limit: a MAC provides authenticity and integrity, but not non-repudiation. Both parties hold the same secret, so the MAC cannot show anyone else which of them produced a message, and anyone who has the secret can produce requests that verify.
This article calls that value a signature, as the rest of this website does. Strictly speaking, it is a MAC.
How HMAC-SHA256 works
HMAC is described in RFC 2104 (RFC Editor website, opens in a new tab), one of the Internet’s technical documents known as Requests for Comments. It builds a MAC out of a hash function and a secret key. The sender computes the HMAC of the message with its key and sends it with the message. The receiver holds the same key, computes the HMAC of what arrived and compares the two values. If one byte of the message changed on the way, or if the sender used a different key, the values do not match.
HMAC-SHA256 is the instance of HMAC that uses SHA-256, one of the hash algorithms that NIST specifies in FIPS 180-4 (NIST website, opens in a new tab), the Secure Hash Standard. SHA-256 reduces a message to a digest of 256 bits. According to the standard, any change to a message will, with a very high probability, produce a different digest, so verification fails when SHA-256 is used in a digital signature or in a MAC such as HMAC. A digest without a key proves nothing about the sender, because whoever alters a message can compute a new digest for it.
The signature covers the exact bytes
A signature is computed over bytes, not over meaning. Two bodies can carry the same data and still differ byte by byte, and only one of them will match the signature sent with it.
JSON shows how easily that happens. RFC 8259 (RFC Editor website, opens in a new tab), which defines the format, treats an object as an unordered collection of members, allows insignificant whitespace around its structural characters and lets any character be written as an escape sequence. A body that is parsed and written out again can change without any change to its data:
- the members of an object come out in a different order;
- spaces and line breaks appear or disappear;
- an accented letter in a beneficiary’s name is written as itself in one version and escaped in the other;
- an amount loses a trailing zero when a program reads it as a number and writes it back.
Each of these changes the bytes, and with them the signature.
So your platform signs the exact bytes it sends, and the receiver has to verify the signature over the bytes it receives, not over a version it has parsed and rebuilt. In code, that means building the body once, signing that buffer and handing the same buffer to the HTTP client, rather than an object that a library serializes again on the way out. A gateway that reformats the body along the way breaks the signature for the same reason. At NubixPay, we recompute the signature over the body exactly as we receive it.
The timestamp and replay attacks
A signature over the body alone does not expire. Anyone who obtains a copy of a signed request, from an application log that kept too much for example, can send it again, and the signature will still match. NIST’s glossary calls this a replay attack: previously captured messages are replayed to masquerade as the legitimate party.
A timestamp inside what is signed limits that risk. The receiver accepts a request only for a limited time after its timestamp, and nobody can change the timestamp without breaking the signature. Each API sets that limit by weighing clock differences and network delays against exposure.
Two habits prevent failures that have nothing to do with an attack. Keep the clocks of the servers that sign requests synchronized with a reliable time source. And sign each request when it is sent, not when the payment entered a queue: a request signed at the start of a long batch can arrive too old at the end of it. A retry is signed again when it goes out; its body does not change.
Within the time limit, a timestamp on its own does not stop the same request from arriving twice. That is the job of idempotency. At NubixPay, if the same request is sent again, we return the first result and never create a second transfer. The article on idempotent payout requests explains how to retry without that risk.
Looking after the shared secret
Each NubixPay client has its own credentials and can rotate them from the client portal.
RFC 2104 asks for keys chosen at random and refreshed periodically. It sets no frequency, but it gives the reason: periodic refreshment helps against potential weaknesses of the function and of the keys, and limits the damage of an exposed key. It also strongly discourages keys shorter than the output of the hash function.
On your side, custody of the secret rests on these rules:
- the secret lives in a secrets store, never in source code, in configuration files kept in a repository, in support tickets or in chat;
- only the services that sign requests can read it, and people see it only when their work requires it;
- it never appears in application logs, and those that keep complete signed requests are protected too, because a copied request is exactly what a replay needs;
- it is rotated when someone who had access leaves, when exposure is suspected and on a regular schedule, and before each rotation the team knows every service that signs, so that none keeps using the old secret.
If your platform also verifies signatures, from its own partners for example, compare them in constant time. A comparison that stops at the first byte that differs takes slightly longer the more bytes match, and that difference can be measured from outside.
What happens at NubixPay when a signature does not match
When a signature does not match, no transfer is recorded. The call itself still appears in the API log that your team sees in the client portal, so a failed attempt is visible on your side as well as ours.
The first places to look are on the signing side: a body serialized again after it was signed, or a secret left over from before a rotation.
The full API specification is available to each client from onboarding. The payout API page sums up the properties of the API, and How it works follows a transfer from the signed request to payment.
Sources
- RFC 2104: HMAC: Keyed-Hashing for Message Authentication (RFC Editor website, opens in a new tab)www.rfc-editor.org
- FIPS 180-4, Secure Hash Standard (SHS) (NIST website, opens in a new tab)csrc.nist.gov
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format (RFC Editor website, opens in a new tab)www.rfc-editor.org
- message authentication code (MAC) - Glossary (NIST website, opens in a new tab)csrc.nist.gov
- replay attack - Glossary (NIST website, opens in a new tab)csrc.nist.gov