Really Applied Cryptography

Nick Galbreath

NYCBUG 05-SEP-2007

Last Revision: 08-SEPT-2007

If you are using Opera, go into fullscreen mode (F11 or Alt-F11), and use [fn] PageUp/PageDown.

But first, a word from our sponsors

Oh, you laugh? I made 59 CENTS last month. Actually I'm just curious to see what the big brains at Google come up with for an advertisement.

Currently Available Information

Lots of information on algorithms, algorithm implementation, and communication protocols (i.e. SSL).

Lots of bable on how secure a particular algorithm is

Not so much information on how to use it, and when to use it to get work done

My first attempt "Cryptography for Internet and Database Applications" -- still too long.

Scope: Web-Dev Crypto

This focuses on application-level cryptography aka "web-dev crypto."

Crypto Mini-Review

Not included today: random number generation

Applications

Hashes (non-cryptographic)

Hash Examples

These are not cryptographic

Cryptographic Hashes

Fingerprints

Another way of thinking about cryptographic hashes is to compare them to fingerprints.

Compare these principals to the "dumb" non-cryptographic hashes.

Crypto Hash: MD5

Do not use for new applications

Crypto Hash: SHA-1

Use this

Everything else

From home page of mhash:

At the time of writing this, the library supports the algorithms: SHA1, SHA160, SHA192, SHA224, SHA384, SHA512, HAVAL128, HAVAL160, HAVAL192, HAVAL224, HAVAL256, RIPEMD128, RIPEMD256, RIPEMD320, MD4, MD5, TIGER, TIGER128, TIGER160, ALDER32, CRC32, CRC32b, WHIRLPOOL, GOST, SNEFRU128, SNEFRU256

BULLSHIT

you only need SHA-1

Command Line Fun

$ echo "this is a message" | md5
1fb0076c4f2eaa1c788679154c51aa89

$ echo "this is a message" | openssl dgst -md5
1fb0076c4f2eaa1c788679154c51aa89

$ echo "this is a message" | openssl dgst -sha1
54b7b1ec23aae1997dfb8d6fb0d94cac389aa2a0
      

Secret Key Cryptography

HMACS

HMAC Implementations

Secret Key Ciphers

Ciphers

Three you need to know about

DES, Triple DES

Blowfish

AES

Cipher: DES, Triple-DES

DO NOT USE

Cipher: Blowfish

OK for New Projects, but...

Cipher: AES/Rijndael

YES Use this for new applications

Everything else

As usual there are dozens of other ciphers to use in any given library.

Ignore them all

Modes of Operation

Block ciphers work on a single block. Modes of operation deal with working with longer messages.

You have to specify the mode when using a crypto API

Only Two You Need to Know

ECB Mode

CBC Mode

9 of 10 times you want this.

Other Modes

IGNORE

Other Issues

Lot of special considerations. Most of these are handled by the crypto package

Public Key Ciphers

Two you need to know about

Not of a focus of this talk, but to be complete:

RSA

Elliptic Curve

PKC Implementations

Application: Communication

Application: Logging

Sometimes data/messages with sensitive information needs to be logged (e.g. suspect credit card fraud)

Application: Storing Passwords

I'll assume you know better than to store it as plaintext

Password Facts

Take 1: Encrypt it

Take 2: Hash it

Take 3: HMAC it

Take 4: HMAC + Salt

HMAC + Key Management

Sample Schema

CREATE TABLE password {
    uid INT NOT NULL PRIMARY KEY,
    salt INT NOT NULL,      /* could be char(X) too */
    hmac CHAR(48) NOT NULL, /* or so */
    kid INT NOT NULL DEFAULT 0, /* TBD */
};

New problem

You are stuck since you don't know the original password

Take 5: HMAC+Salt+PKC

Passwords are checked often, never read, and only rarely written to.

Take 6: The HMAC of the Hash

By storing the HMAC of the hash of the password (e.g. HMAC(SHA1(password) + salt you can allow for client login where the password is never sent in the clear. This is good if you don't allow SSL logins

Really not that complicated

I know "Take 6" sounds insane, but here's the pseudocode:

salt = random();
hex_encode(hmac(md5(password + salt), key)

in practice it's probably 5 lines of code

Application: Tamper Proof URLs/Cookies

Prevent:

tamper-proof urls

Allow:

Encode Recipe - Part 1

Encode Recipe - Part 2

IMPORTANT: notice how you are hmac-ing the meta data too!

Decode Recipe

Hints

UNIT TEST MANIA

Performance Impact

Summary: Zero

I've added tamper-proof cookies/url on every page to two high volume websites (i.e. Alexa top 50) doing thousands of requests per second

Unnoticeable on CPU load graphs when code went out.

Cost is sub-millisecond

Use It

More fun

Good Books

Change Log