Security II

Cryptography Basics

The goal of cryptography is to take an input and produce a difficult to inverse output. In the process, we aim to achieve confidentiality, integrity, non-repudiation, and authentication.

Confidentiality - only the person for whom the information is intended can read it

Integrity - data is not tampered with during transmission

Non-repudiation - the sender can't deny intention of sending data at a later stage

Authentication - the identity of the sender is confirmed

Terminology

hash function
hash
key
collision
salt
digest
encryption
decryption
cipher
plaintext
ciphertext
nonce

Keyless Cryptography (Hash Function)

We don’t use any keys here, just a collision-resistant one-way hash function that takes in a message and produces a single deterministic fixed-size output.

Ex - BLAKE2, MD5, SHA256, Bcrypt, Scrypt etc.

Uses - For verifying file integrity and hashing passwords for storage.

Password Hashing

Always hash passwords when storing them in a database so that they can’t be read by anyone dealing with the code or the database. We check for password validity later by hashing using the same hash function and matching it with hash value stored in database.

Password hashing hash functions like bcrpyt and scrypt are designed to be slow by deliberately making it use large amount of memory.

We couldn’t have used SHA-2 or SHA-3 family for hashing passwords they are very fast and we don’t want attackers to be fast in performing a Dictionary attack and guessing our password.

It also uses a random salt for hashing which is a random string that is appended to the password string and then hash function is applied over the composite string. We can have diff salts for diff users.

scrpyt is in theory faster than bcrypt and is used in some cryptocurrencies. bcrypt, on the other hand is old reliable.

SHA Family

Secure Hash Algorithms (SHA) are a family of heavily standardized cryptographic hash functions.

  • SHA-0: published in 1993, withdrawn shortly after due to an undisclosed “significant flaw”
  • SHA-1: considered insecure since 2010
  • SHA-2: a set of algorithms like SHA-256, SHA-384, SHA-512, etc. They are is still secure and widely used
  • SHA-3: a set of algorithms; very secure and fast

MD5

It has been proven to be highly collision prone long back (in 1996) and it is even possible to come up with collisions in a few seconds.

It still continues to be used widely today. Don’t use it anywhere. Period.

Keyless Two-Way Hash?

Doesn’t make sense lol. It will basically be encoding.

Symmetric Cryptography

Also known as private-key or secret-key cryptography. It uses only one key that is used for both encryption and decryption (so its two-way) and the key must be transmitted over a trusted channel.

Ex - HMAC, DES, AES, etc.

Uses - For checking file integrity and hashing passwords for storage. It can be used both for authentication (HMAC) and encryption.

Drawback - The biggest problem being how to find a secure network to share private key over, because no network can be secure enough tbh.

Asymmetric Cryptography

Also known as public-key cryptography. The sender uses two keys here - one private and one public. The keys are such that the message encrypted by public key can be decoded only by the corresponding private key.

Public key - can be distributed publically

Private key - secret

We can distribute the public key and people can encrypt messages with it and send to us that we decrypt using the private key.

In some systems like RSA, the private key can be used to encrypt and then public key can be used to decrypt. This is often done to check digital signatures (since they’re signed by private key).

Ex - RSA, PGP, ECDH etc.

Shared Secret Key Agreement (Key Exchange)

Public-key cryptography is slower than private-key cryptography. To make it better we can generate a single shared key.

Sender sends their public key to Receiver and vice-versa. They both combine their private key with other’s public key and end up with a exactly the same key that they can use to encrypt and decrypt.

Both the parties have to agree on a common generator (g) beforehand and it is changed for every connection. Also, in the above image, the operation is not exponentiation as the notation implies and it should be theoretically impossible to separate x and g from g^x.

Ex - Diffie-Hellman key exchange. Two variants - DH and ECDH (Elliptic-curve Diffie-Hellman)

Reference

Digital Signatures

Sign using private key, verify using public key.

We often share items (aka message) we want to sign unencrypted (plain-text or base64) and include digital signature with it for verification by others.

We create a canonical string of all items we want to sign, then we calc its hash and sign with private key to generate a digital signature.

Send over message + signature and a receivers who already have a copy of the public key can verify that the message was signed by its corresponding private key by hashing the message again and then verify using public key. If the message has been tampered with, the hash won’t calc the exact same again.

hash = SHA256(canonicalString)
signature = ECDSA_sign(privateKey, hash)

---

Verify(publicKey, hash, signature)

Ex - RSA, EdDSA

Uses - Asymmetric JWT, TLS Certificates, Pre-signed URLs in S3 and CDN, etc.

Be aware that it isn’t exactly encryption and decryption in digital signatures but rather a mathematical operation that can sign and verify without actually encryption/decryption.

Encoding & Compression

Often keyless and always designed to be reversible by nature and not cryptographic as the output doesn’t need to be hidden from a third party.

Encoding - Base64

Compression - gzip

Ex - Videos are often encoded and compresssed, but not encrypted.

References