28e715c0f1a4e0076f93a5e532eac52fe7b31dde
[platform/upstream/iotjs.git] / src / js / ble_hci_socket_crypto.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /* Copyright (C) 2015 Sandeep Mistry sandeep.mistry@gmail.com
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining a copy
19  * of this software and associated documentation files (the "Software"), to deal
20  * in the Software without restriction, including without limitation the rights
21  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22  * copies of the Software, and to permit persons to whom the Software is
23  * furnished to do so, subject to the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be included in
26  * all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36
37 var crypto = require('ble_hci_socket_crypto');
38
39 function r() {
40   return crypto.randomBytes(16);
41 }
42
43 function c1(k, r, pres, preq, iat, ia, rat, ra) {
44   var p1 = Buffer.concat([
45     iat,
46     rat,
47     preq,
48     pres
49   ]);
50
51   var p2 = Buffer.concat([
52     ra,
53     ia,
54     new Buffer('00000000', 'hex')
55   ]);
56
57   var res = xor(r, p1);
58   res = e(k, res);
59   res = xor(res, p2);
60   res = e(k, res);
61
62   return res;
63 }
64
65 function s1(k, r1, r2) {
66   return e(k, Buffer.concat([
67     r2.slice(0, 8),
68     r1.slice(0, 8)
69   ]));
70 }
71
72 function e(key, data) {
73   key = swap(key);
74   data = swap(data);
75
76   var cipher = crypto.createCipheriv('aes-128-ecb', key, '');
77   cipher.setAutoPadding(false);
78
79   return swap(Buffer.concat([
80     cipher.update(data),
81     cipher.final()
82   ]));
83 }
84
85 function xor(b1, b2) {
86   var result = new Buffer(b1.length);
87
88   for (var i = 0; i < b1.length; i++) {
89     //result[i] = b1[i] ^ b2[i];
90     result.writeUInt8(b1.readUInt8(i) ^ b2.readUInt8(i), i);
91   }
92
93   return result;
94 }
95
96 function swap(input) {
97   var output = new Buffer(input.length);
98
99   for (var i = 0; i < output.length; i++) {
100     //output[i] = input[input.length - i - 1];
101     output.writeUInt8(input.readUInt8(input.length - i - 1), i);
102   }
103
104   return output;
105 }
106
107 module.exports = {
108   r: r,
109   c1: c1,
110   s1: s1,
111   e: e
112 };