aes-siv.c 3.5 KB

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