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:

- sha1
- sha256
- sha512
- blake2s
- crc32c

Please bear in mind that sha1 is practically broken. Do not use it 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.

### 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

### blake2s

Same scheme as with the other hashes.

      blake2s_ctx ctx ;
      char const *message ;
      size_t messagelen ;
      size_t outlen = 32 ;  /* the user gives the length of the digest */
      char digest[outlen] ;

      blake2s_init(&ctx, outlen) ;
      blake2s_update(&ctx, message, messagelen) ;
      blake2s_final(&ctx, digest) ;

### crc32c

This isn't a cryptography primitive, but just a function to compute the Castagnoli version of a cyclic redundancy code.

      uint32_t crc = 0 ;  /* no need for context except that crc needs to be initialized to 0 */
      char const *message ;
      size_t messagelen ;

      crc = crc32c(crc, message, messagelen) ;