forked from XMSS/xmss-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpots.c
149 lines (120 loc) · 4.29 KB
/
pots.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdint.h>
#include <string.h>
#include <openssl/evp.h>
#include <stdio.h>
#include "utils.h"
#include "hash.h"
#include "pots.h"
#include "hash_address.h"
#include "params.h"
static void expand_seed(const xmss_params *params,
unsigned char *outseeds, const unsigned char *inseed,
const unsigned char *pub_seed, uint32_t addr[8])
{
uint32_t i, j;
unsigned char buf[params->n + 32];
set_key_and_mask(addr, 0);
memcpy(buf, pub_seed, params->n);
for (i = 0; i < params->wots_len1; i++) {
set_chain_addr(addr, i);
for (j = 0; j < params->wots_w; j++) {
set_hash_addr(addr, j);
addr_to_bytes(buf + params->n, addr);
prf_keygen(params, outseeds + (i * params->wots_w + j) * params->n, buf, inseed);
}
}
}
void pots_pkgen(const xmss_params *params, unsigned char *sk,
unsigned char *pk, const unsigned char *seed,
const unsigned char *pub_seed, uint32_t addr[8])
{
unsigned char temp[params->n];
int outlen;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
expand_seed(params, sk, seed, pub_seed, addr);
for (uint32_t chain = 0; chain < params->wots_len1; chain++) {
for (uint32_t i = 0; i < params->wots_w; i++) {
uint32_t offset = (chain * params->wots_w + i) * params->n;
unsigned char iv[16] = {0};
unsigned char ctr[16] = {0};
EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, sk + offset, iv);
unsigned char input[params->n];
if (i == 0) {
memset(input, 0, params->n);
} else {
unsigned char *src = pk + offset - params->n;
memcpy(input, src, params->n);
}
EVP_EncryptUpdate(ctx, temp, &outlen, input, params->n);
memcpy(pk + offset, temp, params->n);
}
}
EVP_CIPHER_CTX_free(ctx);
}
static void base_w(const xmss_params *params,
int *output, const int out_len, const unsigned char *input)
{
int in = 0;
int out = 0;
unsigned char total;
int bits = 0;
int consumed;
for (consumed = 0; consumed < out_len; consumed++) {
if (bits == 0) {
total = input[in];
in++;
bits += 8;
}
bits -= params->wots_log_w;
output[out] = (total >> bits) & (params->wots_w - 1);
out++;
}
}
void pots_sign(const xmss_params *params,
unsigned char *sig, const unsigned char *msg,
const unsigned char *sk, const unsigned char *pub_seed,
uint32_t addr[8])
{
int lengths[params->wots_len1];
uint32_t i;
base_w(params, lengths, params->wots_len1, msg);
for (i = 0; i < params->wots_len1; i++) {
memcpy(
sig + ((i) * params->n),
sk + lengths[i]*params->n + i * params->wots_w * params->n,
params->n
);
}
}
int pots_ver(const xmss_params *params,
const unsigned char *sig, const unsigned char *msg,
const unsigned char *pk)
{
int lengths[params->wots_len1];
base_w(params, lengths, params->wots_len1, msg);
unsigned char temp[params->n];
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
int outlen;
for (uint32_t i = 0; i < params->wots_len1; i++) {
unsigned char iv[16] = {0}; // Initialization vector
unsigned char ctr[16] = {0};
EVP_EncryptInit_ex(ctx, EVP_aes_256_ctr(), NULL, sig + (i * params->n), iv);
unsigned char input[params->n];
if (lengths[i] == 0) {
memset(input, 0, params->n);
} else {
uint32_t chain_offset = i * params->wots_w * params->n;
uint32_t pos_offset = (lengths[i]-1) * params->n;
unsigned char *src = pk + chain_offset + pos_offset;
memcpy(input, src, params->n);
}
EVP_EncryptUpdate(ctx, temp, &outlen, input, params->n);
unsigned char *next_pk = pk + i * params->wots_w * params->n + (lengths[i]) * params->n;
if (memcmp(temp, next_pk, params->n) != 0) {
printf("Verification failed\n");
return 0;
}
}
EVP_CIPHER_CTX_free(ctx);
return 1;
}