Hey! I'm David, a security engineer at the Blockchain team of Facebook, previously a security consultant for the Cryptography Services of NCC Group. I'm also the author of the Real World Cryptography book. This is my blog about cryptography and security and other related topics that I find interesting.

# What are x509 certificates? RFC? ASN.1? DER? posted April 2015

## RFC

So, RFC means Request For Comments and they are a bunch of text files that describe different protocols. If you want to understand how SSL, TLS (the new SSL) and x509 certificates (the certificates used for SSL and TLS) all work, for example you want to code your own OpenSSL, then you will have to read the corresponding RFC for TLS: rfc5280 for x509 certificates and rfc5246 for the last version of TLS (1.2).

## x509

x509 is the name for certificates which are defined for:

informal internet electronic mail, IPsec, and WWW applications

There used to be a version 1, and then a version 2. But now we use the version 3. Reading the corresponding RFC you will be able to read such structures:

Certificate  ::=  SEQUENCE  {
tbsCertificate       TBSCertificate,
signatureAlgorithm   AlgorithmIdentifier,
signatureValue       BIT STRING  }

those are ASN.1 structures. This is actually what a certificate should look like, it's a SEQUENCE of objects.

• The first object contains everything of interest that will be signed, that's why we call it a To Be Signed Certificate
• The second object contains the type of signature the CA used to sign this certificate (ex: sha256)
• The last object is not an object, its just some bits that correspond to the signature of the TBSCertificate after it has been encoded with DER

## ASN.1

It looks small, but each object has some depth to it.

The TBSCertificate is the biggest one, containing a bunch of information about the client, the CA, the publickey of the client, etc...

TBSCertificate  ::=  SEQUENCE  {
version         [0]  EXPLICIT Version DEFAULT v1,
serialNumber         CertificateSerialNumber,
signature            AlgorithmIdentifier,
issuer               Name,
validity             Validity,
subject              Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
issuerUniqueID  [1]  IMPLICIT UniqueIdentifier OPTIONAL,
-- If present, version MUST be v2 or v3
subjectUniqueID [2]  IMPLICIT UniqueIdentifier OPTIONAL,
-- If present, version MUST be v2 or v3
extensions      [3]  EXPLICIT Extensions OPTIONAL
-- If present, version MUST be v3
}

## DER

A certificate is of course not sent like this. We use DER to encode this in a binary format.

Every fieldname is ignored, meaning that if we don't know how the certificate was formed, it will be impossible for us to understand what each value means.

Every value is encoded as a TLV triplet: [TAG, LENGTH, VALUE]

For example you can check the GITHUB certificate here

On the right is the hexdump of the DER encoded certificate, on the left is its translation in ASN.1 format.

As you can see, without the RFC near by we don't really know what each value corresponds to. For completeness here's the same certificate parsed by openssl x509 command tool:

## How to read the DER encoded certificate

So go back and check the hexdump of the GITHUB certificate, here is the beginning:

30 82 05 E0 30 82 04 C8 A0 03 02 01 02

As we saw in the RFC for x509 certificates, we start with a SEQUENCE.

Certificate  ::=  SEQUENCE  {

Microsoft made a documentation that explains pretty well how each ASN.1 TAG is encoded in DER, here's the page on SEQUENCE

30 82 05 E0

So 30 means SEQUENCE. Since we have a huge sequence (more than 127 bytes) we can't code the length on the one byte that follows:

If it is more than 127 bytes, bit 7 of the Length field is set to 1 and bits 6 through 0 specify the number of additional bytes used to identify the content length.

(in their documentation the least significant bit on the far right is bit zero)

So the following byte 82, converted in binary: 1000 0010, tells us that the length of the SEQUENCE will be written in the following 2 bytes 05 E0 (1504 bytes)

30 82 04 C8 A0 03 02 01 02

Another Sequence embedded in the first one, the TBSCertificate SEQUENCE

TBSCertificate  ::=  SEQUENCE  {
version         [0]  EXPLICIT Version DEFAULT v1,

The first value should be the version of the certificate:

A0 03

Now this is a different kind of TAG, there are 4 classes of TAGs in ASN.1: UNIVERSAL, APPICATION, PRIVATE, and context-specific. Most of what we use are UNIVERSAL tags, they can be understood by any application that knows ASN.1. The A0 is the [0] (and the following 03 is the length). [0] is a context specific TAG and is used as an index when you have a series of object. The github certificate is a good example of this, because you can see that the next index used is [3] the extensions object:

TBSCertificate  ::=  SEQUENCE  {
version         [0]  EXPLICIT Version DEFAULT v1,
serialNumber         CertificateSerialNumber,
signature            AlgorithmIdentifier,
issuer               Name,
validity             Validity,
subject              Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
issuerUniqueID  [1]  IMPLICIT UniqueIdentifier OPTIONAL,
-- If present, version MUST be v2 or v3
subjectUniqueID [2]  IMPLICIT UniqueIdentifier OPTIONAL,
-- If present, version MUST be v2 or v3
extensions      [3]  EXPLICIT Extensions OPTIONAL
-- If present, version MUST be v3
}

Since those obects are all optionals, skipping some without properly indexing them would have caused trouble parsing the certificate.

Following next is:

02 01 02

  _______ tag:      integer
|   ____ length: 1 byte
|  |   _ value:  2
|  |  |
|  |  |
v  v  v
02 01 02 

The rest is pretty straight forward except for IOD: Object Identifier.

## Object Identifiers

They are basically strings of integers that reads from left to right like a tree.

So in our Github's cert example, we can see the first IOD is 1.2.840.113549.1.1.11 and it is supposed to represent the signature algorithm.

So go to http://www.alvestrand.no/objectid/top.html and click on 1, and then 1.2, and then 1.2.840, etc... until you get down to the latest branch of our tree and you will end up on sha256WithRSAEncryption.

Here's a more detailed explanation on IOD and here's the microsoft doc on how to encode IOD in DER.

# Cryptography Services to audit Let's Encrypt posted April 2015

Like the audit of OpenSSL wasn't awesome enough, today we learned that we were going to audit Let's Encrypt this summer as well. Pretty exciting agenda for an internship!

https://letsencrypt.org/2015/04/14/ncc-group-audit.html

ISRG has engaged the NCC Group Crypto Services team to perform a security review of Let’s Encrypt’s certificate authority software, boulder, and the ACME protocol. NCC Group’s team was selected due to their strong reputation for cryptography expertise, which brought together Matasano Security, iSEC Partners, and Intrepidus Group.

comment on this story

# ASN.1 vs DER vs PEM vs x509 vs PKCS#7 vs .... posted April 2015

I was really confused about all those acronyms when I started digging into OpenSSL and RFCs. So here's a no bullshit quick intro to them.

# PKCS#7

Or Public-Key Crypto Standard number 7. It's just a guideline, set of rules, on how to send messages, sign messages, etc... There are a bunch of PKCS that tells you exactly how to do stuff using crypto. PKCS#7 is the one who tells you how to sign and encrypt messages using certificates. If you ever see "pkcs#7 padding", it just refers to the padding explained in pkcs#7.

# X509

In a lot of things in the world (I'm being very vague), we use certificates. For example each person can have a certificate, and each person's certificate can be signed by the government certificate. So if you want to verify that this person is really the person he pretends to be, you can check his certificate and check if the government signature on his certificate is valid.

TLS use x509 certificates to authenticate servers. If you go on https://www.facebook.com, you will first check their certificate, see who signed it, checked the signer's certificate, and on and on until you end up with a certificate you can trust. And then! And only then, you will encrypt your session.

So x509 certificates are just objects with the name of the server, the name of who signed his certificate, the signature, etc...

Example from wikipedia:

    Certificate
Version
Serial Number
Algorithm ID
Issuer
Validity
Not Before
Not After
Subject
Subject Public Key Info
Public Key Algorithm
Subject Public Key
Issuer Unique Identifier (optional)
Subject Unique Identifier (optional)
Extensions (optional)
...
Certificate Signature Algorithm
Certificate Signature

# ASN.1

So, how should we write our certificate in a computer format? There are a billion ways of formating a document and if we don't agree on one then we will never be able to ask a computer to parse a x509 certificate.

That's what ASN.1 is for, it tells you exactly how you should write your object/certificate

# DER

ASN.1 defines the abstract syntax of information but does not restrict the way the information is encoded. Various ASN.1 encoding rules provide the transfer syntax (a concrete representation) of the data values whose abstract syntax is described in ASN.1.

Now to encode our ASN.1 object we can use a bunch of different encodings specified in ASN.1, the most common one being used in TLS is DER

DER is a TLV kind of encoding, meaning you first write the Tag (for example, "serial number"), and then the Length of the following value, and then the Value (in our example, the serial number).

DER is also more than that:

DER is intended for situations when a unique encoding is needed, such as in cryptography, and ensures that a data structure that needs to be digitally signed produces a unique serialized representation.

So there is only one way to write a DER document, you can't re-order the elements.

And a made up example for an ASN.1 object:

OPERATION ::= CLASS
{
&operationCode INTEGER UNIQUE,

&InvocationParsType,

&ResponseParsAndResultType,

&ExceptionList ERROR OPTIONAL
}

And its DER encoding:

0110 0111 0010 110...

# Base64

Base64 is just a way of writing binary data in a string, so you can pass it to someone on facebook messenger for exemple

From the openssl Wiki:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
0000000000111111111122222222223333333333444444444455555555556666
0123456789012345678901234567890123456789012345678901234567890123

And if you see any equal sign =, it's for padding.

So if the first 6 bits of your file is '01' in base 10, then you will write that as B in plaintext. See an example if you still have no idea about what I'm talking about.

# PEM

A pem file is just two comments (that are very important) and the data in base64 in the middle. For example the pem file of an encrypted private key:

-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIS2qgprFqPxECAggA
MBQGCCqGSIb3DQMHBAgD1kGN4ZslJgSCBMi1xk9jhlPxP3FyaMIUq8QmckXCs3Sa
9g73NQbtqZwI+9X5OhpSg/2ALxlCCjbqvzgSu8gfFZ4yo+Xd8VucZDmDSpzZGDod
X0R+meOaudPTBxoSgCCM51poFgaqt4l6VlTN4FRpj+c/WZeoMM/BVXO+nayuIMyH
H0ga/iLNvWYexG7FHLRiq5hTj0g9mUPEbeTXuPtOkTEb/0ckVE2iZH9l7g5edmUZ
GEs=
-----END ENCRYPTED PRIVATE KEY-----

And yes the number of - are important

# Video: How the RSA attacks using lattices work posted April 2015

This is my second video, after the first explaining how DPA works. I'm still trying to figure out how to do that but I think it is already better than the first one. Here I explain how Coppersmith used LLL, an algorithm to reduce lattices basis, to attack RSA. I also explain how his attack was simplified by Howgrave-Graham, and the following Boneh and Durfee attack simplified by Herrmann and May as well.

The repo is here, you can check the survey here as well.

comment on this story

# I'm officialy an intern at Cryptography Services posted April 2015

I haven't been posting for a while, and this is because I was busy looking for a place in Chicago. I finally found it! And I just accomplished my first day at Cryptography Services, or rather at Matasano since I'm in their office, or rather at NCC Group since everything must be complicated :D

I arrived and received a bag of swags along with a brand new macbook pro! That's awesome except for the fact that I spent way too much time trying to understand how to properly use it. A few things I've discovered:

• you can pipe to pbcopy and use pbpaste to play with the clipboard
• open . in the console opens the current directory in Finder (on windows with cygwin I use explorer .)
• in the terminal preference: check "use option as meta key" to have all the unix shortcuts in the terminal (alt+b, ctrl+a, etc...)
• get homebrew to install all the things

I don't know what I'll be blogging about next, because I can't really disclose the work I'll be doing there. But so far the people have been really nice and welcoming, the projects seem to be amazingly interesting (and yeah, I will be working on OpenSSL!! (the audit is public so that I can say :D)). The city is also amazing and I've been really impressed by the food. Every place, every dish and every bite has been a delight :)

# Talk: RSA and LLL attacks posted March 2015

I posted previously about my researches on RSA attacks using lattice's basis reductions techniques, I gave a talk today that went really well and you can check the slides on the github repo

Also on SlideShare

I wanted to record myself so I could have put that on youtube along with the slides but... I completely forgot once I got on stage. But this is OK as I got corrected on some points, it will make the new recording better :) I will try to make it as soon as possible and upload it on youtube.

comment on this story

# End to End encryption for Yahoo mail users (plugin) posted March 2015

Yahoo has released a plugin that allows end to end encryption for yahoo mail users. It's seems to be part of the new "yahoo" redesign:

we’ve heard you loud and clear: We’re building the best products to ensure a more secure user experience and overall digital ecosystem.

It's open sourced and they also setup a bug bounty program (from 50$to 15,000$)

While at this stage we’re rolling out the source code for feedback from the wider security industry

More on their tumblr (this sounds weird).

Glancing over the code it looks like it's cumbersome to use:

The extension requires a keyserver implementing this API to fetch keys for other users.

comment on this story

# Survey on RSA Attacks using Lattice reduction techniques (LLL) posted March 2015

And here's the survey of what I talked about previously: https://github.com/mimoo/RSA-and-LLL-attacks/raw/master/survey_final.pdf

It's my first survey ever and I had much fun writing it! I don't really know if I can call it a survey, it reads like a vulgarization/explanation of the papers from Coppersmith, Howgrave-Graham, Boneh and Durfee, Herrmann and May. There is a short table of the running times at the end of each sections. There is also the code of the implementations I coded at the end of the survey.

If you spot a typo or something weird, wrong, or badly explained. Please tell me!

comment on this story

# Implementation of Boneh and Durfee attack on RSA's low private exponents posted March 2015

I've Implemented a Coppersmith-type attack (using LLL reductions of lattice basis). It was done by Boneh and Durfee and later simplified by Herrmann and May. The program can be found on my github.

The attack allows us to break RSA and the private exponent d. Here's why RSA works (where e is the public exponent, phi is euler's totient function, N is the public modulus):

$ed = 1 \pmod{\varphi(N)}$ $\implies ed = k \cdot \varphi(N) + 1 \text{ over } \mathbb{Z}$ $\implies k \cdot \varphi(N) + 1 = 0 \pmod{e}$ $\implies k \cdot (N + 1 - p - q) + 1 = 0 \pmod{e}$ $\implies 2k \cdot (\frac{N + 1}{2} + \frac{-p -q}{2}) + 1 = 0 \pmod{e}$

The last equation gives us a bivariate polynomial $f(x,y) = 1 + x \cdot (A + y)$. Finding the roots of this polynomial will allow us to easily compute the private exponent d.

The attack works if the private exponent d is too small compared to the modulus: $d < N^{0.292}$.

To use it:

• look at the tests in boneh_durfee.sage and make your own with your own values for the public exponent e and the public modulus N.
• guess how small the private exponent d is and modify delta so you have d < N^delta
• tweak m and t until you find something. You can use Herrmann and May optimized t = tau * m with tau = 1-2*delta. Keep in mind that the bigger they are, the better it is, but the longer it will take. Also we must have 1 <= t <= m.
• you can also decrease X as it might be too high compared to the root of x you are trying to find. This is a last recourse tweak though.

Here is the tweakable part in the code:

# Tweak values here !
delta = 0.26 # so that d < N^delta
m = 3        # x-shifts
t = 1        # y-shifts # we must have 1 <= t <= m

# Pretty diagrams with Tikz in LaTeX posted March 2015

Because studying Cryptography is also about using LaTeX, it's nice to spend a bit of time understanding how to make pretty documents. Because, you know, it's nicer to read.

Here's an awesome quick introduction of Tikz that allows to make beautiful diagram with great precision in a short time:

And I'm bookmarking one more that seems go way further.

comment on this story

# Babun, Cmder and Tmux posted February 2015

I've used Cmder for a while on Windows. Which is a pretty terminal that brings a lot of tools and shortcuts from the linux world. I also have Chocolatey as packet manager. And all in all it works pretty great except Cmder is pretty slow.

I've ran into Babun yesterday, that seems to be kind of the same thing, but with zsh, oh-my-zsh and another packet manager: pact. The first thing I did was downloading tmux and learning how to use it. It works pretty well and I think I have found a replacement for Cmder =)

Here is a video of what is tmux:

# Implementation of Coppersmith attack (RSA attack using lattice reductions) posted February 2015

I've implemented the work of Coppersmith (to be correct the reformulation of his attack by Howgrave-Graham) in Sage.

You can see the code here on github.

I won't go too much into the details because this is for a later post, but you can use such an attack on several relaxed RSA models (meaning you have partial information, you are not totally in the dark).

I've used it in two examples in the above code:

## Stereotyped messages

For example if you know the most significant bits of the message. You can find the rest of the message with this method.

The usual RSA model is this one: you have a ciphertext c a modulus N and a public exponent e. Find m such that m^e = c mod N.

Now, this is the relaxed model we can solve: you have c = (m + x)^e, you know a part of the message, m, but you don't know x. For example the message is always something like "the password today is: [password]". Coppersmith says that if you are looking for N^1/e of the message it is then a small root and you should be able to find it pretty quickly.

let our polynomial be f(x) = (m + x)^e - c which has a root we want to find modulo N. Here's how to do it with my implementation:

dd = f.degree()
beta = 1
epsilon = beta / 7
mm = ceil(beta**2 / (dd * epsilon))
tt = floor(dd * mm * ((1/beta) - 1))
XX = ceil(N**((beta**2/dd) - epsilon))
roots = coppersmith_howgrave_univariate(f, N, beta, mm, tt, XX)

You can play with the values until it finds the root. The default values should be a good start. If you want to tweak:

• beta is always 1 in this case.
• XX is your upper bound on the root. The bigger is the unknown, the bigger XX should be. And the bigger it is... the more time it takes.

## Factoring with high bits known

Another case is factoring N knowing high bits of q.

The Factorization problem normally is: give N = pq, find q. In our relaxed model we know an approximation q' of q.

Here's how to do it with my implementation:

let f(x) = x - q' which has a root modulo q.
This is because x - q' = x - ( q + diff ) = x - diff mod q with the difference being diff = | q - q' |.

beta = 0.5
dd = f.degree()
epsilon = beta / 7
mm = ceil(beta**2 / (dd * epsilon))
tt = floor(dd * mm * ((1/beta) - 1))
XX = ceil(N**((beta**2/dd) - epsilon)) + 1000000000000000000000000000000000
roots = coppersmith_howgrave_univariate(f, N, beta, mm, tt, XX)

What is important here if you want to find a solution:

• we should have q >= N^beta
• as usual XX is the upper bound of the root, so the difference should be: |diff| < XX
1 comment

# Nothing up my sleeve numbers posted February 2015

Why does the round constants in sha1 have those specific values?

Kt is the round constant of round t;

from SH1-1 - Wikipedia:

The constant values used are chosen to be nothing up my sleeve numbers: the four round constants k are 230 times the square roots of 2, 3, 5 and 10. The first four starting values for h0 through h3 are the same with the MD5 algorithm, and the fifth (for h4) is similar.

In cryptography, nothing up my sleeve numbers are any numbers which, by their construction, are above suspicion of hidden properties. They are used in creating cryptographic functions such as hashes and ciphers. These algorithms often need randomized constants for mixing or initialization purposes. The cryptographer may wish to pick these values in a way that demonstrates the constants were not selected for a nefarious purpose, for example, to create a backdoor to the algorithm. These fears can be allayed by using numbers created in a way that leaves little room for adjustment. An example would be the use of initial digits from the number π as the constants. Using digits of π millions of places into its definition would not be considered as trustworthy because the algorithm designer might have selected that starting point because it created a secret weakness the designer could later exploit.

# Flint vs NTL posted February 2015

I'm digging into the code source of Sage and I see that a lot of functions are implemented with Shoup's NTL. There is also FLINT used. I was wondering what were the differences. I can see that NTL is in c++ and FLINT is in C. On wikipedia:

It is developed by William Hart of the University of Warwick and David Harvey of Harvard University to address the speed limitations of the Pari and NTL libraries.

Although in the code source of Sage I'm looking at they use FLINT by default and switch to NTL when the modulus is getting too large.

By the way, all of that is possible because Sage uses Cython, which allows it to use C in python. I really should learn that...

EDIT:

This implementation is generally slower than the FLINT implementation in :mod:~sage.rings.polynomial.polynomial_zmod_flint, so we use FLINT by default when the modulus is small enough; but NTL does not require that n be int-sized, so we use it as default when n is too large for FLINT.

So the reason behind it seems to be that NTL is better for large numbers.

comment on this story

# Magma vs Sage vs Pari posted February 2015

I was looking for a way to know what are the real differences between magma, sage and pari. I only worked with sage and pari (and by the way, pari was invented at my university!) but heard of magma from sage contributors.

From the sage website: The Sage-Pari-Magma ecosystem

The biggest difference between Sage and Magma is that Magma is closed source, not free, and difficult for users to extend. This means that most of Magma cannot be changed except by the core Magma developers, since Magma itself is well over two million lines of compiled C code, combined with about a half million lines of interpreted Magma code (that anybody can read and modify). In designing Sage, we carried over some of the excellent design ideas from Magma, such as the parent, element, category hierarchy.

Any mathematician who is serious about doing extensive computational work in algebraic number theory and arithmetic geometry is strongly urged to become familiar with all three systems, since they all have their pros and cons. Pari is sleek and small, Magma has much unique functionality for computations in arithmetic geometry, and Sage has a wide range of functionality in most areas of mathematics, a large developer community, and much unique new code.

I also noticed that Sage provides an interface to Shoup's NTL through ntl. functions. Good to know!

1 comment