1251876Speter/*
2251876Speter * This is work is derived from material Copyright RSA Data Security, Inc.
3251876Speter *
4251876Speter * The RSA copyright statement and Licence for that original material is
5251876Speter * included below. This is followed by the Apache copyright statement and
6251876Speter * licence for the modifications made to that material.
7251876Speter */
8251876Speter
9251876Speter/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
10251876Speter   rights reserved.
11251876Speter
12251876Speter   License to copy and use this software is granted provided that it
13251876Speter   is identified as the "RSA Data Security, Inc. MD5 Message-Digest
14251876Speter   Algorithm" in all material mentioning or referencing this software
15251876Speter   or this function.
16251876Speter
17251876Speter   License is also granted to make and use derivative works provided
18251876Speter   that such works are identified as "derived from the RSA Data
19251876Speter   Security, Inc. MD5 Message-Digest Algorithm" in all material
20251876Speter   mentioning or referencing the derived work.
21251876Speter
22251876Speter   RSA Data Security, Inc. makes no representations concerning either
23251876Speter   the merchantability of this software or the suitability of this
24251876Speter   software for any particular purpose. It is provided "as is"
25251876Speter   without express or implied warranty of any kind.
26251876Speter
27251876Speter   These notices must be retained in any copies of any part of this
28251876Speter   documentation and/or software.
29251876Speter */
30251876Speter
31251876Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
32251876Speter * contributor license agreements.  See the NOTICE file distributed with
33251876Speter * this work for additional information regarding copyright ownership.
34251876Speter * The ASF licenses this file to You under the Apache License, Version 2.0
35251876Speter * (the "License"); you may not use this file except in compliance with
36251876Speter * the License.  You may obtain a copy of the License at
37251876Speter *
38251876Speter *     http://www.apache.org/licenses/LICENSE-2.0
39251876Speter *
40251876Speter * Unless required by applicable law or agreed to in writing, software
41251876Speter * distributed under the License is distributed on an "AS IS" BASIS,
42251876Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43251876Speter * See the License for the specific language governing permissions and
44251876Speter * limitations under the License.
45251876Speter */
46251876Speter
47251876Speter#ifndef APR_MD5_H
48251876Speter#define APR_MD5_H
49251876Speter
50251876Speter#include "apu.h"
51251876Speter#include "apr_xlate.h"
52251876Speter
53251876Speter#ifdef __cplusplus
54251876Speterextern "C" {
55251876Speter#endif
56251876Speter/**
57251876Speter * @file apr_md5.h
58251876Speter * @brief APR MD5 Routines
59251876Speter */
60251876Speter
61251876Speter/**
62251876Speter * @defgroup APR_MD5 MD5 Routines
63251876Speter * @ingroup APR
64251876Speter * @{
65251876Speter */
66251876Speter
67251876Speter/** The MD5 digest size */
68251876Speter#define APR_MD5_DIGESTSIZE 16
69251876Speter
70251876Speter/** @see apr_md5_ctx_t */
71251876Spetertypedef struct apr_md5_ctx_t apr_md5_ctx_t;
72251876Speter
73251876Speter/** MD5 context. */
74251876Speterstruct apr_md5_ctx_t {
75251876Speter    /** state (ABCD) */
76251876Speter    apr_uint32_t state[4];
77251876Speter    /** number of bits, modulo 2^64 (lsb first) */
78251876Speter    apr_uint32_t count[2];
79251876Speter    /** input buffer */
80251876Speter    unsigned char buffer[64];
81251876Speter    /** translation handle
82251876Speter     *  ignored if xlate is unsupported
83251876Speter     */
84251876Speter    apr_xlate_t *xlate;
85251876Speter};
86251876Speter
87251876Speter/**
88251876Speter * MD5 Initialize.  Begins an MD5 operation, writing a new context.
89251876Speter * @param context The MD5 context to initialize.
90251876Speter */
91251876SpeterAPU_DECLARE(apr_status_t) apr_md5_init(apr_md5_ctx_t *context);
92251876Speter
93251876Speter/**
94251876Speter * MD5 translation setup.  Provides the APR translation handle to be used
95251876Speter * for translating the content before calculating the digest.
96251876Speter * @param context The MD5 content to set the translation for.
97251876Speter * @param xlate The translation handle to use for this MD5 context
98251876Speter */
99251876SpeterAPU_DECLARE(apr_status_t) apr_md5_set_xlate(apr_md5_ctx_t *context,
100251876Speter                                            apr_xlate_t *xlate);
101251876Speter
102251876Speter/**
103251876Speter * MD5 block update operation.  Continue an MD5 message-digest operation,
104251876Speter * processing another message block, and updating the context.
105251876Speter * @param context The MD5 content to update.
106251876Speter * @param input next message block to update
107251876Speter * @param inputLen The length of the next message block
108251876Speter */
109251876SpeterAPU_DECLARE(apr_status_t) apr_md5_update(apr_md5_ctx_t *context,
110251876Speter                                         const void *input,
111251876Speter                                         apr_size_t inputLen);
112251876Speter
113251876Speter/**
114251876Speter * MD5 finalization.  Ends an MD5 message-digest operation, writing the
115251876Speter * message digest and zeroing the context
116251876Speter * @param digest The final MD5 digest
117251876Speter * @param context The MD5 content we are finalizing.
118251876Speter */
119251876SpeterAPU_DECLARE(apr_status_t) apr_md5_final(unsigned char digest[APR_MD5_DIGESTSIZE],
120251876Speter                                        apr_md5_ctx_t *context);
121251876Speter
122251876Speter/**
123251876Speter * MD5 in one step
124251876Speter * @param digest The final MD5 digest
125251876Speter * @param input The message block to use
126251876Speter * @param inputLen The length of the message block
127251876Speter */
128251876SpeterAPU_DECLARE(apr_status_t) apr_md5(unsigned char digest[APR_MD5_DIGESTSIZE],
129251876Speter                                  const void *input,
130251876Speter                                  apr_size_t inputLen);
131251876Speter
132251876Speter/**
133251876Speter * Encode a password using an MD5 algorithm
134251876Speter * @param password The password to encode
135253734Speter * @param salt The salt string to use for the encoding
136251876Speter * @param result The string to store the encoded password in
137251876Speter * @param nbytes The size of the result buffer
138251876Speter */
139251876SpeterAPU_DECLARE(apr_status_t) apr_md5_encode(const char *password, const char *salt,
140251876Speter                                         char *result, apr_size_t nbytes);
141251876Speter
142253734Speter/**
143253734Speter * Encode a password using the bcrypt algorithm
144253734Speter * @param password The password to encode
145253734Speter * @param count The cost of the encoding, possible values are 4 to 31
146253734Speter * @param salt Pointer to binary data to be used as salt for the encoding
147253734Speter * @param salt_len The size of the salt data (must be >= 16)
148253734Speter * @param out The string to store the encoded password in
149253734Speter * @param out_len The size of the result buffer (must be >= 61)
150253734Speter */
151253734SpeterAPU_DECLARE(apr_status_t) apr_bcrypt_encode(const char *pw,
152253734Speter                                            unsigned int count,
153253734Speter                                            const unsigned char *salt,
154253734Speter                                            apr_size_t salt_len,
155253734Speter                                            char *out, apr_size_t out_len);
156251876Speter
157251876Speter/**
158253734Speter * Validate hashes created by APR-supported algorithms: md5, bcrypt, and sha1.
159251876Speter * hashes created by crypt are supported only on platforms that provide
160251876Speter * crypt(3), so don't rely on that function unless you know that your
161251876Speter * application will be run only on platforms that support it.  On platforms
162251876Speter * that don't support crypt(3), this falls back to a clear text string
163251876Speter * comparison.
164251876Speter * @param passwd The password to validate
165251876Speter * @param hash The password to validate against
166251876Speter */
167251876SpeterAPU_DECLARE(apr_status_t) apr_password_validate(const char *passwd,
168251876Speter                                                const char *hash);
169251876Speter
170251876Speter
171251876Speter/** @} */
172251876Speter#ifdef __cplusplus
173251876Speter}
174251876Speter#endif
175251876Speter
176251876Speter#endif /* !APR_MD5_H */
177