pbkdf2.c

Basic pbkdf2 implementation
git clone git://git.finwo.net/lib/pbkdf2.c
Log | Files | Refs | README | LICENSE

README.md (2055B)


      1 # pbkdf2
      2 
      3 Pure C implementation of PBKDF2-HMAC with SHA-1 and SHA-256 support.
      4 
      5 ## Installation
      6 
      7 ```sh
      8 dep add finwo/pbkdf2
      9 dep install
     10 ```
     11 
     12 After that, simply add `include lib/.dep/config.mk` in your makefile and include
     13 the header file by adding `#include "finwo/pbkdf2.h"`.
     14 
     15 ## Usage
     16 
     17 ```c
     18 #include "finwo/pbkdf2.h"
     19 
     20 uint8_t output[32];
     21 pbkdf2(
     22   (uint8_t *)"password", 8,      // password
     23   (uint8_t *)"salt", 4,          // salt
     24   4096,                          // iterations
     25   PBKDF2_SHA256,                 // hash algorithm
     26   output, 32                     // output buffer & length
     27 );
     28 ```
     29 
     30 ## API
     31 
     32 ### Functions
     33 
     34 #### `pbkdf2`
     35 
     36 ```c
     37 void pbkdf2(
     38   const uint8_t *password, size_t password_len,
     39   const uint8_t *salt,     size_t salt_len,
     40   uint64_t iterations,
     41   enum pbkdf2_hash hash,
     42   uint8_t *output, size_t output_len
     43 );
     44 ```
     45 
     46 Derives a key using PBKDF2-HMAC.
     47 
     48 | Parameter | Description |
     49 |-----------|-------------|
     50 | `password` | Password bytes |
     51 | `password_len` | Length of password |
     52 | `salt` | Salt bytes |
     53 | `salt_len` | Length of salt |
     54 | `iterations` | Number of iterations |
     55 | `hash` | Hash algorithm (`PBKDF2_SHA1` or `PBKDF2_SHA256`) |
     56 | `output` | Output buffer |
     57 | `output_len` | Desired output length |
     58 
     59 #### `hmac_sha1` / `hmac_sha256`
     60 
     61 ```c
     62 void hmac_sha1(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *output);
     63 void hmac_sha256(const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *output);
     64 ```
     65 
     66 Standalone HMAC functions for testing.
     67 
     68 #### `sha1_*` / `sha256_*`
     69 
     70 ```c
     71 void sha1_init(struct sha1_ctx *ctx);
     72 void sha1_update(struct sha1_ctx *ctx, const uint8_t *data, size_t len);
     73 void sha1_final(struct sha1_ctx *ctx, uint8_t *hash);
     74 
     75 void sha256_init(struct sha256_ctx *ctx);
     76 void sha256_update(struct sha256_ctx *ctx, const uint8_t *data, size_t len);
     77 void sha256_final(struct sha256_ctx *ctx, uint8_t *hash);
     78 ```
     79 
     80 Standalone hash functions for testing.
     81 
     82 ## Testing
     83 
     84 ```sh
     85 make test
     86 ./test
     87 ```
     88 
     89 ## License
     90 
     91 This project is licensed under [FPGL](LICENSE.md)