sigadd.c 3.72 KB
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <stdlib.h>
#include <string.h>
#include <sha1.h>
#include <PR/bbmetadata.h>
#include <PR/bbcert.h>
#include <stdlib.h>
#include <bb_nn.h>
#include <PR/bbcrl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <algorithms.h>
/*
#define DEBUG
*/
   
#define FILE_SIZE (16*1024)

typedef struct {
    char name[12];
    BbEccSig sig;
} sig_entry_t;

/* return zero if state file name not found */
int lookupSig(FILE *sigptr, char *statename, BbEccSig sig, sig_entry_t *sigret){
  int i;
  sig_entry_t sigentry;
  int num_compare;
  char charac;
  if(strchr(statename, 'p') != NULL){
    /* Controller pak : .pak file */
    num_compare = 1;
    charac= 'p';
  }
  else{
    /* default .sta file */
    num_compare = 8;
    charac= 's';
  }
  while(fread((void *) &sigentry, sizeof(sig_entry_t), 1, sigptr) ==1){
    
    if((strncmp(sigentry.name, statename, num_compare) == 0)
       &&(strchr(sigentry.name+8, charac) != NULL)){
      printf("state found = %s\n", sigentry.name);
      memcpy(sig, sigentry.sig, sizeof(BbEccSig));
      memcpy(sigret, &sigentry, sizeof(sig_entry_t));
      for(i=0; i< sizeof(BbEccSig)/4; i++){
	sig[i] = ntohl(sig[i]);
      }
      return 1;
    }
  }
  return 0;
}

   
   
     
int main(int argc, char **argv){
  int i;
  unsigned char hash_data[20];  
  SHA1Context sha;
  u8 *stateBlob;
  FILE *statedataptr;
  FILE *sigptr;
  sig_entry_t sigentry;      
  struct stat statbuf;
  int state_size;
  
  BbEccSig sig;
  unsigned char zero;
  FILE *testptr;

  if(argc != 3){
    fprintf(stderr,"usage: %s  statefile signaturefile\n", argv[0]);
    exit(1);
  }

  /* pull state data from file */
  statedataptr = fopen(argv[1], "r");
  if(statedataptr ==0){
    fprintf(stderr,"couldnt find your state data in local directory\n");
    exit(1);
  }
     

  /* read in state data */
  stat(argv[1], &statbuf);
  state_size = (long) statbuf.st_size;
  stateBlob= malloc(state_size);
  fread(stateBlob, state_size, 1, statedataptr);

  /* compute hash */
  SHA1Reset(&sha);
  SHA1Input(&sha, ((u8 *)(stateBlob)),state_size);
  SHA1Result(&sha, hash_data);  

  if(statedataptr) fclose(statedataptr);

    
  if((sigptr = fopen(argv[2], "r")) == 0){
    fprintf(stderr,"cant open bb sig file\n");
    exit(1);
  }
  /* look up signature corresponding to the file */
  if( lookupSig(sigptr, argv[1], sig, &sigentry) ==0){
    fprintf(stderr, "Lookup failed: no signature\n");
    exit(1);
  }

  /* write a whole sig.db with that sig */
  testptr = fopen("sig.db.1", "w");
  for(i =0; i < FILE_SIZE/sizeof(sig_entry_t); i++){
    fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
  }
  zero = 0x00;
  for(i= (FILE_SIZE/sizeof(sig_entry_t)) *sizeof(sig_entry_t); i< FILE_SIZE; i++){
    fwrite(&zero, 1, sizeof(unsigned char), testptr);
  }
  fclose(testptr);

  /* write a sig.db with first entry + random state name */
    
  testptr = fopen("sig.db.2", "w");
  fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
  /* change the name */
  sigentry.name[0] += 1;
  for(i =0; i < FILE_SIZE/sizeof(sig_entry_t)-1; i++){
    fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
  }
  zero = 0x00;
  for(i= (FILE_SIZE/sizeof(sig_entry_t)) *sizeof(sig_entry_t); i< FILE_SIZE; i++){
    fwrite(&zero, 1, sizeof(unsigned char), testptr);
  }
  fclose(testptr);
  
  
  /* write a whole sig.db with some random state name */
  testptr = fopen("sig.db.3", "w");
  sigentry.name[0] +=1;
  for(i =0; i < FILE_SIZE/sizeof(sig_entry_t); i++){
    fwrite(&sigentry, sizeof(sig_entry_t), 1, testptr);
  }
  zero = 0x00;
  for(i= (FILE_SIZE/sizeof(sig_entry_t)) *sizeof(sig_entry_t); i< FILE_SIZE; i++){
    fwrite(&zero, 1, sizeof(unsigned char), testptr);
  }
  fclose(testptr);
  
  return 0;
}