/
SHE_decrypt.js
167 lines (154 loc) · 4.79 KB
/
SHE_decrypt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env node
/** @fileOverview Javascript cryptography implementation
* for MiyaguchiPreneel Compression function.
*
*
*/
(function(root) {
"use strict";
/*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
/*global document, window, escape, unescape, module, require, Uint32Array */
var aesjs = require('aes-js');
var MP = require('miyaguchipreneel');
/*
* dk = KDF(k)
*
* Key Derivation Function used in the SHE protocol specification
*
*/
SHE_decrypt.prototype.KDF = (k) =>
{
return(
SHE_decrypt.prototype.mp.comp(
SHE_decrypt.prototype.bufferIV,
Buffer.concat(
[
(k instanceof Buffer ?
Buffer.from(k) :
Buffer.from(k, 'hex')
),
SHE_decrypt.prototype.KeyUpdateEncCte
]
)
)
);
}
/*
* bufM2 = decrypt_M2(msg, key)
*
* This method will decipher the SHE command M2 argument register
* provided for a Key Provisionning.
* This register will also allows, when deciphered, to get CID, FID
* and Key.
* (see SHE protocol specification on AUTOSAR web site for details)
*
* Arguments:
* msg: The message ciphered transfered in a CAN/Eth frame
* key: The kMasterEcu key used for ciphering the frame
*
* Returns:
* The deciphered M2 register value for SHE (Secure Hardware Extension)
*/
SHE_decrypt.prototype.decrypt_M2 = (msg, key) =>
{
var aesCbc =
new aesjs.ModeOfOperation.cbc(
aesjs.utils.hex.toBytes(SHE_decrypt.prototype.KDF(key).toString('hex')),
aesjs.utils.hex.toBytes(SHE_decrypt.prototype.bufferIV.toString('hex'))
);
var m2Str = aesCbc.decrypt(
aesjs.utils.hex.toBytes(msg.toString('hex'))
);
return(Buffer.from(m2Str));
}
/*
* SHE_decrypt constructor
*
*/
function SHE_decrypt()
{
const KeyUpdateEncCte = Buffer.from('010153484500800000000000000000b0', 'hex');
const bufferIV = Buffer.from('00000000000000000000000000000000', 'hex');
const mp = new MP();
this.KeyUpdateEncCte = KeyUpdateEncCte;
this.bufferIV = bufferIV;
this.mp = mp;
SHE_decrypt.prototype.KeyUpdateEncCte = KeyUpdateEncCte;
SHE_decrypt.prototype.bufferIV = bufferIV;
SHE_decrypt.prototype.mp = mp;
SHE_decrypt.prototype.KDF = this.KDF;
SHE_decrypt.prototype.decrypt_M2 = this.decrypt_M2;
/*
* cid = getCID(m2)
*
* Extract CID from the SHE command M2 argument register
*
* Arguments:
* m2: The M2 argument register of the SHE command
*
* Returns:
* CID extracted from the M2 SHE command register
*/
SHE_decrypt.prototype.getCID = (m2) =>
{
return(m2.subarray(16,48).subarray(0, 4).toString('hex').substring(0, 7));
}
/*
* fid = getFID(m2)
*
* Extract FID from the SHE command M2 argument register
*
* Arguments:
* m2: The M2 argument register of the SHE command
*
* Returns:
* FID extracted from the M2 SHE command register
*/
SHE_decrypt.prototype.getFID = (m2) =>
{
var decM2 = m2.subarray(16,48);
return(((decM2[3] & 0x0F) << 1) + ((decM2[4] >> 7) & 0x01));
}
/*
* key = getKEY(m2)
*
* Extract KEY provisionned from the SHE command M2 argument register
*
* Arguments:
* m2: The M2 argument register of the SHE command
*
* Returns:
* KEY extracted from the M2 SHE command register
*/
SHE_decrypt.prototype.getKEY = (m2) =>
{
var decM2 = m2.subarray(16,48);
return(decM2.subarray(16,48));
}
return(this);
}
// NodeJS
if (typeof exports !== 'undefined')
{
exports.SHE_decrypt = SHE_decrypt;
exports.KDF = SHE_decrypt.prototype.KDF;
exports.decrypt_M2 = SHE_decrypt.prototype.decrypt_M2;
module.exports = SHE_decrypt;
}
// RequireJS/AMD
// http://www.requirejs.org/docs/api.html
// https://github.com/amdjs/amdjs-api/wiki/AMD
else if (typeof(define) === 'function' && define.amd)
{
define([], function() { return SHE_decrypt; });
}
// Web Browsers
else
{
root.SHE_decrypt = SHE_decrypt;
}
})(this);
/*
* vim: et:ts=4:sw=4:sts=4
* -*- mode: JavaScript; coding: utf-8-unix; tab-width: 4 -*-
*/