91 lines
4.3 KiB
C
Executable File
91 lines
4.3 KiB
C
Executable File
/*
|
|
------------------------------------------------------------------------------
|
|
This software is available under 2 licenses -- choose whichever you prefer.
|
|
------------------------------------------------------------------------------
|
|
ALTERNATIVE A - Public Domain (www.unlicense.org)
|
|
This is free and unencumbered software released into the public domain.
|
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
|
software, either in source code form or as a compiled binary, for any purpose,
|
|
commercial or non-commercial, and by any means.
|
|
In jurisdictions that recognize copyright laws, the author or authors of this
|
|
software dedicate any and all copyright interest in the software to the public
|
|
domain. We make this dedication for the benefit of the public at large and to
|
|
the detriment of our heirs and successors. We intend this dedication to be an
|
|
overt act of relinquishment in perpetuity of all present and future rights to
|
|
this software under copyright law.
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
------------------------------------------------------------------------------
|
|
ALTERNATIVE B - MIT License
|
|
Copyright (c) 2019 monolifed
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
the Software without restriction, including without limitation the rights to
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
of the Software, and to permit persons to whom the Software is furnished to do
|
|
so, subject to the following conditions:
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
------------------------------------------------------------------------------
|
|
*/
|
|
|
|
#ifndef PBKDF2_SHA256_INCLUDE
|
|
#define PBKDF2_SHA256_INCLUDE
|
|
|
|
#define SHA256_BLOCKLEN 64ul //size of message block buffer
|
|
#define SHA256_DIGESTLEN 32ul //size of digest in uint8_t
|
|
#define SHA256_DIGESTINT 8ul //size of digest in uint32_t
|
|
|
|
// #ifndef PBKDF2_SHA256_STATIC
|
|
// #define PBKDF2_SHA256_DEF extern
|
|
// #else
|
|
// #define PBKDF2_SHA256_DEF static
|
|
// #endif
|
|
|
|
#include "stdint.h"
|
|
|
|
#define PBKDF2_SHA256_DEF extern
|
|
|
|
typedef struct sha256_ctx_t
|
|
{
|
|
uint64_t len; // processed message length
|
|
uint32_t h[SHA256_DIGESTINT]; // hash state
|
|
uint8_t buf[SHA256_BLOCKLEN]; // message block buffer
|
|
} SHA256_CTX;
|
|
|
|
PBKDF2_SHA256_DEF void sha256_init(SHA256_CTX *ctx);
|
|
PBKDF2_SHA256_DEF void sha256_update(SHA256_CTX *ctx, const uint8_t *m, uint32_t mlen);
|
|
// resets state: calls sha256_init
|
|
PBKDF2_SHA256_DEF void sha256_final(SHA256_CTX *ctx, uint8_t *md);
|
|
|
|
typedef struct hmac_sha256_ctx_t
|
|
{
|
|
uint8_t buf[SHA256_BLOCKLEN]; // key block buffer, not needed after init
|
|
uint32_t h_inner[SHA256_DIGESTINT];
|
|
uint32_t h_outer[SHA256_DIGESTINT];
|
|
SHA256_CTX sha;
|
|
} HMAC_SHA256_CTX;
|
|
|
|
PBKDF2_SHA256_DEF void hmac_sha256_init(HMAC_SHA256_CTX *hmac, const uint8_t *key, uint32_t keylen);
|
|
PBKDF2_SHA256_DEF void hmac_sha256_update(HMAC_SHA256_CTX *hmac, const uint8_t *m, uint32_t mlen);
|
|
// resets state to hmac_sha256_init
|
|
PBKDF2_SHA256_DEF void hmac_sha256_final(HMAC_SHA256_CTX *hmac, uint8_t *md);
|
|
|
|
PBKDF2_SHA256_DEF void pbkdf2_sha256(HMAC_SHA256_CTX *ctx,
|
|
const uint8_t *key, uint32_t keylen, const uint8_t *salt, uint32_t saltlen, uint32_t rounds,
|
|
uint8_t *dk, uint32_t dklen);
|
|
|
|
#endif // PBKDF2_SHA256_INCLUDE
|
|
|
|
//------------------------------------------------------------------------------
|