Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 2 | Copyright (C) 2006-2013 Werner Dittmann |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 5 | it under the terms of the GNU Lesser General Public License as published by |
Alexandre Lision | 51140e1 | 2013-12-02 10:54:09 -0500 | [diff] [blame] | 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Functions to compute SHA256 digest. |
| 20 | * |
| 21 | * @author: Werner Dittmann <Werner.Dittmann@t-online.de> |
| 22 | */ |
| 23 | |
| 24 | #ifndef _SHA256_H |
| 25 | #define _SHA256_H |
| 26 | |
| 27 | /** |
| 28 | * @file sha256.h |
| 29 | * @brief Function that provide SHA256 support |
| 30 | * |
| 31 | * @ingroup GNU_ZRTP |
| 32 | * @{ |
| 33 | */ |
| 34 | |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | #ifndef SHA256_DIGEST_LENGTH |
| 38 | #define SHA256_DIGEST_LENGTH 32 |
| 39 | #endif |
| 40 | |
| 41 | /** |
| 42 | * Compute SHA256 digest. |
| 43 | * |
| 44 | * This functions takes one data chunk and computes its SHA256 digest. This |
| 45 | * function creates and deletes an own SHA256 context to perform the SHA256 |
| 46 | * operations. |
| 47 | * |
| 48 | * @param data |
| 49 | * Points to the data chunk. |
| 50 | * @param data_length |
| 51 | * Length of the data in bytes |
| 52 | * @param digest |
| 53 | * Points to a buffer that receives the computed digest. This |
| 54 | * buffer must have a size of at least 32 bytes (SHA256_DIGEST_LENGTH). |
| 55 | */ |
| 56 | void sha256(unsigned char *data, |
| 57 | unsigned int data_length, |
| 58 | unsigned char *digest); |
| 59 | |
| 60 | /** |
| 61 | * Compute SHA256 digest over several data cunks. |
| 62 | * |
| 63 | * This functions takes several data chunks and computes the SHA256 digest. |
| 64 | * This function creates and deletes an own SHA256 context to perform the |
| 65 | * SHA256 operations. |
| 66 | * |
| 67 | * @param data |
| 68 | * Points to an array of pointers that point to the data chunks. A NULL |
| 69 | * pointer in an array element terminates the data chunks. |
| 70 | * @param data_length |
| 71 | * Points to an array of integers that hold the length of each data chunk. |
| 72 | * @param digest |
| 73 | * Points to a buffer that receives the computed digest. This |
| 74 | * buffer must have a size of at least 32 bytes (SHA256_DIGEST_LENGTH). |
| 75 | */ |
| 76 | void sha256(unsigned char *data[], |
| 77 | unsigned int data_length[], |
| 78 | unsigned char *digest); |
| 79 | /** |
| 80 | * Create and initialize a SHA256 context. |
| 81 | * |
| 82 | * An application uses this context to hash several data into one SHA256 |
| 83 | * digest. See also sha256Ctx(...) and closeSha256Context(...). |
| 84 | * |
| 85 | * @return Returns a pointer to the initialized SHA256 context |
| 86 | */ |
| 87 | void* createSha256Context(); |
| 88 | |
| 89 | /** |
| 90 | * Compute a digest and close the SHa256 digest. |
| 91 | * |
| 92 | * An application uses this function to compute the SHA256 digest and to |
| 93 | * close the SHA256 context. |
| 94 | * |
| 95 | * @param ctx |
| 96 | * Points to the SHA256 context. |
| 97 | * @param digest |
| 98 | * If this pointer is not NULL then it must point to a byte array that |
| 99 | * is big enough to hold the SHA256 digest (256 bit = 32 Bytes). If this |
| 100 | * pointer is NULL then the functions does not compute the digest but |
| 101 | * closes the context only. The context cannot be used anymore. |
| 102 | */ |
| 103 | void closeSha256Context(void* ctx, |
| 104 | unsigned char* digest); |
| 105 | |
| 106 | /** |
| 107 | * Update the SHA256 context with data. |
| 108 | * |
| 109 | * This functions updates the SHA256 context with some data. |
| 110 | * See also CloseSha256Context(...) how to get the digest. |
| 111 | * |
| 112 | * @param ctx |
| 113 | * Points to the SHA256 context. |
| 114 | * @param data |
| 115 | * Points to the data to update the context. |
| 116 | * @param dataLength |
| 117 | * The length of the data in bytes. |
| 118 | */ |
| 119 | void sha256Ctx(void* ctx, unsigned char* data, |
| 120 | unsigned int dataLength); |
| 121 | |
| 122 | /** |
| 123 | * Update the SHA256 context with several data chunks. |
| 124 | * |
| 125 | * This functions updates the SHA256 context with some data. |
| 126 | * See also CloseSha256Context(...) how to get the digest. |
| 127 | * |
| 128 | * @param ctx |
| 129 | * Points to the SHA256 context. |
| 130 | * @param dataChunks |
| 131 | * Points to an array of pointers that point to the data chunks. A NULL |
| 132 | * pointer in an array element terminates the data chunks. |
| 133 | * @param dataChunkLength |
| 134 | * Points to an array of integers that hold the length of each data chunk. |
| 135 | * |
| 136 | */ |
| 137 | void sha256Ctx(void* ctx, unsigned char* dataChunks[], |
| 138 | unsigned int dataChunkLength[]); |
| 139 | |
| 140 | /** |
| 141 | * @} |
| 142 | */ |
| 143 | #endif |
| 144 | |