apr_sha1.h revision 251876
1209513Simp/* Licensed to the Apache Software Foundation (ASF) under one or more
2209513Simp * contributor license agreements.  See the NOTICE file distributed with
3209513Simp * this work for additional information regarding copyright ownership.
4209513Simp * The ASF licenses this file to You under the Apache License, Version 2.0
5209513Simp * (the "License"); you may not use this file except in compliance with
6209513Simp * the License.  You may obtain a copy of the License at
7209513Simp *
8209513Simp *     http://www.apache.org/licenses/LICENSE-2.0
9209513Simp *
10209513Simp * Unless required by applicable law or agreed to in writing, software
11209513Simp * distributed under the License is distributed on an "AS IS" BASIS,
12209513Simp * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13209513Simp * See the License for the specific language governing permissions and
14209513Simp * limitations under the License.
15218776Sjpaetzel */
16209513Simp/* NIST Secure Hash Algorithm
17218776Sjpaetzel * 	heavily modified by Uwe Hollerbach uh@alumni.caltech edu
18209513Simp * 	from Peter C. Gutmann's implementation as found in
19209513Simp * 	Applied Cryptography by Bruce Schneier
20209513Simp * 	This code is hereby placed in the public domain
21209513Simp */
22209513Simp
23209513Simp#ifndef APR_SHA1_H
24209513Simp#define APR_SHA1_H
25209513Simp
26209513Simp#include "apu.h"
27209513Simp#include "apr_general.h"
28209513Simp
29209513Simp#ifdef __cplusplus
30218776Sjpaetzelextern "C" {
31218776Sjpaetzel#endif
32218776Sjpaetzel
33218776Sjpaetzel/**
34218776Sjpaetzel * @file apr_sha1.h
35218776Sjpaetzel * @brief APR-UTIL SHA1 library
36218776Sjpaetzel */
37218776Sjpaetzel
38218776Sjpaetzel/** size of the SHA1 DIGEST */
39218776Sjpaetzel#define APR_SHA1_DIGESTSIZE 20
40218776Sjpaetzel
41218776Sjpaetzel/**
42218776Sjpaetzel * Define the Magic String prefix that identifies a password as being
43209513Simp * hashed using our algorithm.
44209513Simp */
45209513Simp#define APR_SHA1PW_ID "{SHA}"
46209513Simp
47209513Simp/** length of the SHA Password */
48209513Simp#define APR_SHA1PW_IDLEN 5
49209513Simp
50209513Simp/** @see apr_sha1_ctx_t */
51209513Simptypedef struct apr_sha1_ctx_t apr_sha1_ctx_t;
52209513Simp
53209513Simp/**
54209513Simp * SHA1 context structure
55209513Simp */
56209513Simpstruct apr_sha1_ctx_t {
57209513Simp    /** message digest */
58209513Simp    apr_uint32_t digest[5];
59209513Simp    /** 64-bit bit counts */
60209513Simp    apr_uint32_t count_lo, count_hi;
61209513Simp    /** SHA data buffer */
62209513Simp    apr_uint32_t data[16];
63209513Simp    /** unprocessed amount in data */
64209513Simp    int local;
65209513Simp};
66209513Simp
67209513Simp/**
68209513Simp * Provide a means to SHA1 crypt/encode a plaintext password in a way which
69209513Simp * makes password file compatible with those commonly use in netscape web
70209513Simp * and ldap installations.
71209513Simp * @param clear The plaintext password
72209513Simp * @param len The length of the plaintext password
73209513Simp * @param out The encrypted/encoded password
74209513Simp * @note SHA1 support is useful for migration purposes, but is less
75209513Simp *     secure than Apache's password format, since Apache's (MD5)
76209513Simp *     password format uses a random eight character salt to generate
77209513Simp *     one of many possible hashes for the same password.  Netscape
78209513Simp *     uses plain SHA1 without a salt, so the same password
79209513Simp *     will always generate the same hash, making it easier
80209513Simp *     to break since the search space is smaller.
81209513Simp */
82209513SimpAPU_DECLARE(void) apr_sha1_base64(const char *clear, int len, char *out);
83209513Simp
84209513Simp/**
85209513Simp * Initialize the SHA digest
86209513Simp * @param context The SHA context to initialize
87209513Simp */
88209513SimpAPU_DECLARE(void) apr_sha1_init(apr_sha1_ctx_t *context);
89209513Simp
90209513Simp/**
91209513Simp * Update the SHA digest
92209513Simp * @param context The SHA1 context to update
93209513Simp * @param input The buffer to add to the SHA digest
94209513Simp * @param inputLen The length of the input buffer
95209513Simp */
96209513SimpAPU_DECLARE(void) apr_sha1_update(apr_sha1_ctx_t *context, const char *input,
97209513Simp                                unsigned int inputLen);
98209513Simp
99209513Simp/**
100209513Simp * Update the SHA digest with binary data
101209513Simp * @param context The SHA1 context to update
102209513Simp * @param input The buffer to add to the SHA digest
103209513Simp * @param inputLen The length of the input buffer
104209513Simp */
105209513SimpAPU_DECLARE(void) apr_sha1_update_binary(apr_sha1_ctx_t *context,
106209513Simp                                       const unsigned char *input,
107209513Simp                                       unsigned int inputLen);
108209513Simp
109209513Simp/**
110209513Simp * Finish computing the SHA digest
111209513Simp * @param digest the output buffer in which to store the digest
112209513Simp * @param context The context to finalize
113209513Simp */
114209513SimpAPU_DECLARE(void) apr_sha1_final(unsigned char digest[APR_SHA1_DIGESTSIZE],
115209513Simp                               apr_sha1_ctx_t *context);
116209513Simp
117209513Simp#ifdef __cplusplus
118209513Simp}
119209513Simp#endif
120209513Simp
121209513Simp#endif	/* APR_SHA1_H */
122209513Simp