libskarnet
skalibs
Software
skarnet.org

# The `stdcrypto` library interface

`stdcrypto` is a small collection of standard, public-domain cryptographic primitives. Currently, the following operations are provided:

- rc4
- md5
- sha1
- sha256
- sha512

Please bear in mind that rc4 and md5 are broken, and that sha1 is about to be. Do not use them in security-critical applications.

## Compiling

- Use `#include <skalibs/stdcrypto.h>`

## Programming

You should refer to the `skalibs/stdcrypto.h` header and included headers for the exact function prototypes.

### RC4

      RC4Schedule ctx ;
      unsigned char const *key ;
      size_t keylen ;
      unsigned char const *in ;
      unsigned char *out ;
      size_t len ;

      rc4_init(&ctx, key, keylen) ;
      rc4(&ctx, in, out, len) ;

- `rc4_init()` initializes a RC4Schedule structure with a key key, of length keylen. It then computes and throws away the first `RC4_THROWAWAY` bytes, usually 100
- `rc4()` encrypts len bytes of in with the RC4 flow, and stores the results into out

### MD5

      MD5Schedule ctx ;
      char const *message ;
      size_t messagelen ;
      char digest[16] ;

      md5_init(&ctx) ;
      md5_update(&ctx, message, messagelen) ;
      md5_final(&ctx, digest) ;

- `md5_init()` prepares a MD5Schedule structure for computation
- `md5_update()` adds message to the message to be digested
- `md5_final()` computes the digest

### SHA1

      SHA1Schedule ctx ;
      char const *message ;
      size_t messagelen ;
      unsigned char digest[20] ;

      sha1_init(&ctx) ;
      sha1_update(&ctx, message, messagelen) ;
      sha1_final(&ctx, digest) ;

- `sha1_init()` prepares a SHA1Schedule structure for computation
- `sha1_update()` adds message to the message to be digested
- `sha1_final()` computes the digest

### SHA256

      SHA256Schedule ctx ;
      char const *message ;
      size_t messagelen ;
      char digest[32] ;

      sha256_init(&ctx) ;
      sha256_update(&ctx, message, messagelen) ;
      sha256_final(&ctx, digest) ;

- `sha256_init()` prepares a SHA256Schedule structure for computation
- `sha256_update()` adds message to the message to be digested
- `sha256_final()` computes the digest

### SHA512

      SHA512Schedule ctx ;
      char const *message ;
      size_t messagelen ;
      char digest[64] ;

      sha512_init(&ctx) ;
      sha512_update(&ctx, message, messagelen) ;
      sha512_final(&ctx, digest) ;

- `sha512_init()` prepares a SHA512Schedule structure for computation
- `sha512_update()` adds message to the message to be digested
- `sha512_final()` computes the digest