All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Functions | Variables
comp128.c File Reference

Implementations of comp128v1, comp128v2, comp128v3 algorithms. More...

#include "comp128.h"
#include <stdio.h>
+ Include dependency graph for comp128.c:

Go to the source code of this file.

Functions

static void _comp128_bitsfrombytes (uint8_t *x, uint8_t *bits)
 
static void _comp128_compression (uint8_t *x)
 
static void _comp128_compression_round (uint8_t *x, int n, const uint8_t *tbl)
 
static void _comp128_permutation (uint8_t *x, uint8_t *bits)
 
static void _comp128v23 (uint8_t *rand, uint8_t const *kxor)
 
void comp128v1 (uint8_t *sres, uint8_t *kc, uint8_t const *ki, uint8_t const *rand)
 Calculate comp128v1 sres and kc from ki and rand. More...
 
void comp128v23 (uint8_t *sres, uint8_t *kc, uint8_t const *ki, uint8_t const *rand, bool v2)
 Calculate comp128v2 or comp128v3 sres and kc from ki and rand. More...
 

Variables

static uint8_t const * _comp128_table [] = { comp128v1_t0, comp128v1_t1, comp128v1_t2, comp128v1_t3, comp128v1_t4 }
 
static uint8_t const comp128v1_t0 []
 
static uint8_t const comp128v1_t1 []
 
static uint8_t const comp128v1_t2 []
 
static uint8_t const comp128v1_t3 []
 
static uint8_t const comp128v1_t4 []
 
static uint8_t const comp128v23_t0 []
 
static uint8_t const comp128v23_t1 []
 

Detailed Description

Implementations of comp128v1, comp128v2, comp128v3 algorithms.

Id:
6205c50c7b5e401439bd84ffe45e62294c6f52da

Comp128v1 was inspired by code from: Marc Briceno marc@.nosp@m.scar.nosp@m.d.org, Ian Goldberg iang@.nosp@m.cs.b.nosp@m.erkel.nosp@m.ey.e.nosp@m.du, and David Wagner daw@c.nosp@m.s.be.nosp@m.rkele.nosp@m.y.ed.nosp@m.u

But it has been fully rewritten (Sylvain Munaut tnt@2.nosp@m.46tN.nosp@m.t.com) from various PDFs found online describing the algorithm because the licence of the code referenced above was unclear. A comment snippet from the original code is included below, it describes where the doc came from and how the algorithm was reverse engineered.

Comp128v2 & v3 is a port of the python code from: http://www.hackingprojects.net/ The author of the original code is Tamas Jos tamas.nosp@m..jos.nosp@m.@skel.nosp@m.sec..nosp@m.com

Note
The above GPL license only applies to comp128v1, the license for comp128v2 and comp128v3 is unknown.

Definition in file comp128.c.

Function Documentation

static void _comp128_bitsfrombytes ( uint8_t *  x,
uint8_t *  bits 
)
inlinestatic

Definition at line 185 of file comp128.c.

+ Here is the caller graph for this function:

static void _comp128_compression ( uint8_t *  x)
inlinestatic

Definition at line 177 of file comp128.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void _comp128_compression_round ( uint8_t *  x,
int  n,
const uint8_t *  tbl 
)
inlinestatic

Definition at line 161 of file comp128.c.

+ Here is the caller graph for this function:

static void _comp128_permutation ( uint8_t *  x,
uint8_t *  bits 
)
inlinestatic

Definition at line 197 of file comp128.c.

+ Here is the caller graph for this function:

static void _comp128v23 ( uint8_t *  rand,
uint8_t const *  kxor 
)
static

Definition at line 288 of file comp128.c.

+ Here is the caller graph for this function:

void comp128v1 ( uint8_t *  sres,
uint8_t *  kc,
uint8_t const *  ki,
uint8_t const *  rand 
)

Calculate comp128v1 sres and kc from ki and rand.

This code derived from a leaked document from the GSM standards. Some missing pieces were filled in by reverse-engineering a working SIM. We have verified that this is the correct COMP128 algorithm.

The first page of the document identifies it as Technical Information: GSM System Security Study. 10-1617-01, 10th June 1988. The bottom of the title page is marked Racal Research Ltd. Worton Drive, Worton Grange Industrial Estate, Reading, Berks. RG2 0SB, England. Telephone: Reading (0734) 868601 Telex: 847152 The relevant bits are in Part I, Section 20 (pages 66–67). Enjoy!

Note: There are three typos in the spec (discovered by reverse-engineering). First, "z = (2 * x[n] + x[n]) mod 2^(9-j)" should clearly read "z = (2 * x[m] + x[n]) mod 2^(9-j)". Second, the "k" loop in the "Form bits from bytes" section is severely botched: the k index should run only from 0 to 3, and clearly the range on "the (8-k)th bit of byte j" is also off (should be 0..7, not 1..8, to be consistent with the subsequent section). Third, SRES is taken from the first 8 nibbles of x[], not the last 8 as claimed in the document. (And the document doesn't specify how Kc is derived, but that was also easily discovered with reverse engineering.) All of these typos have been corrected in the following code.

Parameters
[out]sres4 byte value derived from ki and rand.
[out]kc12 byte value derived from ki and rand.
[in]kiknown only by the SIM and AuC (us in this case).
[in]rand16 bytes of randomness.

Definition at line 239 of file comp128.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void comp128v23 ( uint8_t *  sres,
uint8_t *  kc,
uint8_t const *  ki,
uint8_t const *  rand,
bool  v2 
)

Calculate comp128v2 or comp128v3 sres and kc from ki and rand.

Parameters
[out]sres4 byte value derived from ki and rand.
[out]kc8 byte value derived from ki and rand.
[in]kiknown only by the SIM and AuC (us in this case).
[in]rand16 bytes of randomness.
[in]v2if true we use version comp128-2 else we use comp128-3.

Definition at line 335 of file comp128.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

uint8_t const* _comp128_table[] = { comp128v1_t0, comp128v1_t1, comp128v1_t2, comp128v1_t3, comp128v1_t4 }
static

Definition at line 121 of file comp128.c.

uint8_t const comp128v1_t0[]
static

Definition at line 45 of file comp128.c.

uint8_t const comp128v1_t1[]
static
Initial value:
= {
19, 11, 80, 114, 43, 1, 69, 94, 39, 18, 127, 117, 97, 3, 85, 43,
27, 124, 70, 83, 47, 71, 63, 10, 47, 89, 79, 4, 14, 59, 11, 5,
35, 107, 103, 68, 21, 86, 36, 91, 85, 126, 32, 50, 109, 94, 120, 6,
53, 79, 28, 45, 99, 95, 41, 34, 88, 68, 93, 55, 110, 125, 105, 20,
90, 80, 76, 96, 23, 60, 89, 64, 121, 56, 14, 74, 101, 8, 19, 78,
76, 66, 104, 46, 111, 50, 32, 3, 39, 0, 58, 25, 92, 22, 18, 51,
57, 65, 119, 116, 22, 109, 7, 86, 59, 93, 62, 110, 78, 99, 77, 67,
12, 113, 87, 98, 102, 5, 88, 33, 38, 56, 23, 8, 75, 45, 13, 75,
95, 63, 28, 49, 123, 120, 20, 112, 44, 30, 15, 98, 106, 2, 103, 29,
82, 107, 42, 124, 24, 30, 41, 16, 108, 100, 117, 40, 73, 40, 7, 114,
82, 115, 36, 112, 12, 102, 100, 84, 92, 48, 72, 97, 9, 54, 55, 74,
113, 123, 17, 26, 53, 58, 4, 9, 69, 122, 21, 118, 42, 60, 27, 73,
118, 125, 34, 15, 65, 115, 84, 64, 62, 81, 70, 1, 24, 111, 121, 83,
104, 81, 49, 127, 48, 105, 31, 10, 6, 91, 87, 37, 16, 54, 116, 126,
31, 38, 13, 0, 72, 106, 77, 61, 26, 67, 46, 29, 96, 37, 61, 52,
101, 17, 44, 108, 71, 52, 66, 57, 33, 51, 25, 90, 2, 119, 122, 35}

Definition at line 80 of file comp128.c.

uint8_t const comp128v1_t2[]
static
Initial value:
= {
52, 50, 44, 6, 21, 49, 41, 59, 39, 51, 25, 32, 51, 47, 52, 43,
37, 4, 40, 34, 61, 12, 28, 4, 58, 23, 8, 15, 12, 22, 9, 18,
55, 10, 33, 35, 50, 1, 43, 3, 57, 13, 62, 14, 7, 42, 44, 59,
62, 57, 27, 6, 8, 31, 26, 54, 41, 22, 45, 20, 39, 3, 16, 56,
48, 2, 21, 28, 36, 42, 60, 33, 34, 18, 0, 11, 24, 10, 17, 61,
29, 14, 45, 26, 55, 46, 11, 17, 54, 46, 9, 24, 30, 60, 32, 0,
20, 38, 2, 30, 58, 35, 1, 16, 56, 40, 23, 48, 13, 19, 19, 27,
31, 53, 47, 38, 63, 15, 49, 5, 37, 53, 25, 36, 63, 29, 5, 7}

Definition at line 99 of file comp128.c.

uint8_t const comp128v1_t3[]
static
Initial value:
= {
1, 5, 29, 6, 25, 1, 18, 23, 17, 19, 0, 9, 24, 25, 6, 31,
28, 20, 24, 30, 4, 27, 3, 13, 15, 16, 14, 18, 4, 3, 8, 9,
20, 0, 12, 26, 21, 8, 28, 2, 29, 2, 15, 7, 11, 22, 14, 10,
17, 21, 12, 30, 26, 27, 16, 31, 11, 7, 13, 23, 10, 5, 22, 19}

Definition at line 110 of file comp128.c.

uint8_t const comp128v1_t4[]
static
Initial value:
= {
15, 12, 10, 4, 1, 14, 11, 7, 5, 0, 14, 7, 1, 2, 13, 8,
10, 3, 4, 9, 6, 0, 3, 2, 5, 6, 8, 9, 11, 13, 15, 12}

Definition at line 117 of file comp128.c.

uint8_t const comp128v23_t0[]
static
Initial value:
= {
197, 235, 60, 151, 98, 96, 3, 100, 248, 118, 42, 117, 172, 211, 181, 203,
61, 126, 156, 87, 149, 224, 55, 132, 186, 63, 238, 255, 85, 83, 152, 33,
160, 184, 210, 219, 159, 11, 180, 194, 130, 212, 147, 5, 215, 92, 27, 46,
113, 187, 52, 25, 185, 79, 221, 48, 70, 31, 101, 15, 195, 201, 50, 222,
137, 233, 229, 106, 122, 183, 178, 177, 144, 207, 234, 182, 37, 254, 227, 231,
54, 209, 133, 65, 202, 69, 237, 220, 189, 146, 120, 68, 21, 125, 38, 30,
2, 155, 53, 196, 174, 176, 51, 246, 167, 76, 110, 20, 82, 121, 103, 112,
56, 173, 49, 217, 252, 0, 114, 228, 123, 12, 93, 161, 253, 232, 240, 175,
67, 128, 22, 158, 89, 18, 77, 109, 190, 17, 62, 4, 153, 163, 59, 145,
138, 7, 74, 205, 10, 162, 80, 45, 104, 111, 150, 214, 154, 28, 191, 169,
213, 88, 193, 198, 200, 245, 39, 164, 124, 84, 78, 1, 188, 170, 23, 86,
226, 141, 32, 6, 131, 127, 199, 40, 135, 16, 57, 71, 91, 225, 168, 242,
206, 97, 166, 44, 14, 90, 236, 239, 230, 244, 223, 108, 102, 119, 148, 251,
29, 216, 8, 9, 249, 208, 24, 105, 94, 34, 64, 95, 115, 72, 134, 204,
43, 247, 243, 218, 47, 58, 73, 107, 241, 179, 116, 66, 36, 143, 81, 250,
139, 19, 13, 142, 140, 129, 192, 99, 171, 157, 136, 41, 75, 35, 165, 26}

Definition at line 124 of file comp128.c.

uint8_t const comp128v23_t1[]
static
Initial value:
= {
170, 42, 95, 141, 109, 30, 71, 89, 26, 147, 231, 205, 239, 212, 124, 129,
216, 79, 15, 185, 153, 14, 251, 162, 0, 241, 172, 197, 43, 10, 194, 235,
6, 20, 72, 45, 143, 104, 161, 119, 41, 136, 38, 189, 135, 25, 93, 18,
224, 171, 252, 195, 63, 19, 58, 165, 23, 55, 133, 254, 214, 144, 220, 178,
156, 52, 110, 225, 97, 183, 140, 39, 53, 88, 219, 167, 16, 198, 62, 222,
76, 139, 175, 94, 51, 134, 115, 22, 67, 1, 249, 217, 3, 5, 232, 138,
31, 56, 116, 163, 70, 128, 234, 132, 229, 184, 244, 13, 34, 73, 233, 154,
179, 131, 215, 236, 142, 223, 27, 57, 246, 108, 211, 8, 253, 85, 66, 245,
193, 78, 190, 4, 17, 7, 150, 127, 152, 213, 37, 186, 2, 243, 46, 169,
68, 101, 60, 174, 208, 158, 176, 69, 238, 191, 90, 83, 166, 125, 77, 59,
21, 92, 49, 151, 168, 99, 9, 50, 146, 113, 117, 228, 65, 230, 40, 82,
54, 237, 227, 102, 28, 36, 107, 24, 44, 126, 206, 201, 61, 114, 164, 207,
181, 29, 91, 64, 221, 255, 48, 155, 192, 111, 180, 210, 182, 247, 203, 148,
209, 98, 173, 11, 75, 123, 250, 118, 32, 47, 240, 202, 74, 177, 100, 80,
196, 33, 248, 86, 157, 137, 120, 130, 84, 204, 122, 81, 242, 188, 200, 149,
226, 218, 160, 187, 106, 35, 87, 105, 96, 145, 199, 159, 12, 121, 103, 112}

Definition at line 143 of file comp128.c.