void key_calculation(UCHAR *keybit) { INT32 i,j; for (i=0;i<56;i++) /* permuted choice 1 */ key_cd[i] = keybit[des_pc1[i]-1]; for (i=0;i<16;i++) { left_shift(key_cd,des_lshift[i]); /* left shift */ left_shift(key_cd+28,des_lshift[i]); for (j=0;j<48;j++) /* permuted choice 2 */ key_k[i][j] = key_cd[des_pc2[j]-1]; } }
void shift(int A[MAX], int units) { if(units < 0) { left_shift(A, -units); } else if(units > 0) { right_shift(A, units); } }
/** * Multiply h1 by h2, overwriting the value of h1. */ void multiply( huge *h1, huge *h2 ) { unsigned char mask; unsigned int i; int result_sign; huge temp; set_huge( &temp, 0 ); copy_huge( &temp, h1 ); result_sign = !( h1->sign == h2->sign ); set_huge( h1, 0 ); i = h2->size; do { i--; for ( mask = 0x01; mask; mask <<= 1 ) { if ( mask & h2->rep[ i ] ) { add( h1, &temp ); } left_shift( &temp ); } } while ( i ); h1->sign = result_sign; }
static void double_block(const uchar *in, uchar *out) { left_shift(in, AES_BLOCK_SIZE, out, 1); if (*in & 0x80) { out[AES_BLOCK_SIZE-1] ^= 0x87; } }
//利用用户提供的key产生加密和解密用的钥匙 void generate_keys(long long a) { //根据用户输入的long long,得到钥匙 bitset<64> key; get_key(a,key); //选择置换PC-1 bitset<56> key1; for (int i = 0; i < 56; i++) { key1[i] = key[63 - PC_1[i]];//因为bitset对象的下标是逆序的63~0,所以为63 - PC_1[i] } //cout<<"key1:"<<key1<<endl; //将56bit分为两组 bitset<28> c;//左 bitset<28> d;//右 for (int i = 0; i < 28; i++) { c[i] = key1[i]; d[i] = key1[i+28]; } //cout<<"c:"<<c<<endl<<"d:"<<d<<endl; //重复生成加密和解密中使用的Kn for (int j = 0; j < 16; j++) { //左巡回转换 left_shift(c,left_shift_num[j]); left_shift(d,left_shift_num[j]); //合并 bitset<56> key3; for (int i = 0; i < 28; i++) { key3[i] = c[i]; key[i+28] = d[i]; } //压缩置换PC-2 for (int i = 0; i < 48; i++) { KEY[j][i] = key3[56 - PC_2[i]]; } cout<<KEY[j]<<endl; } }
int main(void){ int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; left_shift(a, sizeof(a)/sizeof(a[0]), 5); print_all(a, sizeof(a)/sizeof(a[0])); return 0; }
/** DEBUT BIGINT factorielle(BIGINT, INT); DEBUT **/ BIGINT factorielle(unsigned int nb) { BIGINT tmp, tmp2, tmp3; BIGINT p = chaine2liste("1"); BIGINT r = chaine2liste("1"); int h = 0, shift = 0, high = 1; unsigned int y = nb; initListe(&tmp); initListe(&tmp2); initListe(&tmp3); if (nb < 2) return chaine2liste("1"); N = 1; int log2n = -1; /// Equivaut à log2(n) /// while (y) { y >>= 1; log2n++; } while (h != nb) { shift += h; h = nb >> log2n--; int len = high; high = (h - 1) | 1; len = (high - len) / 2; if (len > 0) { tmp = produit(len); tmp2 = multiplication(p, tmp); viderListe(&p); p = tmp2; viderListe(&tmp); tmp3 = multiplication(r, p); viderListe(&r); r = tmp3; } } viderListe(&p); tmp = left_shift(r, shift); viderListe(&r); return tmp; }
void gen_keys() { int key[10],i,keyip[10]; int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9}; printf("Enter Key :"); for(i=0;i<10;i++) scanf("%d", &key[i]); for(i=0;i<10;i++) // Permutation P10 keyip[i] = key[p10[i]-1]; left_shift(keyip,1); // Left Shifting (Array,No of bts) printf("\nKey1 :"); for(i=0;i<8;i++){ //Permuting P8 on key1 keys[0][i] = keyip[p8[i]-1];// Key1 Generated!! printf("%d",keys[0][i]); } left_shift(keyip,2);// Generating Key2 . . printf("\nKey2 :"); for(i=0;i<8;i++){ keys[1][i] = keyip[p8[i]-1];// Key2 Generated!! printf("%d",keys[1][i]); } }
static void offset_init(const ocb_aes_t *ctx, char *offset) { const int stretch_size = AES_BLOCK_SIZE + (AES_BLOCK_SIZE/2); uchar nonce[AES_BLOCK_SIZE]; int bottom; uchar ktop[AES_BLOCK_SIZE]; uchar stretch[stretch_size]; uchar shifted_block[stretch_size]; memset(offset, 0, AES_BLOCK_SIZE); nonce_init(ctx, nonce); bottom = 0; bottom = (nonce[AES_BLOCK_SIZE - 1] & 0x1F); ktop_init(ctx, nonce, ktop); stretch_init(ktop, stretch); left_shift(stretch, stretch_size, shifted_block, bottom); cpy_block(offset, shifted_block); }
/** * dividend = numerator, divisor = denominator * * Note that this process destroys divisor (and, of course, * overwrites quotient). The dividend is the remainder of the * division (if that's important to the caller). The divisor will * be modified by this routine, but it will end up back where it * “started”. */ void divide( huge *dividend, huge *divisor, huge *quotient ) { int bit_size, bit_position; // "bit_position" keeps track of which bit, of the quotient, // is being set or cleared on the current operation. bit_size = bit_position = 0; // First, left-shift divisor until it's >= than the dividend while ( compare( divisor, dividend ) < 0 ) { left_shift( divisor ); bit_size++; } // overestimates a bit in some cases if ( quotient ) { quotient->sign = !( dividend->sign == dividend->sign ); quotient->size = ( bit_size / 8 ) + 1; quotient->rep = ( unsigned char * ) calloc(quotient->size, sizeof( unsigned char ) ); memset( quotient->rep, 0, quotient->size ); } bit_position = 8 - ( bit_size % 8 ) - 1; do { if ( compare( divisor, dividend ) <= 0 ) { subtract_magnitude( dividend, divisor ); // dividend -= divisor if ( quotient ) { quotient->rep[ ( int ) ( bit_position / 8 ) ] |= ( 0x80 >> ( bit_position % 8 ) ); } } if ( bit_size ) { right_shift( divisor ); } bit_position++; }
/*************************************************** * MOVE DOWN ***************************************************/ char jet_moveDown(struct Jet* input) { if (input -> yLocation == 3 && input -> bot_row[1] == 127) { return 1; } else { char move_index; //Shift the values to make the object appear to move down for (move_index = 0; move_index < JET_LENGTH; move_index++) { left_shift(&input -> mid_row[move_index], &input -> bot_row[move_index]); if ((input -> top_row[move_index] & 0x80) == 0x80) { input -> mid_row[move_index] = input -> mid_row[move_index] | 0x01; } //Righ shift top row input -> top_row[move_index] = input -> top_row[move_index] << 1; } input -> yPos--; //Shuffle variables that contain the object top = mid, mid = bot, bot = 0x00 if (input -> yPos == 0 && input -> yLocation != 3) { set_LCD_Cursor(input -> xLocation, input -> yLocation); for (move_index = 0; move_index < JET_LENGTH; move_index++) { LcdWrite(0x00); input -> top_row[move_index] = input -> mid_row[move_index]; input -> mid_row[move_index] = input -> bot_row[move_index]; input -> bot_row[move_index] = 0x00; } input -> yPos = 8; input -> yLocation++; } input -> yPixelPosition++; display_jet(input, 0); return 0; } }
/**************************************************** * Falcon MOVE DOWN ****************************************************/ char Falcon_moveDown(struct Falcon* Falcon) { if (Falcon -> yPixelPosition > 48 - FALCON_HEIGHT) { return 1; } else { char falcon_index; //Shift the values to make the object appear to move down for (falcon_index = 0; falcon_index < SHIP_LENGTH; falcon_index++) { left_shift(&Falcon -> mid_row[falcon_index], &Falcon -> bot_row[falcon_index]); if ((Falcon -> top_row[falcon_index] & 0x80) == 0x80) { Falcon -> mid_row[falcon_index] = Falcon -> mid_row[falcon_index] | 0x01; } //Righ shift top row Falcon -> top_row[falcon_index] = Falcon -> top_row[falcon_index] << 1; } Falcon -> yPos--; //Shuffle variables that contain the object top = mid, mid = bot, bot = 0x00 if (Falcon -> yPos == 0 && Falcon -> yLocation != 3) { set_LCD_Cursor(Falcon -> xLocation, Falcon -> yLocation); for (falcon_index = 0; falcon_index < SHIP_LENGTH; falcon_index++) { LcdWrite(0x00); Falcon -> top_row[falcon_index] = Falcon -> mid_row[falcon_index]; Falcon -> mid_row[falcon_index] = Falcon -> bot_row[falcon_index]; Falcon -> bot_row[falcon_index] = 0x00; } Falcon -> yPos = 8; Falcon -> yLocation++; } Falcon -> yPixelPosition++; displayFalcon(Falcon, 0); return 0; } }
exprt flatten_byte_extract( const exprt &src, const namespacet &ns) { assert(src.id()==ID_byte_extract_little_endian || src.id()==ID_byte_extract_big_endian); assert(src.operands().size()==2); bool little_endian; if(src.id()==ID_byte_extract_little_endian) little_endian=true; else if(src.id()==ID_byte_extract_big_endian) little_endian=false; else assert(false); if(src.id()==ID_byte_extract_big_endian) throw "byte_extract flattening of big endian not done yet"; unsigned width= integer2long(pointer_offset_size(ns, src.type())); const typet &t=src.op0().type(); if(t.id()==ID_array) { const array_typet &array_type=to_array_type(t); const typet &subtype=array_type.subtype(); // byte-array? if((subtype.id()==ID_unsignedbv || subtype.id()==ID_signedbv) && subtype.get_int(ID_width)==8) { // get 'width'-many bytes, and concatenate exprt::operandst op; op.resize(width); for(unsigned i=0; i<width; i++) { // the most significant byte comes first in the concatenation! unsigned offset_i= little_endian?(width-i-1):i; plus_exprt offset(from_integer(offset_i, src.op1().type()), src.op1()); index_exprt index_expr(subtype); index_expr.array()=src.op0(); index_expr.index()=offset; op[i]=index_expr; } if(width==1) return op[0]; else // width>=2 { concatenation_exprt concatenation(src.type()); concatenation.operands().swap(op); return concatenation; } } else // non-byte array { const exprt &root=src.op0(); const exprt &offset=src.op1(); const typet &array_type=ns.follow(root.type()); const typet &offset_type=ns.follow(offset.type()); const typet &element_type=ns.follow(array_type.subtype()); mp_integer element_width=pointer_offset_size(ns, element_type); if(element_width==-1) // failed throw "failed to flatten non-byte array with unknown element width"; mp_integer result_width=pointer_offset_size(ns, src.type()); mp_integer num_elements=(element_width+result_width-2)/element_width+1; // compute new root and offset concatenation_exprt concat( unsignedbv_typet(integer2long(element_width*8*num_elements))); exprt first_index= (element_width==1)?offset : div_exprt(offset, from_integer(element_width, offset_type)); // 8*offset/el_w for(mp_integer i=num_elements; i>0; --i) { plus_exprt index(first_index, from_integer(i-1, offset_type)); concat.copy_to_operands(index_exprt(root, index)); } // the new offset is width%offset exprt new_offset= (element_width==1)?from_integer(0, offset_type): mod_exprt(offset, from_integer(element_width, offset_type)); // build new byte-extract expression exprt tmp(src.id(), src.type()); tmp.copy_to_operands(concat, new_offset); return tmp; } } else // non-array { // We turn that into logical right shift and extractbits const exprt &offset=src.op1(); const typet &offset_type=ns.follow(offset.type()); mult_exprt times_eight(offset, from_integer(8, offset_type)); lshr_exprt left_shift(src.op0(), times_eight); extractbits_exprt extractbits; extractbits.src()=left_shift; extractbits.type()=src.type(); extractbits.upper()=from_integer(width*8-1, offset_type); extractbits.lower()=from_integer(0, offset_type); return extractbits; } }
static void do_lsh(enum size_tag size, gp_boolean is_const, int value, char *name) { int i; char *reg1 = NULL; char *reg2 = NULL; char *label1 = NULL; char *label2 = NULL; switch (size) { case size_bit: assert(0); break; case size_uint8: case size_int8: reg1 = codegen_get_temp(size); if (is_const) { if (value > 3) { codegen_write_asm("andlw 0x0f"); codegen_write_asm("movwf %s", reg1); codegen_write_asm("swapf %s, f", reg1); value -= 4; } else { codegen_write_asm("movwf %s", reg1); } for (i = 0; i < value; i++) { codegen_write_asm("bcf STATUS, C"); codegen_write_asm("rlf %, f", reg1, i); } codegen_write_asm("movf %s, w", reg1); /* move the result into w */ } else { reg2 = codegen_get_temp(size); label1 = codegen_next_label(); label2 = codegen_next_label(); codegen_write_asm("movwf %s", reg1); codegen_write_asm("movf %s, w", name); codegen_write_asm("movwf %s", reg2); codegen_write_label(label1); codegen_write_asm("btfsc STATUS, Z"); codegen_write_asm("goto %s", label2); codegen_write_asm("bcf STATUS, C"); codegen_write_asm("rlf %s, f", reg1); codegen_write_asm("decf %s, f", reg2); codegen_write_asm("goto %s", label1); codegen_write_label(label2); codegen_write_asm("movf %s, w", reg1); /* move the result into w */ } break; case size_uint16: case size_int16: case size_uint24: case size_int24: case size_uint32: case size_int32: reg1 = codegen_get_temp(size_uint8); label1 = codegen_next_label(); label2 = codegen_next_label(); if (is_const) { codegen_write_asm("movlw %#x", value); codegen_write_asm("addlw 0", value); } else { /* never can shift more than 31 so read the bottom byte only */ codegen_write_asm("movf %s, w", name); } codegen_write_asm("movwf %s", reg1); codegen_write_label(label1); codegen_write_asm("btfsc STATUS, Z"); codegen_write_asm("goto %s", label2); codegen_write_asm("bcf STATUS, C"); left_shift(size); codegen_write_asm("decf %s, f", reg1); codegen_write_asm("goto %s", label1); codegen_write_label(label2); break; case size_float: default: assert(0); } if (reg1) free(reg1); if (reg2) free(reg2); if (label1) free(label1); if (label2) free(label2); }
// Multiply n by 10^p Integer Integer::operator<<(long long p) const { return left_shift(*this, p); }