Commit 4d039b9b4b7969b3a4e0a3547bac10941dc9c243

Authored by Ji Luo
Committed by Ye Li
1 parent 04e5532e16

MA-15015 Add sha256_hmac support

Add sha256 hmac support in u-boot.

Test: hmac calculation.

Change-Id: I0f1438fed8290620a1bb0663d19c21e20098eb5a
Signed-off-by: Ji Luo <ji.luo@nxp.com>
(cherry picked from 1e06de6ef23c1ae9d51383f3c57bb045ea180c03)

Showing 2 changed files with 43 additions and 0 deletions Side-by-side Diff

include/u-boot/sha256.h
... ... @@ -22,5 +22,8 @@
22 22 void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
23 23 unsigned char *output, unsigned int chunk_sz);
24 24  
  25 +void sha256_hmac(const unsigned char *key, int keylen,
  26 + const unsigned char *input, unsigned int ilen,
  27 + unsigned char *output);
25 28 #endif /* _SHA256_H */
... ... @@ -289,4 +289,44 @@
289 289  
290 290 sha256_finish(&ctx, output);
291 291 }
  292 +
  293 +/*
  294 + * Output = HMAC-SHA-256( input buffer, hmac key )
  295 + */
  296 +void sha256_hmac(const unsigned char *key, int keylen,
  297 + const unsigned char *input, unsigned int ilen,
  298 + unsigned char *output)
  299 +{
  300 + int i;
  301 + sha256_context ctx;
  302 + unsigned char k_ipad[64];
  303 + unsigned char k_opad[64];
  304 + unsigned char tmpbuf[32];
  305 +
  306 + memset (k_ipad, 0x36, 64);
  307 + memset (k_opad, 0x5C, 64);
  308 +
  309 + for (i = 0; i < keylen; i++) {
  310 + if (i >= 64)
  311 + break;
  312 +
  313 + k_ipad[i] ^= key[i];
  314 + k_opad[i] ^= key[i];
  315 + }
  316 +
  317 + sha256_starts (&ctx);
  318 + sha256_update (&ctx, k_ipad, 64);
  319 + sha256_update (&ctx, input, ilen);
  320 + sha256_finish (&ctx, tmpbuf);
  321 +
  322 + sha256_starts (&ctx);
  323 + sha256_update (&ctx, k_opad, 64);
  324 + sha256_update (&ctx, tmpbuf, 32);
  325 + sha256_finish (&ctx, output);
  326 +
  327 + memset (k_ipad, 0, 64);
  328 + memset (k_opad, 0, 64);
  329 + memset (tmpbuf, 0, 32);
  330 + memset (&ctx, 0, sizeof (sha256_context));
  331 +}