This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Lippai
committed
Feb 7, 2018
1 parent
1bfbeb8
commit 3adb482
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { AES_ECB } from '../ecb/ecb'; | ||
import { AES_CBC_Encrypt } from '../cbc/cbc'; | ||
import { bytes_to_hex, hex_to_bytes } from '../../utils'; | ||
|
||
/** | ||
* @param {Uint8Array} data | ||
*/ | ||
function mul2(data) { | ||
const t = data[0] & 0x80; | ||
for (var i = 0; i < 15; i++) { | ||
data[i] = (data[i] << 1) ^ ((data[i+1] & 0x80) ? 1 : 0); | ||
} | ||
data[15] = (data[15] << 1) ^ (t ? 0x87 : 0); | ||
} | ||
|
||
export class AES_CMAC { | ||
/** | ||
* @param {Uint8Array} key | ||
*/ | ||
constructor(key) { | ||
this.k = new AES_ECB(key).encrypt(new Uint8Array(16)).result; | ||
mul2(this.k); | ||
this.cbc = new AES_CBC_Encrypt(key, new Uint8Array(16), false); | ||
|
||
this.buffer = new Uint8Array(16); | ||
this.bufferLength = 0; | ||
this.result = null; | ||
} | ||
|
||
/** | ||
* @param {Uint8Array} data | ||
*/ | ||
process(data) { | ||
if (this.bufferLength + data.length > 16) { | ||
this.cbc.process(this.buffer.subarray(0, this.bufferLength)); | ||
const offset = ((this.bufferLength + data.length - 1) & ~15) - this.bufferLength; | ||
this.cbc.process(data.subarray(0, offset)); | ||
this.buffer.set(data.subarray(offset)); | ||
this.bufferLength = data.length - offset; | ||
} else { | ||
this.buffer.set(data, this.bufferLength); | ||
this.bufferLength += data.length; | ||
} | ||
return this; | ||
} | ||
|
||
finish() { | ||
if (this.bufferLength !== 16) { | ||
this.buffer[this.bufferLength] = 0x80; | ||
for (let i = this.bufferLength + 1; i < 16; i++) { | ||
this.buffer[i] = 0; | ||
} | ||
mul2(this.k); | ||
} | ||
|
||
for (let i = 0; i < 16; i++) { | ||
this.buffer[i] ^= this.k[i]; | ||
} | ||
|
||
this.result = this.cbc.process(this.buffer).result; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { AES_CMAC } from './cmac'; | ||
|
||
/** | ||
* @param {Uint8Array} data | ||
* @param {Uint8Array} key | ||
* @returns {Uint8Array} | ||
*/ | ||
function AES_CMAC_bytes(data, key) { | ||
if (data === undefined) throw new SyntaxError('data required'); | ||
if (key === undefined) throw new SyntaxError('key required'); | ||
return new AES_CMAC(key).process(data).finish().result; | ||
} | ||
|
||
AES_CMAC.bytes = AES_CMAC_bytes; | ||
|
||
export { AES_CMAC }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters