blob: f8041adf08232a9f3432b31e51896eefe37772cc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butchera02fe7c2016-01-03 16:14:14 +00002 * \file 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é-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_SHA256_H
24#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020036// Regular implementation
37//
38
Paul Bakker407a0da2013-06-27 14:29:21 +020039#ifdef __cplusplus
40extern "C" {
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043/**
44 * \brief SHA-256 context structure
45 */
46typedef struct
47{
Paul Bakker5c2364c2012-10-01 14:41:15 +000048 uint32_t total[2]; /*!< number of bytes processed */
49 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000050 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000051 int is224; /*!< 0 => SHA-256, else SHA-224 */
52}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
Paul Bakker5b4af392014-06-26 12:09:34 +020056 * \brief Initialize SHA-256 context
57 *
58 * \param ctx SHA-256 context to be initialized
59 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020061
62/**
63 * \brief Clear SHA-256 context
64 *
65 * \param ctx SHA-256 context to be cleared
66 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020068
69/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020070 * \brief Clone (the state of) a SHA-256 context
71 *
72 * \param dst The destination context
73 * \param src The context to be cloned
74 */
75void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
76 const mbedtls_sha256_context *src );
77
78/**
Paul Bakker5121ce52009-01-03 21:22:43 +000079 * \brief SHA-256 context setup
80 *
81 * \param ctx context to be initialized
82 * \param is224 0 = use SHA256, 1 = use SHA224
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000085
86/**
87 * \brief SHA-256 process buffer
88 *
89 * \param ctx SHA-256 context
90 * \param input buffer holding the data
91 * \param ilen length of the input data
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020094 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/**
97 * \brief SHA-256 final digest
98 *
99 * \param ctx SHA-256 context
100 * \param output SHA-224/256 checksum result
101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakker90995b52013-06-24 19:20:35 +0200104/* Internal use */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
Paul Bakker90995b52013-06-24 19:20:35 +0200106
107#ifdef __cplusplus
108}
109#endif
110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200112#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200114
115#ifdef __cplusplus
116extern "C" {
117#endif
118
Paul Bakker5121ce52009-01-03 21:22:43 +0000119/**
120 * \brief Output = SHA-256( input buffer )
121 *
122 * \param input buffer holding the data
123 * \param ilen length of the input data
124 * \param output SHA-224/256 checksum result
125 * \param is224 0 = use SHA256, 1 = use SHA224
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_sha256( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 unsigned char output[32], int is224 );
129
130/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 * \brief Checkup routine
132 *
133 * \return 0 if successful, or 1 if the test failed
134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137#ifdef __cplusplus
138}
139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#endif /* mbedtls_sha256.h */