Back to index

openldap  2.4.31
sha2.h
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /*
00003  * FILE:      sha2.h
00004  * AUTHOR:    Aaron D. Gifford - http://www.aarongifford.com/
00005  * 
00006  * Copyright (c) 2000-2001, Aaron D. Gifford
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. Neither the name of the copyright holder nor the names of contributors
00018  *    may be used to endorse or promote products derived from this software
00019  *    without specific prior written permission.
00020  * 
00021  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
00025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
00034  */
00035 
00036 #ifndef __SHA2_H__
00037 #define __SHA2_H__
00038 
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042 
00043 
00044 /*
00045  * Import u_intXX_t size_t type definitions from system headers.  You
00046  * may need to change this, or define these things yourself in this
00047  * file.
00048  */
00049 #include <sys/types.h>
00050 
00051 #ifdef SHA2_USE_INTTYPES_H
00052 
00053 #include <inttypes.h>
00054 
00055 #endif /* SHA2_USE_INTTYPES_H */
00056 
00057 
00058 /*** SHA-256/384/512 Various Length Definitions ***********************/
00059 #define SHA256_BLOCK_LENGTH        64
00060 #define SHA256_DIGEST_LENGTH              32
00061 #define SHA256_DIGEST_STRING_LENGTH       (SHA256_DIGEST_LENGTH * 2 + 1)
00062 #define SHA384_BLOCK_LENGTH        128
00063 #define SHA384_DIGEST_LENGTH              48
00064 #define SHA384_DIGEST_STRING_LENGTH       (SHA384_DIGEST_LENGTH * 2 + 1)
00065 #define SHA512_BLOCK_LENGTH        128
00066 #define SHA512_DIGEST_LENGTH              64
00067 #define SHA512_DIGEST_STRING_LENGTH       (SHA512_DIGEST_LENGTH * 2 + 1)
00068 
00069 
00070 /*** SHA-256/384/512 Context Structures *******************************/
00071 /* NOTE: If your architecture does not define either u_intXX_t types or
00072  * uintXX_t (from inttypes.h), you may need to define things by hand
00073  * for your system:
00074  */
00075 #if 0
00076 typedef unsigned char u_int8_t;           /* 1-byte  (8-bits)  */
00077 typedef unsigned int u_int32_t;           /* 4-bytes (32-bits) */
00078 typedef unsigned long long u_int64_t;     /* 8-bytes (64-bits) */
00079 #endif
00080 /*
00081  * Most BSD systems already define u_intXX_t types, as does Linux.
00082  * Some systems, however, like Compaq's Tru64 Unix instead can use
00083  * uintXX_t types defined by very recent ANSI C standards and included
00084  * in the file:
00085  *
00086  *   #include <inttypes.h>
00087  *
00088  * If you choose to use <inttypes.h> then please define: 
00089  *
00090  *   #define SHA2_USE_INTTYPES_H
00091  *
00092  * Or on the command line during compile:
00093  *
00094  *   cc -DSHA2_USE_INTTYPES_H ...
00095  */
00096 #ifdef SHA2_USE_INTTYPES_H
00097 
00098 typedef struct _SHA256_CTX {
00099        uint32_t      state[8];
00100        uint64_t      bitcount;
00101        uint8_t       buffer[SHA256_BLOCK_LENGTH];
00102 } SHA256_CTX;
00103 typedef struct _SHA512_CTX {
00104        uint64_t      state[8];
00105        uint64_t      bitcount[2];
00106        uint8_t       buffer[SHA512_BLOCK_LENGTH];
00107 } SHA512_CTX;
00108 
00109 #else /* SHA2_USE_INTTYPES_H */
00110 
00111 typedef struct _SHA256_CTX {
00112        u_int32_t     state[8];
00113        u_int64_t     bitcount;
00114        u_int8_t      buffer[SHA256_BLOCK_LENGTH];
00115 } SHA256_CTX;
00116 typedef struct _SHA512_CTX {
00117        u_int64_t     state[8];
00118        u_int64_t     bitcount[2];
00119        u_int8_t      buffer[SHA512_BLOCK_LENGTH];
00120 } SHA512_CTX;
00121 
00122 #endif /* SHA2_USE_INTTYPES_H */
00123 
00124 typedef SHA512_CTX SHA384_CTX;
00125 
00126 
00127 /*** SHA-256/384/512 Function Prototypes ******************************/
00128 #ifndef NOPROTO
00129 #ifdef SHA2_USE_INTTYPES_H
00130 
00131 void SHA256_Init(SHA256_CTX *);
00132 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
00133 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
00134 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
00135 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
00136 
00137 void SHA384_Init(SHA384_CTX*);
00138 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
00139 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
00140 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
00141 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
00142 
00143 void SHA512_Init(SHA512_CTX*);
00144 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
00145 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
00146 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
00147 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
00148 
00149 #else /* SHA2_USE_INTTYPES_H */
00150 
00151 void SHA256_Init(SHA256_CTX *);
00152 void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
00153 void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
00154 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
00155 char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
00156 
00157 void SHA384_Init(SHA384_CTX*);
00158 void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
00159 void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
00160 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
00161 char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
00162 
00163 void SHA512_Init(SHA512_CTX*);
00164 void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
00165 void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
00166 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
00167 char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
00168 
00169 #endif /* SHA2_USE_INTTYPES_H */
00170 
00171 #else /* NOPROTO */
00172 
00173 void SHA256_Init();
00174 void SHA256_Update();
00175 void SHA256_Final();
00176 char* SHA256_End();
00177 char* SHA256_Data();
00178 
00179 void SHA384_Init();
00180 void SHA384_Update();
00181 void SHA384_Final();
00182 char* SHA384_End();
00183 char* SHA384_Data();
00184 
00185 void SHA512_Init();
00186 void SHA512_Update();
00187 void SHA512_Final();
00188 char* SHA512_End();
00189 char* SHA512_Data();
00190 
00191 #endif /* NOPROTO */
00192 
00193 #ifdef __cplusplus
00194 }
00195 #endif /* __cplusplus */
00196 
00197 #endif /* __SHA2_H__ */
00198