/*
* Copyright 2006 Apple Computer, Inc. All rights reserved.
*
* iTunes U Sample Code License
* IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple")
* in consideration of your agreement to the following terms, and your use,
* installation, modification or distribution of this Apple software constitutes
* acceptance of these terms. If you do not agree with these terms, please do not use,
* install, modify or distribute this Apple software.
*
* In consideration of your agreement to abide by the following terms and subject to
* these terms, Apple grants you a personal, non-exclusive, non-transferable license,
* under Apple's copyrights in this original Apple software (the "Apple Software"):
*
* (a) to internally use, reproduce, modify and internally distribute the Apple
* Software, with or without modifications, in source and binary forms, within your
* educational organization or internal campus network for the sole purpose of
* integrating Apple's iTunes U software with your internal campus network systems; and
*
* (b) to redistribute the Apple Software to other universities or educational
* organizations, with or without modifications, in source and binary forms, for the
* sole purpose of integrating Apple's iTunes U software with their internal campus
* network systems; provided that the following conditions are met:
*
* - If you redistribute the Apple Software in its entirety and without
* modifications, you must retain the above copyright notice, this entire license
* and the disclaimer provisions in all such redistributions of the Apple Software.
* - If you modify and redistribute the Apple Software, you must indicate that you
* have made changes to the Apple Software, and you must retain the above
* copyright notice, this entire license and the disclaimer provisions in all
* such redistributions of the Apple Software and/or derivatives thereof created
* by you.
* - Neither the name, trademarks, service marks or logos of Apple may be used to
* endorse or promote products derived from the Apple Software without specific
* prior written permission from Apple.
*
* Except as expressly stated above, no other rights or licenses, express or implied,
* are granted by Apple herein, including but not limited to any patent rights that may
* be infringed by your derivative works or by other works in which the Apple Software
* may be incorporated. THE APPLE SOFTWARE IS PROVIDED BY APPLE ON AN "AS IS" BASIS.
* APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, AND HEREBY DISCLAIMS ALL WARRANTIES,
* INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE
* OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS OR SYSTEMS.
* APPLE IS NOT OBLIGATED TO PROVIDE ANY MAINTENANCE, TECHNICAL OR OTHER SUPPORT FOR
* THE APPLE SOFTWARE, OR TO PROVIDE ANY UPDATES TO THE APPLE SOFTWARE. IN NO EVENT
* SHALL APPLE BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
* OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Rev. 120806
*
* This source code file contains a self-contained ANSI C program with no
* external dependencies except for standard ANSI C libraries. On Mac OS X, it
* can be compiled and run by executing the following commands in a terminal
* window:
* gcc -o seconds seconds.c
* ./seconds
*/
// Compile note added by RKW
// gcc -o hmac-sha256 hmac-sha256.c
// should work on latter-day gcc installs, but c99 can be made explicit this way:
// gcc -std=c99 -o hmac-sha256 hmac-sha256.c
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
/* #include <string.h> */
#include <stdint.h> // Added by RKW, needed for types uint8_t, uint32_t; requires C99 compiler
/******************************************************************************
* SHA-256.
*/
typedef struct {
uint8_t hash[32]; // Changed by RKW, unsigned char becomes uint8_t
uint32_t buffer[16]; // Changed by RKW, unsigned long becomes uint32_t
uint32_t state[8]; // Changed by RKW, unsinged long becomes uint32_t
uint8_t length[8]; // Changed by RKW, unsigned char becomes uint8_t
} sha256;
void sha256_initialize(sha256 *sha) {
int i;
for (i = 0; i < 17; ++i) sha->buffer[i] = 0;
sha->state[0] = 0x6a09e667;
sha->state[1] = 0xbb67ae85;
sha->state[2] = 0x3c6ef372;
sha->state[3] = 0xa54ff53a;
sha->state[4] = 0x510e527f;
sha->state[5] = 0x9b05688c;
sha->state[6] = 0x1f83d9ab;
sha->state[7] = 0x5be0cd19;
for (i = 0; i < 8; ++i) sha->length[i] = 0;
}
// Changed by RKW, formal args are now const uint8_t, uint_32
// from const unsigned char, unsigned long respectively
void sha256_update(sha256 *sha,
const uint8_t *message,
uint32_t length) {
int i, j;
/* Add the length of the received message, counted in
* bytes, to the total length of the messages hashed to
* date, counted in bits and stored in 8 separate bytes. */
for (i = 7; i >= 0; --i) {
int bits;
if (i == 7)
bits = length << 3;
else if (i == 0 || i == 1 || i == 2)
bits = 0;
else
bits = length >> (53 - 8 * i);
bits &= 0xff;
if (sha->length[i] + bits > 0xff) {
for (j = i - 1; j >= 0 && sha->length[j]++ == 0xff; --j);
}
sha->length[i] += bits;
}
/* Add the received message to the SHA buffer, updating the
* hash at each block (each time the buffer is filled). */
while (length > 0) {
/* Find the index in the SHA buffer at which to
* append what's left of the received message. */
int index = sha->length[6] % 2 * 32 + sha->length[7] / 8;
index = (index + 64 - length % 64) % 64;
/* Append the received message bytes to the SHA buffer until
* we run out of message bytes or until the buffer is filled. */
for (;length > 0 && index < 64; ++message, ++index, --length) {
sha->buffer[index / 4] |= *message << (24 - index % 4 * 8);
}
/* Update the hash with the buffer contents if the buffer is full. */
if (index == 64) {
/* Update the hash with a block of message content. See FIPS 180-2
* (<csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf>)
* for a description of and details on the algorithm used here. */
// Changed by RKW, const unsigned long becomes const uint32_t
const uint32_t k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82
评论0