blake2smac_memory_multi.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. */
  9. #include "tomcrypt.h"
  10. #include <stdarg.h>
  11. #ifdef LTC_BLAKE2SMAC
  12. /**
  13. BLAKE2S MAC multiple blocks of memory to produce the authentication tag
  14. @param key The secret key
  15. @param keylen The length of the secret key (octets)
  16. @param mac [out] Destination of the authentication tag
  17. @param maclen [in/out] Max size and resulting size of authentication tag
  18. @param in The data to BLAKE2S MAC
  19. @param inlen The length of the data to BLAKE2S MAC (octets)
  20. @param ... tuples of (data,len) pairs to BLAKE2S MAC, terminated with a (NULL,x) (x=don't care)
  21. @return CRYPT_OK if successful
  22. */
  23. int blake2smac_memory_multi(const unsigned char *key, unsigned long keylen, unsigned char *mac, unsigned long *maclen, const unsigned char *in, unsigned long inlen, ...)
  24. {
  25. blake2smac_state st;
  26. int err;
  27. va_list args;
  28. const unsigned char *curptr;
  29. unsigned long curlen;
  30. LTC_ARGCHK(key != NULL);
  31. LTC_ARGCHK(in != NULL);
  32. LTC_ARGCHK(mac != NULL);
  33. LTC_ARGCHK(maclen != NULL);
  34. va_start(args, inlen);
  35. curptr = in;
  36. curlen = inlen;
  37. if ((err = blake2smac_init(&st, *maclen, key, keylen)) != CRYPT_OK) { goto LBL_ERR; }
  38. for (;;) {
  39. if ((err = blake2smac_process(&st, curptr, curlen)) != CRYPT_OK) { goto LBL_ERR; }
  40. curptr = va_arg(args, const unsigned char*);
  41. if (curptr == NULL) break;
  42. curlen = va_arg(args, unsigned long);
  43. }
  44. err = blake2smac_done(&st, mac, maclen);
  45. LBL_ERR:
  46. #ifdef LTC_CLEAN_STACK
  47. zeromem(&st, sizeof(blake2smac_state));
  48. #endif
  49. va_end(args);
  50. return err;
  51. }
  52. #endif
  53. /* ref: $Format:%D$ */
  54. /* git commit: $Format:%H$ */
  55. /* commit time: $Format:%ai$ */