GithubHelp home page GithubHelp logo

pombredanne / randombytes Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dsprenkels/randombytes

0.0 0.0 0.0 59 KB

A portable C library for generating cypto-secure random bytes

License: MIT License

Makefile 8.46% C 91.54%

randombytes's Introduction

Pluggable randombytes function

Travis-CI Appveyor

randombytes is a library that exposes a single function for retrieving crypto-secure random bytes. It is loosely based on Libsodium's random bytes API. If you can, you should use that one. Otherwise, you can use this library.

Usage

randombytes takes two arguments:

  1. A pointer to the buffer
  2. The length of the buffer in bytes

The function will always return an int which will be 0 on success. The caller must check this. If some kind of error occured, errno MAY contain a hint to what the error was, and a subsequent call to the randombytes function MAY succeed. An example of when the function may fail is when /dev/urandom could not be opened, because there were no file descriptors left to use for the process.

On sensible systems (like the ones with arc4random) the latency is very low. However, this is totally not guaranteed. Do not expect this function to be very fast. Benchmark for your specific setup, and use a fast CSPRNG if you need.

Example code:

#include "randombytes.h"
#include <inttypes.h>
#include <stdio.h>

int main()
{
    // Generate some random bytes and print them in hex
    int ret;
    uint8_t buf[20];
    size_t i;

    ret = randombytes(&buf[0], sizeof(buf));
    if (ret != 0) {
        printf("Error in `randombytes`");
        return 1;
    }    
    for (i = 0; i < sizeof(buf); ++i) {
        printf("%02hhx", buf[i]);
    }
    printf("\n");
    return 0;
}

How secure is it really?

While building this I keep one rule of thumb which is: Trust the OS. Most OS'es implement a secure random generator, which is seeded by a good entropy source. We will always use this random source. This essentially means that the implementation is highly platform-dependent. For example we use getrandom on Linux and arc4random on BSD systems.

What if the OS's random generator is bad?

If you are dealing with an OS that has a compromised random generator you are out of luck. The reason why you cannot generate high quality random data from userspace is that userspace is made so that everything is too deterministic. A secure random generator needs a good source of entropy, such as 2.4 GHz noise or the user's mouse movements. Collecting these kinds of events only works well when working on the lowest level.

Questions

It does not compile on my platform!

Please open an issue. If possible I will try to make a randombytes implementation for your platform.

Do you have bindings for language x?

No, your language probably already has a random source. Use that one.

Other

Feel free to send me an email on my Github associated e-mail address.

randombytes's People

Contributors

dsprenkels avatar jaromil avatar john-sharratt avatar joostrijneveld avatar niekbouman avatar peterwilli avatar scurest avatar thijsmie avatar thomwiggers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.