sha256.c File Reference

SHA-256 (Secure Hash Algorithm 256) More...

#include "core/crypto.h"
#include "hash/sha256.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 
#define W(n)   w[(n) & 0x0F]
 
#define CH(x, y, z)   (((x) & (y)) | (~(x) & (z)))
 
#define MAJ(x, y, z)   (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
 
#define SIGMA1(x)   (ROR32(x, 2) ^ ROR32(x, 13) ^ ROR32(x, 22))
 
#define SIGMA2(x)   (ROR32(x, 6) ^ ROR32(x, 11) ^ ROR32(x, 25))
 
#define SIGMA3(x)   (ROR32(x, 7) ^ ROR32(x, 18) ^ SHR32(x, 3))
 
#define SIGMA4(x)   (ROR32(x, 17) ^ ROR32(x, 19) ^ SHR32(x, 10))
 

Functions

__weak_func error_t sha256Compute (const void *data, size_t length, uint8_t *digest)
 Digest a message using SHA-256. More...
 
__weak_func void sha256Init (Sha256Context *context)
 Initialize SHA-256 message digest context. More...
 
__weak_func void sha256Update (Sha256Context *context, const void *data, size_t length)
 Update the SHA-256 context with a portion of the message being hashed. More...
 
__weak_func void sha256Final (Sha256Context *context, uint8_t *digest)
 Finish the SHA-256 message digest. More...
 
__weak_func void sha256FinalRaw (Sha256Context *context, uint8_t *digest)
 Finish the SHA-256 message digest (no padding added) More...
 
__weak_func void sha256ProcessBlock (Sha256Context *context)
 Process message in 16-word blocks. More...
 

Variables

const uint8_t SHA256_OID [9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}
 
const HashAlgo sha256HashAlgo
 

Detailed Description

SHA-256 (Secure Hash Algorithm 256)

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneCRYPTO Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Description

SHA-256 is a secure hash algorithm for computing a condensed representation of an electronic message. Refer to FIPS 180-4 for more details

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.0

Definition in file sha256.c.

Macro Definition Documentation

◆ CH

#define CH (   x,
  y,
  z 
)    (((x) & (y)) | (~(x) & (z)))

Definition at line 50 of file sha256.c.

◆ MAJ

#define MAJ (   x,
  y,
  z 
)    (((x) & (y)) | ((x) & (z)) | ((y) & (z)))

Definition at line 51 of file sha256.c.

◆ SIGMA1

#define SIGMA1 (   x)    (ROR32(x, 2) ^ ROR32(x, 13) ^ ROR32(x, 22))

Definition at line 52 of file sha256.c.

◆ SIGMA2

#define SIGMA2 (   x)    (ROR32(x, 6) ^ ROR32(x, 11) ^ ROR32(x, 25))

Definition at line 53 of file sha256.c.

◆ SIGMA3

#define SIGMA3 (   x)    (ROR32(x, 7) ^ ROR32(x, 18) ^ SHR32(x, 3))

Definition at line 54 of file sha256.c.

◆ SIGMA4

#define SIGMA4 (   x)    (ROR32(x, 17) ^ ROR32(x, 19) ^ SHR32(x, 10))

Definition at line 55 of file sha256.c.

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 37 of file sha256.c.

◆ W

#define W (   n)    w[(n) & 0x0F]

Definition at line 47 of file sha256.c.

Function Documentation

◆ sha256Compute()

__weak_func error_t sha256Compute ( const void *  data,
size_t  length,
uint8_t *  digest 
)

Digest a message using SHA-256.

Parameters
[in]dataPointer to the message being hashed
[in]lengthLength of the message
[out]digestPointer to the calculated digest
Returns
Error code

Definition at line 116 of file sha256.c.

◆ sha256Final()

__weak_func void sha256Final ( Sha256Context context,
uint8_t *  digest 
)

Finish the SHA-256 message digest.

Parameters
[in]contextPointer to the SHA-256 context
[out]digestCalculated digest (optional parameter)

Definition at line 226 of file sha256.c.

◆ sha256FinalRaw()

__weak_func void sha256FinalRaw ( Sha256Context context,
uint8_t *  digest 
)

Finish the SHA-256 message digest (no padding added)

Parameters
[in]contextPointer to the SHA-256 context
[out]digestCalculated digest

Definition at line 275 of file sha256.c.

◆ sha256Init()

__weak_func void sha256Init ( Sha256Context context)

Initialize SHA-256 message digest context.

Parameters
[in]contextPointer to the SHA-256 context to initialize

Definition at line 161 of file sha256.c.

◆ sha256ProcessBlock()

__weak_func void sha256ProcessBlock ( Sha256Context context)

Process message in 16-word blocks.

Parameters
[in]contextPointer to the SHA-256 context

Definition at line 301 of file sha256.c.

◆ sha256Update()

__weak_func void sha256Update ( Sha256Context context,
const void *  data,
size_t  length 
)

Update the SHA-256 context with a portion of the message being hashed.

Parameters
[in]contextPointer to the SHA-256 context
[in]dataPointer to the buffer being hashed
[in]lengthLength of the buffer

Definition at line 187 of file sha256.c.

Variable Documentation

◆ SHA256_OID

const uint8_t SHA256_OID[9] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01}

Definition at line 80 of file sha256.c.

◆ sha256HashAlgo

const HashAlgo sha256HashAlgo
Initial value:
=
{
"SHA-256",
sizeof(SHA256_OID),
sizeof(Sha256Context),
}
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:956
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:965
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:963
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:961
void(* HashAlgoInit)(void *context)
Definition: crypto.h:959
#define TRUE
Definition: os_port.h:50
const uint8_t SHA256_OID[9]
Definition: sha256.c:80
__weak_func void sha256Update(Sha256Context *context, const void *data, size_t length)
Update the SHA-256 context with a portion of the message being hashed.
Definition: sha256.c:187
__weak_func void sha256Final(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest.
Definition: sha256.c:226
__weak_func void sha256FinalRaw(Sha256Context *context, uint8_t *digest)
Finish the SHA-256 message digest (no padding added)
Definition: sha256.c:275
__weak_func error_t sha256Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using SHA-256.
Definition: sha256.c:116
__weak_func void sha256Init(Sha256Context *context)
Initialize SHA-256 message digest context.
Definition: sha256.c:161
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
#define SHA256_BLOCK_SIZE
Definition: sha256.h:43
#define SHA256_MIN_PAD_SIZE
Definition: sha256.h:47
SHA-256 algorithm context.
Definition: sha256.h:62

Definition at line 83 of file sha256.c.