mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-11-24 02:05:01 +00:00
Refactor SHA*_file functions into a separate file. Generate them using a C macro.
Review URL: http://codereview.chromium.org/558025
This commit is contained in:
@@ -53,20 +53,31 @@ typedef struct {
|
||||
void SHA1_init(SHA1_CTX* ctx);
|
||||
void SHA1_update(SHA1_CTX* ctx, const uint8_t* data, int len);
|
||||
uint8_t* SHA1_final(SHA1_CTX* ctx);
|
||||
|
||||
void SHA256_init(SHA256_CTX* ctx);
|
||||
void SHA256_update(SHA256_CTX* ctx, const uint8_t* data, int len);
|
||||
uint8_t* SHA256_final(SHA256_CTX* ctx);
|
||||
|
||||
void SHA512_init(SHA512_CTX* ctx);
|
||||
void SHA512_update(SHA512_CTX* ctx, const uint8_t* data, int len);
|
||||
uint8_t* SHA512_final(SHA512_CTX* ctx);
|
||||
|
||||
/* Convenience function for SHA-1. Computes hash on [data] of length [len].
|
||||
* and stores it into [digest]. [digest] should be pre-allocated to
|
||||
* SHA1_DIGEST_SIZE bytes.
|
||||
*/
|
||||
uint8_t* SHA1(const void* data, int len, uint8_t* digest);
|
||||
|
||||
void SHA256_init(SHA256_CTX* ctx);
|
||||
void SHA256_update(SHA256_CTX* ctx, const uint8_t* data, int len);
|
||||
uint8_t* SHA256_final(SHA256_CTX* ctx);
|
||||
/* Convenience function for SHA-256. Computes hash on [data] of length [len].
|
||||
* and stores it into [digest]. [digest] should be pre-allocated to
|
||||
* SHA256_DIGEST_SIZE bytes.
|
||||
*/
|
||||
uint8_t* SHA256(const uint8_t* data, int len, uint8_t* digest);
|
||||
|
||||
void SHA512_init(SHA512_CTX* ctx);
|
||||
void SHA512_update(SHA512_CTX* ctx, const uint8_t* data, int len);
|
||||
uint8_t* SHA512_final(SHA512_CTX* ctx);
|
||||
/* Convenience function for SHA-512. Computes hash on [data] of length [len].
|
||||
* and stores it into [digest]. [digest] should be pre-allocated to
|
||||
* SHA512_DIGEST_SIZE bytes.
|
||||
*/
|
||||
uint8_t* SHA512(const uint8_t* data, int len, uint8_t* digest);
|
||||
|
||||
|
||||
|
||||
@@ -2,17 +2,19 @@
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
SRCS=sha_tests.c verify_data.c
|
||||
SRCS=sha_tests.c verify_data.c digest_utility.c
|
||||
OBJS=$(SRCS:.c=.o)
|
||||
LIBS=$(TOP)/crypto/libcrypto.a $(TOP)/common/libcommon.a
|
||||
|
||||
tests: sha_tests verify_data
|
||||
|
||||
sha_tests: sha_tests.c
|
||||
$(CC) $(CFLAGS) $(INCLUDES) $< -o $@ $(LIBS)
|
||||
$(CC) $(CFLAGS) -DNDEBUG $(INCLUDES) $< -o $@ $(LIBS)
|
||||
|
||||
verify_data: verify_data.c
|
||||
$(CC) $(CFLAGS) $(INCLUDES) $< -o $@ $(LIBS)
|
||||
verify_data: verify_data.c digest_utility.o
|
||||
$(CC) $(CFLAGS) -DNDEBUG $(INCLUDES) $< -o $@ digest_utility.o $(LIBS)
|
||||
|
||||
digest_utility.o: digest_utility.c
|
||||
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
|
||||
clean:
|
||||
rm -f $(OBJS) sha_tests verify_data
|
||||
|
||||
105
tests/digest_utility.c
Normal file
105
tests/digest_utility.c
Normal file
@@ -0,0 +1,105 @@
|
||||
/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*
|
||||
* Utility functions for message digest functions.
|
||||
*/
|
||||
|
||||
#include "digest_utility.h"
|
||||
#include "sha.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
uint8_t* SHA1_file(char* input_file) {
|
||||
int input_fd, len;
|
||||
uint8_t data[ SHA1_BLOCK_SIZE], *digest = NULL, *p = NULL;
|
||||
SHA1_CTX ctx;
|
||||
if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
|
||||
fprintf(stderr, "Couldn't open input file.\n");
|
||||
return NULL;
|
||||
}
|
||||
SHA1_init(&ctx);
|
||||
while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) ==
|
||||
SHA1_BLOCK_SIZE)
|
||||
SHA1_update(&ctx, data, len);
|
||||
if (len != -1)
|
||||
SHA1_update(&ctx, data, len);
|
||||
p = SHA1_final(&ctx);
|
||||
close(input_fd);
|
||||
digest = (uint8_t*) malloc(SHA1_DIGEST_SIZE);
|
||||
if (!digest)
|
||||
return NULL;
|
||||
memcpy(digest, p, SHA1_DIGEST_SIZE);
|
||||
return digest;
|
||||
}
|
||||
|
||||
uint8_t* SHA256_file(char* input_file) {
|
||||
int input_fd, len;
|
||||
uint8_t data[ SHA256_BLOCK_SIZE], *digest = NULL, *p = NULL;
|
||||
SHA256_CTX ctx;
|
||||
if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
|
||||
fprintf(stderr, "Couldn't open input file.\n");
|
||||
return NULL;
|
||||
}
|
||||
SHA256_init(&ctx);
|
||||
while ( (len = read(input_fd, data, SHA256_BLOCK_SIZE)) ==
|
||||
SHA256_BLOCK_SIZE)
|
||||
SHA256_update(&ctx, data, len);
|
||||
if (len != -1)
|
||||
SHA256_update(&ctx, data, len);
|
||||
p = SHA256_final(&ctx);
|
||||
close(input_fd);
|
||||
digest = (uint8_t*) malloc(SHA256_DIGEST_SIZE);
|
||||
if (!digest)
|
||||
return NULL;
|
||||
memcpy(digest, p, SHA256_DIGEST_SIZE);
|
||||
return digest;
|
||||
}
|
||||
|
||||
uint8_t* SHA512_file(char* input_file) {
|
||||
int input_fd, len;
|
||||
uint8_t data[ SHA512_BLOCK_SIZE], *digest = NULL, *p = NULL;
|
||||
SHA512_CTX ctx;
|
||||
if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
|
||||
fprintf(stderr, "Couldn't open input file.\n");
|
||||
return NULL;
|
||||
}
|
||||
SHA512_init(&ctx);
|
||||
while ( (len = read(input_fd, data, SHA512_BLOCK_SIZE)) ==
|
||||
SHA512_BLOCK_SIZE)
|
||||
SHA512_update(&ctx, data, len);
|
||||
if (len != -1)
|
||||
SHA512_update(&ctx, data, len);
|
||||
p = SHA512_final(&ctx);
|
||||
close(input_fd);
|
||||
digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE);
|
||||
if (!digest)
|
||||
return NULL;
|
||||
memcpy(digest, p, SHA512_DIGEST_SIZE);
|
||||
return digest;
|
||||
}
|
||||
|
||||
uint8_t* calculate_digest(char *input_file, int algorithm) {
|
||||
typedef uint8_t* (*Hash_file_ptr) (char*);
|
||||
Hash_file_ptr hash_file[] = {
|
||||
SHA1_file, /* RSA 1024 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
SHA1_file, /* RSA 2048 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
SHA1_file, /* RSA 4096 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
SHA1_file, /* RSA 8192 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
};
|
||||
return hash_file[algorithm](input_file);
|
||||
}
|
||||
34
tests/digest_utility.h
Normal file
34
tests/digest_utility.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
/* Utility functions for message digest functions. */
|
||||
|
||||
#ifndef VBOOT_REFERENCE_DIGEST_UTILITY_H_
|
||||
#define VBOOT_REFERENCE_DIGEST_UTILITY_H_
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
/* Returns the SHA-1 digest of data in [input_file].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* SHA1_file(char *input_file);
|
||||
|
||||
/* Returns the SHA-256 digest of data in [input_file].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* SHA256_file(char *input_file);
|
||||
|
||||
/* Returns the SHA-512 digest of data in [input_file].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* SHA512_file(char *input_file);
|
||||
|
||||
/* Returns the appropriate digest for the data in [input_file]
|
||||
* based on the signature [algorithm].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* calculate_digest(char *input_file, int algorithm);
|
||||
|
||||
#endif /* VBOOT_REFERENCE_DIGEST_UTILITY_H_ */
|
||||
@@ -15,12 +15,11 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "digest_utility.h"
|
||||
#include "padding.h"
|
||||
#include "rsa.h"
|
||||
#include "sha.h"
|
||||
#include "verify_data.h"
|
||||
|
||||
|
||||
RSAPublicKey* read_RSAkey(char *input_file, int len) {
|
||||
int key_fd;
|
||||
RSAPublicKey *key = NULL;
|
||||
@@ -70,111 +69,6 @@ RSAPublicKey* read_RSAkey(char *input_file, int len) {
|
||||
return key;
|
||||
}
|
||||
|
||||
uint8_t* SHA1_file(char *input_file) {
|
||||
int i, input_fd, len;
|
||||
uint8_t data[SHA1_BLOCK_SIZE], *digest = NULL, *p = NULL;
|
||||
SHA1_CTX ctx;
|
||||
|
||||
if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
|
||||
fprintf(stderr, "Couldn't open input file.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Calculate SHA1 hash of input blocks, reading one block at a time. */
|
||||
SHA1_init(&ctx);
|
||||
while ( (len = read(input_fd, data, SHA1_BLOCK_SIZE)) == SHA1_BLOCK_SIZE)
|
||||
SHA1_update(&ctx, data, len);
|
||||
if (len != -1)
|
||||
SHA1_update(&ctx, data, len);
|
||||
p = SHA1_final(&ctx);
|
||||
close(input_fd);
|
||||
|
||||
digest = (uint8_t*) malloc(SHA1_DIGEST_SIZE);
|
||||
if (!digest)
|
||||
return NULL;
|
||||
for (i=0; i < SHA1_DIGEST_SIZE; i++)
|
||||
digest[i] = *p++;
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
uint8_t* SHA256_file(char *input_file) {
|
||||
int i, input_fd, len;
|
||||
uint8_t data[SHA256_BLOCK_SIZE], *digest = NULL, *p = NULL;
|
||||
SHA256_CTX ctx;
|
||||
|
||||
if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
|
||||
fprintf(stderr, "Couldn't open input file.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Calculate SHA256 hash of file, reading one block at a time. */
|
||||
SHA256_init(&ctx);
|
||||
while ( (len = read(input_fd, data, SHA256_BLOCK_SIZE)) == SHA256_BLOCK_SIZE)
|
||||
SHA256_update(&ctx, data, len);
|
||||
if (len != -1)
|
||||
SHA256_update(&ctx, data, len);
|
||||
p = SHA256_final(&ctx);
|
||||
close(input_fd);
|
||||
|
||||
digest = (uint8_t*) malloc(SHA256_DIGEST_SIZE);
|
||||
if (!digest)
|
||||
return NULL;
|
||||
for (i=0; i < SHA256_DIGEST_SIZE; i++)
|
||||
digest[i] = *p++;
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
uint8_t* SHA512_file(char* input_file) {
|
||||
int input_fd;
|
||||
uint8_t data[SHA512_BLOCK_SIZE], *digest = NULL, *p = NULL;
|
||||
int i, len;
|
||||
SHA512_CTX ctx;
|
||||
|
||||
if( (input_fd = open(input_file, O_RDONLY)) == -1 ) {
|
||||
fprintf(stderr, "Couldn't open input file.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Calculate SHA512 hash of file, reading one block at a time. */
|
||||
SHA512_init(&ctx);
|
||||
while ( (len = read(input_fd, data, SHA512_BLOCK_SIZE)) == SHA512_BLOCK_SIZE)
|
||||
SHA512_update(&ctx, data, len);
|
||||
if (len != -1)
|
||||
SHA512_update(&ctx, data, len);
|
||||
p = SHA512_final(&ctx);
|
||||
close(input_fd);
|
||||
|
||||
digest = (uint8_t*) malloc(SHA512_DIGEST_SIZE);
|
||||
if (!digest)
|
||||
return NULL;
|
||||
for (i=0; i < SHA512_DIGEST_SIZE; i++)
|
||||
digest[i] = *p++;
|
||||
|
||||
return digest;
|
||||
}
|
||||
|
||||
|
||||
uint8_t* calculate_digest(char *input_file, int algorithm) {
|
||||
typedef uint8_t* (*Hash_file_ptr) (char*);
|
||||
Hash_file_ptr hash_file[] = {
|
||||
SHA1_file, /* RSA 1024 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
SHA1_file, /* RSA 2048 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
SHA1_file, /* RSA 4096 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
SHA1_file, /* RSA 8192 */
|
||||
SHA256_file,
|
||||
SHA512_file,
|
||||
};
|
||||
return hash_file[algorithm](input_file);
|
||||
}
|
||||
|
||||
uint8_t* read_signature(char *input_file, int len) {
|
||||
int i, sigfd;
|
||||
uint8_t *signature = NULL;
|
||||
|
||||
@@ -12,27 +12,6 @@
|
||||
*/
|
||||
RSAPublicKey* read_RSAkey(char *input_file, int len);
|
||||
|
||||
/* Returns the SHA-1 digest of [input_file].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* SHA1_file(char *input_file);
|
||||
|
||||
/* Returns the SHA-256 digest of [input_file].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* SHA256_file(char *input_file);
|
||||
|
||||
/* Returns the SHA-512 digest of [input_file].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* SHA512_file(char *input_file);
|
||||
|
||||
/* Returns the appropriate digest for the [input_file] based on the
|
||||
* signature [algorithm].
|
||||
* Caller owns the returned digest and must free it.
|
||||
*/
|
||||
uint8_t* calculate_digest(char *input_file, int algorithm);
|
||||
|
||||
/* Return a signature of [len] bytes read from [input_file].
|
||||
* Caller owns the returned signature and must free it.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user