-
Notifications
You must be signed in to change notification settings - Fork 9
/
mac.ts
322 lines (301 loc) · 10.8 KB
/
mac.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { BytearrayWrapper } from '../bytearray-wrapper';
import { Encoder, MessageEncoder } from '../encoder';
import {
bddt16BlindMacGenerate,
bddt16MacGenerate,
bddt16MacVerify,
bddt16UnblindMac,
bddt16MacProofOfValidity,
bddt16MacVerifyProofOfValidity,
VerifyResult,
bddt16MacGenerateConstantTime,
bddt16MacVerifyConstantTime,
bddt16BlindMacGenerateConstantTime
} from 'crypto-wasm-new';
import { MessageStructure, SignedMessages } from '../types';
import { BBDT16MacParams } from './params';
import { BBDT16MacSecretKey, BBDT16MacPublicKeyG1 } from './keys';
import { encodeRevealedMessageObject, getBlindedIndicesAndRevealedMessages } from '../sign-verify-js-objs';
/**
* Proof of knowledge of BBDT16 MAC protocol
*/
export class BBDT16Mac extends MessageEncoder {
/**
* Signer creates a new MAC
* @param messages - Ordered list of messages. Order and contents should be kept same for both signer and verifier
* @param secretKey
* @param params
* @param encodeMessages - If true, the messages are encoded as field elements otherwise they are assumed to be already encoded.
*/
static generate(
messages: Uint8Array[],
secretKey: BBDT16MacSecretKey,
params: BBDT16MacParams,
encodeMessages: boolean
): BBDT16Mac {
if (messages.length !== params.supportedMessageCount()) {
throw new Error(
`Number of messages ${
messages.length
} is different from ${params.supportedMessageCount()} supported by the MAC params`
);
}
const mac = bddt16MacGenerateConstantTime(messages, secretKey.value, params.value, encodeMessages);
return new BBDT16Mac(mac);
}
/**
* Verify the MAC
* @param messages - Ordered list of messages. Order and contents should be kept same for both signer and verifier
* @param secretKey
* @param params
* @param encodeMessages - If true, the messages are encoded as field elements otherwise they are assumed to be already encoded.
*/
verify(
messages: Uint8Array[],
secretKey: BBDT16MacSecretKey,
params: BBDT16MacParams,
encodeMessages: boolean
): VerifyResult {
if (messages.length !== params.supportedMessageCount()) {
throw new Error(
`Number of messages ${
messages.length
} is different from ${params.supportedMessageCount()} supported by the MAC params`
);
}
return bddt16MacVerifyConstantTime(messages, this.value, secretKey.value, params.value, encodeMessages);
}
static signMessageObject(
messages: Object,
secretKey: BBDT16MacSecretKey,
labelOrParams: Uint8Array | BBDT16MacParams,
encoder: Encoder
): SignedMessages<BBDT16Mac> {
const encodedMessages = encoder.encodeMessageObjectAsObjectConstantTime(messages);
const encodedMessageList = Object.values(encodedMessages);
const sigParams = BBDT16MacParams.getMacParamsOfRequiredSize(encodedMessageList.length, labelOrParams);
const signature = BBDT16Mac.generate(encodedMessageList, secretKey, sigParams, false);
return {
encodedMessages,
signature
};
}
static getSignedMessageObjectWithProof(
messages: Object,
secretKey: BBDT16MacSecretKey,
publicKey: BBDT16MacPublicKeyG1,
labelOrParams: Uint8Array | BBDT16MacParams,
encoder: Encoder
): [SignedMessages<BBDT16Mac>, BBDT16MacProofOfValidity] {
const encodedMessages = encoder.encodeMessageObjectAsObjectConstantTime(messages);
const encodedMessageList = Object.values(encodedMessages);
const sigParams = BBDT16MacParams.getMacParamsOfRequiredSize(encodedMessageList.length, labelOrParams);
const signature = BBDT16Mac.generate(encodedMessageList, secretKey, sigParams, false);
const proof = new BBDT16MacProofOfValidity(signature, secretKey, publicKey, sigParams);
return [
{
encodedMessages,
signature
},
proof
];
}
/**
* Verifies the MAC on the given messages. Takes the messages as a JS object, flattens it, encodes the values similar
* to signing and then verifies the MAC.
* @param messages
* @param secretKey
* @param labelOrParams
* @param encoder
* @param useConstantTimeEncoding
*/
verifyMessageObject(
messages: object,
secretKey: BBDT16MacSecretKey,
labelOrParams: Uint8Array | BBDT16MacParams,
encoder: Encoder,
useConstantTimeEncoding = true
): VerifyResult {
const [_, encodedValues] = useConstantTimeEncoding
? encoder.encodeMessageObjectConstantTime(messages)
: encoder.encodeMessageObject(messages);
const msgCount = encodedValues.length;
const sigParams = BBDT16MacParams.getMacParamsOfRequiredSize(msgCount, labelOrParams);
return this.verify(encodedValues, secretKey, sigParams, false);
}
}
export class BBDT16BlindMac extends MessageEncoder {
/**
* Generates a blind MAC over the commitment of unrevealed messages and revealed messages
* @param commitment - Commitment over unrevealed messages sent by the requester of the blind MAC. Its assumed that
* the signers has verified the knowledge of committed messages
* @param revealedMessages
* @param secretKey
* @param params
* @param encodeMessages
*/
static generate(
commitment: Uint8Array,
revealedMessages: Map<number, Uint8Array>,
secretKey: BBDT16MacSecretKey,
params: BBDT16MacParams,
encodeMessages: boolean
): BBDT16BlindMac {
if (revealedMessages.size >= params.supportedMessageCount()) {
throw new Error(
`Number of messages ${
revealedMessages.size
} must be less than ${params.supportedMessageCount()} supported by the MAC params`
);
}
const sig = bddt16BlindMacGenerateConstantTime(
commitment,
revealedMessages,
secretKey.value,
params.value,
encodeMessages
);
return new BBDT16BlindMac(sig);
}
/**
* Generate a blind MAC from request
* @param request
* @param secretKey
* @param params
* @returns {BBDT16BlindMac}
*/
static fromRequest(
{ commitment, revealedMessages }: BBDT16BlindMacRequest,
secretKey: BBDT16MacSecretKey,
params: BBDT16MacParams
): BBDT16BlindMac {
return this.generate(commitment, revealedMessages ?? new Map(), secretKey, params, false);
}
/**
* Unblind the blind MAC to get a regular MAC that can be verified
* @param blinding
*/
unblind(blinding: Uint8Array): BBDT16Mac {
const sig = bddt16UnblindMac(this.value, blinding);
return new BBDT16Mac(sig);
}
/**
* Generate a request for a blind MAC
* @param messagesToBlind - messages the requester wants to hide from the signer. The key of the map is the index of the
* message as per the params.
* @param params
* @param encodeMessages
* @param blinding - If not provided, a random blinding is generated
* @param revealedMessages - Any messages that the requester wishes to inform the signer about. This is for informational
* purpose only and has no cryptographic use.
*/
static generateRequest(
messagesToBlind: Map<number, Uint8Array>,
params: BBDT16MacParams,
encodeMessages: boolean,
blinding?: Uint8Array,
revealedMessages?: Map<number, Uint8Array>
): [Uint8Array, BBDT16BlindMacRequest] {
const [commitment, b] = params.commitToMessagesConstantTime(messagesToBlind, encodeMessages, blinding);
const [blindedIndices, encodedRevealedMessages] = getBlindedIndicesAndRevealedMessages(
messagesToBlind,
encodeMessages,
revealedMessages
);
return [b, { commitment, blindedIndices, revealedMessages: encodedRevealedMessages }];
}
/**
* Used by the signer to create a blind MAC
* @param blindSigRequest - The blind sig request sent by user.
* @param revealedMessages - The messages known to the signer
* @param secretKey
* @param msgStructure
* @param labelOrParams
* @param encoder
*/
static blindSignMessageObject(
blindSigRequest: BBDT16BlindMacRequest,
revealedMessages: object,
secretKey: BBDT16MacSecretKey,
msgStructure: MessageStructure,
labelOrParams: Uint8Array | BBDT16MacParams,
encoder: Encoder
): SignedMessages<BBDT16BlindMac> {
const {
encodedByName: encodedMessages,
encodedByIndex: revealedMessagesEncoded,
total
} = encodeRevealedMessageObject(revealedMessages, blindSigRequest.blindedIndices.length, msgStructure, encoder);
const macParams = BBDT16MacParams.getMacParamsOfRequiredSize(total, labelOrParams);
const signature = this.generate(blindSigRequest.commitment, revealedMessagesEncoded, secretKey, macParams, false);
return {
encodedMessages,
signature
};
}
}
/**
* Structure to send to the signer to request a blind MAC for BBDT16 scheme.
*/
export interface BBDT16BlindMacRequest {
/**
* The commitment to the blinded messages
*/
commitment: Uint8Array;
/**
* The messages at these indices were committed to in the commitment and are not revealed to the signer. This is expected
* to be sorted in ascending order
*/
blindedIndices: number[];
/**
* The messages which are known to the signer. Here the key is message index (as per the `MACParams`). This is not
* mandatory as the signer might already know the messages to sign. This is used when the requester wants to inform the
* signer of some or all of the message
*/
revealedMessages?: Map<number, Uint8Array>;
}
/**
* This MAC cannot be verified without the secret key but the signer can give a proof to the user that the MAC is
* correct, i.e. it was created using the secret key.
*/
export class BBDT16MacProofOfValidity extends BytearrayWrapper {
constructor(mac: BBDT16Mac, secretKey: BBDT16MacSecretKey, publicKey: BBDT16MacPublicKeyG1, params: BBDT16MacParams) {
const proof = bddt16MacProofOfValidity(mac.value, secretKey.value, publicKey.value, params.value);
super(proof);
}
verify(
mac: BBDT16Mac,
messages: Uint8Array[],
publicKey: BBDT16MacPublicKeyG1,
params: BBDT16MacParams,
encodeMessages: boolean
): VerifyResult {
if (messages.length !== params.supportedMessageCount()) {
throw new Error(
`Number of messages ${
messages.length
} is different from ${params.supportedMessageCount()} supported by the MAC params`
);
}
return bddt16MacVerifyProofOfValidity(
this.value,
mac.value,
messages,
publicKey.value,
params.value,
encodeMessages
);
}
verifyWithMessageObject(
mac: BBDT16Mac,
messages: object,
publicKey: BBDT16MacPublicKeyG1,
labelOrParams: Uint8Array | BBDT16MacParams,
encoder: Encoder
): VerifyResult {
const [_, encodedValues] = encoder.encodeMessageObjectConstantTime(messages);
const msgCount = encodedValues.length;
const params = BBDT16MacParams.getMacParamsOfRequiredSize(msgCount, labelOrParams);
return this.verify(mac, encodedValues, publicKey, params, false);
}
}