// RIPEMD-160 written and placed in the public domain by Wei Dai /* * This code implements the MD4 message-digest algorithm. * The algorithm is due to Ron Rivest. This code was * written by Colin Plumb in 1993, no copyright is claimed. * This code is in the public domain; do with it what you wish. */ /* Adapted for TrueCrypt */ /* Adapted for VeraCrypt */ #if !defined(_UEFI) #include #endif // !defined(_UEFI) #include "Common/Tcdefs.h" #include "Common/Endian.h" #include "Rmd160.h" #define F(x, y, z) (x ^ y ^ z) #define G(x, y, z) (z ^ (x & (y^z))) #define H(x, y, z) (z ^ (x | ~y)) #define I(x, y, z) (y ^ (z & (x^y))) #define J(x, y, z) (x ^ (y | ~z)) #define PUT_64BIT_LE(cp, value) do { \ (cp)[7] = (byte) ((value) >> 56); \ (cp)[6] = (byte) ((value) >> 48); \ (cp)[5] = (byte) ((value) >> 40); \ (cp)[4] = (byte) ((value) >> 32); \ (cp)[3] = (byte) ((value) >> 24); \ (cp)[2] = (byte) ((value) >> 16); \ (cp)[1] = (byte) ((value) >> 8); \ (cp)[0] = (byte) (value); } while (0) #define PUT_32BIT_LE(cp, value) do { \ (cp)[3] = (byte) ((value) >> 24); \ (cp)[2] = (byte) ((value) >> 16); \ (cp)[1] = (byte) ((value) >> 8); \ (cp)[0] = (byte) (value); } while (0) #ifndef TC_MINIMIZE_CODE_SIZE static byte PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; #else static byte PADDING[64]; #endif void RMD160Init (RMD160_CTX *ctx) { ctx->count = 0; ctx->state[0] = 0x67452301; ctx->state[1] = 0xefcdab89; ctx->state[2] = 0x98badcfe; ctx->state[3] = 0x10325476; ctx->state[4] = 0xc3d2e1f0; PADDING[0] = 0x80; } /* * Update context to reflect the concatenation of another buffer full * of bytes. */ void RMD160Update (RMD160_CTX *ctx, const unsigned char *input, unsigned __int32 lenArg) { #ifndef TC_WINDOWS_BOOT uint64 len = lenArg; #else uint32 len = lenArg; #endif unsigned int have, need; /* Check how many bytes we already have and how many more we need. */ have = (unsigned int) ((ctx->count) & (RIPEMD160_BLOCK_LENGTH - 1)); need = RIPEMD160_BLOCK_LENGTH - have; /* Update bitcount */ ctx->count += len; if (len >= need) { if (have != 0) { memcpy (ctx->buffer + have, input, (size_t) need); RMD160Transform ((uint32 *) ctx->state, (const uint32 *) ctx->buffer); input += need; len -= need; have = 0; } /* Process data in RIPEMD160_BLOCK_LENGTH-byte chunks. */ while (len >= RIPEMD160_BLOCK_LENGTH) { RMD160Transform ((uint32 *) ctx->state, (const uint32 *) input); input += RIPEMD160_BLOCK_LENGTH; len -= RIPEMD160_BLOCK_LENGTH; } } /* Handle any remaining bytes of data. */ if (len != 0) memcpy (ctx->buffer + have, input, (size_t) len); } /* * Pad pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ static void RMD160Pad(RMD160_CTX *ctx) { byte count[8]; uint32 padlen; /* Convert count to 8 bytes in little endian order. */ #ifndef TC_WINDOWS_BOOT uint64 bitcount = ctx->count << 3; PUT_64BIT_LE(count, bitcount); #else *(uint32 *) (count + 4) = 0; *(uint32 *) (count + 0) = ctx->count << 3; #endif /* Pad out to 56 mod 64. */ padlen = RIPEMD160_BLOCK_LENGTH - (uint32)((ctx->count) & (RIPEMD160_BLOCK_LENGTH - 1)); if (padlen < 1 + 8) padlen += RIPEMD160_BLOCK_LENGTH; RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ RMD160Update(ctx, count, 8); } /* * Final wrapup--call RMD160Pad, fill in digest and zero out ctx. */ void RMD160Final(unsigned char *digest, RMD160_CTX *ctx) { int i; RMD160Pad(ctx); if (digest) { for (i = 0; i < 5; i++) PUT_32BIT_LE(digest + i * 4, ctx->state[i]); #ifndef TC_WINDOWS_BOOT burn (ctx, sizeof(*ctx)); #endif } } #ifndef TC_MINIMIZE_CODE_SIZE #define word32 unsigned __int32 #define k0 0 #define k1 0x5a827999UL #define k2 0x6ed9eba1UL #define k3 0x8f1bbcdcUL #define k4 0xa953fd4eUL #define k5 0x50a28be6UL #define k6 0x5c4dd124UL #define k7 0x6d703ef3UL #define k8 0x7a6d76e9UL #define k9 0 static word32 rotlFixed (word32 x, unsigned int y) { return (word32)((x<>(sizeof(word32)*8-y))); } #define Subround(f, a, b, c, d, e, x, s, k)
/*
 Legal Notice: Some portions of the source code contained in this file were
 derived from the source code of Encryption for the Masses 2.02a, which is
 Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License
 Agreement for Encryption for the Masses'. Modifications and additions to
 the original source code (contained in this file) and all other portions
 of this file are Copyright (c) 2003-2008 TrueCrypt Developers Association
 and are governed by the TrueCrypt License 3.0 the full text of which is
 contained in the file License.txt included in TrueCrypt binary and source
 code distribution packages. */

#ifndef TC_HEADER_PKCS5
#define TC_HEADER_PKCS5

#include "Tcdefs.h"

#if defined(__cplusplus)
extern "C"
{
#endif
void hmac_sha256 (char *k, int lk, char *d, int ld, char *out);
void derive_u_sha256 (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *u, int b);
void derive_key_sha256 (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *dk, int dklen);

void hmac_sha512 (char *k, int lk, char *d, int ld, char *out, int t);
void derive_u_sha512 (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *u, int b);
void derive_key_sha512 (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *dk, int dklen);
void hmac_ripemd160 (char *key, int keylen, char *input, int len, char *digest);
void derive_u_ripemd160 (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *u, int b);
void derive_key_ripemd160 (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *dk, int dklen);
void hmac_whirlpool (char *k, int lk, char *d, int ld, char *out, int t);
void derive_u_whirlpool (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *u, int b);
void derive_key_whirlpool (char *pwd, int pwd_len, char *salt, int salt_len, int iterations, char *dk, int dklen);
int get_pkcs5_iteration_count (int pkcs5_prf_id, BOOL truecryptMode, BOOL bBoot);
char *get_pkcs5_prf_name (int pkcs5_prf_id);

#if defined(__cplusplus)
}
#endif

#endif // TC_HEADER_PKCS5