# Computer network technologies and services/Elements of security and cryptography
## Cryptography basic goals and applications

In network context, basic goals of security mechanisms are:
* endpoint authentication;
* data integrity: we want to ensure that the data has not been changed in the path from source to destination;
* confidentiality: we want to ensure that the data is not read by anyone other than the intended destination.
These goals are achieved is through the use of cryptographic mechanisms. These are employed in two different contexts, that is, for two different actions:
* encryption: it consists in encrypting the data exchanged, that is, changing the contents of the packets so that only who has been authorized can rebuild the original content;
* signing: it is used to guarantee data integrity and sender authentication, and it is performed by appending to the message a small sequence of bytes, which depends on the data itself and on some information which the sender has:
** the endpoints can check whether the data has been modified, recomputing this sequence of bytes;
** the operation is similar to the one of an error detection code, but unlike it the sequence of bytes is based on a secret key.

## Types of keys

There are two types of keys:
* shared (or symmetric) key: the same key, which is a sequence of bytes, is used both to encrypt/sign, and to decrypt/authenticate the data. The key must be kept secret between the communicating stations, and this represents a difficulty because the communication put in place to negotiate the key should itself be secure;
* asymmetric key: two different keys are used to encrypt/sign and decrypt/authenticate data. Either of the two, the one called public key, is used to decrypt the packets that the source host has encrypted through its private key, and can be shared without concern; the other one must be kept secret. The two keys are such that what is encrypted by means of either of the two can only be decrypted by means of the other one.
** For example, if you want to send a file securely, you can apply an cryptographic algorithm by using the private key and spread the public one: in this way who wants to communicate securely with that host can use that key to encrypt the message, since only that host will have the private key and will be able to decode it.
** To check the identity of the sender, the mechanism is similar: if you want to make sure that users can check that a message comes from a certain sender, they just need to use the sender's public key to decrypt the messages sent by it.

### Advantages and disadvantages of the types of key

* Asymmetric keys are less robust and require more computational resources for the algorithm that uses them than symmetric keys.
* In many cases, asymmetric keys are used to communicate securely and agree on a symmetric key:
** a host sends the public key it intends to use for that one-way communication with the destination host;
** the destination host chooses a symmetric key and forwards it by encrypting it by means of the public key received before;
** the source host decrypts the message by using the private key, and from that moment it will use the symmetric key included in it.
Key point: when someone receives the public key relating to an entity, it must be sure of the identity of the entity, that is, it really is who it claims to be: for this purpose we use certificates.

## Certificates

They are documents that allow you to check the belonging of a public key to an entity. A digital certificate contains:
* information about the key;
* information about owners' identities;
* the digital signature of an entity which has verified the certificate contents.
The signature is nothing more than a sequence of bytes, a sort of digest, encrypted by means of the private key from the certification authority. Verifying the signature means to check that the certificate has been validated by the certification authority, so you just have to use its certificate, which will contain the public key, to decrypt the signature. The certificate of the certifying entity may already be known by the host, for example because it is already present in the operating system or in the web browser, or it may have to be downloaded and in turn verified in the same way. The Root CA certificate must be inevitably obtained in a reliable way.