Chapter 2 is about cryptography and
Technology details.
Our objectives are to make sure you fully understand the
cryptography necessary for blockchain.
Have a deeper understanding of hashes and digital signatures.
As well as understand how these relate to nodes in the blockchain.
Obviously a single lesson is not
going to make you an expert in cryptography, but you
do need a deeper knowledge than other people
working in blockchain if you wish to be a developer.
Let's start with cryptographic hashes. Now in lesson
one, we gave you an overview of the properties required for
cryptographic hash.
But there are many different algorithms to choose from.
Charm one has long since been considered no longer
viable and secure.
Sha2 has two primary sizes at
256 bit and a 512 bit.
There are also truncated versions such as shot 224
and shot 384. These are all designed
by the National Security Agency.
The shot two is still something you can use it still
considered secure.
But in October of 2012 there was
a into a competition to find
a Shaw 3 not to shawtu is wrong or insecure,
but just to be ready for the future the kekak
algorithm was selected as the winner of that
hash function competition.
Shaw 3 uses something called a sponge contraction construction
and the message blocks or exclusively
ordered to the initial bits of the state. Now. This
has a totally different mathematical structure
than shot two or Shaw one and it
can also have variable size output.
Now that doesn't mean that when you put in an input
each time, you'll have different output size. It means
you as the implementer of Shaw 3 get to pick what size
you want.
There are a lot of hashes out there the ones you see in bold
are the ones most common in the United States.
Ntlm and ntlm version 2 are used
exclusively for Windows password authentication.
Around the world. You'll see many others like rightmd have all
and tiger.
Before you select a particular hash algorithm. The
best thing to do is simply do a little research on
is it known to be secure are there known issues with
it that sort of thing?
Now in addition to hashes, there's something called a
message authentication code. The idea here
is to have something in addition to the hatch to work
together with it. The most common is a hash
message authentication code what happens there is
the parties involved exchange a
random string of bits.
In each time Ash is calculated. It's then exclusively
ordered that random string of bits. So to
recreate the hash you would have to also have that random
string of bits a key.
Another way to do this.
Is Cypher blockchaining with message authentication code? You
essentially use a traditional symmetric Cipher with
Cipher block chaining and then
you only produce the final block as your hash equivalent
both of these methods both basically
incorporate a key with hashing to
make it a little more secure.
Now we mentioned.
Collisions in the first lesson, basically if
two different inputs produce the same output and
you see here password a and password B produce the
same output. Then you have a collision one of
the things you'd like to investigate When selecting a hashing algorithm
is how resistant is it to collisions?
Most of the ones I've listed are very very resistant. You're
highly unlikely to ever find a collision. But if you want to
try a new algorithm, you'll have to investigate that issue.
Related to hashes is a thing called the birthday Paradox. Now
this might seem totally in a
different track for just a moment prepare with me.
How many people would you need to have in a room to have
a strong likelihood better than 50% The two
would have the same birthday and by that I mean month and day
I don't mean you.
Now if you put 367 people in the room, at least
two of the must have the same birthday since there
are 365 days in the air plus one more for
leap year the 367th person must match
at least one of the others.
Now this is not asking how many people you need to guarantee a
match?
It's asking how many people you need to have a strong probability of
a match.
It turns out and we'll show you the math in just a
moment.
But with even 23 people in a room.
You have a 50% chance that two will
have the same birthday. Now. What does this have to do with hashes?
It's about producing collisions some types of attacks.
Try to create a collision.
in order to match a hash
You can't unhash something. You learned that in lesson one,
but you can try to create a collision.
What the birth they Paradox is telling you.
It's a collision and birthdays.
And what we're saying is that you're going to need a lot
less than you might think to create a collision which is
why generally if the hashing algorithm is sound
the larger the hash the better. So if you
have a chance to use Shaw 2 with 512 bits.
Instead of 256 that's better. But now
let's explain the math on this 23 people. You might
be doubting that.
Let's say the very first person comes in the
room.
Now since they're the first person there's a 100% chance
that they don't share a birthday with anyone else in the
room because there's no one else in the room and if we want to write this
as days in a year.
for getting about leap year for a moment 100% is the same
as 365 over 365
Now the next person comes to the room the odds that
person has a birthday different from the first one.
Or 364 at a 365 pretty high as
we would expect the third person is 363 out
of 365. Well, you can
convert those into percentages as you see
here and once we get to the 23rd person
There's only a 49% chance that they
don't match someone in the room, which means there's a
51% chance that they do.
Now this goes back to the odds of two inputs
to a hash with the same output or Collision.
Now just to be clear.
We're not saying that the size of the hash is the only consideration the
robustness of the algorithm is also clearly important.
But the size will play a role in resisting collisions.
Now when we talk about cryptography, we have
two main types of cryptography symmetric and asymmetric.
symmetric means
you and I share a key and I'll use a key to encrypt and
you'll use the same key to decrypt.
DS was probably the most widely known but long
since considered outdated and insecure.
AES for advanced encryption standard
is probably the most widely known and used symmetric Cipher
today. It was built on the reindel Block Cipher.
You can choose three different key sizes 128 192
or 200 56 bit.
Blowfish is another one. You'll see quite frequently was designed
by Bruce schneier. All of Bruce schneier's output
is released with no copyright no patent free for
anyone to use. So you see algorithms like this
a lot in open source Solutions
Now his has varying key lengths you
can choose key sizes and 32-bit increments from 32-bit
up to 448 bit.
Serpent is another one. It has a structure
almost identical to AES and
it has key sizes of 128 192
and 256 just like AES.
But the one of the main differences is with AES.
Each symmetric Cipher will go through a certain number of rounds and
each round is basically encrypting and encrypting and encrypting
again, for example, DS uses 16 rounds.
AES will use 10 Rounds with
128 bit key.
12 rounds with the 192 bit
key and 14 rounds with 256 bit but serpent
uses 32 rounds regardless of the key size.
Now let's talk about asymmetric cryptography which is used in
digital signatures, which we see a lot in blockchain.
Basically, there are two different keys that are mathematically related.
So that one if it's used to encrypt a message
cannot decrypt the message.
The other key is used to decrypt it.
So for example, I put out my public key and I
would not care. If every single person on the planet had a
copy of it.
You can only be used to encrypt messages to me.
You can't decrypt them only my private key that I keep secret
can decrypt them.
Now in general asymmetric algorithms are slower than
symmetric.
But they allow us to securely communicate without having
to pre-exchange a key.
It works kind of like this if Alice would like to send Bob
a message see first gets Bob's public
key.
Encrypts the message with Bob's public key and sends it to
him.
Bob then decrypts the message with Bob's private
key.
Now if Eve intercepts the message, even if she
has Bob's public key, it won't decrypt the
message only his private key will.
Now when Bob is ready to respond.
He wants to send a message back to Alice. He gets Alice's public key encrypts
the message sends it and then she'll decrypt
it with her private key.
There are much fewer.
Asymmetrics than there are symmetric algorithms. RSA
is widely used first publicly described in
1977 by Ron River study Schumer
and Leonard Adelman.
Diffie helmets and other popular one with key exchange protocols and
there are many variations of diffie-hellman such as
mqv and El Gamal.
And then there are elliptic curve cryptography Solutions, which
we're not going to deal much with in this lesson.
But it might help you to understand how RSA works.
High frequently find that when presenting cryptography to
novices people that are new to it. They're puzzled
by this idea. How can one key encrypt but it
can't decrypt and I often find it helps them to
see RSA how it works. You don't have to
memorize all the RSA functionality. But if
you see the general flow, it might help you.
So let's start with generating a key.
The very first thing you do.
Is generate two large prime numbers?
They need to be approximately equal size and they need
to be number such that their product is the required bit
length. So if you want a 4,096 bit RSA key.
P and Q multiplied together to get in in needs
to be 4,096 bits long.
So far nothing particularly difficult in the mathematics.
You then will compute P times q and get in.
So far easy math.
The next one is you're going to compute P minus 1 times
Q minus 1 now that might puzzle you as to
why frankly there's a whole bunch of
mathematics behind that including relationships between numbers
that are called coprime numbers that have no
common factors a thing called Euler's totient and
things of that nature that really aren't important for our purposes. If
you do want more detail and cryptography the
ecouncil certified encryption specialist course
gets into all those details and the math
behind them, but for now, we just take P minus
1 times Q minus 1 to get M. That's mathematics
you could do.
We choose some small number e that only has to have two properties.
It's smaller than M and its co-prime pm.
And then we solve this little mathematical problem what number
D such that D times e modem that's
modulus operator is congruent to
one now again, if you're not familiar with modules operations and
congruences ignore them for now. It's
not necessary if you'd understand blockchain, and again,
I recommend the EC Council certified encryption specialist
to get more detail.
Well now you'll publish en as the public
key and DNA your private key.
And in fact something should become clear right now. You're
probably familiar with mathematicians liking to
use x's and y's, why did we use d and e
because e encrypts and D decrypts. Let's
see how that works. Once you've generated the keys.
If you would like to send a message.
You take that plain text raise it to the eighth power mod in.
To decrypt it the recipient raise it
to the Deep power mod and we'll get back the plane text.
Now real RSA and the real world users pretty
large numbers.
Let's take a look at it with fairly small numbers just seemed
at the concepts down.
Let's say we pick the numbers p and
Q for p equals 17 and Q equal 11.
multiplying them together as fairly trivial you get 187
P minus 1 or 17 minus 1 is 16
Q minus 1 11 minus 1 is 10
and 16 times 10 is 160.
I'm going to select an evalue of seven. And if
I solve that little equation of what D times e
mod n is going to give me.
One it turns out D is 23.
So what that means is my public key is 7 e
and 187 in.
My private key is d23 and
187 in.
Well, let's see how that works.
Let's say for some strange reason. I want to encrypt
the number three.
So remember our key values there I take 3 to
the seventh power mod 187 and I'm
going to get 130.
I send that to you.
You take 130 to the 23rd power.
Mod 187 you're going to get back three. So that's
how RSA works.
And yes, the mathematics the actual implementation is quite
simple the why is a little more complicated.
Hopefully this lesson gives you an overview of how
RSA functions. So you're more comfortable with
the concept of asymmetric cryptography and why the
two keys work interchangeably there has to
be some underlying mathematics that ties the two
keys together in this case e and D are tied
together because they are part of that equation D
times e volume is congruent
to one.
And that's also why using e again the public
key again won't decrypt a message. It will
just further scramble it.
Dimension elliptic curve we're not going to go in much
detail here. The mathematics is a bit tougher.
But it's based. The security is based on the fact that finding
the discrete logarithm.
Of a random elliptic curve element with respect to
a publicly known base point is difficult to
the point of being impractical.
Basically elliptic curve is another option for public
key cryptography.
Well now that you have a better understanding of public key cryptography.
Let's talk about digital signatures when I'm
using cryptography. My concern is confidentiality. Bob
wants to send Alice a message or vice
versa and they don't want Eve to
be able to read it.
Digital signatures are not concerned by themselves
with confidentiality. They want you
to know who did it. For example, if I'm
adding a transaction to a blockchain. I want to confirm it
was me that added it.
So what I do there?
Is I sign something with my private key?
If you have my public key and pretty much everyone can
get it. It's public you can verify my
my message that I signed it because if
I didn't sign it if someone else did pretending to be
me, they don't have my private key when you try
to decrypt it or confirm it with my public key. You'll
get out gibberish.
Now I'm not sending my private key. I'm encrypting a
message with my private key. And normally what's encrypted
is a hash of the message value. So to
confirm the signature you would decrypt with my public
key generate your own hash compare it and if
they didn't match it obviously wasn't me that signed it
a lot of email Solutions will do this for you if
you set it up and they'll do it automatically.
So just to compare the two.
In normal, asymmetric encryption. We're
trying to keep someone else from intercepting
and reading it.
So I encrypt your message with the recipients public
key.
The recipient that will decrypt it with their private
key that protects confidentiality with a
digital signature. I want you to know for a fact that I sent
it.
So I use my private key to sign it anyone who receives
it can verify it with my public key.
And this takes us naturally to what is a digital certificate.
Well to begin with I keep saying anyone can have my
public key.
How do they get it normally through distribution of
a digital certificate? It's a document that contains
your public key and some information to verify whose
key it is.
The public key infrastructure is the infrastructure. Whereby
all these digital certificate vendors trust each
other and they trust each other because they're all using
public key cryptography standards that were
put in place by RSA the company not the algorithm.
And a certificate is essentially a digital document that
identifies you.
as confirmed by some trusted third party
A certificate Authority. Is that trusted third party?
Like thought verifying Go Daddy.
A registration Authority is used to handle verification prior
to issuing the certificates.
And a certificate policies a set of rules the determines
how a certificate may be used.
Now x509 is the international standard used for
most digital certificates the most common found in
the world.
most certificate authorities support
x509
You have certificate revocation list, which tell
you in a certificates no longer valid. They can be put out
by a push or pull models. And then there's the
restrict DNS recursive service which allows authorized
users to check
on validation of a specific certificate.
It's an alternative to certificate revocation
lists.
Now this takes us to blockchain remember blockchain is
a decentralized system that
prevents control by a single entity transactions are
immutable. You can't change them and the blockchain network
lives in a state of consensus.
There are no need for third-party intermediaries. Well, a
lot of the cryptography we just talked about is the basis for
how this functions.
So for example, when a transaction is requested, someone
has to digitally sign it.
And then as you probably already know each block is
connected to the last block the previous
block via cryptographic hash. So someone
requests that transaction there requested transactions
broadcast to the P2P Network.
the network validates via consensus
that verified transaction as we've
talked about before could be any number of things but the most
common is cryptocurrency exchanges could also be contracts or
insurance or what have you and in less than three. We'll talk
about some of those other options.
When the transaction is verified a new block is
then added to the existing blockchain. So everything is
on alterable and permanent.
There's no real physical form. There's no centralized Bank.
This is all done with peer-to-peer networks
and cryptography.
Now we've used the word node in less than one in here.
A node is a computer connected to a blockchain network
using a client that performs the validating task
on the relaying of transactions.
It gets a copy of the blockchain which gets downloaded automatically as
soon as you join the network.
Every note is an administrator and joins
the network voluntarily.
Now a host node.
Is one that's looking at.
Trying to be a major part of the
network and it does this by proof of work proof
of work is usually what we call crypto mining.
And it's done by solving computational mathematical issues and
then using that transactions to the blockchain.
This allows a decentralized incentivized
methodology for adding blocks.
In this lesson, we've talked about blockchain cryptography
hashes and digital signatures combined
with the information from lesson 1 this should
clarify for you how blockchain works.