pdkim API cleanup
[users/jgh/exim.git] / src / src / pdkim / sha2.h
1 /* $Cambridge: exim/src/src/pdkim/sha2.h,v 1.1.2.2 2009/03/17 14:56:55 tom Exp $ */
2 /**
3  * \file sha2.h
4  *
5  *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
6  *
7  *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 #ifndef POLARSSL_SHA2_H
24 #define POLARSSL_SHA2_H
25
26 /**
27  * \brief          SHA-256 context structure
28  */
29 #ifndef HAVE_SHA2_CONTEXT
30 #define HAVE_SHA2_CONTEXT
31 typedef struct sha2_context sha2_context;
32 #endif
33
34 struct sha2_context
35 {
36     unsigned long total[2];     /*!< number of bytes processed  */
37     unsigned long state[8];     /*!< intermediate digest state  */
38     unsigned char buffer[64];   /*!< data block being processed */
39
40     unsigned char ipad[64];     /*!< HMAC: inner padding        */
41     unsigned char opad[64];     /*!< HMAC: outer padding        */
42     int is224;                  /*!< 0 => SHA-256, else SHA-224 */
43 };
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /**
50  * \brief          SHA-256 context setup
51  *
52  * \param ctx      context to be initialized
53  * \param is224    0 = use SHA256, 1 = use SHA224
54  */
55 void sha2_starts( sha2_context *ctx, int is224 );
56
57 /**
58  * \brief          SHA-256 process buffer
59  *
60  * \param ctx      SHA-256 context
61  * \param input    buffer holding the  data
62  * \param ilen     length of the input data
63  */
64 void sha2_update( sha2_context *ctx, unsigned char *input, int ilen );
65
66 /**
67  * \brief          SHA-256 final digest
68  *
69  * \param ctx      SHA-256 context
70  * \param output   SHA-224/256 checksum result
71  */
72 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
73
74 /**
75  * \brief          Output = SHA-256( input buffer )
76  *
77  * \param input    buffer holding the  data
78  * \param ilen     length of the input data
79  * \param output   SHA-224/256 checksum result
80  * \param is224    0 = use SHA256, 1 = use SHA224
81  */
82 void sha2( unsigned char *input, int ilen,
83            unsigned char output[32], int is224 );
84
85 /**
86  * \brief          Output = SHA-256( file contents )
87  *
88  * \param path     input file name
89  * \param output   SHA-224/256 checksum result
90  * \param is224    0 = use SHA256, 1 = use SHA224
91  *
92  * \return         0 if successful, 1 if fopen failed,
93  *                 or 2 if fread failed
94  */
95 int sha2_file( char *path, unsigned char output[32], int is224 );
96
97 /**
98  * \brief          SHA-256 HMAC context setup
99  *
100  * \param ctx      HMAC context to be initialized
101  * \param key      HMAC secret key
102  * \param keylen   length of the HMAC key
103  * \param is224    0 = use SHA256, 1 = use SHA224
104  */
105 void sha2_hmac_starts( sha2_context *ctx, unsigned char *key, int keylen,
106                        int is224 );
107
108 /**
109  * \brief          SHA-256 HMAC process buffer
110  *
111  * \param ctx      HMAC context
112  * \param input    buffer holding the  data
113  * \param ilen     length of the input data
114  */
115 void sha2_hmac_update( sha2_context *ctx, unsigned char *input, int ilen );
116
117 /**
118  * \brief          SHA-256 HMAC final digest
119  *
120  * \param ctx      HMAC context
121  * \param output   SHA-224/256 HMAC checksum result
122  */
123 void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
124
125 /**
126  * \brief          Output = HMAC-SHA-256( hmac key, input buffer )
127  *
128  * \param key      HMAC secret key
129  * \param keylen   length of the HMAC key
130  * \param input    buffer holding the  data
131  * \param ilen     length of the input data
132  * \param output   HMAC-SHA-224/256 result
133  * \param is224    0 = use SHA256, 1 = use SHA224
134  */
135 void sha2_hmac( unsigned char *key, int keylen,
136                 unsigned char *input, int ilen,
137                 unsigned char output[32], int is224 );
138
139 #ifdef __cplusplus
140 }
141 #endif
142
143 #endif /* sha2.h */