Updated cryto wrapper API.

This commit is contained in:
Armin Novak 2016-02-24 20:32:20 +01:00
parent ada2b16c50
commit 6e8ea34f06
3 changed files with 89 additions and 50 deletions

View File

@ -648,10 +648,10 @@ typedef union _WINPR_MD5_CTX WINPR_MD5_CTX;
extern "C" { extern "C" {
#endif #endif
WINPR_API void winpr_MD5_Init(WINPR_MD5_CTX* ctx); WINPR_API BOOL winpr_MD5_Init(WINPR_MD5_CTX* ctx);
WINPR_API void winpr_MD5_Update(WINPR_MD5_CTX* ctx, const BYTE* input, size_t ilen); WINPR_API BOOL winpr_MD5_Update(WINPR_MD5_CTX* ctx, const BYTE* input, size_t ilen);
WINPR_API void winpr_MD5_Final(WINPR_MD5_CTX* ctx, BYTE* output); WINPR_API BOOL winpr_MD5_Final(WINPR_MD5_CTX* ctx, BYTE* output);
WINPR_API void winpr_MD5(const BYTE* input, size_t ilen, BYTE* output); WINPR_API BOOL winpr_MD5(const BYTE* input, size_t ilen, BYTE* output);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -689,10 +689,10 @@ typedef union _WINPR_MD4_CTX WINPR_MD4_CTX;
extern "C" { extern "C" {
#endif #endif
WINPR_API void winpr_MD4_Init(WINPR_MD4_CTX* ctx); WINPR_API BOOL winpr_MD4_Init(WINPR_MD4_CTX* ctx);
WINPR_API void winpr_MD4_Update(WINPR_MD4_CTX* ctx, const BYTE* input, size_t ilen); WINPR_API BOOL winpr_MD4_Update(WINPR_MD4_CTX* ctx, const BYTE* input, size_t ilen);
WINPR_API void winpr_MD4_Final(WINPR_MD4_CTX* ctx, BYTE* output); WINPR_API BOOL winpr_MD4_Final(WINPR_MD4_CTX* ctx, BYTE* output);
WINPR_API void winpr_MD4(const BYTE* input, size_t ilen, BYTE* output); WINPR_API BOOL winpr_MD4(const BYTE* input, size_t ilen, BYTE* output);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -732,10 +732,10 @@ typedef union _WINPR_SHA1_CTX WINPR_SHA1_CTX;
extern "C" { extern "C" {
#endif #endif
WINPR_API void winpr_SHA1_Init(WINPR_SHA1_CTX* ctx); WINPR_API BOOL winpr_SHA1_Init(WINPR_SHA1_CTX* ctx);
WINPR_API void winpr_SHA1_Update(WINPR_SHA1_CTX* ctx, const BYTE* input, size_t ilen); WINPR_API BOOL winpr_SHA1_Update(WINPR_SHA1_CTX* ctx, const BYTE* input, size_t ilen);
WINPR_API void winpr_SHA1_Final(WINPR_SHA1_CTX* ctx, BYTE* output); WINPR_API BOOL winpr_SHA1_Final(WINPR_SHA1_CTX* ctx, BYTE* output);
WINPR_API void winpr_SHA1(const BYTE* input, size_t ilen, BYTE* output); WINPR_API BOOL winpr_SHA1(const BYTE* input, size_t ilen, BYTE* output);
#ifdef __cplusplus #ifdef __cplusplus
} }
@ -896,9 +896,9 @@ typedef union _WINPR_RC4_CTX WINPR_RC4_CTX;
extern "C" { extern "C" {
#endif #endif
WINPR_API void winpr_RC4_Init(WINPR_RC4_CTX* ctx, const BYTE* key, size_t keylen); WINPR_API BOOL winpr_RC4_Init(WINPR_RC4_CTX* ctx, const BYTE* key, size_t keylen);
WINPR_API int winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const BYTE* input, BYTE* output); WINPR_API BOOL winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const BYTE* input, BYTE* output);
WINPR_API void winpr_RC4_Final(WINPR_RC4_CTX* ctx); WINPR_API BOOL winpr_RC4_Final(WINPR_RC4_CTX* ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -43,7 +43,7 @@
* RC4 * RC4
*/ */
void winpr_RC4_Init(WINPR_RC4_CTX* ctx, const BYTE* key, size_t keylen) BOOL winpr_RC4_Init(WINPR_RC4_CTX* ctx, const BYTE* key, size_t keylen)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
RC4_set_key((RC4_KEY*) ctx, keylen, key); RC4_set_key((RC4_KEY*) ctx, keylen, key);
@ -51,25 +51,29 @@ void winpr_RC4_Init(WINPR_RC4_CTX* ctx, const BYTE* key, size_t keylen)
mbedtls_arc4_init((mbedtls_arc4_context*) ctx); mbedtls_arc4_init((mbedtls_arc4_context*) ctx);
mbedtls_arc4_setup((mbedtls_arc4_context*) ctx, key, keylen); mbedtls_arc4_setup((mbedtls_arc4_context*) ctx, key, keylen);
#endif #endif
return TRUE;
} }
int winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const BYTE* input, BYTE* output) BOOL winpr_RC4_Update(WINPR_RC4_CTX* ctx, size_t length, const BYTE* input, BYTE* output)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
RC4((RC4_KEY*) ctx, length, input, output); RC4((RC4_KEY*) ctx, length, input, output);
return 0;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C)
return mbedtls_arc4_crypt((mbedtls_arc4_context*) ctx, length, input, output); if (mbedtls_arc4_crypt((mbedtls_arc4_context*) ctx, length, input, output) != 0)
return FALSE;
#endif #endif
return TRUE;
} }
void winpr_RC4_Final(WINPR_RC4_CTX* ctx) BOOL winpr_RC4_Final(WINPR_RC4_CTX* ctx)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_ARC4_C)
mbedtls_arc4_free((mbedtls_arc4_context*) ctx); mbedtls_arc4_free((mbedtls_arc4_context*) ctx);
#endif #endif
return TRUE;
} }
/** /**

View File

@ -43,123 +43,158 @@
* MD5 * MD5
*/ */
void winpr_MD5_Init(WINPR_MD5_CTX* ctx) BOOL winpr_MD5_Init(WINPR_MD5_CTX* ctx)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
MD5_Init((MD5_CTX*) ctx); if (MD5_Init((MD5_CTX*) ctx) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C)
mbedtls_md5_init((mbedtls_md5_context*) ctx); mbedtls_md5_init((mbedtls_md5_context*) ctx);
mbedtls_md5_starts((mbedtls_md5_context*) ctx); mbedtls_md5_starts((mbedtls_md5_context*) ctx);
#endif #endif
return TRUE;
} }
void winpr_MD5_Update(WINPR_MD5_CTX* ctx, const BYTE* input, size_t ilen) BOOL winpr_MD5_Update(WINPR_MD5_CTX* ctx, const BYTE* input, size_t ilen)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
MD5_Update((MD5_CTX*) ctx, input, ilen); if (MD5_Update((MD5_CTX*) ctx, input, ilen) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C)
mbedtls_md5_update((mbedtls_md5_context*) ctx, input, ilen); mbedtls_md5_update((mbedtls_md5_context*) ctx, input, ilen);
#endif #endif
return TRUE;
} }
void winpr_MD5_Final(WINPR_MD5_CTX* ctx, BYTE* output) BOOL winpr_MD5_Final(WINPR_MD5_CTX* ctx, BYTE* output)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
MD5_Final(output, (MD5_CTX*) ctx); if (MD5_Final(output, (MD5_CTX*) ctx) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD5_C)
mbedtls_md5_finish((mbedtls_md5_context*) ctx, output); mbedtls_md5_finish((mbedtls_md5_context*) ctx, output);
mbedtls_md5_free((mbedtls_md5_context*) ctx); mbedtls_md5_free((mbedtls_md5_context*) ctx);
#endif #endif
return TRUE;
} }
void winpr_MD5(const BYTE* input, size_t ilen, BYTE* output) BOOL winpr_MD5(const BYTE* input, size_t ilen, BYTE* output)
{ {
WINPR_MD5_CTX ctx; WINPR_MD5_CTX ctx;
winpr_MD5_Init(&ctx);
winpr_MD5_Update(&ctx, input, ilen); if (!winpr_MD5_Init(&ctx))
winpr_MD5_Final(&ctx, output); return FALSE;
if (!winpr_MD5_Update(&ctx, input, ilen))
return FALSE;
return winpr_MD5_Final(&ctx, output);
} }
/** /**
* MD4 * MD4
*/ */
void winpr_MD4_Init(WINPR_MD4_CTX* ctx) BOOL winpr_MD4_Init(WINPR_MD4_CTX* ctx)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
MD4_Init((MD4_CTX*) ctx); if (MD4_Init((MD4_CTX*) ctx) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C)
mbedtls_md4_init((mbedtls_md4_context*) ctx); mbedtls_md4_init((mbedtls_md4_context*) ctx);
mbedtls_md4_starts((mbedtls_md4_context*) ctx); mbedtls_md4_starts((mbedtls_md4_context*) ctx);
#endif #endif
return TRUE;
} }
void winpr_MD4_Update(WINPR_MD4_CTX* ctx, const BYTE* input, size_t ilen) BOOL winpr_MD4_Update(WINPR_MD4_CTX* ctx, const BYTE* input, size_t ilen)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
MD4_Update((MD4_CTX*) ctx, input, ilen); if (MD4_Update((MD4_CTX*) ctx, input, ilen) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C)
mbedtls_md4_update((mbedtls_md4_context*) ctx, input, ilen); mbedtls_md4_update((mbedtls_md4_context*) ctx, input, ilen);
#endif #endif
return TRUE;
} }
void winpr_MD4_Final(WINPR_MD4_CTX* ctx, BYTE* output) BOOL winpr_MD4_Final(WINPR_MD4_CTX* ctx, BYTE* output)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
MD4_Final(output, (MD4_CTX*) ctx); if (MD4_Final(output, (MD4_CTX*) ctx) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C)
mbedtls_md4_finish((mbedtls_md4_context*) ctx, output); mbedtls_md4_finish((mbedtls_md4_context*) ctx, output);
mbedtls_md4_free((mbedtls_md4_context*) ctx); mbedtls_md4_free((mbedtls_md4_context*) ctx);
#endif #endif
return TRUE;
} }
void winpr_MD4(const BYTE* input, size_t ilen, BYTE* output) BOOL winpr_MD4(const BYTE* input, size_t ilen, BYTE* output)
{ {
WINPR_MD4_CTX ctx; WINPR_MD4_CTX ctx;
winpr_MD4_Init(&ctx);
winpr_MD4_Update(&ctx, input, ilen); if (!winpr_MD4_Init(&ctx))
winpr_MD4_Final(&ctx, output); return FALSE;
if (!winpr_MD4_Update(&ctx, input, ilen))
return FALSE;
return winpr_MD4_Final(&ctx, output);
} }
/** /**
* SHA1 * SHA1
*/ */
void winpr_SHA1_Init(WINPR_SHA1_CTX* ctx) BOOL winpr_SHA1_Init(WINPR_SHA1_CTX* ctx)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
SHA1_Init((SHA_CTX*) ctx); if (SHA1_Init((SHA_CTX*) ctx) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C)
mbedtls_sha1_init((mbedtls_sha1_context*) ctx); mbedtls_sha1_init((mbedtls_sha1_context*) ctx);
mbedtls_sha1_starts((mbedtls_sha1_context*) ctx); mbedtls_sha1_starts((mbedtls_sha1_context*) ctx);
#endif #endif
return TRUE;
} }
void winpr_SHA1_Update(WINPR_SHA1_CTX* ctx, const BYTE* input, size_t ilen) BOOL winpr_SHA1_Update(WINPR_SHA1_CTX* ctx, const BYTE* input, size_t ilen)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
SHA1_Update((SHA_CTX*) ctx, input, ilen); if (SHA1_Update((SHA_CTX*) ctx, input, ilen) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C)
mbedtls_sha1_update((mbedtls_sha1_context*) ctx, input, ilen); mbedtls_sha1_update((mbedtls_sha1_context*) ctx, input, ilen);
#endif #endif
return TRUE;
} }
void winpr_SHA1_Final(WINPR_SHA1_CTX* ctx, BYTE* output) BOOL winpr_SHA1_Final(WINPR_SHA1_CTX* ctx, BYTE* output)
{ {
#if defined(WITH_OPENSSL) #if defined(WITH_OPENSSL)
SHA1_Final(output, (SHA_CTX*) ctx); if (SHA1_Final(output, (SHA_CTX*) ctx) != 1)
return FALSE;
#elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C) #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_SHA1_C)
mbedtls_sha1_finish((mbedtls_sha1_context*) ctx, output); mbedtls_sha1_finish((mbedtls_sha1_context*) ctx, output);
mbedtls_sha1_free((mbedtls_sha1_context*) ctx); mbedtls_sha1_free((mbedtls_sha1_context*) ctx);
#endif #endif
return TRUE;
} }
void winpr_SHA1(const BYTE* input, size_t ilen, BYTE* output) BOOL winpr_SHA1(const BYTE* input, size_t ilen, BYTE* output)
{ {
WINPR_SHA1_CTX ctx; WINPR_SHA1_CTX ctx;
winpr_SHA1_Init(&ctx);
winpr_SHA1_Update(&ctx, input, ilen); if (!winpr_SHA1_Init(&ctx))
winpr_SHA1_Final(&ctx, output); return FALSE;
if (!winpr_SHA1_Update(&ctx, input, ilen))
return FALSE;
return winpr_SHA1_Final(&ctx, output);
} }
/** /**