digest_cmd.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <unistd.h>
00022 #include <gpxe/command.h>
00023 #include <gpxe/image.h>
00024 #include <gpxe/crypto.h>
00025
00026 #include <gpxe/md5.h>
00027 #include <gpxe/sha1.h>
00028
00029
00030
00031
00032
00033
00034 static void digest_syntax ( char **argv ) {
00035 printf ( "Usage:\n"
00036 " %s <image name>\n"
00037 "\n"
00038 "Calculate the %s of an image\n",
00039 argv[0], argv[0] );
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 static int digest_exec ( int argc, char **argv,
00051 struct digest_algorithm *digest ) {
00052 const char *image_name;
00053 struct image *image;
00054 uint8_t digest_ctx[digest->ctxsize];
00055 uint8_t digest_out[digest->digestsize];
00056 uint8_t buf[128];
00057 size_t offset;
00058 size_t len;
00059 size_t frag_len;
00060 int i;
00061 unsigned j;
00062
00063 if ( argc < 2 ||
00064 !strcmp ( argv[1], "--help" ) ||
00065 !strcmp ( argv[1], "-h" ) ) {
00066 digest_syntax ( argv );
00067 return 1;
00068 }
00069
00070 for ( i = 1 ; i < argc ; i++ ) {
00071 image_name = argv[i];
00072
00073
00074 image = find_image ( image_name );
00075 if ( ! image ) {
00076 printf ( "No such image: %s\n", image_name );
00077 continue;
00078 }
00079 offset = 0;
00080 len = image->len;
00081
00082
00083 digest_init ( digest, digest_ctx );
00084 while ( len ) {
00085 frag_len = len;
00086 if ( frag_len > sizeof ( buf ) )
00087 frag_len = sizeof ( buf );
00088 copy_from_user ( buf, image->data, offset, frag_len );
00089 digest_update ( digest, digest_ctx, buf, frag_len );
00090 len -= frag_len;
00091 offset += frag_len;
00092 }
00093 digest_final ( digest, digest_ctx, digest_out );
00094
00095 for ( j = 0 ; j < sizeof ( digest_out ) ; j++ )
00096 printf ( "%02x", digest_out[j] );
00097
00098 printf ( " %s\n", image->name );
00099 }
00100
00101 return 0;
00102 }
00103
00104 static int md5sum_exec ( int argc, char **argv ) {
00105 return digest_exec ( argc, argv, &md5_algorithm );
00106 }
00107
00108 static int sha1sum_exec ( int argc, char **argv ) {
00109 return digest_exec ( argc, argv, &sha1_algorithm );
00110 }
00111
00112 struct command md5sum_command __command = {
00113 .name = "md5sum",
00114 .exec = md5sum_exec,
00115 };
00116
00117 struct command sha1sum_command __command = {
00118 .name = "sha1sum",
00119 .exec = sha1sum_exec,
00120 };