mirror of https://github.com/ipxe/ipxe.git
Updated digest commands to include SHA-2 support, hash strings, perform multi-round hashing and apply the result in the configuration settings
parent
0b3000bbec
commit
ce09351cc9
|
@ -29,6 +29,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <ipxe/crypto.h>
|
#include <ipxe/crypto.h>
|
||||||
#include <ipxe/md5.h>
|
#include <ipxe/md5.h>
|
||||||
#include <ipxe/sha1.h>
|
#include <ipxe/sha1.h>
|
||||||
|
#include <ipxe/sha256.h>
|
||||||
|
#include <ipxe/sha512.h>
|
||||||
|
#include <ipxe/settings.h>
|
||||||
#include <usr/imgmgmt.h>
|
#include <usr/imgmgmt.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
|
@ -38,15 +41,25 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** "digest" options */
|
/** "digest" options */
|
||||||
struct digest_options {};
|
struct digest_options {
|
||||||
|
/** String to digest */
|
||||||
|
char *str;
|
||||||
|
/** Rounds to rehash */
|
||||||
|
unsigned int rounds;
|
||||||
|
};
|
||||||
|
|
||||||
/** "digest" option list */
|
/** "digest" option list */
|
||||||
static struct option_descriptor digest_opts[] = {};
|
static struct option_descriptor digest_opts[] = {
|
||||||
|
OPTION_DESC ( "rounds", 'r', required_argument,
|
||||||
|
struct digest_options, rounds, parse_integer),
|
||||||
|
OPTION_DESC ( "str", 's', required_argument,
|
||||||
|
struct digest_options, str, parse_string ),
|
||||||
|
};
|
||||||
|
|
||||||
/** "digest" command descriptor */
|
/** "digest" command descriptor */
|
||||||
static struct command_descriptor digest_cmd =
|
static struct command_descriptor digest_cmd =
|
||||||
COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
|
COMMAND_DESC ( struct digest_options, digest_opts, 0, MAX_ARGUMENTS,
|
||||||
"<image> [<image>...]" );
|
"[<image>] [<image>...]" );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The "digest" command
|
* The "digest" command
|
||||||
|
@ -60,30 +73,47 @@ static int digest_exec ( int argc, char **argv,
|
||||||
struct digest_algorithm *digest ) {
|
struct digest_algorithm *digest ) {
|
||||||
struct digest_options opts;
|
struct digest_options opts;
|
||||||
struct image *image;
|
struct image *image;
|
||||||
|
struct named_setting setting;
|
||||||
uint8_t digest_ctx[digest->ctxsize];
|
uint8_t digest_ctx[digest->ctxsize];
|
||||||
uint8_t digest_out[digest->digestsize];
|
uint8_t digest_out[digest->digestsize];
|
||||||
uint8_t buf[128];
|
uint8_t buf[128];
|
||||||
size_t offset;
|
size_t offset;
|
||||||
size_t len;
|
size_t len;
|
||||||
size_t frag_len;
|
size_t frag_len;
|
||||||
|
unsigned long origlen;
|
||||||
int i;
|
int i;
|
||||||
unsigned j;
|
unsigned j, r;
|
||||||
int rc;
|
int rc;
|
||||||
|
char hashstr[130];
|
||||||
|
|
||||||
|
if ( argc < 2 ) {
|
||||||
|
print_usage ( &digest_cmd, argv );
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Parse options */
|
/* Parse options */
|
||||||
if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
|
if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
|
||||||
return rc;
|
return rc;
|
||||||
|
|
||||||
for ( i = optind ; i < argc ; i++ ) {
|
for ( i = optind ; i < argc || opts.str ; i++ ) {
|
||||||
|
|
||||||
|
hashstr[0] = '\0';
|
||||||
|
|
||||||
/* Acquire image */
|
/* Acquire image */
|
||||||
if ( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 )
|
if ( ( ! opts.str ) &&
|
||||||
|
( ( rc = imgacquire ( argv[i], 0, &image ) ) != 0 ) )
|
||||||
continue;
|
continue;
|
||||||
offset = 0;
|
|
||||||
len = image->len;
|
|
||||||
|
|
||||||
/* calculate digest */
|
/* calculate digest */
|
||||||
digest_init ( digest, digest_ctx );
|
digest_init ( digest, digest_ctx );
|
||||||
|
if ( opts.str ) {
|
||||||
|
origlen = strlen( opts.str );
|
||||||
|
digest_update ( digest, digest_ctx, opts.str,
|
||||||
|
origlen );
|
||||||
|
} else {
|
||||||
|
offset = 0;
|
||||||
|
len = image->len;
|
||||||
|
origlen = image->len;
|
||||||
while ( len ) {
|
while ( len ) {
|
||||||
frag_len = len;
|
frag_len = len;
|
||||||
if ( frag_len > sizeof ( buf ) )
|
if ( frag_len > sizeof ( buf ) )
|
||||||
|
@ -93,12 +123,38 @@ static int digest_exec ( int argc, char **argv,
|
||||||
len -= frag_len;
|
len -= frag_len;
|
||||||
offset += frag_len;
|
offset += frag_len;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
digest_final ( digest, digest_ctx, digest_out );
|
digest_final ( digest, digest_ctx, digest_out );
|
||||||
|
|
||||||
for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
|
for ( r = 1 ; r < opts.rounds ; r++ ) {
|
||||||
printf ( "%02x", digest_out[j] );
|
digest_init ( digest, digest_ctx );
|
||||||
|
digest_update ( digest, digest_ctx, digest_out,
|
||||||
|
sizeof ( digest_out ) );
|
||||||
|
digest_final ( digest, digest_ctx, digest_out );
|
||||||
|
}
|
||||||
|
|
||||||
printf ( " %s\n", image->name );
|
if ( sizeof( hashstr ) >= sizeof ( digest_out ) )
|
||||||
|
for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
|
||||||
|
sprintf ( hashstr + j*2, "%02x", digest_out[j] );
|
||||||
|
|
||||||
|
if ( parse_autovivified_setting ( "hash", &setting ) == 0 ) {
|
||||||
|
setting.setting.type = &setting_type_string;
|
||||||
|
storef_setting ( setting.settings, &setting.setting,
|
||||||
|
hashstr );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( parse_autovivified_setting ( "hashlen", &setting ) == 0 ) {
|
||||||
|
setting.setting.type = &setting_type_int32;
|
||||||
|
storen_setting ( setting.settings, &setting.setting,
|
||||||
|
origlen );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( opts.str ) {
|
||||||
|
printf( "%s\n", hashstr );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf ( "%s %s\n", hashstr, image->name );
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -112,6 +168,22 @@ static int sha1sum_exec ( int argc, char **argv ) {
|
||||||
return digest_exec ( argc, argv, &sha1_algorithm );
|
return digest_exec ( argc, argv, &sha1_algorithm );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int sha224sum_exec ( int argc, char **argv ) {
|
||||||
|
return digest_exec ( argc, argv, &sha224_algorithm );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sha256sum_exec ( int argc, char **argv ) {
|
||||||
|
return digest_exec ( argc, argv, &sha256_algorithm );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sha384sum_exec ( int argc, char **argv ) {
|
||||||
|
return digest_exec ( argc, argv, &sha384_algorithm );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sha512sum_exec ( int argc, char **argv ) {
|
||||||
|
return digest_exec ( argc, argv, &sha512_algorithm );
|
||||||
|
}
|
||||||
|
|
||||||
struct command md5sum_command __command = {
|
struct command md5sum_command __command = {
|
||||||
.name = "md5sum",
|
.name = "md5sum",
|
||||||
.exec = md5sum_exec,
|
.exec = md5sum_exec,
|
||||||
|
@ -121,3 +193,24 @@ struct command sha1sum_command __command = {
|
||||||
.name = "sha1sum",
|
.name = "sha1sum",
|
||||||
.exec = sha1sum_exec,
|
.exec = sha1sum_exec,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct command sha224sum_command __command = {
|
||||||
|
.name = "sha224sum",
|
||||||
|
.exec = sha224sum_exec,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct command sha256sum_command __command = {
|
||||||
|
.name = "sha256sum",
|
||||||
|
.exec = sha256sum_exec,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct command sha384sum_command __command = {
|
||||||
|
.name = "sha384sum",
|
||||||
|
.exec = sha384sum_exec,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct command sha512sum_command __command = {
|
||||||
|
.name = "sha512sum",
|
||||||
|
.exec = sha512sum_exec,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue