digest_cmd.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Daniel Verkamp <daniel@drv.nu>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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  * "digest" command syntax message
00031  *
00032  * @v argv              Argument list
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  * The "digest" command
00044  *
00045  * @v argc              Argument count
00046  * @v argv              Argument list
00047  * @v digest            Digest algorithm
00048  * @ret rc              Exit code
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                 /* find image */
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                 /* calculate digest */
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 };

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1