Merge branch 'master' of github.com:RfidResearchGroup/proxmark3
[RRG-proxmark3.git] / armsrc / optimized_cipherutils.c
blobc51f83f9bcd812c5573c89ef8e04a663167483c1
1 /*****************************************************************************
2 * WARNING
4 * THIS CODE IS CREATED FOR EXPERIMENTATION AND EDUCATIONAL USE ONLY.
6 * USAGE OF THIS CODE IN OTHER WAYS MAY INFRINGE UPON THE INTELLECTUAL
7 * PROPERTY OF OTHER PARTIES, SUCH AS INSIDE SECURE AND HID GLOBAL,
8 * AND MAY EXPOSE YOU TO AN INFRINGEMENT ACTION FROM THOSE PARTIES.
10 * THIS CODE SHOULD NEVER BE USED TO INFRINGE PATENTS OR INTELLECTUAL PROPERTY RIGHTS.
12 *****************************************************************************
14 * This file is part of loclass. It is a reconstructon of the cipher engine
15 * used in iClass, and RFID techology.
17 * The implementation is based on the work performed by
18 * Flavio D. Garcia, Gerhard de Koning Gans, Roel Verdult and
19 * Milosch Meriac in the paper "Dismantling IClass".
21 * Copyright (C) 2014 Martin Holst Swende
23 * This is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License version 2 as published
25 * by the Free Software Foundation, or, at your option, any later version.
27 * This file is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with loclass. If not, see <http://www.gnu.org/licenses/>.
36 ****************************************************************************/
37 #include "optimized_cipherutils.h"
38 #include <stdint.h>
40 /**
42 * @brief Return and remove the first bit (x0) in the stream : <x0 x1 x2 x3 ... xn >
43 * @param stream
44 * @return
46 bool headBit(BitstreamIn *stream) {
47 int bytepos = stream->position >> 3; // divide by 8
48 int bitpos = (stream->position++) & 7; // mask out 00000111
49 return (*(stream->buffer + bytepos) >> (7 - bitpos)) & 1;
51 /**
52 * @brief Return and remove the last bit (xn) in the stream: <x0 x1 x2 ... xn>
53 * @param stream
54 * @return
56 bool tailBit(BitstreamIn *stream) {
57 int bitpos = stream->numbits - 1 - (stream->position++);
59 int bytepos = bitpos >> 3;
60 bitpos &= 7;
61 return (*(stream->buffer + bytepos) >> (7 - bitpos)) & 1;
63 /**
64 * @brief Pushes bit onto the stream
65 * @param stream
66 * @param bit
68 void pushBit(BitstreamOut *stream, bool bit) {
69 int bytepos = stream->position >> 3; // divide by 8
70 int bitpos = stream->position & 7;
71 *(stream->buffer + bytepos) |= (bit) << (7 - bitpos);
72 stream->position++;
73 stream->numbits++;
76 /**
77 * @brief Pushes the lower six bits onto the stream
78 * as b0 b1 b2 b3 b4 b5 b6
79 * @param stream
80 * @param bits
82 void push6bits(BitstreamOut *stream, uint8_t bits) {
83 pushBit(stream, bits & 0x20);
84 pushBit(stream, bits & 0x10);
85 pushBit(stream, bits & 0x08);
86 pushBit(stream, bits & 0x04);
87 pushBit(stream, bits & 0x02);
88 pushBit(stream, bits & 0x01);
91 /**
92 * @brief bitsLeft
93 * @param stream
94 * @return number of bits left in stream
96 int bitsLeft(BitstreamIn *stream) {
97 return stream->numbits - stream->position;
99 /**
100 * @brief numBits
101 * @param stream
102 * @return Number of bits stored in stream
104 void x_num_to_bytes(uint64_t n, size_t len, uint8_t *dest) {
105 while (len--) {
106 dest[len] = (uint8_t) n;
107 n >>= 8;
111 uint64_t x_bytes_to_num(uint8_t *src, size_t len) {
112 uint64_t num = 0;
113 while (len--) {
114 num = (num << 8) | (*src);
115 src++;
117 return num;
120 uint8_t reversebytes(uint8_t b) {
121 b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
122 b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
123 b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
124 return b;
127 void reverse_arraybytes(uint8_t *arr, size_t len) {
128 uint8_t i;
129 for (i = 0; i < len ; i++) {
130 arr[i] = reversebytes(arr[i]);
134 void reverse_arraycopy(uint8_t *arr, uint8_t *dest, size_t len) {
135 uint8_t i;
136 for (i = 0; i < len ; i++) {
137 dest[i] = reversebytes(arr[i]);