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