вторник, 22 июля 2014 г.

cron java and utf-8

If you ever run java application from cron job you might notice that any unicode characters that java application emits looks like ????????

In order to avoid this situation in RHEL-based distros you need to create /etc/environment file and then put here a value of LANG variable. For example: LANG=ru_RU.UTF-8  (without word export or something else)  

воскресенье, 6 июля 2014 г.

Solving random numbers problem

This library https://bitbucket.org/mike1452/msprandom demonstrates a technique which allows to solve the problem of getting random numbers on computers and small devices for cryptographic purposes. So, if you need encrypt and sign data on a small device or computer without hardware RNG do the following:
  1. Create true random numbers vault using this library on your computer or notebook.
  2. Put this vault on your device, computer or server where you need encrypt and sign data.
  3. Load the vault once at the start of the program when you need encrypt or sign data.
  4. Call secure-rand function to get random bytes as many times as you need.
Vault is encrypted and secured with HMAC. Random data in a vault is updated every time you load random seed with unpredictable way, so HMAC is recalculated too.

Gathering a true random data

To get a true random data a human input is used. Algorithm of collecting a random data:
  1. Run separate thread where atomic counter increments every tic from 0..255 with a very high speed.
  2. Wait for unbuffered key press by human and get a scan code of pressed button.
  3. Take current nanoseconds value from start of Epoch and take mod 256 to convert its value to a random byte.
  4. Xor values between each other: scan-code-byte ^ current-counter-value ^ nanoseconds to produce random byte.
  5. Add random byte to output vector. We suppose that only 3 bits has true randomness in this random byte. So, to get true random 32 bytes we need ~ 32*3 button press from user input. 6 Repeat steps 2-5 until we get required amount of random bytes.
  6. If we collected required amount of random data then do final step -> hash output vector with cryptographically strong hash function GOST 3411-94 to guarantee that probability 1 and 0 bits in output vector will be 0.5. Note, that hash function used here only to mix random bits and do not influence to the quality of random data. So hash(random data) = random data. Hash will produce vector of 32 bytes containing a random data.
  7. Repeat steps 1..7 if we need more than 32 bytes of random data.
Using this algorithm we collect a true 512 random bits. Why 512? Well, every PRNG needs a true random seed. If an attacker knows a seed then you can't protect your data. 256 bit length is far enough to keep millitary grade secrets. I did 512 to close the security question of random seed. My opinion, 512 bit of true random data is enough to use in PRNG: generating keys, signatures, etc.