/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the Collabora Office project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#pragma once

#include "sal/config.h"

#include "sal/saldllapi.h"
#include "sal/types.h"

#ifdef __cplusplus
extern "C" {
#endif

/** Digest Handle opaque type.
 */
typedef void* rtlDigest;


/** Digest Algorithm enumeration.
    @see rtl_digest_create()
 */
enum __rtl_DigestAlgorithm
{
    rtl_Digest_AlgorithmSHA1_StarOfficeBug,

    rtl_Digest_AlgorithmInvalid,
    rtl_Digest_Algorithm_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
};

/** Digest Algorithm type.
 */
typedef enum __rtl_DigestAlgorithm rtlDigestAlgorithm;


/** Error Code enumeration.
 */
enum __rtl_DigestError
{
    rtl_Digest_E_None,
    rtl_Digest_E_Argument,
    rtl_Digest_E_Algorithm,
    rtl_Digest_E_BufferSize,
    rtl_Digest_E_Memory,
    rtl_Digest_E_Unknown,
    rtl_Digest_E_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
};

/** Error Code type.
 */
typedef enum __rtl_DigestError rtlDigestError;


/** Create a digest handle for the given algorithm.
    @see rtlDigestAlgorithm

    @param[in] Algorithm digest algorithm.
    @return Digest handle, or 0 upon failure.
 */
SAL_DLLPUBLIC rtlDigest SAL_CALL rtl_digest_create  (
    rtlDigestAlgorithm Algorithm
) SAL_THROW_EXTERN_C();


/** Destroy a digest handle.
    @post Digest handle destroyed and invalid.
    @param[in] Digest digest handle to be destroyed.
 */
SAL_DLLPUBLIC void SAL_CALL rtl_digest_destroy (
    rtlDigest Digest
) SAL_THROW_EXTERN_C();


/** Query the algorithm of a given digest.
    @param[in] Digest digest handle.
    @return digest algorithm, or <code>rtl_Digest_AlgorithmInvalid</code> upon failure.
 */
SAL_DLLPUBLIC rtlDigestAlgorithm SAL_CALL rtl_digest_queryAlgorithm (
    rtlDigest Digest
) SAL_THROW_EXTERN_C();


/** Query the length of a given digest.
    @param[in] Digest digest handle.
    @return digest length, or 0 upon failure.
 */
SAL_DLLPUBLIC sal_uInt32 SAL_CALL rtl_digest_queryLength (
    rtlDigest Digest
) SAL_THROW_EXTERN_C();


/** Initialize a digest with given data.
    @param[in] Digest  digest handle.
    @param[in] pData   data buffer.
    @param[in] nDatLen data length.

    @retval rtl_Digest_E_None upon success.
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_init (
    rtlDigest Digest,
    const sal_uInt8 *pData, sal_uInt32 nDatLen
) SAL_THROW_EXTERN_C();


/** Update a digest with given data.
    @param[in] Digest  digest handle.
    @param[in] pData   data buffer.
    @param[in] nDatLen data length.

    @retval rtl_Digest_E_None upon success.
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_update (
    rtlDigest Digest,
    const void *pData, sal_uInt32 nDatLen
) SAL_THROW_EXTERN_C();


/** Finalize a digest and retrieve the digest value.
    @pre  Digest value length must not be less than digest length.
    @post Digest initialized to accept another update sequence.
    @see      rtl_digest_queryLength()
    @see      rtl_digest_update()

    @param[in] Digest  digest handle.
    @param[in] pBuffer digest value buffer.
    @param[in] nBufLen digest value length.

    @retval rtl_Digest_E_None upon success.
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_get (
    rtlDigest Digest,
    sal_uInt8 *pBuffer, sal_uInt32 nBufLen
) SAL_THROW_EXTERN_C();

#define RTL_DIGEST_LENGTH_MD5 16

/** Evaluate a MD5 inspired Microsoft Office compatible digest value
    which is not actually MD5 from given data.

    This function performs an optimized call sequence on a
    single data buffer, avoiding digest creation and destruction.

    @param[in] pData   data buffer.
    @param[in] nDatLen data length.
    @param[in] pBuffer digest value buffer.
    @param[in] nBufLen digest value length.

    @retval rtl_Digest_E_None upon success.
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_MD5_MSOffice (
    const void *pData,   sal_uInt32 nDatLen,
    sal_uInt8  *pBuffer, sal_uInt32 nBufLen
) SAL_THROW_EXTERN_C();

/*========================================================================
 *
 * rtl_digest_SHA1_StarOfficeBug interface.
 *
 *======================================================================*/
#define RTL_DIGEST_LENGTH_SHA1 20

/** Create a SHA1 digest handle.

    The SHA1 digest algorithm is specified in
    FIPS PUB 180-1 (Supersedes FIPS PUB 180)
      Secure Hash Standard

    @deprecated The implementation is buggy and generates incorrect results
                for 52 <= (len % 64) <= 55; use only for bug-compatibility.

    @see rtl_digest_create()
 */
SAL_DLLPUBLIC rtlDigest SAL_CALL rtl_digest_createSHA1_StarOfficeBug (void) SAL_THROW_EXTERN_C();

/** Destroy a SHA1 digest handle.

    @deprecated The implementation is buggy and generates incorrect results
                for 52 <= (len % 64) <= 55; use only for bug-compatibility.

    @see rtl_digest_destroy()
 */
SAL_DLLPUBLIC void SAL_CALL rtl_digest_destroySHA1 (
    rtlDigest Digest
) SAL_THROW_EXTERN_C();

/** Update a SHA1 digest with given data.

    @deprecated The implementation is buggy and generates incorrect results
                for 52 <= (len % 64) <= 55; use only for bug-compatibility.

    @see rtl_digest_update()
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_updateSHA1 (
    rtlDigest Digest,
    const void *pData, sal_uInt32 nDatLen
) SAL_THROW_EXTERN_C();

/** Finalize a SHA1 digest and retrieve the digest value.

    @deprecated The implementation is buggy and generates incorrect results
                for 52 <= (len % 64) <= 55; use only for bug-compatibility.

    @see rtl_digest_get()
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_getSHA1 (
    rtlDigest Digest,
    sal_uInt8 *pBuffer, sal_uInt32 nBufLen
) SAL_THROW_EXTERN_C();

/** Evaluate a SHA1 digest value from given data.

    This function performs an optimized call sequence on a
    single data buffer, avoiding digest creation and destruction.

    @deprecated The implementation is buggy and generates incorrect results
                for 52 <= (len % 64) <= 55; use only for bug-compatibility.

    @see rtl_digest_updateSHA1()
    @see rtl_digest_getSHA1()

    @param[in] pData   data buffer.
    @param[in] nDatLen data length.
    @param[in] pBuffer digest value buffer.
    @param[in] nBufLen digest value length.

    @retval rtl_Digest_E_None upon success.
 */
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_SHA1_StarOfficeBug (
    const void *pData,   sal_uInt32 nDatLen,
    sal_uInt8  *pBuffer, sal_uInt32 nBufLen
) SAL_THROW_EXTERN_C();

/** Password-Based Key Derivation Function.

    The PBKDF2 key derivation function is specified in
    RFC 2898 (Informational)
      PKCS #5: Password-Based Cryptography Specification Version 2.0

    @deprecated The implementation is buggy and generates incorrect results
                for 52 <= (len % 64) <= 55; use only for bug-compatibility
                or if the input is guaranteed to have a good length
                by a start-key derivation round.

    @param[out] pKeyData  derived key
    @param[in]  nKeyLen   derived key length
    @param[in]  pPassData password
    @param[in]  nPassLen  password length
    @param[in]  pSaltData salt
    @param[in]  nSaltLen  salt length
    @param[in]  nCount    iteration count

    @retval rtl_Digest_E_None upon success.
*/
SAL_DLLPUBLIC rtlDigestError SAL_CALL rtl_digest_PBKDF2 (
    sal_uInt8       *pKeyData , sal_uInt32 nKeyLen,
    const sal_uInt8 *pPassData, sal_uInt32 nPassLen,
    const sal_uInt8 *pSaltData, sal_uInt32 nSaltLen,
    sal_uInt32       nCount
) SAL_THROW_EXTERN_C();

#ifdef __cplusplus
}
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
