Found in "gnokii-working" directory, some November-patches version
[gnokii.git] / common / fbus-6110-auth.c
1 /*
2
3   $Id$
4   
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   This file provides Nokia authentication protocol.
14
15   This code is written specially for gnokii project by Odinokov Serge.
16   If you have some special requests for Serge just write him to
17   apskaita@post.omnitel.net or serge@takas.lt
18
19   Reimplemented in C by Pavel Janík ml.
20
21   $Log$
22   Revision 1.1.1.1  2001/11/25 21:58:58  short
23   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Sun Nov 25 22:56 CET 2001
24
25   Revision 1.4  2001/06/28 00:28:45  pkot
26   Small docs updates (Pawel Kot)
27
28
29 */
30
31 #include "fbus-6110-auth.h"
32
33 /* Nokia authentication protocol is used in the communication between Nokia
34    mobile phones (e.g. Nokia 6110) and Nokia Cellular Data Suite software,
35    commercially sold by Nokia Corp.
36
37    The authentication scheme is based on the token send by the phone to the
38    software. The software does it's magic (see the function
39    FB61_GetNokiaAuth()) and returns the result back to the phone. If the
40    result is correct the phone responds with the message "Accessory
41    connected!" displayed on the LCD. Otherwise it will display "Accessory not
42    supported" and some functions will not be available for use.
43
44    The specification of the protocol is not publicly available, no comment. */
45
46 void FB61_GetNokiaAuth(unsigned char *Imei, unsigned char *MagicBytes, unsigned char *MagicResponse)
47 {
48
49   int i, j, CRC=0;
50
51   /* This is our temporary working area. */
52
53   unsigned char Temp[16];
54
55   /* Here we put FAC (Final Assembly Code) and serial number into our area. */
56
57   Temp[0]  = Imei[6];
58   Temp[1]  = Imei[7];
59   Temp[2]  = Imei[8];
60   Temp[3]  = Imei[9];
61   Temp[4]  = Imei[10];
62   Temp[5]  = Imei[11];
63   Temp[6]  = Imei[12];
64   Temp[7]  = Imei[13];
65
66   /* And now the TAC (Type Approval Code). */
67
68   Temp[8]  = Imei[2];
69   Temp[9]  = Imei[3];
70   Temp[10] = Imei[4];
71   Temp[11] = Imei[5];
72
73   /* And now we pack magic bytes from the phone. */
74
75   Temp[12] = MagicBytes[0];
76   Temp[13] = MagicBytes[1];
77   Temp[14] = MagicBytes[2];
78   Temp[15] = MagicBytes[3];
79
80   for (i=0; i<=11; i++)
81     if (Temp[i + 1]& 1)
82       Temp[i]<<=1;
83
84   switch (Temp[15] & 0x03) {
85
86   case 1:
87   case 2:
88     j = Temp[13] & 0x07;
89
90     for (i=0; i<=3; i++)
91       Temp[i+j] ^= Temp[i+12];
92
93     break;
94
95   default:
96     j = Temp[14] & 0x07;
97
98     for (i=0; i<=3; i++)
99       Temp[i + j] |= Temp[i + 12];
100   }
101
102   for (i=0; i<=15; i++)
103     CRC ^= Temp[i];
104
105   for (i=0; i<=15; i++) {
106
107     switch (Temp[15 - i] & 0x06) {
108
109     case 0:
110       j = Temp[i] | CRC;
111       break;
112
113     case 2:
114     case 4:
115       j = Temp[i] ^ CRC;
116       break;
117
118     case 6:
119       j = Temp[i] & CRC;
120       break;
121     }
122   
123     if (j == CRC)
124       j = 0x2c;
125
126     if (Temp[i] == 0)
127       j = 0;
128
129     MagicResponse[i] = j;
130
131   }
132 }