blob: e4a991fd3209ade638c2bcdc68d8362a56c72ae0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002 * \file mbedtls_sha256.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief SHA-224 and SHA-256 cryptographic hash function
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_SHA256_H
25#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
Paul Bakker69e095c2011-12-10 21:55:01 +000037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020039// Regular implementation
40//
41
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
45
Paul Bakker5121ce52009-01-03 21:22:43 +000046/**
47 * \brief SHA-256 context structure
48 */
49typedef struct
50{
Paul Bakker5c2364c2012-10-01 14:41:15 +000051 uint32_t total[2]; /*!< number of bytes processed */
52 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000053 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000054 int is224; /*!< 0 => SHA-256, else SHA-224 */
55}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker5121ce52009-01-03 21:22:43 +000058/**
Paul Bakker5b4af392014-06-26 12:09:34 +020059 * \brief Initialize SHA-256 context
60 *
61 * \param ctx SHA-256 context to be initialized
62 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020064
65/**
66 * \brief Clear SHA-256 context
67 *
68 * \param ctx SHA-256 context to be cleared
69 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020071
72/**
Paul Bakker5121ce52009-01-03 21:22:43 +000073 * \brief SHA-256 context setup
74 *
75 * \param ctx context to be initialized
76 * \param is224 0 = use SHA256, 1 = use SHA224
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000079
80/**
81 * \brief SHA-256 process buffer
82 *
83 * \param ctx SHA-256 context
84 * \param input buffer holding the data
85 * \param ilen length of the input data
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020088 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/**
91 * \brief SHA-256 final digest
92 *
93 * \param ctx SHA-256 context
94 * \param output SHA-224/256 checksum result
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Paul Bakker90995b52013-06-24 19:20:35 +020098/* Internal use */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
Paul Bakker90995b52013-06-24 19:20:35 +0200100
101#ifdef __cplusplus
102}
103#endif
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200106#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200108
109#ifdef __cplusplus
110extern "C" {
111#endif
112
Paul Bakker5121ce52009-01-03 21:22:43 +0000113/**
114 * \brief Output = SHA-256( input buffer )
115 *
116 * \param input buffer holding the data
117 * \param ilen length of the input data
118 * \param output SHA-224/256 checksum result
119 * \param is224 0 = use SHA256, 1 = use SHA224
120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121void mbedtls_sha256( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 unsigned char output[32], int is224 );
123
124/**
125 * \brief Output = SHA-256( file contents )
126 *
127 * \param path input file name
128 * \param output SHA-224/256 checksum result
129 * \param is224 0 = use SHA256, 1 = use SHA224
130 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 * \return 0 if successful, or MBEDTLS_ERR_SHA256_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_sha256_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \brief Checkup routine
137 *
138 * \return 0 if successful, or 1 if the test failed
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Paul Bakker5121ce52009-01-03 21:22:43 +0000142#ifdef __cplusplus
143}
144#endif
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* mbedtls_sha256.h */