2
0

eax_init.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* EAX Implementation by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef EAX_MODE
  14. int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen,
  15. const unsigned char *nonce, unsigned long noncelen,
  16. const unsigned char *header, unsigned long headerlen)
  17. {
  18. unsigned char *buf;
  19. int err, blklen;
  20. omac_state *omac;
  21. unsigned long len;
  22. _ARGCHK(eax != NULL);
  23. _ARGCHK(key != NULL);
  24. _ARGCHK(nonce != NULL);
  25. if (headerlen > 0) {
  26. _ARGCHK(header != NULL);
  27. }
  28. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  29. return err;
  30. }
  31. blklen = cipher_descriptor[cipher].block_length;
  32. /* allocate ram */
  33. buf = XMALLOC(MAXBLOCKSIZE);
  34. omac = XMALLOC(sizeof(omac_state));
  35. if (buf == NULL || omac == NULL) {
  36. if (buf != NULL) {
  37. XFREE(buf);
  38. }
  39. if (omac != NULL) {
  40. XFREE(omac);
  41. }
  42. return CRYPT_MEM;
  43. }
  44. /* N = OMAC_0K(nonce) */
  45. zeromem(buf, MAXBLOCKSIZE);
  46. if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
  47. goto __ERR;
  48. }
  49. /* omac the [0]_n */
  50. if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
  51. goto __ERR;
  52. }
  53. /* omac the nonce */
  54. if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
  55. goto __ERR;
  56. }
  57. /* store result */
  58. len = sizeof(eax->N);
  59. if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
  60. goto __ERR;
  61. }
  62. /* H = OMAC_1K(header) */
  63. zeromem(buf, MAXBLOCKSIZE);
  64. buf[blklen - 1] = 1;
  65. if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
  66. goto __ERR;
  67. }
  68. /* omac the [1]_n */
  69. if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
  70. goto __ERR;
  71. }
  72. /* omac the header */
  73. if (headerlen != 0) {
  74. if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
  75. goto __ERR;
  76. }
  77. }
  78. /* note we don't finish the headeromac, this allows us to add more header later */
  79. /* setup the CTR mode */
  80. if ((err = ctr_start(cipher, eax->N, key, keylen, 0, &eax->ctr)) != CRYPT_OK) {
  81. goto __ERR;
  82. }
  83. /* use big-endian counter */
  84. eax->ctr.mode = 1;
  85. /* setup the OMAC for the ciphertext */
  86. if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
  87. goto __ERR;
  88. }
  89. /* omac [2]_n */
  90. zeromem(buf, MAXBLOCKSIZE);
  91. buf[blklen-1] = 2;
  92. if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
  93. goto __ERR;
  94. }
  95. err = CRYPT_OK;
  96. __ERR:
  97. #ifdef CLEAN_STACK
  98. zeromem(buf, MAXBLOCKSIZE);
  99. zeromem(omac, sizeof(omac_state));
  100. #endif
  101. XFREE(omac);
  102. XFREE(buf);
  103. return err;
  104. }
  105. #endif