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