A working intro to Cryptography

1. Symmetric Keys

  1. Block Cipher
  2. Stream Cipher

1.1 Block Ciphers

Operate on blocks of data, typically 16 bytes (16个字母)

8bit = 1 byte

假如不够,就在末尾加.通常第一 byte 是0x80,其后是0x00.

Example:
ABCDABCDABCDAB
变为
ABCDABCDABCDAB\x80\x00\x00\x00

1.2 Block mode

  • cipher block chaining - CBC
  • counter - CTR
  • cipher feedback - CFB
  • electronic codebook - ECB (Extremely Insecure)

常用的是cipher block chaining ,俗称CBC.

CBC是XOR('前一个 block 的 ciphertext' + 'current block'). 但是对于第一个 block, 它是最前的, 没有'前一个block 的 ciphertext',于是需要一个 IV(Initialization Vector). 由此可见, IV 应该是随机的, 不嫩被估计得到.

Key Length

AES:

  • 128bit (16-byte)
  • 192bit (24-byte)
  • 256bit (32-byte)

1.3 Confidentiality vs Integrity & Authenticity

AES 能保证 confidentiality.
HMAC 才能保证 Integrity. 常说的 hash, digest. 如 SHA

Message tag 为 HMAC-SHA-384, 就表示它是一个48byte 的 digest.

1.4 Key Generation

生成 key常用的算法是 PBKDF2. 该算法需要三个参数, passphrase, salt, 迭代的次数(推荐16384次).

2. Public-Key cryptography

常用的有, Elliptic Curve Cryptography (ECC).

Elliptic Curve Cryptography:

  • NIST P256 (相等于 AES-128 Key)
  • NIST P384 (相等于 AES-192 Key)
  • NIST P521 (相等于 AES-256 Key)

Alternatively

  1. Curve25519 for Key exchange
  2. ED25519 for digital signatures

With elliptic curves, we have two types of operations that we generally want to accomplish:

  • Digital signatures are the public key equivalent of Message Authentication Codes (MAC). Alice signs a document using her private key, and users verify the signature against her public key.
  • Encryption with elliptic curves is done by performing a key exchange. Alice uses a function called elliptic curve Diffie-Hellman (ECDH) to generate a shared key to encrypt messages to Bob.

2.1 Signing Messages

Normally when we do signatures, we compute the hash of the message and sign that.

2.2 Encryption

Using elliptic curves, we encrypt using a function that generates a symmetric key using a public and private key pair. The function that we use, ECDH (elliptic curve Diffie-Hellman), works such that:

ECDH(alice_pub, bob_priv) == ECDH(bob_pub, alice_priv)  

ECDH with Alice's private key and Bob's public key returns the same shared key as ECDH with Bob's private key and Alice's public key.

2.3 Key Exchange

Bob 如何知道 key 属于 Alice?

有两种做法: centralized and decentralized.

1. Centralized

常见的是 CA, SSL Certificate.

A root certificate authority (CA) signs intermediary CA keys, which then sign user keys.

2. Decentralized

常见的是 PGP.

  1. Carol 想与 Bob 交流, 于是将其 public key 发给 Bob.
  2. Bob 看 Carol 的 key 是否被其他人所 sign.
  3. 假如 Bob 确认 Alice 的 Key 属于 Alice, 并且 Bob 信任 Alice, 一旦 Carol 的 Key 被 Alice 所 Sign, Bob 就有理由相信那 Key 是属于 Carol 了

3. Bonus

3.1 Salt

Salt 的作用是, 令到同一个 common 的 password, 在hash 后得到值不一样.

不同的人使用不同的 salt,于是尽管他们使用一样的密码, hash 后得到的值都不一样, 由此提高黑客在获取 DB 后,利用彩虹表获得原始密码的难度.

Hash(salt||password)  

Salt 可明文存于 DB 中, next to hash 值.

Slow down the attacker

Hash(Hash(Hash(...(Hash(salt||password))...)))  

Ref

Read A Working Intro to Cryptography | Leanpub