rtmfp协议分析详解代码实现(二)

来源:互联网 发布:nfc软件读取银行卡 编辑:程序博客网 时间:2024/05/17 04:52
</pre><pre code_snippet_id="1920077" snippet_file_name="blog_20161009_2_3271150" name="code" class="cpp">//计算校验和int checksum(unsigned char *buff, unsigned int len){unsigned char *pos;unsigned int sum = 0;unsigned int vlen = 0;unsigned short n_tmp = 0;pos = buff;vlen = len;while(vlen > 0 && pos){if(vlen == 1){sum += *((unsigned char *)pos);(unsigned char *)pos++;vlen -= 1;}else{n_tmp = *((unsigned short *)pos);n_tmp = ntohs(n_tmp);sum += n_tmp;//sum += *((unsigned short *)pos);(unsigned char *)pos++;(unsigned char *)pos++;vlen -= 2;}}/* add back carry outs from top 16 bits to low 16 bits */sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */sum += (sum >> 16);                     /* add carry */return ~sum; /* truncate to 16 bits */}
//读校验和int read_crc(tOptBuff *buff){unsigned short read_sum = 0;unsigned short check_sum = 0;// 2bytes checksum valueread_half_word(buff, &read_sum);if(read_sum == 0){return -1;}read_sum = ntohs(read_sum);//count cheksum for raw packagecheck_sum = checksum(buff->data + buff->dlen, buff->tlen - buff->dlen);return (read_sum == check_sum);}<pre name="code" class="cpp">//读检验和值int write_crc(tOptBuff *buff){unsigned short sum;sum = checksum(buff->data+6, buff->dlen-6);sum = htons(sum);//replace fourth bytes for start two bytesreturn write_buff_value(buff->data+4, 2, &sum);}


//写证书长度,最高位表示是否还有下个字节void write_7bit_variable_length(tOptBuff *buff, unsigned int length){unsigned char shift = 0;unsigned char value = 0;int is_max = 0;shift = (get_7bit_value_size(length)-1)*7;if(shift>=21) { // 4 bytes maximumshift = 22;is_max = 1;}while(shift>=7) {value = 0x80 | ((length>>shift)&0x7F);write_bytes(buff, value);shift -= 7;}value = is_max ? length&0xFF : length&0x7F;write_bytes(buff, value);}
//读证书长度,最高位表示是否还有下个字节unsigned short read_7bit_variable_length(tOptBuff *buff){unsigned char b = 0x00, n = 0x00;unsigned short result = 0;// first 1bytes variable lengthread_bytes(buff, &b);while ((b&0x80) && n < 8) {        result <<= 7;        result |= (b&0x7F);//peel secondread_bytes(buff, &b);        ++n;       }// Use all 8 bits from the 4th byteresult <<= ((n<8) ? 7 : 8);        result |= b;return result;}<pre name="code" class="cpp">//AES加解密void aes_crypto(unsigned char *in, int in_len, unsigned char *out, unsigned char *key, eCrypt crypt_type){    unsigned char ivec[16] = {0};    AES_KEY aes_key;    if( crypt_type == aes_decrypt)    {AES_set_decrypt_key(key, 0x80, &aes_key);    }    else    {AES_set_encrypt_key(key, 0x80,&aes_key);    }    AES_cbc_encrypt(in, out, in_len, &aes_key, ivec,  crypt_type);    return ;}
//加密void encode(tOptBuff *buff, tEncodeKey *encode_key, tEncodetype type){if(type != empty){unsigned int  buff_len ;int len;buff_len = buff->dlen;len  = (0xFFFFFFFF - buff_len + 5)&0x0F;generate_paddings(buff->data + buff_len, len);buff_tail_push(buff,len);}write_crc(buff);aes_crypto(buff->data + 4, buff->dlen - 4, buff->data+4, encode_key->key, aes_encrypt);}
//解密int decode(tOptBuff *buff, tDecodeKey *decode_key, tDecodetype type){aes_crypto(buff->data + 4, buff->tlen - 4, buff->data+4, decode_key->key, aes_decrypt);return read_crc(buff);}

//IHello封装包int initiatior_hello(tOptBuff *ihello, unsigned char epd_type, unsigned char *epd, unsigned char epd_len){unsigned char tags[default_tag_size] = {0};unsigned char unkown;int start_len = 0;start_len = ihello->dlen;//unkownunkown = epd_len + 2;write_bytes(ihello, unkown);// epd lenunkown = epd_len + 1;write_bytes(ihello, unkown);write_bytes(ihello, epd_type);write_buff_offset(ihello, epd_len, epd);generate_tag_string(tags, default_tag_size);write_buff_offset(ihello, default_tag_size, tags);return ihello->dlen - start_len;}

//RHello封包int responder_hello(tOptBuff *rhello, tTag *tags ,tCookie *cookie, tComponent *component){int start_len = 0;start_len = rhello->dlen;// 16bytes tags write_bytes(rhello, tags->length);write_buff_offset(rhello, tags->length, tags->data);// 64bytes cookiewrite_bytes(rhello, cookie->length);write_buff_offset(rhello, cookie->length, cookie->data);// 77bytes componentwrite_bytes(rhello, component->length);write_buff_offset(rhello, component->length, component->data);return rhello->dlen - start_len;}
//IIKey封包int initiatior_key(tOptBuff *ikey, tCookie *cookie, tCertificate *certificate, tComponent* component){unsigned int session_id = 0x02;unsigned int start_len = 0;start_len = ikey->dlen;// 4bytes session idsession_id = htons(session_id);write_word(ikey, session_id);//cookie write_bytes(ikey, cookie->length);write_buff_offset(ikey, cookie->length, cookie->data);//certificate write_7bit_variable_length(ikey, certificate->length);write_buff_offset(ikey, certificate->length, certificate->data);//component(nonce)write_bytes(ikey, component->length);write_buff_offset(ikey, component->length, component->data);// X 0x58write_bytes(ikey, 'X');return ikey->dlen - start_len;}

//RIKey封包int responder_initial_keying(tOptBuff *rikey, unsigned int session_id, tCertificate *certificate){unsigned int start_len = 0;start_len = rikey->dlen;// 4bytes session idsession_id = htons(session_id);write_word(rikey, session_id);//certificate write_7bit_variable_length(rikey, certificate->length);write_buff_offset(rikey, certificate->length, certificate->data);// X 0x58write_bytes(rikey, 'X');return rikey->dlen - start_len;}

//计算共享密钥static int generate_shared_key(DH *pDH, tCertificate *ertificate, tDHSharedSecret *shared_key, unsigned int offset){return dh_compute_shared_key(pDH, ertificate->data + offset, ertificate->length - offset, shared_key);}//计算加密密钥static int generate_encode_key(tComponent *responder_nonce, tCertificate *init_nonce,  tDHSharedSecret *shared_key, tEncodeKey * encode_key){return encode_secret_key(responder_nonce->data, responder_nonce->length, init_nonce->data, init_nonce->length, shared_key, encode_key);}//计算解密密钥static int generate_decode_key(tCertificate *init_nonce, tComponent *responder_nonce ,tDHSharedSecret *shared_key, tDecodeKey *decode_key){return decode_secret_key(init_nonce->data, init_nonce->length, responder_nonce->data, responder_nonce->length, shared_key, decode_key);}

0 0
原创粉丝点击