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