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