wrapper.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009 Adam Ierymenko <[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 as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #ifndef _SPARK_WRAPPER_H
  17. #define _SPARK_WRAPPER_H
  18. #include <openssl/sha.h>
  19. #include "../libanode/aes128.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Spark uses SHA-256 with hash length 32 */
  24. #define SPARK_HASH_LENGTH 32
  25. // Wrap a segment for forward propagation
  26. static inline void Spark_wrap(void *data,unsigned long len,void *plaintext_hash_buf,void *global_hash_buf)
  27. {
  28. unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE];
  29. SHA256((const unsigned char *)data,len,(unsigned char *)plaintext_hash_buf);
  30. Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash_buf);
  31. Anode_aes128_cfb_encrypt(expkey,((const unsigned char *)plaintext_hash_buf) + 16,(unsigned char *)data,len);
  32. SHA256((const unsigned char *)data,len,(unsigned char *)global_hash_buf);
  33. }
  34. // Unwrap a segment and check its integrity
  35. static inline int Spark_unwrap(void *data,unsigned long len,const void *plaintext_hash)
  36. {
  37. unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE];
  38. unsigned char check_hash[32];
  39. unsigned long i;
  40. Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash);
  41. Anode_aes128_cfb_decrypt(expkey,((const unsigned char *)plaintext_hash) + 16,(unsigned char *)data,len);
  42. SHA256((const unsigned char *)data,len,check_hash);
  43. for(i=0;i<32;++i) {
  44. if (check_hash[i] != ((const unsigned char *)plaintext_hash)[i])
  45. return 0;
  46. }
  47. return 1;
  48. }
  49. #ifdef __cplusplus
  50. }
  51. #endif
  52. #endif