/
header.go
540 lines (485 loc) · 14.7 KB
/
header.go
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright (c) 2016, Joseph deBlaquiere <jadeblaquiere@yahoo.com>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of ciphrtxt nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package ciphrtxt
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/hex"
//"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"time"
)
type MessageHeader interface {
Serialize() string
ExportBytes() []byte
Deserialize(string) error
ImportBytes([]byte) error
MessageTime() time.Time
ExpireTime() time.Time
IKey() []byte
JKey() []byte
KKey() []byte
Hash() []byte
dbKeys(uint32) (*dbkeys, error)
}
const MessageHeaderLengthV1 = 5 + 1 + // "M0100" + ":"
8 + 1 + // time(32 bit hex) + ":"
8 + 1 + // expire(32 bit hex) + ":"
66 + 1 + // I (256-bit point, compressed hex) + ":"
66 + 1 + // I (256-bit point, compressed hex) + ":"
66 + 1 + // I (256-bit point, compressed hex) + ":"
64 + 1 + // r (256-bit integer, hex) + ":"
64 // s (256-bit integer, hex)
type SerializedMessageHeaderV1 [MessageHeaderLengthV1]byte
// V2 message header format:
// "M\0x02\0x00\0x00" => 4 bytes => Message File, v 2.00 / 0200
// Message Time => 4 bytes => 32-bit unsigned UNIX time
// Message Expire => 4 bytes => 32-bit unsigned UNIX time
// I (point) => 33 bytes => 256-bit ECC point, compressed
// J (point) => 33 bytes => 256-bit ECC point, compressed
// K (point) => 33 bytes => 256-bit ECC point, compressed
// blocklength => 4 bytes => 32 bit block length
// reserved => 8 bytes => 64 bits reserved (should be zero)
// short (subtotal) =============> 123 bytes (164 bytes in base64 encoding)
// r, s => 64 bytes => 2*256-bit ECDSA signature
// nonce => 5 bytes => 40-bit Nonce
// sig + nonce =============> 69 bytes (92 bytes in base64 encoding)
// 192 bytes (256 bytes in base64)
// the encrypted message is signed along with the short header and then the
// nonce is calculated to ensure the hash of the long header has nbits zeros
// (see also Authenticated Encryption with Additional Data, AEAD, and Hashcash)
//const ShortMessageHeaderLengthV2 = (4+4+4+33+33+33+4+8)
const ShortMessageHeaderLengthV2 = (123)
//const ShortMessageHeaderLengthB64V2 = ((ShortMessageHeaderLengthV2 * 4) / 3)
const ShortMessageHeaderLengthB64V2 = (164)
//const MessageHeaderLengthV2 = (ShortMessageHeaderLengthV2+32+32+5)
const MessageHeaderLengthV2 = (192)
//const MessageHeaderLengthB64V2 = ((MessageHeaderLengthV2 * 4) / 3)
const MessageHeaderLengthB64V2 = (256)
type SerializedMessageHeaderV2 [MessageHeaderLengthB64V2]byte
type BinaryMessageHeaderV2 [MessageHeaderLengthV2]byte
// RawMessageHeader treats larger data objects (EC Points, big integers) as strings
// instead of parsing them to their numerical value
type MessageHeaderJSON struct {
Version string `json:"version"`
Time uint32 `json:"time"`
Expire uint32 `json:"expire"`
TimeStr string `json:"time_str"`
ExpireStr string `json:"expire_str"`
I string `json:"I"`
J string `json:"J"`
K string `json:"K"`
Size uint64 `json:"Size"`
R string `json:"sig_r"`
S string `json:"sig_s"`
Nonce uint64 `json:"nonce"`
}
type RawMessageHeader struct {
version string
time uint32
expire uint32
I []byte
J []byte
K []byte
blocklen uint32
reserved uint64
r []byte
s []byte
nonce uint64
}
type RawMessageHeaderSlice []RawMessageHeader
func (z *RawMessageHeader) deserializeV1(s string) error {
var err error
var t64 uint64
var d = strings.Split(s, ":")
if len(d) != 8 || strings.Compare(d[0], "M0100") != 0 {
return errors.New("V1 version string error")
}
z.version = "0100"
t64, _ = strconv.ParseUint(d[1], 16, 32)
z.time = uint32(t64)
t64, _ = strconv.ParseUint(d[2], 16, 32)
z.expire = uint32(t64)
z.I, err = hex.DecodeString(d[3])
if err != nil {
return errors.New("V1 Error decoding I value as hex")
}
z.J, err = hex.DecodeString(d[4])
if err != nil {
return errors.New("V1 Error decoding J value as hex")
}
z.K, err = hex.DecodeString(d[5])
if err != nil {
return errors.New("V1 Error decoding K value as hex")
}
z.r, err = hex.DecodeString(d[6])
if err != nil {
return errors.New("V1 Error decoding r value as hex")
}
z.s, err = hex.DecodeString(d[7])
if err != nil {
return errors.New("V1 Error decoding s value as hex")
}
return nil
}
func (z *RawMessageHeader) deserializeV2(s string) error {
var err error
smh := make([]byte, 0)
if len(s) < ShortMessageHeaderLengthB64V2 {
//fmt.Println("message too short")
return errors.New("V2 Header too short")
}
if len(s) >= MessageHeaderLengthB64V2 {
smh, err = base64.StdEncoding.DecodeString(s[:MessageHeaderLengthB64V2])
} else {
smh, err = base64.StdEncoding.DecodeString(s[:ShortMessageHeaderLengthB64V2])
}
if err != nil {
//fmt.Println("base64 conversion failed")
return errors.New("V2 not in base64")
}
return z.importBinaryHeaderV2(smh[:])
}
func (z *RawMessageHeader) importBinaryHeaderV2(smh []byte) error {
if len(smh) < ShortMessageHeaderLengthV2 {
return errors.New("V2 Header too short")
}
if bytes.Compare(smh[:4], []byte("M\x02\x00\x00")) != 0 {
//fmt.Println("v0200 version string mismatch")
return errors.New("V2 version string mismatch")
}
z.version = "0200"
z.time = binary.BigEndian.Uint32(smh[4:8])
z.expire = binary.BigEndian.Uint32(smh[8:12])
//z.I = string(smh[12:45])
//z.J = string(smh[45:78])
//z.K = string(smh[78:111])
z.I = make([]byte, 33)
copy(z.I, smh[12:45])
z.J = make([]byte, 33)
copy(z.J, smh[45:78])
z.K = make([]byte, 33)
copy(z.K, smh[78:111])
z.blocklen = binary.BigEndian.Uint32(smh[111:115])
z.reserved = binary.BigEndian.Uint64(smh[115:123])
if len(smh) >= MessageHeaderLengthV2 {
var ui8 uint8
var ui32 uint32
z.r = make([]byte, 32)
copy(z.r, smh[123:155])
z.s = make([]byte, 32)
copy(z.s, smh[155:187])
bufnonce := bytes.NewBuffer(smh[187:188])
binary.Read(bufnonce, binary.BigEndian, &ui8)
bufnonce = bytes.NewBuffer(smh[188:192])
binary.Read(bufnonce, binary.BigEndian, &ui32)
z.nonce = ((uint64)(ui8) << 32)
z.nonce += (uint64)(ui32)
}
//jsontxt, _ := json.Marshal(z.JSON())
//fmt.Printf("imported as (JSON) %s\n", jsontxt)
return nil
}
func (z *RawMessageHeader) Deserialize(s string) error {
if strings.Compare(s[:3], "M01") == 0 {
return z.deserializeV1(s)
} else {
return z.deserializeV2(s)
}
}
func (z *RawMessageHeader) ImportBytes(b []byte) error {
if bytes.Compare(b[:3], []byte("M01")) == 0 {
s := string(b)
return z.deserializeV1(s)
} else {
return z.importBinaryHeaderV2(b)
}
}
func (z *RawMessageHeader) serializeV1() *SerializedMessageHeaderV1 {
smh := new(SerializedMessageHeaderV1)
I := hex.EncodeToString(z.I)
J := hex.EncodeToString(z.J)
K := hex.EncodeToString(z.K)
r := hex.EncodeToString(z.r)
s := hex.EncodeToString(z.s)
ss := fmt.Sprintf("M%s:%08X:%08X:%s:%s:%s:%s:%s", z.version, z.time, z.expire, I, J, K, r, s)
//fmt.Println("serialized as : " + ss)
if len(ss) != MessageHeaderLengthV1 {
//fmt.Printf("Message length invalid: %d chars\n", len(ss))
return nil
}
copy(smh[:], ss)
return smh
}
func (z *RawMessageHeader) exportBinaryHeaderV2() *BinaryMessageHeaderV2 {
buf := new(bytes.Buffer)
buf.WriteString("M\x02\x00\x00")
binary.Write(buf, binary.BigEndian, z.time)
binary.Write(buf, binary.BigEndian, z.expire)
buf.Write(z.I)
buf.Write(z.J)
buf.Write(z.K)
binary.Write(buf, binary.BigEndian, z.blocklen)
binary.Write(buf, binary.BigEndian, z.reserved)
buf.Write(z.r)
buf.Write(z.s)
binary.Write(buf, binary.BigEndian, uint8(z.nonce>>32))
binary.Write(buf, binary.BigEndian, uint32(z.nonce&0xFFFFFFFF))
//fmt.Println("serialized as : " + hex.EncodeToString(buf.Bytes()))
if buf.Len() != MessageHeaderLengthV2 {
//fmt.Printf("Message length invalid: %d chars\n", buf.Len())
return nil
}
bmh := new(BinaryMessageHeaderV2)
copy(bmh[:], buf.Bytes()[:])
return bmh
}
func (z *RawMessageHeader) serializeV2() *SerializedMessageHeaderV2 {
bmh := z.exportBinaryHeaderV2()
b64 := make([]byte, MessageHeaderLengthB64V2)
base64.StdEncoding.Encode(b64, bmh[:])
//fmt.Println("as b64 " + string(b64))
smh := new(SerializedMessageHeaderV2)
copy(smh[:], b64)
return smh
}
func (z *RawMessageHeader) Serialize() string {
if strings.Compare(z.version, "0100") == 0 {
return string(z.serializeV1()[:])
} else {
return string(z.serializeV2()[:])
}
}
func (z *RawMessageHeader) SerializeV1() *SerializedMessageHeaderV1 {
if strings.Compare(z.version, "0100") == 0 {
return z.serializeV1()
} else {
return nil
}
}
func (z *RawMessageHeader) SerializeV2() *SerializedMessageHeaderV2 {
if strings.Compare(z.version, "0100") == 0 {
return nil
} else {
return z.serializeV2()
}
}
func (z *RawMessageHeader) ExportBinaryHeaderV2() *BinaryMessageHeaderV2 {
if strings.Compare(z.version, "0100") == 0 {
return nil
} else {
return z.exportBinaryHeaderV2()
}
}
func ImportBinaryHeaderV2(smh []byte) *RawMessageHeader {
z := new(RawMessageHeader)
if z.importBinaryHeaderV2(smh) != nil {
return nil
}
return z
}
func (z *RawMessageHeader) ExportBytes() []byte {
if strings.Compare(z.version, "0100") == 0 {
return []byte(string(z.SerializeV1()[:]))
} else {
return z.exportBinaryHeaderV2()[:]
}
}
func (z *RawMessageHeader) MessageTime() time.Time {
return time.Unix(int64(z.time), 0)
}
func (z *RawMessageHeader) ExpireTime() time.Time {
return time.Unix(int64(z.expire), 0)
}
func (z *RawMessageHeader) IKey() (k []byte) {
return z.I
}
func (z *RawMessageHeader) JKey() (k []byte) {
return z.J
}
func (z *RawMessageHeader) KKey() (k []byte) {
return z.K
}
func (z *RawMessageHeader) Hash() []byte {
hashval := sha256.Sum256([]byte(z.Serialize()))
return hashval[:]
}
// Len, Less, Swap used for sorting slices of RMH
func (z RawMessageHeaderSlice) Len() int {
return len(z)
}
func (z RawMessageHeaderSlice) Less(i, j int) bool {
if z[i].time < z[j].time {
return true
}
if z[i].time > z[j].time {
return false
}
for x := 0; x < 33; x++ {
if z[i].I[x] < z[j].I[x] {
return true
}
if z[i].I[x] > z[j].I[x] {
return false
}
}
return false
}
func (z RawMessageHeaderSlice) Swap(i, j int) {
t1 := z[i].Serialize()
t2 := z[j].Serialize()
z[j].Deserialize(t1)
z[i].Deserialize(t2)
}
func (z *RawMessageHeader) JSON() *MessageHeaderJSON {
r := new(MessageHeaderJSON)
r.Version = z.version
r.Time = z.time
r.Expire = z.expire
r.TimeStr = time.Unix(int64(z.time), 0).UTC().Format("2006-01-02 15:04:05")
r.ExpireStr = time.Unix(int64(z.expire), 0).UTC().Format("2006-01-02 15:04:05")
r.I = hex.EncodeToString(z.I)
r.J = hex.EncodeToString(z.J)
r.K = hex.EncodeToString(z.K)
r.Size = uint64(z.blocklen+1) * MessageHeaderLengthB64V2
r.R = hex.EncodeToString(z.r)
r.S = hex.EncodeToString(z.s)
r.Nonce = z.nonce
return r
}
func (h *RawMessageHeader) dbKeys(servertime uint32) (dbk *dbkeys, err error) {
dbk = new(dbkeys)
dbk.date, err = hex.DecodeString(fmt.Sprintf("D0%08X", h.time))
if err != nil {
return nil, err
}
dbk.date = append(dbk.date, h.I...)
dbk.servertime, err = hex.DecodeString(fmt.Sprintf("C0%08X", servertime))
if err != nil {
return nil, err
}
dbk.servertime = append(dbk.servertime, h.I...)
dbk.expire, err = hex.DecodeString(fmt.Sprintf("E0%08X", h.expire))
if err != nil {
return nil, err
}
dbk.expire = append(dbk.expire, h.I...)
dbk.I = h.I
return dbk, err
}
type FullMessageHeader struct {
rmh RawMessageHeader
I big.Int
J big.Int
K big.Int
r big.Int
s big.Int
}
func padbytes(x *big.Int, nbyte int) []byte {
if x.Sign() < 0 {
return nil
}
lsbytes := x.Bytes()
lenls := len(lsbytes)
if lenls > nbyte {
return nil
} else if lenls == nbyte {
return lsbytes
}
pad := make([]byte, nbyte)
copy(pad[(nbyte-lenls):], lsbytes[:])
return pad
}
func (z *FullMessageHeader) Serialize() string {
z.rmh.I = padbytes(&z.I, 33)
z.rmh.J = padbytes(&z.J, 33)
z.rmh.K = padbytes(&z.K, 33)
z.rmh.r = padbytes(&z.r, 32)
z.rmh.s = padbytes(&z.s, 32)
return z.rmh.Serialize()
}
func (z *FullMessageHeader) ExportBytes() []byte {
z.rmh.I = padbytes(&z.I, 33)
z.rmh.J = padbytes(&z.J, 33)
z.rmh.K = padbytes(&z.K, 33)
z.rmh.r = padbytes(&z.r, 32)
z.rmh.s = padbytes(&z.s, 32)
return z.rmh.ExportBytes()
}
func (z *FullMessageHeader) Deserialize(s string) error {
err := z.rmh.Deserialize(s)
if err != nil {
return err
}
z.I.SetBytes(z.rmh.I)
z.J.SetBytes(z.rmh.J)
z.K.SetBytes(z.rmh.K)
z.r.SetBytes(z.rmh.r)
z.s.SetBytes(z.rmh.s)
return nil
}
func (z *FullMessageHeader) ImportBytes(b []byte) error {
err := z.rmh.ImportBytes(b)
if err != nil {
return err
}
z.I.SetBytes(z.rmh.I)
z.J.SetBytes(z.rmh.J)
z.K.SetBytes(z.rmh.K)
z.r.SetBytes(z.rmh.r)
z.s.SetBytes(z.rmh.s)
return nil
}
func (z *FullMessageHeader) MessageTime() time.Time {
return z.rmh.MessageTime()
}
func (z *FullMessageHeader) ExpireTime() time.Time {
return z.rmh.ExpireTime()
}
func (z *FullMessageHeader) IKey() []byte {
return padbytes(&z.I, 33)
}
func (z *FullMessageHeader) JKey() []byte {
return padbytes(&z.J, 33)
}
func (z *FullMessageHeader) KKey() []byte {
return padbytes(&z.K, 33)
}
func (z *FullMessageHeader) Hash() []byte {
hashval := sha256.Sum256([]byte(z.Serialize()))
return hashval[:]
}
func (z *FullMessageHeader) dbKeys(servertime uint32) (dbk *dbkeys, err error) {
z.rmh.I = padbytes(&z.I, 33)
return z.rmh.dbKeys(servertime)
}