aes-siv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * AES SIV (RFC 5297)
  3. * Copyright (c) 2013 Cozybit, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "aes.h"
  11. #include "aes_wrap.h"
  12. #include "aes_siv.h"
  13. static const u8 zero[AES_BLOCK_SIZE];
  14. static void dbl(u8 *pad)
  15. {
  16. int i, carry;
  17. carry = pad[0] & 0x80;
  18. for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
  19. pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
  20. pad[AES_BLOCK_SIZE - 1] <<= 1;
  21. if (carry)
  22. pad[AES_BLOCK_SIZE - 1] ^= 0x87;
  23. }
  24. static void xor(u8 *a, const u8 *b)
  25. {
  26. int i;
  27. for (i = 0; i < AES_BLOCK_SIZE; i++)
  28. *a++ ^= *b++;
  29. }
  30. static void xorend(u8 *a, int alen, const u8 *b, int blen)
  31. {
  32. int i;
  33. if (alen < blen)
  34. return;
  35. for (i = 0; i < blen; i++)
  36. a[alen - blen + i] ^= b[i];
  37. }
  38. static void pad_block(u8 *pad, const u8 *addr, size_t len)
  39. {
  40. os_memset(pad, 0, AES_BLOCK_SIZE);
  41. os_memcpy(pad, addr, len);
  42. if (len < AES_BLOCK_SIZE)
  43. pad[len] = 0x80;
  44. }
  45. static int aes_s2v(const u8 *key, size_t num_elem, const u8 *addr[],
  46. size_t *len, u8 *mac)
  47. {
  48. u8 tmp[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
  49. u8 *buf = NULL;
  50. int ret;
  51. size_t i;
  52. if (!num_elem) {
  53. os_memcpy(tmp, zero, sizeof(zero));
  54. tmp[AES_BLOCK_SIZE - 1] = 1;
  55. return omac1_aes_128(key, tmp, sizeof(tmp), mac);
  56. }
  57. ret = omac1_aes_128(key, zero, sizeof(zero), tmp);
  58. if (ret)
  59. return ret;
  60. for (i = 0; i < num_elem - 1; i++) {
  61. ret = omac1_aes_128(key, addr[i], len[i], tmp2);
  62. if (ret)
  63. return ret;
  64. dbl(tmp);
  65. xor(tmp, tmp2);
  66. }
  67. if (len[i] >= AES_BLOCK_SIZE) {
  68. buf = os_malloc(len[i]);
  69. if (!buf)
  70. return -ENOMEM;
  71. os_memcpy(buf, addr[i], len[i]);
  72. xorend(buf, len[i], tmp, AES_BLOCK_SIZE);
  73. ret = omac1_aes_128(key, buf, len[i], mac);
  74. bin_clear_free(buf, len[i]);
  75. return ret;
  76. }
  77. dbl(tmp);
  78. pad_block(tmp2, addr[i], len[i]);
  79. xor(tmp, tmp2);
  80. return omac1_aes_128(key, tmp, sizeof(tmp), mac);
  81. }
  82. int aes_siv_encrypt(const u8 *key, const u8 *pw,
  83. size_t pwlen, size_t num_elem,
  84. const u8 *addr[], const size_t *len, u8 *out)
  85. {
  86. const u8 *_addr[6];
  87. size_t _len[6];
  88. const u8 *k1 = key, *k2 = key + 16;
  89. u8 v[AES_BLOCK_SIZE];
  90. size_t i;
  91. u8 *iv, *crypt_pw;
  92. if (num_elem > ARRAY_SIZE(_addr) - 1)
  93. return -1;
  94. for (i = 0; i < num_elem; i++) {
  95. _addr[i] = addr[i];
  96. _len[i] = len[i];
  97. }
  98. _addr[num_elem] = pw;
  99. _len[num_elem] = pwlen;
  100. if (aes_s2v(k1, num_elem + 1, _addr, _len, v))
  101. return -1;
  102. iv = out;
  103. crypt_pw = out + AES_BLOCK_SIZE;
  104. os_memcpy(iv, v, AES_BLOCK_SIZE);
  105. os_memcpy(crypt_pw, pw, pwlen);
  106. /* zero out 63rd and 31st bits of ctr (from right) */
  107. v[8] &= 0x7f;
  108. v[12] &= 0x7f;
  109. return aes_128_ctr_encrypt(k2, v, crypt_pw, pwlen);
  110. }
  111. int aes_siv_decrypt(const u8 *key, const u8 *iv_crypt, size_t iv_c_len,
  112. size_t num_elem, const u8 *addr[], const size_t *len,
  113. u8 *out)
  114. {
  115. const u8 *_addr[6];
  116. size_t _len[6];
  117. const u8 *k1 = key, *k2 = key + 16;
  118. size_t crypt_len;
  119. size_t i;
  120. int ret;
  121. u8 iv[AES_BLOCK_SIZE];
  122. u8 check[AES_BLOCK_SIZE];
  123. if (iv_c_len < AES_BLOCK_SIZE || num_elem > ARRAY_SIZE(_addr) - 1)
  124. return -1;
  125. crypt_len = iv_c_len - AES_BLOCK_SIZE;
  126. for (i = 0; i < num_elem; i++) {
  127. _addr[i] = addr[i];
  128. _len[i] = len[i];
  129. }
  130. _addr[num_elem] = out;
  131. _len[num_elem] = crypt_len;
  132. os_memcpy(iv, iv_crypt, AES_BLOCK_SIZE);
  133. os_memcpy(out, iv_crypt + AES_BLOCK_SIZE, crypt_len);
  134. iv[8] &= 0x7f;
  135. iv[12] &= 0x7f;
  136. ret = aes_128_ctr_encrypt(k2, iv, out, crypt_len);
  137. if (ret)
  138. return ret;
  139. ret = aes_s2v(k1, num_elem + 1, _addr, _len, check);
  140. if (ret)
  141. return ret;
  142. if (os_memcmp(check, iv_crypt, AES_BLOCK_SIZE) == 0)
  143. return 0;
  144. return -1;
  145. }