122

I came across many APIs that give the user both an API key and a secret. But my question is: what is the difference between both?

In my eyes, one key can be enough. Say I have a key and only I and the server know it. I create a HMAC hash with this key and do an API call. On the server, we create the HMAC hash again and compare it with the sent hash. If it's the same, the call is authenticated.

So why use two keys?

Edit: or is that API key used to lookup the API secret?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
EsTeGe
  • 2,975
  • 5
  • 28
  • 42
  • Also see [Best Practices for Managing AWS Access Keys](https://docs.aws.amazon.com/general/latest/gr/aws-access-keys-best-practices.html) – mfaani Jul 24 '19 at 19:58

4 Answers4

82

You need two separate keys, one that tells them who you are, and the other one that proves you are who you say you are.

The "key" is your user ID, and the "secret" is your password. They just use the "key" and "secret" terms because that's how they've implemented it.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • 5
    And what if u are communicating over https? What's the point of encrypting your message with some secret key then? – kamuniaft Jan 12 '18 at 20:09
  • 10
    The point is always to reduce risk. If the https communication is compromised, then an attacker that can read your request won't be able to forge new ones. If your API is about to rank pictures of cats, no big deal, if it's a payment API you better have multiple layers of security :) – Yall Nov 23 '18 at 07:47
  • 1
    I suppose the purpose it's two separate keys, is because different users of a single client app can have different secrets, otherwise if they were to all have the same secret then having a key isn't useful. Right? – mfaani Jul 24 '19 at 18:21
  • 1
    Why don‘t these API use `Bearer:` authentication for that? You would have an ID and a pwd there. – Stefan Haberl May 15 '20 at 05:39
11

Simple answer, if I understood it correctly...

If you use your API key for encryption, how will the service know who is contacting them? How will they decrypt that message?

You use API key to state who you are, this is what you are sending in plain text. The SECRET key you do not send to anyone. You simply use it for encryption. Then you send the encrypted message. You do not send the key that was used for encryption, that would defeat the purpose.

AndroC
  • 4,758
  • 2
  • 46
  • 69
  • You do. You send the api key to the server. So, this means that you are giving that value to anyone who may be intercepting your communication with the server. – AndroC May 11 '18 at 07:28
  • Almost every API I've seen has you send both the key and the secret to the server. The connection to the server is encrypted with theoretically the same level of security. But I never give either to anyone else besides the server. – sudo May 11 '18 at 15:56
  • I never saw sending `secret` in plain text. Can you give me a link? What I saw is using `secret` to encrypt some data. And along with the encrypted data, sending `apiKey` so that the server knows how to decrypt the data. – AndroC May 11 '18 at 16:07
  • https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-bash-command-line and https://nexmo.github.io/Quickstarts/sms/send/ are the examples I saw that prompted me to search on StackOverflow. – sudo May 11 '18 at 16:11
  • Twilio is not exactly using these terms. But Nexmo sure is... But, after a quick glance, it seems like they are just calling the data `secret` and `apiKey` and what they actually doing is `username` and `password`. Which is completely different thing... – AndroC May 11 '18 at 16:23
10

One thing that I did not see mentioned here, although it is an extension of Marcus Adams's answer, is that you should not be using a single piece of information to both identify and authenticate a user if there is a possibility of timing attacks, which can use the differences in response times to guess how far a string comparison got.

If you are using a system which uses a "key" to look up the user or credential, that piece of information could be incrementally guessed over time by sending thousands of requests and examining the time that it takes for your database to find (or not find) a record. This is especially true if the "key" is stored in plaintext instead of a one-way hash of the key. You would want to store users's keys in a plaintext or symmetrically-encrypted for if you need to be able to display the key to the user again.

By having a second piece of information, or "secret", you can first look up the user or credential using the "key", which could be vulnerable to a timing attack, then use a timing-safe compare function to check the value of the "secret".

Here is Python's implementation of that function:

https://github.com/python/cpython/blob/cd8295ff758891f21084a6a5ad3403d35dda38f7/Modules/_operator.c#L727

And it is exposed in the hmac lib (and probably others):

https://docs.python.org/3/library/hmac.html#hmac.compare_digest


One thing to note here is that I don't think that this kind of attack will work on values that are hashed or encrypted before lookup, because the values that are being compared change randomly each time a character in the input string changes. I found a good explanation of this here.

Solutions for storing API keys would then be:

  1. Use a separate key and secret, use the key to look up the record, and use a timing-safe compare to check the secret. This allows you to show the user the key and secret to a user again.
  2. Use a separate key and secret, use symmetrical, deterministic encryption on the secret, and do a normal comparison of encrypted secrets. This allows you to show the user the key and secret again, and could save you from having to implement a timing-safe comparison.
  3. Use a separate key and secret, display the secret, hash and store it, then do a normal comparison of the hashed secret. This removes the necessity to use two-way encryption, and has the added benefit of keeping your secret secure if the system is compromised. It has the downside that you cannot show the secret to the user again.
  4. Use a single key, show it to the user once, hash it, then do a normal lookup of the hashed or encrypted key. This uses a single key, but it is not able to be shown to the user again. Has the benefit of keeping keys secure if the system is compromised.
  5. Use a single key, show it to the user once, encrypt it, and do a normal lookup of the encrypted secret. Can be shown to the user again, but at the cost of having keys vulnerable if they system is compromised.

Of these, I think that 3 is the best balance of security and convenience. I have seen this implemented on many websites when getting keys issued.

Also, I invite any actual security experts to critique this answer. I just wanted to get this out there as another discussion point.

kbuilds
  • 991
  • 11
  • 19
8

There are answers explaining what the secret and (public) key is. It's a public-private key pair that they give confusing names to. But nobody says why the APIs require both, and many APIs only give you one secret! I've also never seen any API's docs explain why they have two keys, so the best I can do is speculate...

It's best to put only your public key in your request and sign the request locally with your private key; sending anything more shouldn't be needed. But some get away with just having the secret in the request. Ok, any good API will use some transport security like TLS (usually over HTTPS). But you're still exposing your private key to the server that way, increasing the risk of them somehow mishandling it (see: GitHub and Twitter's password logging bug recently discovered). And HTTPS is theoretically just as secure, but there are always implementation flaws out there.

But many – actually most it seems – APIs have you send both keys in requests since that's easier than making people do their own signatures; can't have pure cURL examples otherwise! In that case, it's pointless to have them separate. I guess the separate keys are just for in case they change the API later to take advantage of them. Or some have a client library that might do it the more secure way.

sudo
  • 5,604
  • 5
  • 40
  • 78