first and last commit

This commit is contained in:
Chris Nutter committed 2023-07-08 13:03:17 -07:00
commit 1bf4efd552
2612 files changed
+636500

No files matched your search

+164
View File
@@ -0,0 +1,164 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is SHA 180-1 Reference Implementation (Compact version)
*
* The Initial Developer of the Original Code is Paul Kocher of
* Cryptography Research. Portions created by Paul Kocher are
* Copyright (C) 1995-9 by Cryptography Research, Inc. All
* Rights Reserved.
*
* Contributor(s):
*
* Paul Kocher
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
#if (!WITH_CORECRYPTO || !WITH_VFP) && !WITH_HW_SHA1
#include "mozilla_sha.h"
static void shaHashBlock(SHA_CTX *ctx);
void shaInit(SHA_CTX *ctx) {
int i;
ctx->lenW = 0;
ctx->sizeHi = ctx->sizeLo = 0;
/* Initialize H with the magic constants (see FIPS180 for constants)
*/
ctx->H[0] = 0x67452301L;
ctx->H[1] = 0xefcdab89L;
ctx->H[2] = 0x98badcfeL;
ctx->H[3] = 0x10325476L;
ctx->H[4] = 0xc3d2e1f0L;
for (i = 0; i < 80; i++)
ctx->W[i] = 0;
}
void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, u_int32_t len) {
u_int32_t i;
/* Read the data into W and process blocks as they get full
*/
for (i = 0; i < len; i++) {
ctx->W[ctx->lenW / 4] <<= 8;
ctx->W[ctx->lenW / 4] |= (u_int32_t)dataIn[i];
if ((++ctx->lenW) % 64 == 0) {
shaHashBlock(ctx);
ctx->lenW = 0;
}
ctx->sizeLo += 8;
ctx->sizeHi += (ctx->sizeLo < 8);
}
}
void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]) {
unsigned char pad0x80 = 0x80;
unsigned char pad0x00 = 0x00;
unsigned char padlen[8];
int i;
/* Pad with a binary 1 (e.g. 0x80), then zeroes, then length
*/
padlen[0] = (unsigned char)((ctx->sizeHi >> 24) & 255);
padlen[1] = (unsigned char)((ctx->sizeHi >> 16) & 255);
padlen[2] = (unsigned char)((ctx->sizeHi >> 8) & 255);
padlen[3] = (unsigned char)((ctx->sizeHi >> 0) & 255);
padlen[4] = (unsigned char)((ctx->sizeLo >> 24) & 255);
padlen[5] = (unsigned char)((ctx->sizeLo >> 16) & 255);
padlen[6] = (unsigned char)((ctx->sizeLo >> 8) & 255);
padlen[7] = (unsigned char)((ctx->sizeLo >> 0) & 255);
shaUpdate(ctx, &pad0x80, 1);
while (ctx->lenW != 56)
shaUpdate(ctx, &pad0x00, 1);
shaUpdate(ctx, padlen, 8);
/* Output hash
*/
for (i = 0; i < 20; i++) {
hashout[i] = (unsigned char)(ctx->H[i / 4] >> 24);
ctx->H[i / 4] <<= 8;
}
/*
* Re-initialize the context (also zeroizes contents)
*/
shaInit(ctx);
}
#if 0
void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]) {
SHA_CTX ctx;
shaInit(&ctx);
shaUpdate(&ctx, dataIn, len);
shaFinal(&ctx, hashout);
}
#endif
#define SHA_ROTL(X,n) (((X) << (n)) | ((X) >> (32-(n))))
static void shaHashBlock(SHA_CTX *ctx) {
int t;
u_int32_t A,B,C,D,E,TEMP;
for (t = 16; t <= 79; t++)
ctx->W[t] =
SHA_ROTL(ctx->W[t-3] ^ ctx->W[t-8] ^ ctx->W[t-14] ^ ctx->W[t-16], 1);
A = ctx->H[0];
B = ctx->H[1];
C = ctx->H[2];
D = ctx->H[3];
E = ctx->H[4];
for (t = 0; t <= 19; t++) {
TEMP = SHA_ROTL(A,5) + (((C^D)&B)^D) + E + ctx->W[t] + 0x5a827999L;
E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
}
for (t = 20; t <= 39; t++) {
TEMP = SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0x6ed9eba1L;
E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
}
for (t = 40; t <= 59; t++) {
TEMP = SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + E + ctx->W[t] + 0x8f1bbcdcL;
E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
}
for (t = 60; t <= 79; t++) {
TEMP = SHA_ROTL(A,5) + (B^C^D) + E + ctx->W[t] + 0xca62c1d6L;
E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP;
}
ctx->H[0] += A;
ctx->H[1] += B;
ctx->H[2] += C;
ctx->H[3] += D;
ctx->H[4] += E;
}
#endif /* (!WITH_CORECRYPTO || !WITH_VFP) && !WITH_HW_SHA1 */
+57
View File
@@ -0,0 +1,57 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is SHA 180-1 Header File
*
* The Initial Developer of the Original Code is Paul Kocher of
* Cryptography Research. Portions created by Paul Kocher are
* Copyright (C) 1995-9 by Cryptography Research, Inc. All
* Rights Reserved.
*
* Contributor(s):
*
* Paul Kocher
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
#ifndef _MOZILLA_SHA_H_
#define _MOZILLA_SHA_H_
#include <sys/types.h>
__BEGIN_DECLS
typedef struct {
u_int32_t H[5];
u_int32_t W[80];
int lenW;
u_int32_t sizeHi,sizeLo;
} SHA_CTX;
void shaInit(SHA_CTX *ctx);
void shaUpdate(SHA_CTX *ctx, unsigned char *dataIn, u_int32_t len);
void shaFinal(SHA_CTX *ctx, unsigned char hashout[20]);
// void shaBlock(unsigned char *dataIn, int len, unsigned char hashout[20]);
__END_DECLS
#endif /* _MOZILLA_SHA_H_ */
+18
View File
@@ -0,0 +1,18 @@
# Copyright (C) 2007 Apple Inc. All rights reserved.
#
# This document is the property of Apple Inc.
# It is considered confidential and proprietary.
#
# This document may not be reproduced or transmitted in any form,
# in whole or in part, without the express written permission of
# Apple Inc.
#
LOCAL_DIR := $(GET_LOCAL_DIR)
OPTIONS += \
WITH_SHA1=1
ALL_OBJS += \
$(LOCAL_DIR)/sha1.o \
$(LOCAL_DIR)/mozilla_sha.o
+97
View File
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2007-2012 Apple Inc. All rights reserved.
*
* This document is the property of Apple Inc.
* It is considered confidential and proprietary.
*
* This document may not be reproduced or transmitted in any form,
* in whole or in part, without the express written permission of
* Apple Inc.
*/
#include <debug.h>
#include <drivers/sha1.h>
#include <sys/menu.h>
#if WITH_CORECRYPTO
#include <arch.h>
#include <corecrypto/ccdigest.h>
#include <corecrypto/ccsha1.h>
#if WITH_VFP
# define USE_CC_SHA1_OPTIMIZED_IMPLEMENTATION 1
#endif
const struct ccdigest_info *sha1_get_ccsha1_ccdigest_info()
{
#if USE_CC_SHA1_OPTIMIZED_IMPLEMENTATION
return ((const struct ccdigest_info *)ccsha1_di());
#else
return (&ccsha1_ltc_di);
#endif
}
void sha1_calculate(const void *buffer, size_t length, void *result)
{
#if USE_CC_SHA1_OPTIMIZED_IMPLEMENTATION && !WITH_VFP_ALWAYS_ON
arch_task_fp_enable(true);
#endif
ccdigest(sha1_get_ccsha1_ccdigest_info(), length, buffer, result);
}
#elif WITH_HW_SHA1
extern void sha1_hw_calculate(const void *buf, size_t len, u_int32_t *result);
void sha1_calculate(const void *buffer, size_t length, void *result)
{
if ((NULL == buffer) ||
(NULL == result))
panic("SHA1: bad parameters");
sha1_hw_calculate(buffer, length, result);
}
#else
# include "mozilla_sha.h"
void sha1_calculate(const void *buffer, size_t length, void *result)
{
SHA_CTX ctx;
shaInit(&ctx);
shaUpdate(&ctx, (unsigned char *)buffer, length);
shaFinal(&ctx, (unsigned char *)result);
/* software sha-1 implementation? */
}
#endif
#if WITH_MENU && !WITH_SIMPLE_MENU
static int do_sha1(int argc, struct cmd_arg *args)
{
u_int8_t hash[20];
u_int32_t cnt;
if (argc < 3) {
printf("not enough arguments.\n");
printf("%s <address> <len>\n", args[0].str);
return -1;
}
if (!security_allow_memory((void *)args[1].u, args[2].u)) {
printf("Permission Denied\n");
return -1;
}
sha1_calculate((void *)args[1].u, args[2].u, hash);
printf("sha1 of 0x%08lx for 0x%08lx bytes: ", args[1].u, args[2].u);
for (cnt = 0; cnt < 20; cnt++)
printf("%02x%s", hash[cnt], (cnt != 19) ? ":" : "\n");
return 0;
}
MENU_COMMAND_DEVELOPMENT(sha1, do_sha1, "SHA-1 hash of specified memory address range.", NULL);
#endif
+41
View File
@@ -0,0 +1,41 @@
#include <unittest.h>
#include <drivers/sha1.h>
#include <string.h>
struct sha1_test {
const char *data;
uint32_t len;
uint8_t expected[20];
};
static struct sha1_test sha1_tests[] = {
{ "", 0, { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 } },
{ "abc", 3, { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d } },
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56, { 0x84, 0x98, 0x3e, 0x44, 0x1c, 0x3b, 0xd2, 0x6e, 0xba, 0xae, 0x4a, 0xa1, 0xf9, 0x51, 0x29, 0xe5, 0xe5, 0x46, 0x70, 0xf1 } },
};
void test_sha1(uintptr_t param)
{
for (unsigned i = 0; i < sizeof sha1_tests / sizeof(sha1_tests[0]); i++) {
struct sha1_test *test = &sha1_tests[i];
uint8_t calculated[20];
sha1_calculate((void *)test->data, test->len, (void *)calculated);
TEST_ASSERT_MEM_EQ(calculated, test->expected, sizeof(test->expected));
}
}
struct test_suite sha1_suite = {
.name = "sha1",
.test_cases = {
{ "sha1", test_sha1, 0, "Tests the sha1_calculate function" },
TEST_CASE_LAST
}
};
TEST_SUITE(sha1_suite);
+21
View File
@@ -0,0 +1,21 @@
# Copyright (C) 2014 Apple Inc. All rights reserved.
#
# This document is the property of Apple Inc.
# It is considered confidential and proprietary.
#
# This document may not be reproduced or transmitted in any form,
# in whole or in part, without the express written permission of
# Apple Inc.
#
LOCAL_DIR := $(GET_LOCAL_DIR)
TEST_NAME := sha1
TEST_OBJS := \
$(LOCAL_DIR)/sha1.o \
$(LOCAL_DIR)/mozilla_sha.o
TEST_SUPPORT_OBJS := \
tests/unittest-main.o \
$(LOCAL_DIR)/tests.o