int config_header_v1_v2_chk(SEC_IMG_HEADER *sec_hdr) { sec_hdr->sig_len = get_sigature_size(g_sig_type)+get_hash_size(g_hash_type); /* ------------------------------------- */ /* check sign length */ /* ------------------------------------- */ /* check if the whole image should be signed */ if(0 == sec_hdr->s_len) { sec_hdr->s_len = sec_hdr->img_len - sec_hdr->s_off; } /* check if sign offset is greater than image length */ if(sec_hdr->img_len <= sec_hdr->s_off) { MSG("[%s] IMG len (%d) <= sign off (%d)\n",MOD, sec_hdr->img_len, sec_hdr->s_off); MSG("[%s] Invalid sign off\n",MOD); return -1; } /* check if sign length is greater than image length */ if(sec_hdr->img_len < (sec_hdr->s_len + sec_hdr->s_off)) { MSG("[%s] IMG len (%d) < sign len (%d) + sign off (%d)\n",MOD, sec_hdr->img_len, sec_hdr->s_len, sec_hdr->s_off); sec_hdr->s_len = sec_hdr->img_len - sec_hdr->s_off; MSG("[%s] adjust sign len to (%d)\n",MOD,sec_hdr->s_len); } return 0; }
int config_header_v3_chk(SEC_IMG_HEADER *sec_hdr) { int i; SEC_EXTENTION_CFG *p_ext_cfg = (SEC_EXTENTION_CFG *)get_ext_cfg(); unsigned min_pos = 0; unsigned max_pos = sec_hdr->img_len; unsigned region_end = 0; DBG("[%s] sec_hdr->img_len = %d\n", MOD, sec_hdr->img_len); sec_hdr->sig_len = get_sigature_size(g_sig_type)+get_hash_size(g_hash_type); sec_hdr->s_off = SEC_EXTENSION_MAGIC; sec_hdr->s_len = SEC_EXTENSION_MAGIC; if(p_ext_cfg->verify_count == 0) { MSG("[%s] Sign region count is zero, please check config file(v3)\n",MOD); return -1; } /* check for chunk size 0 */ if(p_ext_cfg->chunk_size == 0) { MSG("[%s] Chunk size is 0, truncate verify count to 1(v3)\n",MOD); p_ext_cfg->verify_count = 1; } /* remove exceed region */ for(i=p_ext_cfg->verify_count-1; i>=0; i--) { if(p_ext_cfg->verify_count == 1) { /* do not remove the only one */ break; } //region_end = p_ext_cfg->verify_offset[i] + p_ext_cfg->verify_length[i]; if(p_ext_cfg->verify_offset[i] > sec_hdr->img_len) { MSG("[%s] Remove config of offset %d (0x%x)(v3)\n",MOD, p_ext_cfg->verify_offset[i],p_ext_cfg->verify_offset[i]); p_ext_cfg->verify_count = p_ext_cfg->verify_count - 1; } else { break; } } /* adjust the last region to be aligned with image length */ i = p_ext_cfg->verify_count-1; region_end = p_ext_cfg->verify_offset[i] + p_ext_cfg->verify_length[i]; if( max_pos < region_end ) { MSG("[%s] The last region's original length is %d (0x%x)\n", MOD, p_ext_cfg->verify_length[i], p_ext_cfg->verify_length[i]); p_ext_cfg->verify_length[i] = max_pos - p_ext_cfg->verify_offset[i]; MSG("[%s] Adjust last region's original length to %d (0x%x)\n", MOD, p_ext_cfg->verify_length[i], p_ext_cfg->verify_length[i]); } /* check if need to sign for whole image */ if(p_ext_cfg->verify_count == 1 && p_ext_cfg->verify_length[0]==0 ) { MSG("[%s] The sign length is zero, and sign offset is : %d (0x%x)\n",MOD,p_ext_cfg->verify_offset[0],p_ext_cfg->verify_offset[0]); p_ext_cfg->verify_length[0] = sec_hdr->img_len - p_ext_cfg->verify_offset[0] ; MSG("[%s] Set the sign length to rest whole image length : %d (0x%x)\n",MOD,p_ext_cfg->verify_length[0],p_ext_cfg->verify_length[0]); } /* check if sign region is inside the image length */ p_ext_cfg->verify_offset[p_ext_cfg->verify_count] = max_pos; for(i=0;i<p_ext_cfg->verify_count;i++) { if(sec_hdr->img_len < p_ext_cfg->verify_length[i]) { MSG("[%s] Sign length exceeds image length(v3)\n",MOD); return -1; } if(p_ext_cfg->verify_length[i] == 0) { MSG("[%s] Sign length can't be zero(v3)\n",MOD); return -1; } region_end = p_ext_cfg->verify_offset[i] + p_ext_cfg->verify_length[i]; if(sec_hdr->img_len < region_end) { MSG("[%s] Sign region exceeds image length(v3)\n",MOD); return -1; } DBG("[%d] min_pos is %d\n", i, min_pos); DBG("p_ext_cfg->verify_offset[%d] is %d\n", i, p_ext_cfg->verify_offset[i]); DBG("[%d] region_end is %d\n", i, region_end); DBG("[%d] next region start is %d\n", i, p_ext_cfg->verify_offset[i+1]); /* check if sign region is overlay */ if((min_pos<=p_ext_cfg->verify_offset[i])&& (region_end<=p_ext_cfg->verify_offset[i+1])) { DBG("[%s] Sign region (%d->%d) ok(v3)\n",MOD, p_ext_cfg->verify_offset[i], region_end-1); } else { MSG("[%s] Sign region is overlap(v3)\n",MOD); return -1; } min_pos = region_end; } p_ext_cfg->verify_offset[p_ext_cfg->verify_count] = 0; if(p_ext_cfg->verify_count == 0) { MSG("[%s] Sign region count is zero, please check program(v3)\n",MOD); return -1; } return 0; }
int pro_img_v3(char *hs_name, char *img_name,char *hdr_name) { uint32 br = 0; uchar *d_buf = NULL; uchar *d_buf_prt = NULL; uchar c_buf[SEC_IMG_HDR_SZ]; uchar *sig; uchar *hash; uint32 i = 0, ret = 0; SEC_IMG_HEADER *sec = NULL; SEC_EXTENTION_CFG *p_ext_cfg = (SEC_EXTENTION_CFG *)get_ext_cfg(); SEC_EXTENSTION_CRYPTO *crypto_ext = NULL; SEC_FRAGMENT_CFG *frag_ext = NULL; SEC_EXTENSTION_HASH_ONLY **hash_only_ext; SEC_EXTENSTION_END_MARK *end_ext = NULL; uint32 total_size = 0; uint32 real_chunk_size = 0; /* ------------------------------------- */ /* open hash and signature file */ /* ------------------------------------- */ FILE *hs_fd = fopen(hs_name,"wb"); if(hs_fd == 0) { MSG("[%s] %s not found\n",MOD,hs_name); goto _init_fail; } /* ------------------------------------- */ /* read image header */ /* ------------------------------------- */ FILE *hdr_fd = fopen(hdr_name,"r"); br = fread(c_buf,1,SEC_IMG_HDR_SZ,hdr_fd); /* read header */ sec = (SEC_IMG_HEADER *)c_buf; if(br == 0) { MSG("\n[%s] read '%s' image hdr fail, read bytes = '%d'\n",MOD,hdr_name,br); ret = -1; goto _hdr_fail; } /* ------------------------------------- */ /* initialize buffer */ /* ------------------------------------- */ sig = (uchar*) malloc(get_sigature_size(g_sig_type)); hash = (uchar*) malloc(get_hash_size(g_hash_type)); /* ------------------------------------- */ /* initialize extnesion header buffer */ /* ------------------------------------- */ crypto_ext = (SEC_EXTENSTION_CRYPTO *) allocate_ext_crypto(); frag_ext = (SEC_FRAGMENT_CFG *) allocate_ext_frag(); hash_only_ext = (SEC_EXTENSTION_HASH_ONLY **)malloc(p_ext_cfg->verify_count * sizeof(SEC_EXTENSTION_HASH_ONLY *)); for(i=0;i<p_ext_cfg->verify_count;i++) { hash_only_ext[i] = (SEC_EXTENSTION_HASH_ONLY *) allocate_ext_hash_only(g_hash_type); } end_ext = (SEC_EXTENSTION_END_MARK *) allocate_ext_end(); /* ------------------------------------- */ /* initial extenstion header */ /* ------------------------------------- */ crypto_ext->hash_type = g_hash_type; crypto_ext->sig_type = g_sig_type; crypto_ext->enc_type = SEC_CRYPTO_ENC_UNKNOWN; frag_ext->frag_count = p_ext_cfg->verify_count; frag_ext->chunk_size = p_ext_cfg->chunk_size; for(i=0;i<p_ext_cfg->verify_count;i++) { hash_only_ext[i]->hash_offset = p_ext_cfg->verify_offset[i]; hash_only_ext[i]->hash_len = p_ext_cfg->verify_length[i]; } /* ----------------------------------------- */ /* generate hash for each region by chunk size */ /* ----------------------------------------- */ FILE *img_fd = fopen(img_name,"r"); if(img_fd == 0) { MSG("[%s] %s not found\n",MOD,img_name); ret = -1; goto _img_open_fail; } for(i=0;i<p_ext_cfg->verify_count;i++) { if(frag_ext->chunk_size == 0) { real_chunk_size = hash_only_ext[i]->hash_len; } else { real_chunk_size = frag_ext->chunk_size; } if(gen_hash_by_chunk(img_fd, hash_only_ext[i]->hash_offset, hash_only_ext[i]->hash_len, hash, hash_only_ext[i]->sub_type, real_chunk_size)!=0) { ret = -1; goto _ext_hash_fail; } mcpy(hash_only_ext[i]->hash_data,hash,get_hash_size(g_hash_type)); } /* ------------------------------------- */ /* prepare buffer */ /* ------------------------------------- */ total_size = SEC_IMG_HDR_SZ + p_ext_cfg->verify_count*get_hash_size(g_hash_type)+ sizeof(*crypto_ext)+ sizeof(*frag_ext)+ p_ext_cfg->verify_count*get_ext_hash_only_struct_size(g_hash_type)+ sizeof(*end_ext); d_buf = (uchar*) malloc(total_size); d_buf_prt = d_buf; /* copy header */ mcpy(d_buf_prt,c_buf,SEC_IMG_HDR_SZ); d_buf_prt += SEC_IMG_HDR_SZ; /* copy hash */ for(i=0;i<p_ext_cfg->verify_count;i++) { mcpy(d_buf_prt,hash_only_ext[i]->hash_data,get_hash_size(g_hash_type)); d_buf_prt += get_hash_size(g_hash_type); } /* copy crypto extension */ mcpy(d_buf_prt,crypto_ext,sizeof(*crypto_ext)); d_buf_prt += sizeof(*crypto_ext); /* copy frag extension */ mcpy(d_buf_prt,frag_ext,sizeof(*frag_ext)); d_buf_prt += sizeof(*frag_ext); /* copy hash extension */ for(i=0;i<p_ext_cfg->verify_count;i++) { mcpy(d_buf_prt,hash_only_ext[i],get_ext_hash_only_struct_size(g_hash_type)); d_buf_prt += get_ext_hash_only_struct_size(g_hash_type); } /* copy end mark extension */ mcpy(d_buf_prt,end_ext,sizeof(*end_ext)); d_buf_prt += sizeof(*end_ext); /* ------------------------------------- */ /* generate hash */ /* ------------------------------------- */ if( cust_hash(d_buf,total_size,hash,get_hash_size(g_hash_type)) == -1) { MSG("[%s] Sign %s fail\n",MOD,img_name); ret = -1; goto _final_hash_fail; } /* ------------------------------------- */ /* generate signature */ /* ------------------------------------- */ if( cust_sign(d_buf,total_size,sig,get_sigature_size(g_sig_type)) == -1) { MSG("[%s] Sign %s fail\n",MOD,img_name); ret = -1; goto _final_sign_fail; } /* ------------------------------------- */ /* dump hash value for debug */ /* ------------------------------------- */ #if DUMP_MORE_FOR_DEBUG { unsigned loop_count = total_size/8; unsigned remain_count = total_size%8; MSG("[%s] Total verify size is : %d\n",MOD, total_size); for(i=0; i<loop_count ; i++) { DBG("[%s] Data value [%d-%d]==> (0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x) \n",MOD, i*8, (i+1)*8-1, d_buf[0+i*8], d_buf[1+i*8], d_buf[2+i*8], d_buf[3+i*8], d_buf[4+i*8], d_buf[5+i*8], d_buf[6+i*8], d_buf[7+i*8]); } if(remain_count) { DBG("[%s] Data value [%d-%d]==> (",MOD,loop_count*8, loop_count*8+remain_count); for(i=0; i<remain_count ; i++) { DBG("0x%x,", d_buf[loop_count*8+i]); } DBG(") \n"); } } #endif MSG("[%s] Hash value : \n",MOD); for(i=0;i<get_hash_size(g_hash_type);i++) { MSG("0x%x,",hash[i]); } MSG("\n",MOD); /* ------------------------------------- */ /* write hash and signature */ /* ------------------------------------- */ fwrite (sig , 1 , get_sigature_size(g_sig_type), hs_fd); fwrite (hash , 1 , get_hash_size(g_hash_type) , hs_fd); /* ------------------------------------- */ /* write extension */ /* ------------------------------------- */ d_buf_prt = d_buf; d_buf_prt += SEC_IMG_HDR_SZ + p_ext_cfg->verify_count*get_hash_size(g_hash_type); fwrite (d_buf_prt , 1 , total_size-(SEC_IMG_HDR_SZ+p_ext_cfg->verify_count*get_hash_size(g_hash_type)), hs_fd); fclose (hs_fd); _final_sign_fail: _final_hash_fail: free(d_buf); _ext_hash_fail: _img_open_fail: _img_read_fail: free(end_ext); free(frag_ext); free(crypto_ext); free(hash_only_ext); free(hash); free(sig); _hdr_fail: fclose(hdr_fd); _init_fail: return ret; }
unsigned int get_ext_hash_sig_struct_size(SEC_CRYPTO_HASH_TYPE hash, SEC_CRYPTO_SIGNATURE_TYPE sig) { return get_sigature_size(sig) + get_hash_size(hash) + sizeof(SEC_EXTENSTION_HASH_SIG); }
int pro_img_v1_v2(char *hs_name, char *img_name,char *hdr_name) { uint32 br = 0; uchar *d_buf = NULL; uchar c_buf[SEC_IMG_HDR_SZ]; uchar *sig; uchar *hash; uint32 i = 0; SEC_IMG_HEADER *sec = NULL; /* ------------------------------------- */ /* open hash and signature file */ /* ------------------------------------- */ FILE *hs_fd = fopen(hs_name,"wb"); if(hs_fd == 0) { MSG("[%s] %s not found\n",MOD,hs_name); goto _err; } /* ------------------------------------- */ /* read image header */ /* ------------------------------------- */ FILE *hdr_fd = fopen(hdr_name,"r"); br = fread(c_buf,1,SEC_IMG_HDR_SZ,hdr_fd); /* read header */ sec = (SEC_IMG_HEADER *)c_buf; if(br == 0) { MSG("\n[%s] read '%s' image hdr fail, read bytes = '%d'\n",MOD,hdr_name,br); goto _err; } /* ------------------------------------- */ /* initialize buffer */ /* ------------------------------------- */ d_buf = (uchar*) malloc(SEC_IMG_HDR_SZ + sec->s_len*sizeof(char)); sig = (uchar*) malloc(get_sigature_size(g_sig_type)); hash = (uchar*) malloc(get_hash_size(g_hash_type)); mcpy(d_buf,c_buf,SEC_IMG_HDR_SZ); /* ------------------------------------- */ /* read image content */ /* ------------------------------------- */ FILE *img_fd = fopen(img_name,"r"); if(img_fd == 0) { MSG("[%s] %s not found\n",MOD,img_name); goto _err; } fseek(img_fd,sec->s_off*sizeof(char),SEEK_SET); br = fread(d_buf+SEC_IMG_HDR_SZ,1,sec->s_len,img_fd); if(br == 0) { MSG("\n[%s] read image content fail, read bytes = '%d'\n",MOD,br); goto _err; } /* ------------------------------------- */ /* Sign * @1 : file * @2 : file length to be signed * @3 : signature * @3 : signature length */ /* ------------------------------------- */ if( cust_sign(d_buf,SEC_IMG_HDR_SZ + sec->s_len,sig,get_sigature_size(g_sig_type)) == -1) { MSG("[%s] Sign %s fail\n",MOD,img_name); goto _err; } /* ------------------------------------- */ /* Hash * @1 : file * @2 : file length to be hashed * @3 : hash * @3 : hash length */ /* ------------------------------------- */ if( cust_hash(d_buf,SEC_IMG_HDR_SZ + sec->s_len,hash,get_hash_size(g_hash_type)) == -1) { MSG("[%s] Sign %s fail\n",MOD,img_name); goto _err; } /* ------------------------------------- */ /* dump hash value for debug */ /* ------------------------------------- */ MSG("[%s] Hash value : \n",MOD); for(i=0;i<get_hash_size(g_hash_type);i++) { MSG("0x%x,",hash[i]); } MSG("\n",MOD); /* ------------------------------------- */ /* write hash and signature */ /* ------------------------------------- */ fwrite (sig , 1 , get_sigature_size(g_sig_type), hs_fd); fwrite (hash , 1 , get_hash_size(g_hash_type) , hs_fd); fclose (hs_fd); free(d_buf); return 0; _err: free(d_buf); return -1; }