Created
October 23, 2017 14:43
-
-
Save tsenger/e26a9e3372666648e57aa87899c3397c to your computer and use it in GitHub Desktop.
JavaCard implementation of HMAC SHA256
This file contains hidden or 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
| private short computeHmacSha256(byte[] key, short key_offset, short key_length, | |
| byte[] message, short message_offset, short message_length, | |
| byte[] mac, short mac_offset){ | |
| short BLOCKSIZE=64; | |
| short HASHSIZE=32; | |
| // compute inner hash | |
| for (short i=0; i<key_length; i++){ | |
| hmacBuffer[i]= (byte) (key[(short)(key_offset+i)] ^ (0x36)); | |
| } | |
| Util.arrayFillNonAtomic(hmacBuffer, key_length, (short)(BLOCKSIZE-key_length), (byte)0); | |
| short retLen = Util.arrayCopyNonAtomic(message, message_offset, hmacBuffer, BLOCKSIZE, message_length); | |
| sha256.reset(); | |
| sha256.doFinal(hmacBuffer, (short)0, (short)(BLOCKSIZE+message_length), hmacBuffer, BLOCKSIZE); // copy hash result to data buffer! | |
| // compute outer hash | |
| for (short i=0; i<key_length; i++){ | |
| hmacBuffer[i]= (byte) (key[(short)(key_offset+i)] ^ (0x5c)); | |
| } | |
| Util.arrayFillNonAtomic(hmacBuffer, key_length, (short)(BLOCKSIZE-key_length), (byte)0); | |
| // previous hash already copied to correct offset in scratch | |
| sha256.reset(); | |
| sha256.doFinal(hmacBuffer, (short)0, (short)(BLOCKSIZE+HASHSIZE), mac, mac_offset); | |
| return HASHSIZE; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment