ead-crypt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2008 Felix Fietkau <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <stdbool.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include "ead.h"
  21. #include "sha1.c"
  22. #include "aes.c"
  23. #if EAD_DEBUGLEVEL >= 1
  24. #define DEBUG(n, format, ...) do { \
  25. if (EAD_DEBUGLEVEL >= n) \
  26. fprintf(stderr, format, ##__VA_ARGS__); \
  27. } while (0);
  28. #else
  29. #define DEBUG(n, format, ...) do {} while(0)
  30. #endif
  31. static uint32_t aes_enc_ctx[AES_PRIV_SIZE];
  32. static uint32_t aes_dec_ctx[AES_PRIV_SIZE];
  33. static uint32_t ead_rx_iv;
  34. static uint32_t ead_tx_iv;
  35. static uint32_t ivofs_vec;
  36. static unsigned int ivofs_idx = 0;
  37. static uint32_t W[80]; /* work space for sha1 */
  38. #define EAD_ENC_PAD 64
  39. void
  40. ead_set_key(unsigned char *skey)
  41. {
  42. uint32_t *ivp = (uint32_t *)skey;
  43. memset(aes_enc_ctx, 0, sizeof(aes_enc_ctx));
  44. memset(aes_dec_ctx, 0, sizeof(aes_dec_ctx));
  45. /* first 32 bytes of skey are used as aes key for
  46. * encryption and decryption */
  47. rijndaelKeySetupEnc(aes_enc_ctx, skey);
  48. rijndaelKeySetupDec(aes_dec_ctx, skey);
  49. /* the following bytes are used as initialization vector for messages
  50. * (highest byte cleared to avoid overflow) */
  51. ivp += 8;
  52. ead_rx_iv = ntohl(*ivp) & 0x00ffffff;
  53. ead_tx_iv = ead_rx_iv;
  54. /* the last bytes are used to feed the random iv increment */
  55. ivp++;
  56. ivofs_vec = *ivp;
  57. }
  58. static bool
  59. ead_check_rx_iv(uint32_t iv)
  60. {
  61. if (iv <= ead_rx_iv)
  62. return false;
  63. if (iv > ead_rx_iv + EAD_MAX_IV_INCR)
  64. return false;
  65. ead_rx_iv = iv;
  66. return true;
  67. }
  68. static uint32_t
  69. ead_get_tx_iv(void)
  70. {
  71. unsigned int ofs;
  72. ofs = 1 + ((ivofs_vec >> 2 * ivofs_idx) & 0x3);
  73. ivofs_idx = (ivofs_idx + 1) % 16;
  74. ead_tx_iv += ofs;
  75. return ead_tx_iv;
  76. }
  77. static void
  78. ead_hash_message(struct ead_msg_encrypted *enc, uint32_t *hash, int len)
  79. {
  80. unsigned char *data = (unsigned char *) enc;
  81. /* hash the packet with the stored hash part initialized to zero */
  82. sha_init(hash);
  83. memset(enc->hash, 0, sizeof(enc->hash));
  84. while (len > 0) {
  85. sha_transform(hash, data, W);
  86. len -= 64;
  87. data += 64;
  88. }
  89. }
  90. void
  91. ead_encrypt_message(struct ead_msg *msg, unsigned int len)
  92. {
  93. struct ead_msg_encrypted *enc = EAD_DATA(msg, enc);
  94. unsigned char *data = (unsigned char *) enc;
  95. uint32_t hash[5];
  96. int enclen, i;
  97. len += sizeof(struct ead_msg_encrypted);
  98. enc->pad = (EAD_ENC_PAD - (len % EAD_ENC_PAD)) % EAD_ENC_PAD;
  99. enclen = len + enc->pad;
  100. msg->len = htonl(enclen);
  101. enc->iv = htonl(ead_get_tx_iv());
  102. ead_hash_message(enc, hash, enclen);
  103. for (i = 0; i < 5; i++)
  104. enc->hash[i] = htonl(hash[i]);
  105. DEBUG(2, "SHA1 generate (0x%08x), len=%d\n", enc->hash[0], enclen);
  106. while (enclen > 0) {
  107. rijndaelEncrypt(aes_enc_ctx, data, data);
  108. data += 16;
  109. enclen -= 16;
  110. }
  111. }
  112. int
  113. ead_decrypt_message(struct ead_msg *msg)
  114. {
  115. struct ead_msg_encrypted *enc = EAD_DATA(msg, enc);
  116. unsigned char *data = (unsigned char *) enc;
  117. uint32_t hash_old[5], hash_new[5];
  118. int len = ntohl(msg->len);
  119. int i, enclen = len;
  120. if (!len || (len % EAD_ENC_PAD > 0))
  121. return 0;
  122. while (len > 0) {
  123. rijndaelDecrypt(aes_dec_ctx, data, data);
  124. data += 16;
  125. len -= 16;
  126. }
  127. data = (unsigned char *) enc;
  128. if (enc->pad >= EAD_ENC_PAD) {
  129. DEBUG(2, "Invalid padding length\n");
  130. return 0;
  131. }
  132. if (!ead_check_rx_iv(ntohl(enc->iv))) {
  133. DEBUG(2, "RX IV mismatch (0x%08x <> 0x%08x)\n", ead_rx_iv, ntohl(enc->iv));
  134. return 0;
  135. }
  136. for (i = 0; i < 5; i++)
  137. hash_old[i] = ntohl(enc->hash[i]);
  138. ead_hash_message(enc, hash_new, enclen);
  139. if (memcmp(hash_old, hash_new, sizeof(hash_old)) != 0) {
  140. DEBUG(2, "SHA1 mismatch (0x%08x != 0x%08x), len=%d\n", hash_old[0], hash_new[0], enclen);
  141. return 0;
  142. }
  143. enclen -= enc->pad + sizeof(struct ead_msg_encrypted);
  144. return enclen;
  145. }