v2Create.c 4.86 KB
/* 
 * example and  test program for calling v2 data creation API in libcrypto
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <PR/bcp.h>
#include <PR/bbvirage.h>
#include <PR/bbnand.h>
#include <bbtoolsapi.h>
#include <binary_field.h>

/*
 * -c <cert_out_file>
 * -e <ecc_cert_out_file>
 * -b <bbid>
 */
#define OPTARG      "c:e:b:p:r:"

void usage()
{
    fprintf(stderr,"usage: \n "
            "  v2Create [-b <bbid>] [-p private_key]  -c <cert_out_file> -e <ecc_cert_out_file>"
            "inputdatafile outputviragefile \n");
}

int main(int argc, char ** argv){
    int i;

    FILE *infileptr;
    FILE *outfileptr;
    char *infilename;
    char *outfilename;
    u32  *inblob;
    u32 inblobsize =0;
    u32 *pvt;
    u32 bbid=1; /* default to 1 */
    u32 randoms[12];
    u32 outblob[64];
    FILE *certfileptr=NULL;
    FILE *ecccertfileptr=NULL;
    char* privkey = 0;

    u32 certificateType, sigType, timestamp, exponent;
    BbServerSuffix subjectname;
    BbServerName issuername;
    BbEccPublicKey public;
    u32 publicKey[64];
    u32 rsacert[SIZE_RSA_CERTBLOB_WORDS];
    u32 ecccert[SIZE_BB_CERTBLOB_WORDS];
    char* recrypt = 0;

    /* for getopt usage */
    extern int optind, opterr, optopt;
    extern char *optarg;
    char c;

    optind = 1;
    while ((c = getopt(argc, argv, OPTARG)) != EOF) {
        switch (c) {
        case 'c':
            certfileptr = fopen(optarg, "wb");
            break;
        case 'e':
            ecccertfileptr = fopen(optarg, "wb");
            break;
        case 'b':
            if(optarg[0]=='0' && optarg[1]=='x')
                sscanf(((char *)optarg)+2,"%08lx",&bbid);
            else
                bbid = atol(optarg);
            break;
	case 'p':
	    privkey = optarg;
	    break;
	case 'r':
	    recrypt = optarg;
	    break;
        case 'h':  /* Help */
        default:
            usage();
            return 1;
        }
    }  /* while */

    if( (argc-optind)!=2){
        printf("incorrect number of args!\n\n");
        usage();
        return 1;
    }
    else{
        infilename = argv[optind];
        outfilename = argv[optind+1];
    }
    infileptr = fopen(infilename, "rb");
    if(infileptr == NULL){
        fprintf(stderr,"first create input file example, virage.in\n");
        exit(1);
    }
    outfileptr = fopen(outfilename, "wb");
    if(infileptr == NULL){
        fprintf(stderr,"first create input file example, virage.in\n");
        exit(1);
    }

    /* load content of file into blob: max size 2K bits */
    inblob = (u32 *) malloc(4 * 256);
    inblobsize = fread(inblob, 4, 256, infileptr);
#ifdef DEBUG
    printf("size of inblob = %d\n", inblobsize);
#endif
    
    pvt = (u32 *) malloc(4 * 8);
    /* get random numbers from RNG*/
/*
    for(i = 0; i < 8; i++){
        *((u32 *) (pvt + i)) = rand();
    }
*/
    if (privkey) {
	for(i = 0; i < sizeof(BbEccPrivateKey); i++)
	    sscanf(privkey+8*i, "%08lx", pvt+i);
    } else {
	/*XXXblythe endian-dependent*/
	pvt[0] = (0xa8190276);
	pvt[1] =  (0x7e25db17);
	pvt[2] =  (0x0f3449c5);
	pvt[3] =  (0xd94b162f);
	pvt[4] =  (0xa8190276);
	pvt[5] =  (0x7e25db17);
	pvt[6] =  (0x0f3449c5);
	pvt[7] =  (0xd94b162f);
    }

    /*XXX Set the public key to zero, since this is what Mfr PC does */
    memset(public, 0, sizeof(public));

    for(i = 0; i < 12; i++){
        randoms[i] = 0x00000000;
    }
    if (recrypt) {
	for(i = 0; i < 4; i++)
	    sscanf(recrypt+8*i, "%08lx", randoms+i);
    }

    /* first 4 are boot app key */
    
    generateVirage2Data(inblob, inblobsize * 4, bbid, pvt, public, randoms, outblob);
    /*
    fwrite(&virage, sizeof(virage), 1, outfileptr);
    */
    fwrite(outblob, 4, 64, outfileptr);
    fclose(infileptr);
    fclose(outfileptr);

    /* test for RSA cert */

    certificateType = BB_CERT_TYPE_SERVER;
    sigType = BB_SIG_TYPE_RSA4096;
    timestamp = 0x09062003;
    memset(issuername, 0, sizeof issuername);
    strcpy(issuername, "Root");
    memset(subjectname, 0, sizeof subjectname);
    strcpy(subjectname, "MSCA-00010203");

    for(i =0; i< 64; i++){
        publicKey[i] = rand();
    }
    exponent = 3;

    generateUnsignedRSACert(certificateType, sigType, timestamp, subjectname, issuername, publicKey, exponent, rsacert, SIZE_RSA_CERTBLOB_WORDS);
    
    if(certfileptr)
        fwrite(rsacert, 216, 1, certfileptr);


    /* ecc cert example
     */

    certificateType = BB_CERT_TYPE_BB;
    sigType = BB_SIG_TYPE_RSA2048;
    timestamp = 0x09062003;
    memset(issuername, 0, sizeof issuername);
    strcpy(issuername, "Root-MSCA00010203-MS0a0b0c0d");
    memset(subjectname, 0, sizeof subjectname);
    sprintf(subjectname, "BB%08lx", bbid);

    generateUnsignedBbCert(certificateType, sigType, timestamp, subjectname, issuername, pvt, ecccert, SIZE_BB_CERTBLOB_WORDS);
    
    if(ecccertfileptr)
        fwrite(ecccert, SIZE_BB_CERTBLOB_WORDS, 4, ecccertfileptr);

    return 0;
}