aes.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Libhashkit library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include "libhashkit/common.h"
  37. #include "libhashkit/rijndael.hpp"
  38. #include <cstring>
  39. #define AES_KEY_LENGTH 256 /* 128, 192, 256 */
  40. #define AES_BLOCK_SIZE 16
  41. enum encrypt_t
  42. {
  43. AES_ENCRYPT,
  44. AES_DECRYPT
  45. };
  46. struct _key_t {
  47. int nr;
  48. uint32_t rk[4*(AES_MAXNR +1)];
  49. };
  50. struct aes_key_t {
  51. _key_t encode_key;
  52. _key_t decode_key;
  53. };
  54. aes_key_t* aes_create_key(const char *key, const size_t key_length)
  55. {
  56. aes_key_t* _aes_key= (aes_key_t*)calloc(1, sizeof(aes_key_t));
  57. if (_aes_key)
  58. {
  59. uint8_t rkey[AES_KEY_LENGTH/8];
  60. uint8_t *rkey_end= rkey +AES_KEY_LENGTH/8;
  61. const char *key_end= key +key_length;
  62. memset(rkey, 0, sizeof(rkey)); /* Set initial key */
  63. uint8_t *ptr= rkey;
  64. const char* sptr= key;
  65. for (; sptr < key_end; ptr++,sptr++)
  66. {
  67. if (ptr == rkey_end)
  68. {
  69. ptr= rkey; /* Just loop over tmp_key until we used all key */
  70. }
  71. *ptr^= (uint8_t) *sptr;
  72. }
  73. _aes_key->decode_key.nr= rijndaelKeySetupDec(_aes_key->decode_key.rk, rkey, AES_KEY_LENGTH);
  74. _aes_key->encode_key.nr= rijndaelKeySetupEnc(_aes_key->encode_key.rk, rkey, AES_KEY_LENGTH);
  75. }
  76. return _aes_key;
  77. }
  78. aes_key_t* aes_clone_key(aes_key_t *_aes_key)
  79. {
  80. if (_aes_key == NULL)
  81. {
  82. return NULL;
  83. }
  84. aes_key_t* _aes_clone_key= (aes_key_t*)calloc(1, sizeof(aes_key_t));
  85. if (_aes_clone_key)
  86. {
  87. memcpy(_aes_clone_key, _aes_key, sizeof(aes_key_t));
  88. }
  89. return _aes_clone_key;
  90. }
  91. hashkit_string_st* aes_encrypt(aes_key_t *_aes_key,
  92. const char* source, size_t source_length)
  93. {
  94. if (_aes_key == NULL)
  95. {
  96. return NULL;
  97. }
  98. size_t num_blocks= source_length/AES_BLOCK_SIZE;
  99. hashkit_string_st* destination= hashkit_string_create(source_length);
  100. if (destination)
  101. {
  102. char *dest= hashkit_string_c_str_mutable(destination);
  103. for (size_t x= num_blocks; x > 0; x--) /* Encode complete blocks */
  104. {
  105. rijndaelEncrypt(_aes_key->encode_key.rk, _aes_key->encode_key.nr, (const uint8_t*) source,
  106. (uint8_t*) dest);
  107. source+= AES_BLOCK_SIZE;
  108. dest+= AES_BLOCK_SIZE;
  109. }
  110. uint8_t block[AES_BLOCK_SIZE];
  111. char pad_len= AES_BLOCK_SIZE - (source_length - AES_BLOCK_SIZE*num_blocks);
  112. memcpy(block, source, 16 -pad_len);
  113. memset(block + AES_BLOCK_SIZE -pad_len, pad_len, pad_len);
  114. rijndaelEncrypt(_aes_key->encode_key.rk, _aes_key->encode_key.nr, block, (uint8_t*) dest);
  115. hashkit_string_set_length(destination, AES_BLOCK_SIZE*(num_blocks + 1));
  116. }
  117. return destination;
  118. }
  119. hashkit_string_st* aes_decrypt(aes_key_t *_aes_key,
  120. const char* source, size_t source_length)
  121. {
  122. if (_aes_key == NULL)
  123. {
  124. return NULL;
  125. }
  126. size_t num_blocks= source_length/AES_BLOCK_SIZE;
  127. if ((source_length != num_blocks*AES_BLOCK_SIZE) or num_blocks ==0 )
  128. {
  129. return NULL;
  130. }
  131. hashkit_string_st* destination= hashkit_string_create(source_length);
  132. if (destination)
  133. {
  134. char *dest= hashkit_string_c_str_mutable(destination);
  135. for (size_t x = num_blocks-1; x > 0; x--)
  136. {
  137. rijndaelDecrypt(_aes_key->decode_key.rk, _aes_key->decode_key.nr, (const uint8_t*) source, (uint8_t*) dest);
  138. source+= AES_BLOCK_SIZE;
  139. dest+= AES_BLOCK_SIZE;
  140. }
  141. uint8_t block[AES_BLOCK_SIZE];
  142. rijndaelDecrypt(_aes_key->decode_key.rk, _aes_key->decode_key.nr, (const uint8_t*) source, block);
  143. /* Use last char in the block as size */
  144. unsigned int pad_len= (unsigned int) (unsigned char) block[AES_BLOCK_SIZE-1];
  145. if (pad_len > AES_BLOCK_SIZE)
  146. {
  147. hashkit_string_free(destination);
  148. return NULL;
  149. }
  150. /* We could also check whole padding but we do not really need this */
  151. memcpy(dest, block, AES_BLOCK_SIZE - pad_len);
  152. hashkit_string_set_length(destination, AES_BLOCK_SIZE*num_blocks - pad_len);
  153. }
  154. return destination;
  155. }