]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - sha256.c
insereated GPLv3 stub
[avr-crypto-lib.git] / sha256.c
index 524b1dd2eee05085bd669839833586baddec9e81..1896deea2c5c8a03a71f652ec142264f64e7356d 100644 (file)
--- a/sha256.c
+++ b/sha256.c
@@ -1,8 +1,31 @@
+/* sha256.c */
+/*
+    This file is part of the Crypto-avr-lib/microcrypt-lib.
+    Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
+
+    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 3 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, see <http://www.gnu.org/licenses/>.
+*/
 /**
- * File:               sha256.c
- * Author:     Daniel Otte 
- * Date:               16.05.2006
- * License:    GPL
+ * \file               sha256.c
+ * \author             Daniel Otte 
+ * \date               16.05.2006
+ * 
+ * \par License:       
+ *     GPL
+ * 
+ * \brief SHA-256 implementation.
+ * 
  * 
  */
 
 #endif
 
 
+/*************************************************************************/
+
 uint32_t sha256_init_vector[]={
        0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
     0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 };
 
+
+/*************************************************************************/
+
+/**
+ * \brief \c sh256_init initialises a sha256 context for hashing. 
+ * \c sh256_init c initialises the given sha256 context for hashing
+ * @param state pointer to a sha256 context
+ * @return none
+ */
 void sha256_init(sha256_ctx_t *state){
        state->length=0;
        memcpy(state->h, sha256_init_vector, 8*4);
 }
 
-/*
+/*************************************************************************/
+
+/**
  * rotate x right by n positions
  */
-
 uint32_t rotr32( uint32_t x, uint8_t n){
        return ((x>>n) | (x<<(32-n)));
 }
 
+
+/*************************************************************************/
+
 // #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8))
 
 uint32_t change_endian32(uint32_t x){
@@ -43,6 +81,8 @@ uint32_t change_endian32(uint32_t x){
 }
 
 
+/*************************************************************************/
+
 /* sha256 functions as macros for speed and size, cause they are called only once */
 
 #define CH(x,y,z)  (((x)&(y)) ^ ((~(x))&(z)))
@@ -66,10 +106,12 @@ uint32_t k[]={
 };
 
 
+/*************************************************************************/
+
 /**
  * block must be, 512, Bit = 64, Byte, long !!!
  */
-void sha256_nextBlock (sha256_ctx_t *state, void* block){
+void sha256_nextBlock (sha256_ctx_t *state, const void* block){
        uint32_t w[64]; /* this is 256, byte, large, */
        uint8_t  i;
        uint32_t a[8],t1,t2;
@@ -105,12 +147,18 @@ void sha256_nextBlock (sha256_ctx_t *state, void* block){
                state->length += 512;
 } 
 
-/*
- * length is the length of only THIS block in BITS not in bytes!
+
+/*************************************************************************/
+
+/**
+ * \brief function to process the last block being hashed
+ * @param state Pointer to the context in which this block should be processed.
+ * @param block Pointer to the message wich should be hashed.
+ * @param length is the length of only THIS block in BITS not in bytes!
  *  bits are big endian, meaning high bits come first.
  *     if you have a message with bits at the end, the byte must be padded with zeros 
- * */
-void sha256_lastBlock(sha256_ctx_t *state, void* block, uint16_t length){
+ */
+void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length){
        uint8_t lb[SHA256_BLOCK_BITS/8]; /* local block */
        state->length += length;
        memcpy (&(lb[0]), block, length/8);
@@ -145,16 +193,17 @@ void sha256_lastBlock(sha256_ctx_t *state, void* block, uint16_t length){
 }
 
 
+/*************************************************************************/
+
 /*
  * length in bits!
  */
-
-void sha256(sha256_hash_t *dest, void* msg, uint32_t length){ /* length could be choosen longer but this is for ?C */
+void sha256(sha256_hash_t *dest, const void* msg, uint32_t length){ /* length could be choosen longer but this is for µC */
        sha256_ctx_t s;
        sha256_init(&s);
        while(length >= SHA256_BLOCK_BITS){
                sha256_nextBlock(&s, msg);
-               msg += SHA256_BLOCK_BITS/8;
+               msg = (uint8_t*)msg + SHA256_BLOCK_BITS/8;
                length -= SHA256_BLOCK_BITS;
        }
        sha256_lastBlock(&s, msg, length);
@@ -163,8 +212,9 @@ void sha256(sha256_hash_t *dest, void* msg, uint32_t length){ /* length could be
 
 
 
+/*************************************************************************/
 
-void sha256_ctx2hash(sha256_hash_t *dest, sha256_ctx_t *state){
+void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state){
 #if defined LITTLE_ENDIAN
        uint8_t i;
        for(i=0; i<8; ++i){