DPAPI has 2 functions: CryptUnprotectDataand CryptProtectData.
I read They do the encryption or decryption by using a session key that the function creates by using the user's logon credentials. Does that mean that I do not need store this key anywhere and they will created when I want encrypt or decrypt data?
Also I found An important point to remember is that DPAPI merely applies cryptographic protection to the data. It does not store any of the protected data; therefore applications calling DPAPI must implement their own storage of the protected data. Is this about the key or a file what was encrypted?
The mentioned functions use the key specific to the logged in user. Thus you don't need to store the key. However, these functions are merely for data transformation and not for data storage. This means that it's your job to store encrypted data chunk wherever you want - the CryptProtectData won't do this for you.
Related
Background story:
I'm trying to write my own logging library. It's for hobby purposes. There's one must for me: logged data must be encrypted asymmetrically. The log messages are always directly written into the file, no caching occurs, no waiting for any queue.
This means I'll have to encrypt bunch of small chunks of messages. Even though the bottleneck is probably going to be the lack of caching & IO operations, I'd like to choose the encryption algorithm wisely.
Summary:
I have to encrypt numerous of small (<200 bytes) of data
Algorithm MUST be asymmetric, I'd like to encrypt with the public key and the only be able to decrypt it with my very own private key
What algorithm do you suggest?
It seems that you're interpreting “logged data must be encrypted asymmetrically” literally as a low-level requirement. “Logged data must be encrypted asymmetrically” is not a security requirement, it's an implementation approach. It's a bad implementation approach, because it requires you to design your own cryptographic protocol (you can use standard primitives, but only in a non-standard way), and it would have annoying limitations.
A much more reasonable requirement is “the machine that produces the logs must not be able to decrypt them”. This is a security requirement: it is a requirement on an asset (the logs) concerning its security (specifically their confidentiality).
The way to implement this security requirement is indeed to use asymmetric encryption. But you don't take an asymmetric encryption primitive and pass the logs as input to that. Rather, you use hybrid encryption: generate a symmetric key, encrypt the logs with that, encrypt the symmetric key with the asymmetric key, and erase the symmetric key.
The best way to do this is to use a library that does it well. The crypto_box, crypto_box_easy and crypto_box_seal functions of NaCl or libsodium are the gold standards here. You pass the public key for encryption, the message to sign, and you get an encrypted “box” out which can only be decrypted with the private key. crypto_box_easy and crypto_box also take your own private key as an argument, to sign the logs, which you might not want in your toy example but is usually important in the real world. crypto_box_easy and crypto_box also take a nonce as argument; this can be any value that can be public but that you must not use twice, for example a random string of crypto_box_NONCEBYTES bytes.
If you don't want to use crypto_box, for example because you want to learn how it's done under the hood, you have to assemble the parts manually, using your chosen low-level cryptographic library. The flow is different depending on which flavor of asymmetric encryption you use. With a key establishment method such as ECIES:
Generate a random one-time private key y.
Calculate the corresponding public value gy.
Using the recipient's public key gx, calculate the shared secret gxy.
Apply a key derivation function such as HKDF to deterministically generate a secret symmetric key, for example an AES key or a Chacha20_Poly1305 key.
Use the secret symmetric key to encrypt the log message, for example using AES-GCM or Chacha20_Poly1305.
Optionally, hash the log message and sign it with your public key.
Wipe the one-time private key, the shared secret, the secret symmetric key, the plaintext log, and any other intermediate value from memory.
Send the ciphertext, the public valuegy and optionally the signature.
With a key encryption method such as RSA-OAEP:
Generate a random one-time secret key, for example an AES key or a Chacha20_Poly1305 key.
Use the secret symmetric key to encrypt the log message, for example using AES-GCM or Chacha20_Poly1305.
Optionally, hash the log message and sign it with your public key.
Encrypt the symmetric key using the recipient's public key.
Wipe the one-time secret key, the plaintext log, and any other intermediate value from memory.
Send the ciphertext, the public valuegy and optionally the signature.
Doing the steps manually may have a performance benefit if you decide that it's ok to encrypt multiple log messages with the same symmetric key. This has a performance benefit, because asymmetric operations are slower than symmetric operations. There is no long-term security drawback to doing this. The only security drawback is a short-term one: all logs from the current symmetric key can still be decrypted. If you decide, for example, that it's ok if an attacker who breaches your system can read the last minute's logs, then you can renew the symmetric key every minute.
There are many operating system and programs that hash passwords for authentication.
Even though they can encrypt the password in many different ways and save it
why do they save the hash of them?
Is the only reason to that question that encrypting them may cause in breaking and decrypting them or there are other reasons?
Thanks for answering in advance
User credentials (≈passwords) are among the most valuable assets stored in an application. They are a prime target for attackers, and as a developer, you want to protect them the best you can.
The principle of defense in depth (and common sense) indicates that the more layers of protection you can put around something, the more secure it will be. So as you also mentioned, the purpose of hashing passwords is that even if there is a breach, an attacker still can't get hold of actual user credentials.
The problem with encryption is always key management. If passwords were stored encrypted, they would need to be decrypted (or the received password encrypted with the same key) to be able to verify a password. For this, the application would need to have access to the key. But that negates the purpose of encryption, an attacker would also have access to the key in case of a breach. (Public key cryptography could make it somewhat more difficult, but essentially the same problem of key management would still persist.)
So in short, only storing salted hashes with an algorithm that is slow enough to prevent brute-force attacks (like PBKDF2 or Bcrypt) is both the simplest and the most secure. (Also note that plain salted hashes are not good enough anymore.)
Think of the need: You define your new password, and then every time you log-in the entered password is hashed and checked against the stored value. This is the simplest and most secure policy to handle this (since no one will be able to re-construct your password from the stored value). Moreover, imagine that you use the same password in several systems. If Windows would enable (regardless how hard it would be) to re-construct your password from what is stored in a Windows system, there would be (quite many) people that would blame Microsoft for security breach on other system (which could derivate into legal actions).
To summarize, simplicity and commercially logical approach.
Well, actually it's for security reason.
Hash functions are usually not revertibles, so even if someone finds out the hashes it would be really difficult for him to find which password generated that hash value.
Obviously you can try with a dictionary attack or a brute force one, trying to find out the password which generated the hash but it could take a very long time.
Consider that you have a Database with user and their passowrd, take note that a lot of people use the very same password everywhere.
Now immagine if a cracker manages to crack into your DB and finds all the password written clearly. That would be a disaster.
To start, I am trying to encrypt very sensitive information on a public website. Users will be able to update their information, Administrators will need access to this information. I am worried that if the encrypted data is some how compromised, then everyone's information would be as well due to them all using the same salt and key.
So I know using a salt, and key is always preferred. But as mentioned above if they reverse engineer the encrypted data, what use it is.
My solution, is to have the key and salt stored in a DB, with many rows and columns, any of which can be used for the salt or key. I would have an algorithm that will use "something" fixed in the users account that will be used to figure out which salt and key to use. This way statistically speaking no 2 years will have same combo of salt and key.
Is this over kill, or good?
I question the value of this second database that holds keys and salts. Consider:
The "something" in the user's data that identifies the salt and key will necessarily have to be encrypted differently from the rest of the user's data. Otherwise, you wouldn't be able to get it without first already having it.
Statistical analysis of the encrypted user data would almost certainly discover that the "something" is encrypted differently. That will be like waving a red flag at a bull, and an attacker will concentrate on figuring out why that's different.
You can assume that if an attacker can get the database of encrypted user information, he can also get the database of salts and keys.
Given that, there are two possible scenarios:
The encryption of the "something" that identifies the key and salt is unbreakable. That is, it's so good that the attacker's best efforts fail to reveal the connection between that "something" and the key/salt database.
The attacker discovers the encryption of the "something," and therefore is able to decrypt your sensitive data.
If #1 is the case, then you probably should use that encryption algorithm for all of your user data. Why do something in two steps when you can do it just as effectively in one?
If #2 is the case, then all the work you've done just put up a little bump in the road for the attacker.
So, short answer: what you propose looks like either unnecessary work or ineffective road blocking. In either case, it looks to me like a whole lot of work and added complexity for no appreciable gain.
That said, I could have misinterpreted your brief description. If so, please correct me.
SecOps people disclaimer: I'm an informed amateur at this stuff. It's possible I'm misunderstanding something. If so, please advise :)
I'm doing some work with the Keychain/KeychainItemWrapper in my app. I want to store a user's password in a secure manner, and the Keychain seems like the way to do it.
However, I'm a little confused. I thought basic password security (modelled after unix crypt()) went something like this:
Encrypt and store user specified password, using an encryption algorithm that will give the same results every time (given the same salt)
At a later date, when the user enters their password, encrypt that too
Compare the two encrypted strings. Are they equal? The passwords must be the same
However, it appears that using KeychainItemWrapper (and maybe the entire Keychain API?) is meant towards giving the password as plain text back to the program. However, isn't that insecure? The unencrypted password is just sitting around in memory, waiting for someone to read it, right?
The question: What is the best pattern for storing passwords in Keychain, given my above worries about security and the fact that my program does not actually need to know the encrypted value? Or are my fears unfounded and I should stop worrying and learn to love the (unencrypted) password Keychain gives me?
You're confusing two concepts (hashing vs encryption).
When you need to verify that someone knows a password, you hash it (w/ salt) and store that hash. Then, when someone tries to authenticate as that user, you ask them for the password, hash it with the same salt, and compare with the stored value. This is ideal as hashing is a one-way/non-reversible function. So, if someone accesses your credential store, they gain nothing as they would need to crack that hash (and modern algorithms like crypt/bcrypt are specifically designed to make brute force cracking very difficult).
However, if you need to be able to actually recover the plaintext password, you will encrypt the password instead of hashing it. Unlike cryptographic hashing, encryption is reversible, assuming you know the key.
The Keychain encrypts whatever you store in it with the assumption that you'll need to recover the actual data at some point (for example, if I put my Facebook password in a Keychain, it would be able to decrypt it and supply the actual password, when I need to use it to access Facebook). Keychain is designed to store those secret values encrypted, so that when you need to actually recover the original value you can.
I apologize in advance for the incoming Wall-O-Text. This is (at least, to me) a fairly complex issue that I've put quite a bit of thought into. You can read my question and also see a test implementation in Ruby (very hastily built, not database-backed, and probably very ugly) at this GitHub Gist if you are so inclined.
Introduction
Imagine one was required to create a web-based password management system
(over SSL! :) with the following requirements:
Individual users sign in to the system using their own unique pass
phrase.
This pass phrase should be enough to allow the user to use the system
effectively (e.g. from a smartphone, etc.)--the point being that they
should not have to keep a key file with them.
Users can store arbitrary-length bits of data in the system ("entries").
Entries are encrypted in the database in such a way that there is not
enough information in the database or application alone to read the
encrypted entries.
Users should be able to "share" entries with other users of the system
so that the other user(s) can read the contents of the entry.
I'm no expert in cryptography. After thinking about it for a while, I came up
with the following. My question is: is this implementation secure? Am I
missing something? If so, is the above spec even implementable? Or is this
overkill?
Database
The database is set up as such:
+------------------------------------------------------------------------------+
| users |
+---------+--------------+--------------+---------------+----------------------+
| salt | pub_key | enc_priv_key | priv_key_hmac | |
+---------+--------------+--------------+---------------+----------------------+
| entries |
+---------+--------------+--------------+---------------+----------+-----------+
| user_id | parent_entry | enc_sym_key | sym_key_sig | enc_data | data_hmac |
+---------+--------------+--------------+---------------+----------+-----------+
Basic Use Cases
Let's imagine two users of the system, Alice and Bob.
Bob signs up for the site:
Bob enters a password. This password is sent to the server (but not
stored).
The server generates a random salt and stores it in the salt field.
The server generates the SHA-256 hash of Bob's password and salt.
The server generates an RSA key pair. The public key is stored as plain
text in the pub_key field. The private key is encrypted via AES-256
using the hash generated from Bob's password and salt as the key and
stored in the enc_priv_key field.
The server generates a hash-based message authentication code for Bob's
private key using Bob's password and salt as the key and stores this in
the priv_key_hmac field.
Bob stores an entry in the system:
Bob enters some data to be stored as an entry along with his password.
This data is sent to the server.
The server generates a key to be used as a key for AES-256 encryption.
The server uses this key to encrypt the data and stores the result in
the enc_data field.
The server generates a hash-based message authentication code for the
data using the generated key and stores this in the data_hmac field.
The symmetric key used to encrypt the data is encrypted with Bob's public
key and stored in the enc_sym_key field.
The server uses Bob's private key to generate a signature for the
symmetric key.
Bob retrieves his stored entry:
Bob enters his password and the ID of the entry to retrieve.
The server generates the SHA-256 hash of Bob's password and salt.
Bob's encrypted private key is decrypted via AES-256 encryption using the
hash.
The server verifies that Bob's encrypted private key has not been
tampered with by checking the HMAC in priv_key_hmac.
The server decrypts the symmetric key stored in the enc_sym_key field
using Bob's private key.
The server verifies that the encrypted symmetric key has not been tampered
with by verifying the signature in sym_key_sign using Bob's public key.
The server decrypts the data using the symmetric key.
The server verifies that the encrypted data has not been tampered with
by verifying the HMAC stored in the data_hmac field.
The server returns the decrypted data to Bob.
Bob shares an entry with Alice:
Bob wants Alice to have access to an entry he owns. He enters his
password and the ID of the entry to share.
The data for the entry is decrypted using the method in "Bob retrieves
his stored entry."
A new entry is created for Alice in the same fashion as in "Bob stores
an entry in the system," with the following exceptions:
The entry's parent_entry is set to Bob's entry.
The signature for the symmetric key is calculated using Bob's private
key (since Alice's private key is not available to Bob).
When Alice accesses this new entry, the existence of a non-null
parent_entry causes the system to use Bob's public key to verify
the signature (since his private key was used to create it).
Bob changes the data in his shared entry:
Bob decides to change the data in the entry he shared with Alice. Bob
indicates the entry ID to modify and the new data it should contain.
The system overwrites the data created in "Bob stores an entry in the
system."
The system finds every entry with a parent_entry equal to the entry
that was just modified, and for each one overwrites the data created in
"Bob shares an entry with Alice."
Analysis
Advantages:
It is impossible to decrypt any data from the database without the
password of the user that owns the data, as the private key necessary to
decrypt the data is encrypted with the user's password, and that password
(and it's hash) is not stored in the database.
If a user wants to change their password, only their encrypted private
key needs to be regenerated (decrypt the private key with the old
password/hash, then re-encrypt it with the new password/hash).
Shared entries are stored as actual separate records in the database,
so there is no need to share a key between multiple users/groups of users.
Disadvantages/Problems (that I can think of):
If a shared entry is modified, the system must re-encrypt every child
entry; with a large number of users sharing data, this could potentially
be computationally expensive.
Shared entries depend on the parent user's public key for signature
verification. If the user is deleted, or their key changes, the signatures
are invalid.
Repeated from the introduction: my question is: is this implementation
secure? Am I missing something? If so, is the above spec even implementable?
Or is this overkill?
Thanks for sticking it out this long. I'm interested in your opinions! Am I on the right track, or a complete moron? YOU DECIDE! :)
No IV storage? I guess you could use AES-256-ECB, but that only lets users store 32 byte passwords, and you need to make sure that the generated private key is only ever used for one encryption. (Your current design seems safe in this respect, but if you want to allow passwords longer than 32 bytes, or ever think of making this key do double-duty, you'll need to store an IV for every encryption with it.)
I don't see the security value of priv_key_hmac and data_hmac; if either the private key or the encrypted data has been tampered with, then garbage output will result from decrypting with the private key or the symmetric key. Bob will surely be suspicious when he can't figure out how to type the BEL character. :) (Will humans ever see the output? A human will likely realize the returned password is incorrect without needing to be told. A computer couldn't tell the difference, so if automated systems will ever use the resulting passwords, then sure, keep the fields.)
There is no mechanism for "I forgot my password". Make sure your users know that there is no recovering their data if they forget their password. Users are coddled these days, and might expect to be coddled with your service too.
I see no mechanism for users to specify which entry Bob wants decrypted. You should store a name, or, as ssh(1) does in known_hosts, a hashed version of a name, for each entry. Storing a name directly would remove an SHA-256 operation, but a database compromise that reports the cleartext names of services that a user has accounts with might be every bit as damaging. (Perhaps an online escort service, or off-shore bank, or fight club.)
You don't actually need to duplicate anything other than enc_sym_key when you share an entry with Alice - since the symmetric key is never re-used for more than one entry, you only need one copy of the encrypted data.
Why not use certificates for sharing data between users? The use PKCS#12 certificates for holding the PEM and Private keys of users and the PEM per user or per site can sign and encrypt for data verification and security.
A scenario to illustrate.
Bob wants to share with Alice without Eve reading.
Alice gives Bob her public key. Bob adds Alice's public key to his keychain of trusted users. Bob then uses Alice's public key to encrypt a message while using his own PEM to sign the data. Of course this scenario requires that Alice already have a copy of Bob's public key to perform verification of the signature but you get the idea.
Also, why store a salt or iv? Both of these being stored along with at rest data will be accessible in the event of a db compromise.
Best practices...
Use a keyring for each user account for storage of others public keys/PEM certificates
Only use public key encryption for sharing information between accounts
Encrypt data with the users private key that is not to be shared between accounts
Do NOT use AES, RSA or any other reversible encryption for password storage
User specific salts should be used to further enhance hashing algorithm for password and should NOT be stored
Use of AES using a site wide password COULD be used for storage of at rest data to further improve security (but you would run into the problem you have outlined in the CONS section)