Symmetric/Asymmetric cryptography in one minute

Modern cryptography is composed of two flavors: symmetric & asymmetric.

In symmetric cryptography, there is only one key that is used in both encryption/decryption. It is also called shared-key cryptography.

While in asymmetric cryptography, there is a key-pair, i.e., a public key and a private key, so it is also named as public-key cryptography. The ciphertext which is encrypted by public key can only be decrypted by private key and vice versa. Generally, as the name indicates, public key is accessed by every one, while private key only you can access . When someone wants to send you confidential message, he/she encrypts the information using public key and only you can use private key to decrypt it. For the same reason, you can use private key to make a digital signature and every one can use public key to make sure it is indeed you publish.

Although asymmetric cryptography is more safer than symmetric, but symmetric is more faster. Nowadays, there is usually a hybrid solution, which is for the content, it is encrypted using symmetric cryptography, while for the key transfer, using asymmetric cryptography. This method can leverage the advantages of both methods.

Polymorphic type in Haskell

The parameters and result of functions in Haskell can be one “concerate” type, e.g., BoolInt, etc; or polymorphic type, i.e., not a specified type, represented by a type variable, such as ab, and so on.

Take length function as an example:

length :: [a] -> Int

The return value must be Int type, but the input parmater is a list which can contain any type. But if the polymorphic type is constrained as a set of types, e.g. (+):

(+) :: Num a => a -> a -> a

Num is called “type class”, it means not all types can be use in (+) except the ones which belong to Num. This “constrianed” polynomical type (Num a in (+)) is referred as “Ad hoc polymorphism”, while “no constrianed” one (a in length) is referred as “Parametric polymorphism”.

Linking error of _ntl_gbigint_body in using NTL

I use NTL on ArchLinux, and there is a struct _ntl_gbigint_body which is actually not defined (refer this post):

/*
 * This way of defining the bigint handle type is a bit non-standard,
 * but better for debugging.
 */

struct _ntl_gbigint_body;
typedef _ntl_gbigint_body *_ntl_gbigint;  

You should pay attention to functions who depend on this struct, such as:

void _ntl_gcopy(_ntl_gbigint a, _ntl_gbigint *bb)   

Because for old NTL library, the function prototype generated by compiler is _ntl_gcopy(void*, void**):

$ readelf -sW libntl.so | c++filt | grep ntl_gcopy
2511: 000000000012b750   184 FUNC    GLOBAL DEFAULT   12 _ntl_gcopy(void*, void**)

While for new one it is _ntl_gcopy(_ntl_gbigint_body*, _ntl_gbigint_body**):

$ readelf -sW libntl.so | c++filt | grep ntl_gcopy
615: 0000000000148500   202 FUNC    GLOBAL DEFAULT   11 _ntl_gcopy(_ntl_gbigint_body*, _ntl_gbigint_body**)

So if you meet linking error as following:

undefined reference to `_ntl_gcopy(void*, void**)'

It should be NTL header files and library mismatch. The header files are old, while library is new.

Notice the linking library position on Ubuntu

This week, I ported tcpbench from OpenBSD to Linux. The idiomatic method of OpenBSD is putting the linking library in front of generating final target:

cc -g -O2 -Wall -levent -o tcpbench tcpbench.c

However this doesn’t work in Ubuntu since its the linker uses --as-needed option. So I change the Makefile to put the library at the end:

cc -g -O2 -Wall -o tcpbench tcpbench.c -levent

Please refer this discussion if you are interested.

First installation of OpenBSD-current

I have an old laptop, and tried to install OpenBSD-current on it. Unfortunately, no matter from OpenBSD 6.3 to upgrade, or install it from scratch. the machine couldn’t boot successfully. It displayed:

>>OpenBSD/amd64 BOOT 3.39

Then it flashed one line (I couldn’t see that line clearly, and it should display loading something), and the system would reboot again.

At the end, I gave up, and install OpenBSD-current on a virtual machine.