text
[RRG-proxmark3.git] / common / crapto1 / crypto1.c
blob78d42cec4452525fbf619b41bfb414b9130506d9
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2008-2014 bla <blapost@gmail.com>
3 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // See LICENSE.txt for the text of the license.
16 //-----------------------------------------------------------------------------
17 #include <stdlib.h>
18 #include "crapto1.h"
19 #include "parity.h"
21 #ifdef __OPTIMIZE_SIZE__
22 int filter(uint32_t const x) {
23 uint32_t f;
25 f = 0xf22c0 >> (x & 0xf) & 16;
26 f |= 0x6c9c0 >> (x >> 4 & 0xf) & 8;
27 f |= 0x3c8b0 >> (x >> 8 & 0xf) & 4;
28 f |= 0x1e458 >> (x >> 12 & 0xf) & 2;
29 f |= 0x0d938 >> (x >> 16 & 0xf) & 1;
30 return BIT(0xEC57E80A, f);
32 #endif
34 #define SWAPENDIAN(x)\
35 (x = (x >> 8 & 0xff00ff) | (x & 0xff00ff) << 8, x = x >> 16 | x << 16)
37 void crypto1_init(struct Crypto1State *state, uint64_t key) {
38 if (state == NULL)
39 return;
40 state->odd = 0;
41 state->even = 0;
42 for (int i = 47; i > 0; i -= 2) {
43 state->odd = state->odd << 1 | BIT(key, (i - 1) ^ 7);
44 state->even = state->even << 1 | BIT(key, i ^ 7);
48 void crypto1_deinit(struct Crypto1State *state) {
49 state->odd = 0;
50 state->even = 0;
53 #if !defined(__arm__) || defined(__linux__) || defined(_WIN32) || defined(__APPLE__) // bare metal ARM Proxmark lacks calloc()/free()
54 struct Crypto1State *crypto1_create(uint64_t key) {
55 struct Crypto1State *state = calloc(sizeof(*state), sizeof(uint8_t));
56 if (!state) return NULL;
57 crypto1_init(state, key);
58 return state;
61 void crypto1_destroy(struct Crypto1State *state) {
62 free(state);
64 #endif
66 void crypto1_get_lfsr(struct Crypto1State *state, uint64_t *lfsr) {
67 int i;
68 for (*lfsr = 0, i = 23; i >= 0; --i) {
69 *lfsr = *lfsr << 1 | BIT(state->odd, i ^ 3);
70 *lfsr = *lfsr << 1 | BIT(state->even, i ^ 3);
73 uint8_t crypto1_bit(struct Crypto1State *s, uint8_t in, int is_encrypted) {
74 uint32_t feedin, t;
75 uint8_t ret = filter(s->odd);
77 feedin = ret & (!!is_encrypted);
78 feedin ^= !!in;
79 feedin ^= LF_POLY_ODD & s->odd;
80 feedin ^= LF_POLY_EVEN & s->even;
81 s->even = s->even << 1 | (evenparity32(feedin));
83 t = s->odd;
84 s->odd = s->even;
85 s->even = t;
87 return ret;
89 uint8_t crypto1_byte(struct Crypto1State *s, uint8_t in, int is_encrypted) {
90 uint8_t ret = 0;
91 ret |= crypto1_bit(s, BIT(in, 0), is_encrypted) << 0;
92 ret |= crypto1_bit(s, BIT(in, 1), is_encrypted) << 1;
93 ret |= crypto1_bit(s, BIT(in, 2), is_encrypted) << 2;
94 ret |= crypto1_bit(s, BIT(in, 3), is_encrypted) << 3;
95 ret |= crypto1_bit(s, BIT(in, 4), is_encrypted) << 4;
96 ret |= crypto1_bit(s, BIT(in, 5), is_encrypted) << 5;
97 ret |= crypto1_bit(s, BIT(in, 6), is_encrypted) << 6;
98 ret |= crypto1_bit(s, BIT(in, 7), is_encrypted) << 7;
99 return ret;
101 uint32_t crypto1_word(struct Crypto1State *s, uint32_t in, int is_encrypted) {
102 uint32_t ret = 0;
103 // note: xor args have been swapped because some compilers emit a warning
104 // for 10^x and 2^x as possible misuses for exponentiation. No comment.
105 ret |= crypto1_bit(s, BEBIT(in, 0), is_encrypted) << (24 ^ 0);
106 ret |= crypto1_bit(s, BEBIT(in, 1), is_encrypted) << (24 ^ 1);
107 ret |= crypto1_bit(s, BEBIT(in, 2), is_encrypted) << (24 ^ 2);
108 ret |= crypto1_bit(s, BEBIT(in, 3), is_encrypted) << (24 ^ 3);
109 ret |= crypto1_bit(s, BEBIT(in, 4), is_encrypted) << (24 ^ 4);
110 ret |= crypto1_bit(s, BEBIT(in, 5), is_encrypted) << (24 ^ 5);
111 ret |= crypto1_bit(s, BEBIT(in, 6), is_encrypted) << (24 ^ 6);
112 ret |= crypto1_bit(s, BEBIT(in, 7), is_encrypted) << (24 ^ 7);
114 ret |= crypto1_bit(s, BEBIT(in, 8), is_encrypted) << (24 ^ 8);
115 ret |= crypto1_bit(s, BEBIT(in, 9), is_encrypted) << (24 ^ 9);
116 ret |= crypto1_bit(s, BEBIT(in, 10), is_encrypted) << (24 ^ 10);
117 ret |= crypto1_bit(s, BEBIT(in, 11), is_encrypted) << (24 ^ 11);
118 ret |= crypto1_bit(s, BEBIT(in, 12), is_encrypted) << (24 ^ 12);
119 ret |= crypto1_bit(s, BEBIT(in, 13), is_encrypted) << (24 ^ 13);
120 ret |= crypto1_bit(s, BEBIT(in, 14), is_encrypted) << (24 ^ 14);
121 ret |= crypto1_bit(s, BEBIT(in, 15), is_encrypted) << (24 ^ 15);
123 ret |= crypto1_bit(s, BEBIT(in, 16), is_encrypted) << (24 ^ 16);
124 ret |= crypto1_bit(s, BEBIT(in, 17), is_encrypted) << (24 ^ 17);
125 ret |= crypto1_bit(s, BEBIT(in, 18), is_encrypted) << (24 ^ 18);
126 ret |= crypto1_bit(s, BEBIT(in, 19), is_encrypted) << (24 ^ 19);
127 ret |= crypto1_bit(s, BEBIT(in, 20), is_encrypted) << (24 ^ 20);
128 ret |= crypto1_bit(s, BEBIT(in, 21), is_encrypted) << (24 ^ 21);
129 ret |= crypto1_bit(s, BEBIT(in, 22), is_encrypted) << (24 ^ 22);
130 ret |= crypto1_bit(s, BEBIT(in, 23), is_encrypted) << (24 ^ 23);
132 ret |= crypto1_bit(s, BEBIT(in, 24), is_encrypted) << (24 ^ 24);
133 ret |= crypto1_bit(s, BEBIT(in, 25), is_encrypted) << (24 ^ 25);
134 ret |= crypto1_bit(s, BEBIT(in, 26), is_encrypted) << (24 ^ 26);
135 ret |= crypto1_bit(s, BEBIT(in, 27), is_encrypted) << (24 ^ 27);
136 ret |= crypto1_bit(s, BEBIT(in, 28), is_encrypted) << (24 ^ 28);
137 ret |= crypto1_bit(s, BEBIT(in, 29), is_encrypted) << (24 ^ 29);
138 ret |= crypto1_bit(s, BEBIT(in, 30), is_encrypted) << (24 ^ 30);
139 ret |= crypto1_bit(s, BEBIT(in, 31), is_encrypted) << (24 ^ 31);
140 return ret;
143 /* prng_successor
144 * helper used to obscure the keystream during authentication
146 uint32_t prng_successor(uint32_t x, uint32_t n) {
147 SWAPENDIAN(x);
148 while (n--)
149 x = x >> 1 | (x >> 16 ^ x >> 18 ^ x >> 19 ^ x >> 21) << 31;
151 return SWAPENDIAN(x);