]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - client/nonce2key/crypto1_bs.c
CHG: some clearing of memory
[proxmark3-svn] / client / nonce2key / crypto1_bs.c
... / ...
CommitLineData
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
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
37void 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
46inline 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
62inline 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
81inline 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 memset(values, 0x0, sizeof(values));
85
86 for(slice_idx = 0; slice_idx < MAX_BITSLICES; slice_idx++){
87 for(bit_idx = 0; bit_idx < STATE_SIZE; bit_idx++){
88 bool bit = get_vector_bit(slice_idx, bitsliced_states[bit_idx]);
89 values[slice_idx].value <<= 1;
90 values[slice_idx].value |= bit;
91 }
92 // swap endianness
93 values[slice_idx].value = rev_state_t(values[slice_idx].value);
94 // roll off unused bits
95 //values[slice_idx].value >>= ((sizeof(state_t)*8)-STATE_SIZE); // - 48
96 values[slice_idx].value >>= 16;
97 }
98 memcpy(regular_states, values, sizeof(values));
99}
100
101// bitslice a value
102void crypto1_bs_bitslice_value32(uint32_t value, bitslice_t bitsliced_value[], size_t bit_len){
103 // load nonce bytes with unswapped endianness
104 size_t bit_idx;
105 for(bit_idx = 0; bit_idx < bit_len; bit_idx++){
106 bool bit = get_bit(bit_len-1-bit_idx, rev32(value));
107 if(bit){
108 bitsliced_value[bit_idx].value = bs_ones.value;
109 } else {
110 bitsliced_value[bit_idx].value = bs_zeroes.value;
111 }
112 }
113}
114
115void crypto1_bs_print_states(bitslice_t bitsliced_states[]){
116 size_t slice_idx = 0;
117 state_t values[MAX_BITSLICES] = {{0x00}};
118 crypto1_bs_convert_states(bitsliced_states, values);
119 for(slice_idx = 0; slice_idx < MAX_BITSLICES; slice_idx++){
120 printf("State %03zu: %012"llx"\n", slice_idx, values[slice_idx].value);
121 }
122}
123
Impressum, Datenschutz