]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/nonce2key/crypto1_bs.c
CHG: unified some text messaged.
[proxmark3-svn] / client / nonce2key / crypto1_bs.c
1 // Bit-sliced Crypto-1 implementation
2 // The cipher states are stored with the least significant bit first, hence all bit indexes are reversed here
3 /*
4 Copyright (c) 2015-2016 Aram Verstegen
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 #include "crypto1_bs.h"
26 #include <inttypes.h>
27 #define __STDC_FORMAT_MACROS
28 #define llx PRIx64
29 #define lli PRIi64
30 #define lu PRIu32
31
32 // The following functions use this global or thread-local state
33 // It is sized to fit exactly KEYSTREAM_SIZE more states next to the initial state
34 __thread bitslice_t states[KEYSTREAM_SIZE+STATE_SIZE];
35 __thread bitslice_t * restrict state_p;
36
37 void crypto1_bs_init(){
38 // initialize constant one and zero bit vectors
39 memset(bs_ones.bytes, 0xff, VECTOR_SIZE);
40 memset(bs_zeroes.bytes, 0x00, VECTOR_SIZE);
41 }
42
43 // The following functions have side effects on 48 bitslices at the state_p pointer
44 // use the crypto1_bs_rewind_* macros to (re-)initialize them as needed
45
46 inline const bitslice_value_t crypto1_bs_bit(const bitslice_value_t input, const bool is_encrypted){
47 bitslice_value_t feedback = (state_p[47- 0].value ^ state_p[47- 5].value ^ state_p[47- 9].value ^
48 state_p[47-10].value ^ state_p[47-12].value ^ state_p[47-14].value ^
49 state_p[47-15].value ^ state_p[47-17].value ^ state_p[47-19].value ^
50 state_p[47-24].value ^ state_p[47-25].value ^ state_p[47-27].value ^
51 state_p[47-29].value ^ state_p[47-35].value ^ state_p[47-39].value ^
52 state_p[47-41].value ^ state_p[47-42].value ^ state_p[47-43].value);
53 const bitslice_value_t ks_bits = crypto1_bs_f20(state_p);
54 if(is_encrypted){
55 feedback ^= ks_bits;
56 }
57 state_p--;
58 state_p[0].value = feedback ^ input;
59 return ks_bits;
60 }
61
62 inline const bitslice_value_t crypto1_bs_lfsr_rollback(const bitslice_value_t input, const bool is_encrypted){
63 bitslice_value_t feedout = state_p[0].value;
64 state_p++;
65 const bitslice_value_t ks_bits = crypto1_bs_f20(state_p);
66 if(is_encrypted){
67 feedout ^= ks_bits;
68 }
69 const bitslice_value_t feedback = (feedout ^ state_p[47- 5].value ^ state_p[47- 9].value ^
70 state_p[47-10].value ^ state_p[47-12].value ^ state_p[47-14].value ^
71 state_p[47-15].value ^ state_p[47-17].value ^ state_p[47-19].value ^
72 state_p[47-24].value ^ state_p[47-25].value ^ state_p[47-27].value ^
73 state_p[47-29].value ^ state_p[47-35].value ^ state_p[47-39].value ^
74 state_p[47-41].value ^ state_p[47-42].value ^ state_p[47-43].value);
75 state_p[47].value = feedback ^ input;
76 return ks_bits;
77 }
78
79 // side-effect free from here on
80 // note that bytes are sliced and unsliced with reversed endianness
81 inline void crypto1_bs_convert_states(bitslice_t bitsliced_states[], state_t regular_states[]){
82 size_t bit_idx = 0, slice_idx = 0;
83 state_t values[MAX_BITSLICES];
84 for(slice_idx = 0; slice_idx < MAX_BITSLICES; slice_idx++){
85 for(bit_idx = 0; bit_idx < STATE_SIZE; bit_idx++){
86 bool bit = get_vector_bit(slice_idx, bitsliced_states[bit_idx]);
87 values[slice_idx].value <<= 1;
88 values[slice_idx].value |= bit;
89 }
90 // swap endianness
91 values[slice_idx].value = rev_state_t(values[slice_idx].value);
92 // roll off unused bits
93 values[slice_idx].value >>= ((sizeof(state_t)*8)-STATE_SIZE);
94 }
95 memcpy(regular_states, values, sizeof(values));
96 }
97
98 // bitslice a value
99 void crypto1_bs_bitslice_value32(uint32_t value, bitslice_t bitsliced_value[], size_t bit_len){
100 // load nonce bytes with unswapped endianness
101 size_t bit_idx;
102 for(bit_idx = 0; bit_idx < bit_len; bit_idx++){
103 bool bit = get_bit(bit_len-1-bit_idx, rev32(value));
104 if(bit){
105 bitsliced_value[bit_idx].value = bs_ones.value;
106 } else {
107 bitsliced_value[bit_idx].value = bs_zeroes.value;
108 }
109 }
110 }
111
112 void crypto1_bs_print_states(bitslice_t bitsliced_states[]){
113 size_t slice_idx = 0;
114 state_t values[MAX_BITSLICES];
115 crypto1_bs_convert_states(bitsliced_states, values);
116 for(slice_idx = 0; slice_idx < MAX_BITSLICES; slice_idx++){
117 printf("State %03zu: %012"llx"\n", slice_idx, values[slice_idx].value);
118 }
119 }
120
Impressum, Datenschutz