eax_init.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /**
  10. @file eax_init.c
  11. EAX implementation, initialized EAX state, by Tom St Denis
  12. */
  13. #include "tomcrypt.h"
  14. #ifdef LTC_EAX_MODE
  15. /**
  16. Initialized an EAX state
  17. @param eax [out] The EAX state to initialize
  18. @param cipher The index of the desired cipher
  19. @param key The secret key
  20. @param keylen The length of the secret key (octets)
  21. @param nonce The use-once nonce for the session
  22. @param noncelen The length of the nonce (octets)
  23. @param header The header for the EAX state
  24. @param headerlen The header length (octets)
  25. @return CRYPT_OK if successful
  26. */
  27. int eax_init(eax_state *eax, int cipher,
  28. const unsigned char *key, unsigned long keylen,
  29. const unsigned char *nonce, unsigned long noncelen,
  30. const unsigned char *header, unsigned long headerlen)
  31. {
  32. unsigned char *buf;
  33. int err, blklen;
  34. omac_state *omac;
  35. unsigned long len;
  36. LTC_ARGCHK(eax != NULL);
  37. LTC_ARGCHK(key != NULL);
  38. LTC_ARGCHK(nonce != NULL);
  39. if (headerlen > 0) {
  40. LTC_ARGCHK(header != NULL);
  41. }
  42. if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
  43. return err;
  44. }
  45. blklen = cipher_descriptor[cipher].block_length;
  46. /* allocate ram */
  47. buf = XMALLOC(MAXBLOCKSIZE);
  48. omac = XMALLOC(sizeof(*omac));
  49. if (buf == NULL || omac == NULL) {
  50. if (buf != NULL) {
  51. XFREE(buf);
  52. }
  53. if (omac != NULL) {
  54. XFREE(omac);
  55. }
  56. return CRYPT_MEM;
  57. }
  58. /* N = LTC_OMAC_0K(nonce) */
  59. zeromem(buf, MAXBLOCKSIZE);
  60. if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
  61. goto LBL_ERR;
  62. }
  63. /* omac the [0]_n */
  64. if ((err = omac_process(omac, buf, blklen)) != CRYPT_OK) {
  65. goto LBL_ERR;
  66. }
  67. /* omac the nonce */
  68. if ((err = omac_process(omac, nonce, noncelen)) != CRYPT_OK) {
  69. goto LBL_ERR;
  70. }
  71. /* store result */
  72. len = sizeof(eax->N);
  73. if ((err = omac_done(omac, eax->N, &len)) != CRYPT_OK) {
  74. goto LBL_ERR;
  75. }
  76. /* H = LTC_OMAC_1K(header) */
  77. zeromem(buf, MAXBLOCKSIZE);
  78. buf[blklen - 1] = 1;
  79. if ((err = omac_init(&eax->headeromac, cipher, key, keylen)) != CRYPT_OK) {
  80. goto LBL_ERR;
  81. }
  82. /* omac the [1]_n */
  83. if ((err = omac_process(&eax->headeromac, buf, blklen)) != CRYPT_OK) {
  84. goto LBL_ERR;
  85. }
  86. /* omac the header */
  87. if (headerlen != 0) {
  88. if ((err = omac_process(&eax->headeromac, header, headerlen)) != CRYPT_OK) {
  89. goto LBL_ERR;
  90. }
  91. }
  92. /* note we don't finish the headeromac, this allows us to add more header later */
  93. /* setup the CTR mode */
  94. if ((err = ctr_start(cipher, eax->N, key, keylen, 0, CTR_COUNTER_BIG_ENDIAN, &eax->ctr)) != CRYPT_OK) {
  95. goto LBL_ERR;
  96. }
  97. /* setup the LTC_OMAC for the ciphertext */
  98. if ((err = omac_init(&eax->ctomac, cipher, key, keylen)) != CRYPT_OK) {
  99. goto LBL_ERR;
  100. }
  101. /* omac [2]_n */
  102. zeromem(buf, MAXBLOCKSIZE);
  103. buf[blklen-1] = 2;
  104. if ((err = omac_process(&eax->ctomac, buf, blklen)) != CRYPT_OK) {
  105. goto LBL_ERR;
  106. }
  107. err = CRYPT_OK;
  108. LBL_ERR:
  109. #ifdef LTC_CLEAN_STACK
  110. zeromem(buf, MAXBLOCKSIZE);
  111. zeromem(omac, sizeof(*omac));
  112. #endif
  113. XFREE(omac);
  114. XFREE(buf);
  115. return err;
  116. }
  117. #endif
  118. /* ref: $Format:%D$ */
  119. /* git commit: $Format:%H$ */
  120. /* commit time: $Format:%ai$ */