aes-eax.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * AES-128 EAX
  3. *
  4. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "aes.h"
  12. #include "aes_wrap.h"
  13. /**
  14. * aes_128_eax_encrypt - AES-128 EAX mode encryption
  15. * @key: Key for encryption (16 bytes)
  16. * @nonce: Nonce for counter mode
  17. * @nonce_len: Nonce length in bytes
  18. * @hdr: Header data to be authenticity protected
  19. * @hdr_len: Length of the header data bytes
  20. * @data: Data to encrypt in-place
  21. * @data_len: Length of data in bytes
  22. * @tag: 16-byte tag value
  23. * Returns: 0 on success, -1 on failure
  24. */
  25. int aes_128_eax_encrypt(const u8 *key, const u8 *nonce, size_t nonce_len,
  26. const u8 *hdr, size_t hdr_len,
  27. u8 *data, size_t data_len, u8 *tag)
  28. {
  29. u8 *buf;
  30. size_t buf_len;
  31. u8 nonce_mac[AES_BLOCK_SIZE], hdr_mac[AES_BLOCK_SIZE],
  32. data_mac[AES_BLOCK_SIZE];
  33. int i, ret = -1;
  34. if (nonce_len > data_len)
  35. buf_len = nonce_len;
  36. else
  37. buf_len = data_len;
  38. if (hdr_len > buf_len)
  39. buf_len = hdr_len;
  40. buf_len += 16;
  41. buf = os_malloc(buf_len);
  42. if (buf == NULL)
  43. return -1;
  44. os_memset(buf, 0, 15);
  45. buf[15] = 0;
  46. os_memcpy(buf + 16, nonce, nonce_len);
  47. if (omac1_aes_128(key, buf, 16 + nonce_len, nonce_mac))
  48. goto fail;
  49. buf[15] = 1;
  50. os_memcpy(buf + 16, hdr, hdr_len);
  51. if (omac1_aes_128(key, buf, 16 + hdr_len, hdr_mac))
  52. goto fail;
  53. if (aes_128_ctr_encrypt(key, nonce_mac, data, data_len))
  54. goto fail;
  55. buf[15] = 2;
  56. os_memcpy(buf + 16, data, data_len);
  57. if (omac1_aes_128(key, buf, 16 + data_len, data_mac))
  58. goto fail;
  59. for (i = 0; i < AES_BLOCK_SIZE; i++)
  60. tag[i] = nonce_mac[i] ^ data_mac[i] ^ hdr_mac[i];
  61. ret = 0;
  62. fail:
  63. bin_clear_free(buf, buf_len);
  64. return ret;
  65. }
  66. /**
  67. * aes_128_eax_decrypt - AES-128 EAX mode decryption
  68. * @key: Key for decryption (16 bytes)
  69. * @nonce: Nonce for counter mode
  70. * @nonce_len: Nonce length in bytes
  71. * @hdr: Header data to be authenticity protected
  72. * @hdr_len: Length of the header data bytes
  73. * @data: Data to encrypt in-place
  74. * @data_len: Length of data in bytes
  75. * @tag: 16-byte tag value
  76. * Returns: 0 on success, -1 on failure, -2 if tag does not match
  77. */
  78. int aes_128_eax_decrypt(const u8 *key, const u8 *nonce, size_t nonce_len,
  79. const u8 *hdr, size_t hdr_len,
  80. u8 *data, size_t data_len, const u8 *tag)
  81. {
  82. u8 *buf;
  83. size_t buf_len;
  84. u8 nonce_mac[AES_BLOCK_SIZE], hdr_mac[AES_BLOCK_SIZE],
  85. data_mac[AES_BLOCK_SIZE];
  86. int i;
  87. if (nonce_len > data_len)
  88. buf_len = nonce_len;
  89. else
  90. buf_len = data_len;
  91. if (hdr_len > buf_len)
  92. buf_len = hdr_len;
  93. buf_len += 16;
  94. buf = os_malloc(buf_len);
  95. if (buf == NULL)
  96. return -1;
  97. os_memset(buf, 0, 15);
  98. buf[15] = 0;
  99. os_memcpy(buf + 16, nonce, nonce_len);
  100. if (omac1_aes_128(key, buf, 16 + nonce_len, nonce_mac)) {
  101. os_free(buf);
  102. return -1;
  103. }
  104. buf[15] = 1;
  105. os_memcpy(buf + 16, hdr, hdr_len);
  106. if (omac1_aes_128(key, buf, 16 + hdr_len, hdr_mac)) {
  107. os_free(buf);
  108. return -1;
  109. }
  110. buf[15] = 2;
  111. os_memcpy(buf + 16, data, data_len);
  112. if (omac1_aes_128(key, buf, 16 + data_len, data_mac)) {
  113. os_free(buf);
  114. return -1;
  115. }
  116. os_free(buf);
  117. for (i = 0; i < AES_BLOCK_SIZE; i++) {
  118. if (tag[i] != (nonce_mac[i] ^ data_mac[i] ^ hdr_mac[i]))
  119. return -2;
  120. }
  121. return aes_128_ctr_encrypt(key, nonce_mac, data, data_len);
  122. }