© Rakhesh Sasidharan

Posted: December 29th, 2014  Tags: asymmetric encryption, dsa, ecc, ecdsa, elliptic curves, encryption, public key encryption, rsa, symmetric encryption  Category: Asides, Infrastructure  § Good talk about crypto by Colin Percival. The talk is slightly less than 2 hours long (but that could be due to questions at the end – I don’t know, I am only 50 mins into it so far).
A good talk. With a lot of humor in between. Only reason I decided to watch this video is because I follow Colin’s blog (or rather I try to follow – there’s a lot of crypto stuff in there that goes over my head). He was the FreeBSD Security officer when I was into FreeBSD and that’s how I started following his blog and became aware of him. Am glad I am watching it though, it clarifies a lot of things I was vaguely aware of without going into too much details. I don’t think I’ll ever be doing programming that involves crypto, but it’s good to know and is an area of interest.
Posted: December 1st, 2014  Tags: aes, blowfish, dsa, ecc, ecdsa, elliptic curves, encryption, https, rc4, sha, ssh, twofish  Category: Infrastructure  § I thought I should make a running post on cryptography ciphers (algorithms) and such. For instance, in my previous post I mentioned AES, EDH, etc. but that’s just the tip of the iceberg as there are so many algorithms each suited for different tasks. As I come across these I’ll add them to this post as a quick reference to myself.
Symmetric key algorithms (Private key cryptography)
Both parties share a private key (kept secret between them).
Symmetric key algorithms are what you use for encryption. F0r example: encryption of traffic between a server and client, as well as encryption of data on a disk.
 DES – Data Encryption Standard – designed at IBM
 DES is a standard. The actual algorithm used is also called DES or sometimes DEA (Digital Encryption Algorithm).
 DES is now considered insecure (mainly due to a small key size of 56bits).
 Triple DES (3DES) applies the DES algorithm thrice and thus has better practical security. It has 3 keys of 56bits each (applied to each pass of DES/ DEA).
 DESX is another variant.
 DES is a block cipher.
 IDEA – International Data Encryption Algorithm
 Considered to be a good and secure algorithm.
 Patented but free for noncommercial use.
 IDEA is a block cipher.
 AES – Advanced Encryption Standard – is the successor to DES
 AES is based on the Rijndael cipher. There was a competition to choose the cipher that will become the AES. The Rijndael cipher won the competition. However, there are some differences between Rijndael and its implementation in AES.
 Most CPUs now include hardware AES support making it very fast.
 Supported by TrueCrypt, SSH.
 AES and Rjindael are block ciphers.
 AES can operate in many modes.
 AESGCM (AES operating in Galois/Counter Mode (GCM)) is preferred (check this blog post too). It is fast and secure and works similar to stream ciphers. Can achieve high speeds on low hardware too. Only supported on TLS 1.2 and above.
 AESCBC is what older clients commonly use. AESCBC mode is susceptible to attacks such as Lucky13 and BEAST.
 See this answer for an excellent overview of the various modes.
 Blowfish – designed by Bruce Schneier as an alternative to DES; no issues so far, but can be attacked if the key is weak, better to use Twofish or Threefish.
 Patent free. In public domain.
 Supported by SSH.
 Much faster than DES and IDEA but not as fast as RC4.
 Uses variable size keys of 32 to 448 bits. Considered secure. Designed for fast CPUs, now slower / old er CPUs.
 Blowfish is a block cipher.
 Twofish – designed by Bruce Schneier and others as a successor to Blowfish
 Was one of the finalists in the AES competition
 Most CPUs now include hardware AES support making it very fast than Twofish.
 Patent free. In public domain.
 Uses keys of size 128, 192, or 256 bits. Designed to be more flexible than Blowfish (in terms of hardware requirements).
 Supported by TrueCrypt, SSH.
 Twofish is a block cipher.
 Threefish – designed by Bruce Schneier and others
 Serpent – designed by Ross Anderson, Eli Biham, and Lars Knudsen
 Was one of the finalists in the AES competition
 Patent free. In public domain.
 Has a more conservative approach to security than other AES competition finalists.
 Supported by TrueCrypt.
 Serpent is a block cipher.
 MARS – designed by Don Coppersmith (who was involved in DES) and others at IBM
 Was one of the finalists in the AES competition
 RC6 – Rivest Cipher 6 or Ron’s Code 6 – designed by Ron Rivest and others
 Was one of the finalists in the AES competition
 Proprietary algorithm. Patented by RSA Security.
 RC5 is a predecessor of RC6. Other siblings include RC2 and RC4.
 More on RC5 and RC6 at this RSA link.
 RC5 and RC6 are block ciphers.
 RC4 – Rivest Cipher 4, or Ron’s Code 4 – also known as ARC4 or ARCFOUR (Alleged RC4).
 Used to be an unpatented tradesecret for RSA Data Security Inc (RSADSI). Then someone posted the source code online, anonymously, and it got into the public domain.
 Very fast, but less studied than other algorithms.
 RC4 is a stream cipher. It’s the most widely used stream cipher.
Asymmetric key algorithms (Public key cryptography)
Each party has a private key (kept secret) and a public key (known to all). These are used in the following way:
 Public keys are used for encrypting, Private keys are used for decrypting.
 For example: to send something encrypted to a party use its public key and send the encrypted data. Since only that party has the corresponding private key, only that party can decrypt it. (No point encrypting it with your private key as anyone can then decrypt with your public key!)
 Private keys are used for signing, Public keys are used for verifying.
 For example: to digitally sign something, encrypt it with your private key (usually a hash is made and the hash encrypted). Anyone can decrypt this data (or decrypt the hash & data and perform a hash themselves to verify your hash and their hash match) and verify that since it was signed by your private key the data belongs to you.
These algorithms are usually used to digitally sign data and/ or exchange a secret key which can be used with a symmetric key algorithm to encrypt further data. They are often not used for encrypting the conversation either because they can’t (DSA, DiffieHellman) or because the yield is low and there are speed constraints (RSA). Most of these algorithms make use of hashing functions (see below) for internal purposes.
 RSA – short for the surnames of its designers Ron Rivest, Adi Shamir and Leonard Adleman
 Not used to encrypt data directly because of speed constraints and also because its yield is small (see this post for a good explanation; also this TechNet article).
 Usually RSA is used to share a secret key and then a symmetric key algorithm is used for the actual encryption.
 RSA can be used for digital signing but is slower. DSA (see below) is preferred. However, RSA signatures are faster to verify. To sign data a hash is made of it and the hash encrypted with the private key. (Note: RSA requires that a hash be made rather than encrypt the data itself).
 RSA does not require the use of any particular hash function.
 Public and Private keys are based on two large prime numbers which must be kept secret. RSA’s security is based on the fact that factorization of large integers is difficult. (The public and private keys are large integers which are derived from the two large prime numbers).
 PKCS#1 is a standard for implementing the RSA algorithm. The RSA algorithm can be attacked if certain criteria are met so the PKCS#1 defines things such that these criteria are not met. See this post for more info.
 Was originally patented by the RSA but has since (circa 2000) expired.
 SSH v1 only uses RSA keys (for identity verification).
 DSA – Digital Signature Algorithm – designed by the NSA as part of the Digital Signature Standard (DSS)
 Used for digital signing. Does not do encryption. (But implementations can do encryption using RSA or ElGamal encryption)
 DSA is fast at signing but slow at verifying.
 It mandates the use of SHA hashes when computing digital signatures.
 Unlike RSA which makes a hash of the data and then encrypts it to sign the message – and this data plus encrypted hash is what’s used to verify the signature – DSA has a different process. DSA generates a digital signature composed of two 160bit numbers directly from the private key and a hash of the data to be signed. The corresponding public key can be used to verify the signature. The verifying is slow.
 A note about speed: DSA is faster at signing, slow at verifying. RSA is faster at verifying, slow at signing. The significance of this is different from what you may think. Signing can be used to sign data, it can also be used for authentication. For instance, when using SSH you sign some data with your private key and send to the server. The server verifies the signature and if it succeeds you are authenticated. In such a situation it doesn’t matter that DSA verification is slow because it usually happens on a powerful server. DSA signing, which happens on a relatively slower computer/ phone/ tablet is a much faster process and so less intensive on the processor. In such a scenario DSA is preferred! Remember: where the slow/ fast activity occurs also matters.
 DSA can be used only for signing. So DSA has to use something like DiffieHellman to generate another key for encrypting the conversation.
 This is a good thing as it allows for Perfect Forward Secrecy (PFS).
 Forward Secrecy => the shared key used for encrypting conversation between two parties is not related to their public/ private key.
 Perfect Forward Secrecy => in addition to the above, the shared keys are generated for each conversation and are independent of each other.
 Also, because DSA can be used only for digital signatures and not encryption, it is usually not subject to export or import restrictions. Therefore it can be used more widely.
 Patented but made available royalty free.
 DSA’s security is based on the discrete logarithm problem.
 Section 3.9.1.2 of this article is worth a read. Since it’s designed by the NSA, DSA has some controversies.
 SSH v2 can use RSA or DSA keys (for identity verification). It prefers DSA because RSA used to be patent protected. But now that the patents have expired RSA is supported.
 RSA is supported by all versions of SSL/ TLS.
 DSA (and ECDSA) requires random numbers. If the random number generator is weak then the private key can be figured out from the traffic. See this blog post and RFC for good explanations. These StackExchange answers are worth a read too: 1, 2, and 3.
 ECDSA – Elliptic Curve DSA
 Variant of DSA that uses Elliptic Curve Cryptography (ECC).
 ECC is based on Elliptic Curves theory and solving the “Elliptic Curve Discrete Logarithm Problem (ECDLP)” problem which is considered very hard to break.
 ECC keys are better than RSA & DSA keys in that the algorithm is harder to break. So not only are ECC keys more future proof, you can also use smaller length keys (for instance a 256bit ECC key is as secure as a 3248bit RSA key).
 As with DSA it requires a good source of random numbers. If the source isn’t good then the private key can be leaked.
 Although the ECDLP is hard to solve, there are many attacks that can successfully break ECC if the curve chosen in the implementation if poor. For good ECC security one must use SafeCurves. For example Curve25519 by D.J. Bernstein.
 Used in Bitcoin and extensively in iOS for instance. Many web servers are adopting it too.
 ElGamal – designed by Taher ElGamal
 Used by GnuPG and recent versions of PGP
 Taher ElGamal also designed the ElGamal signature, of which the DSA is a variant. ElGamal signature is not widely used but DSA is.
 DiffieHellman (DH) – designed by Whitfield Diffie, Martin Hellman and Ralph Merkle
 Does not do encryption or signing. It is only used for arriving at a shared key. Unlike RSA where a shared key is chosen by one of the parties and sent to the other via encryption, here the shared key is generated as part of the conversation – neither parties get to choose the key.
 Here’s how it works in brief: (1) the two parties agree upon a large prime number and a smaller number in public, (2) each party then picks a secret number (the private key) for itself and calculates another number (the public key) based on this secret number, the prime number, and the smaller number, (3) the public keys are shared to each other and using each others public key, the prime number, and the small number, each party can calculate the (same) shared key. The beauty of the math involved in this algorithm is that even though a snooper knows the prime number, the small number, and the two public keys, it still cannot deduce the private keys or the shared key! The two parties publicly derive a shared key such that no one snooping on their conversation can derive the key themselves.
 Has two versions:
 a fixed/ static version (called “DH”) where all conversations use the same key,
 an ephemeral version (called “EDH” (Ephermeral DiffieHellman) or “DHE” (DiffieHellman Ephemeral)) where every conversation has a different key.
 Its security too is based on the discrete logarithm problem (like DSA).
 SSHv2 uses DH as its key exchange protocol.
Hashing functions
Hashing functions take input data and return a value (called a hash or digest). The input and message digest have a onetoone mapping, such that given an input you get a unique digest and even a small change to the input will result in a different digest. Hashes are one way functions – given an input you can easily create a digest, but given a digest it is practically impossible to generate the input that created it.
 MD2 – MessageDigest 2 – designed by Ron Rivest
 Is optimized for 8bit computers. Creates a digest of 128bits.
 No longer considered secure but is still in use in Public Key Infrastructure (PKI) certificates but is being phased out?
 MD4 – MessageDigest 4 – designed by Ron Rivest
 Creates a digest of 128bits.
 It is used to create NTLM password hashes in Windows NT, XP, Vista, and 7.
 MD4 is no longer recommended as there are attacks that can generate collisions (i.e. the same hash for different input).
 MD5 – MessageDigest 5 – designed by Ron Rivest to replace MD4

 As with MD4 it creates a digest of 128bits.
 MD5 too is no longer recommended as vulnerabilities have been found in it and actively exploited.
 MD6 – MessageDigest 6 – designed by Ron Rivest and others.
For more on MD2, MD4, and MD5 see this link.
 SHA 0 (a.k.a. SHA) – Secure Hash Algorithm 0 – designed by the NSA
 Creates a 160bit hash.
 Not widely used.
 SHA1 – Secure Hash Algorithm 1 – designed by the NSA
 Creates a 160bit hash.
 Is very similar to SHA0 but corrects many alleged weaknesses. Is related to MD4 too.
 Is very widely used but is not recommended as there are theoretical attacks on it that could become practical as technology improves.
 SHA2 is the new recommendation. Microsoft and Google will stop accepting certificates with SHA1 hashes, for instance, from January 2017.
 SHA2 – Secure Hash Algorithm 2 – designed by the NSA.
 Significantly different from SHA1.
 Patented. But royalty free.
 SHA2 defines a family of hash functions.
 Creates hashes of 224, 256, 384 or 512 bits. These variants are called SHA224, SHA256, SHA384, SHA512, SHA512/224, and SHA512/256.
 SHA256 and SHA512 new hash functions. They are similar to each other. These are the popular functions of this family.
 SHA512 is supported by TrueCrypt.
 SHA256 is used by DKIM signing.
 SHA256 and SHA512 are recommended for DNSSEC.
 SHA224 and SHA384 are truncated versions of the above two.
 SHA512/224 and SHA512/256 are also truncated versions of the above two with some other differences.
 There are theoretical attacks against SHA2 but no practical ones.
 SHA3 – Secure Hash Algorithm 3 – winner of the NIST hash function competition
 Not meant to replace SHA2 currently.
 The actual algorithm name is Keccak.
Some more hash functions are:
 Whirlpool – designed by Vincent Rijmen (cocreator of AES) and Paulo S. L. M. Barreto.
 Patent free. In public domain.
 Creates a 512digest.
 Supported by TrueCrypt.
 RIPEMD – RACE Integrity Primitives Evaluation Message Digest
 Based on the design principles of MD4. Similar in performance to SHA1. Not widely used however.
 Was designed in a the open academic community and meant to be an alternative to the NSA designed SHA1 and SHA2.
 Creates 128bit hashes.
 There are many variants now: RIPEMD128 creates 128bit hashes (as the original RIPEMD hash), RIPEMD160 creates 160bit hashes, RIPEMD256 creates 256bit hashes, RIPEMD320 creates 320bit hashes.
Misc
 PEM (Privacy Enhanced Mail) is the preferred format for storing private keys, digital certificates (the public key), and trusted Certificate Authorities (CAs).
 Supports storing multiple certificates (e.g. a certificate chain).
 If a chain is stored, then first certificate is the server certificate, next is issuer certificate, and so on. Last one can be selfsigned or (of a root CA).
 The file begins with the line
BEGIN CERTIFICATE and ends with the line END CERTIFICATE . The data is in a text format.
 Private key files (i.e. private keys not stored in a keystore) must be in PKCS#5/PKCS#8 PEM format.
 DER (Distinguished Encoding Rules) is another format.
 Can only contain one certificate. The data is in a binary format.
 JKS (Java KeyStore) is the preferred format for key stores.
 P7B (PublicKey Cryptography Standards #7 (PKCS #7)) is a format for storing digital certificates (no private keys)
 Supports storing multiple certificates.
 More about PKCS at WikiPedia. PKCS#7 is used to sign and/ or encrypt messages under a PKI. Also to disseminate certificates.
 PFX/P12 (PublicKey Cryptography Standards #12 (PKCS #12)) is a format for storing private keys, digital certificates (the public key), and trusted CAs.
 PFX is a predecessor to PKCS#12.
 Usually protected with a passwordbased symmetric key.
 CER is a format for storing a single digital certificate (no private keys)
 Base64encoded or DERencoded X.509 certificates.
 SSL/ TLS are protocols that use the above
 SSL – Secure Sockets Layer; TLS – Transport Layer Security
 SSL has version 1.0 to 3.0. SSL version 3.1 became TLS 1.0. TLS has version 1.0 to 1.2. SSL and TLS are not interoperable (TLS 1.0 can have some of the newer features disabled, and hence security weakened, to make it interoperable with SSL 3.0)
 Used for authentication and encryption. Makes use of the ciphers above.
Links
Since writing this post I came across some links related to the topics above. Thought I’d add them to this post in case anyone else finds these useful:
