summaryrefslogtreecommitdiff
path: root/attic/historic/anode/libspark/wrapper.h
blob: eb8c593da8f2babcde80ca58aaebdddcd5639356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* libanode: the Anode C reference implementation
 * Copyright (C) 2009 Adam Ierymenko <adam.ierymenko@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef _SPARK_WRAPPER_H
#define _SPARK_WRAPPER_H

#include <openssl/sha.h>
#include "../libanode/aes128.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Spark uses SHA-256 with hash length 32 */
#define SPARK_HASH_LENGTH 32

// Wrap a segment for forward propagation
static inline void Spark_wrap(void *data,unsigned long len,void *plaintext_hash_buf,void *global_hash_buf)
{
  unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE];

  SHA256((const unsigned char *)data,len,(unsigned char *)plaintext_hash_buf);

  Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash_buf);
  Anode_aes128_cfb_encrypt(expkey,((const unsigned char *)plaintext_hash_buf) + 16,(unsigned char *)data,len);

  SHA256((const unsigned char *)data,len,(unsigned char *)global_hash_buf);
}

// Unwrap a segment and check its integrity
static inline int Spark_unwrap(void *data,unsigned long len,const void *plaintext_hash)
{
  unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE];
  unsigned char check_hash[32];
  unsigned long i;

  Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash);
  Anode_aes128_cfb_decrypt(expkey,((const unsigned char *)plaintext_hash) + 16,(unsigned char *)data,len);

  SHA256((const unsigned char *)data,len,check_hash);

  for(i=0;i<32;++i) {
    if (check_hash[i] != ((const unsigned char *)plaintext_hash)[i])
      return 0;
  }
  return 1;
}

#ifdef __cplusplus
}
#endif

#endif