typenative 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/node/assert.js +2 -2
- package/bin/node/child_process.js +66 -8
- package/bin/node/fs.js +1012 -4
- package/bin/node/histogram.js +1991 -0
- package/bin/node/process.js +14 -2
- package/bin/node/sqlite.js +1488 -5
- package/bin/node/stream.js +2 -1
- package/bin/node/system_extra.js +3 -270
- package/bin/node/test.js +39 -1
- package/bin/node/test_report.js +47 -1
- package/bin/node/test_run.js +57 -2
- package/bin/node/util.js +8 -5
- package/bin/node/webassembly.js +20 -5
- package/bin/node/webview.js +3257 -244
- package/bin/node/zlib.js +118 -14
- package/bin/node/zlib_iter.js +585 -0
- package/bin/transpiler.js +1370 -258
- package/bin/version.js +1 -1
- package/govendor/software.sslmate.com/src/go-pkcs12/.gitattributes +10 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/LICENSE +28 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/README.md +31 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/bmp-string.go +63 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/crypto.go +331 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/errors.go +23 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/go.mod +3 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/internal/rc2/rc2.go +271 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/mac.go +202 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/pbkdf.go +184 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/pkcs12.go +1000 -0
- package/govendor/software.sslmate.com/src/go-pkcs12/safebags.go +107 -0
- package/package.json +10 -1
- package/types/tsconfig.json +17 -0
- package/types/typenative-npm.d.ts +492 -12
- package/types/typenative.d.ts +88 -2
- package/.gitattributes +0 -1
- package/.github/workflows/ci.yml +0 -68
- package/.github/workflows/npm-publish-github-packages.yml +0 -88
- package/CHANGELOG.md +0 -1746
- package/CONTRIBUTION.md +0 -133
- package/conformance.md +0 -1497
- package/examples/README.md +0 -114
- package/examples/angular-templates/README.md +0 -141
- package/examples/angular-templates/bench.mjs +0 -173
- package/examples/angular-templates/dom-type-globals.d.ts +0 -21
- package/examples/angular-templates/dom-type-stubs.ts +0 -21
- package/examples/angular-templates/main.ts +0 -135
- package/examples/angular-templates/parity.mjs +0 -157
- package/examples/angular-templates/run-node.mjs +0 -69
- package/examples/angular-templates/vendor/angular/compiler/LICENSE +0 -21
- package/examples/desktop-notes/README.md +0 -106
- package/examples/desktop-notes/bench.mjs +0 -125
- package/examples/desktop-notes/main.ts +0 -131
- package/examples/desktop-notes/package-lock.json +0 -726
- package/examples/desktop-notes/package.json +0 -9
- package/examples/desktop-notes/ui.ts +0 -176
- package/examples/dupe-finder/README.md +0 -78
- package/examples/dupe-finder/bench.mjs +0 -120
- package/examples/dupe-finder/main.ts +0 -87
- package/examples/fibonacci/README.md +0 -89
- package/examples/fibonacci/bench.mjs +0 -87
- package/examples/fibonacci/main.ts +0 -21
- package/examples/hello-world/README.md +0 -62
- package/examples/hello-world/bench.mjs +0 -66
- package/examples/hello-world/main.ts +0 -7
- package/examples/json-server/README.md +0 -132
- package/examples/json-server/bench.mjs +0 -263
- package/examples/json-server/main.ts +0 -67
- package/examples/mandelbrot-png/README.md +0 -63
- package/examples/mandelbrot-png/bench.mjs +0 -95
- package/examples/mandelbrot-png/main.ts +0 -98
- package/examples/mandelbrot-png/preview.png +0 -0
- package/examples/marked-cli/README.md +0 -174
- package/examples/marked-cli/bench.mjs +0 -178
- package/examples/marked-cli/main.ts +0 -410
- package/examples/marked-cli/parity.mjs +0 -185
- package/examples/marked-cli/vendor/marked/LICENSE +0 -44
- package/examples/marked-cli/vendor/marked/VENDOR.md +0 -26
- package/examples/openai-chat/README.md +0 -148
- package/examples/openai-chat/bench.mjs +0 -175
- package/examples/openai-chat/main.ts +0 -488
- package/examples/react-compiler/README.md +0 -185
- package/examples/react-compiler/bench.mjs +0 -181
- package/examples/react-compiler/main.ts +0 -324
- package/examples/react-compiler/parity.mjs +0 -362
- package/examples/react-compiler/run-node.mjs +0 -90
- package/examples/react-compiler/vendor/babel/LICENSE +0 -22
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +0 -15
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +0 -51
- package/examples/react-compiler/vendor/charcodes/package.json +0 -25
- package/examples/react-compiler/vendor/invariant/LICENSE +0 -21
- package/examples/react-compiler/vendor/invariant/invariant.cjs +0 -51
- package/examples/react-compiler/vendor/invariant/invariant.d.cts +0 -11
- package/examples/react-compiler/vendor/invariant/types/LICENSE +0 -21
- package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +0 -19
- package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +0 -19
- package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +0 -19
- package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +0 -19
- package/examples/react-compiler/vendor/js-tokens/LICENSE +0 -21
- package/examples/react-compiler/vendor/js-tokens/index.cjs +0 -23
- package/examples/react-compiler/vendor/js-tokens/index.d.cts +0 -13
- package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +0 -20
- package/examples/react-compiler/vendor/jsesc/jsesc.cjs +0 -337
- package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +0 -384
- package/examples/react-compiler/vendor/jsesc/types/LICENSE +0 -21
- package/examples/react-compiler/vendor/picocolors/LICENSE +0 -15
- package/examples/react-compiler/vendor/picocolors/picocolors.cjs +0 -75
- package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +0 -5
- package/examples/react-compiler/vendor/picocolors/types.d.ts +0 -51
- package/examples/react-compiler/vendor/react-compiler/LICENSE +0 -21
- package/examples/react-compiler/vendor/stubs/babel-core.ts +0 -31
- package/examples/react-compiler/vendor/stubs/debug.ts +0 -18
- package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +0 -13
- package/examples/react-compiler/vendor/stubs/pretty-format.ts +0 -18
- package/examples/react-compiler/vendor/zod/LICENSE +0 -21
- package/examples/react-compiler/vendor/zod-validation-error/LICENSE +0 -9
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +0 -5
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +0 -49
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +0 -48
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +0 -50
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +0 -6
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +0 -126
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +0 -39
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +0 -78
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +0 -2
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +0 -131
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +0 -54
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +0 -40
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +0 -12
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +0 -76
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +0 -76
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +0 -27
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +0 -27
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +0 -13
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +0 -61
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +0 -38
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +0 -20
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +0 -5
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +0 -8
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +0 -11
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +0 -20
- package/examples/react-compiler/vendor.mjs +0 -688
- package/examples/todo-cli/README.md +0 -69
- package/examples/todo-cli/bench.mjs +0 -89
- package/examples/todo-cli/main.ts +0 -92
- package/examples/yaml-cli/README.md +0 -152
- package/examples/yaml-cli/bench.mjs +0 -265
- package/examples/yaml-cli/main.ts +0 -251
- package/examples/yaml-cli/parity.mjs +0 -156
- package/examples/yaml-cli/vendor/yaml/LICENSE +0 -13
- package/examples/yaml-cli/vendor/yaml/VENDOR.md +0 -25
- package/performance/README.md +0 -248
- package/performance/any-dynamic.ts +0 -21
- package/performance/any-record-view.ts +0 -20
- package/performance/arr-for-of.ts +0 -29
- package/performance/arr-hof.ts +0 -21
- package/performance/arr-push-index.ts +0 -15
- package/performance/arr-push-local.ts +0 -39
- package/performance/arr-slice-concat.ts +0 -24
- package/performance/arr-sort.ts +0 -21
- package/performance/async-await.ts +0 -24
- package/performance/async-promise-all.ts +0 -38
- package/performance/bigint-arith.ts +0 -36
- package/performance/buf-encode.ts +0 -29
- package/performance/class-accessors.ts +0 -48
- package/performance/class-dispatch.ts +0 -52
- package/performance/date-format.ts +0 -18
- package/performance/events-emit.ts +0 -27
- package/performance/exc-throw-rare.ts +0 -25
- package/performance/exc-try-catch.ts +0 -26
- package/performance/fn-closures.ts +0 -29
- package/performance/fn-recursion-tree.ts +0 -31
- package/performance/fn-rest-spread.ts +0 -43
- package/performance/gen-for-of.ts +0 -46
- package/performance/gen-next.ts +0 -32
- package/performance/iter-helpers.ts +0 -38
- package/performance/json-any.ts +0 -30
- package/performance/json-roundtrip.ts +0 -27
- package/performance/map-delete.ts +0 -24
- package/performance/map-iterate.ts +0 -26
- package/performance/map-number-keys.ts +0 -37
- package/performance/map-string-keys.ts +0 -22
- package/performance/num-arith-loop.ts +0 -14
- package/performance/num-bitwise.ts +0 -21
- package/performance/num-hash-imul.ts +0 -36
- package/performance/num-mod-int.ts +0 -16
- package/performance/num-to-string.ts +0 -16
- package/performance/obj-alloc.ts +0 -32
- package/performance/obj-keys-entries.ts +0 -44
- package/performance/obj-optional-chain.ts +0 -42
- package/performance/obj-record-dict.ts +0 -24
- package/performance/obj-spread-destructure.ts +0 -40
- package/performance/regex-exec.ts +0 -29
- package/performance/regex-replace-fn.ts +0 -24
- package/performance/regex-site.ts +0 -28
- package/performance/set-ops.ts +0 -20
- package/performance/str-char-scan.ts +0 -35
- package/performance/str-concat-plus.ts +0 -18
- package/performance/str-concat.ts +0 -24
- package/performance/str-methods.ts +0 -24
- package/performance/str-short-scan.ts +0 -26
- package/performance/str-split-join.ts +0 -23
- package/performance/str-switch-compare.ts +0 -50
- package/performance/str-template.ts +0 -16
- package/performance/str-unicode.ts +0 -25
- package/performance/typed-array.ts +0 -30
- package/performance/union-switch.ts +0 -57
- package/performance/union-typeof.ts +0 -40
- package/performance/wasm-call.ts +0 -63
- package/tsparser/check.go +0 -158
- package/tsparser/go.mod +0 -14
- package/tsparser/go.sum +0 -22
- package/tsparser/main.go +0 -363
- package/tsparser/tsparser.exe +0 -4
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// Copyright 2015 The Go Authors. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a BSD-style
|
|
3
|
+
// license that can be found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
// Package rc2 implements the RC2 cipher
|
|
6
|
+
/*
|
|
7
|
+
https://www.ietf.org/rfc/rfc2268.txt
|
|
8
|
+
http://people.csail.mit.edu/rivest/pubs/KRRR98.pdf
|
|
9
|
+
|
|
10
|
+
This code is licensed under the MIT license.
|
|
11
|
+
*/
|
|
12
|
+
package rc2
|
|
13
|
+
|
|
14
|
+
import (
|
|
15
|
+
"crypto/cipher"
|
|
16
|
+
"encoding/binary"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// The rc2 block size in bytes
|
|
20
|
+
const BlockSize = 8
|
|
21
|
+
|
|
22
|
+
type rc2Cipher struct {
|
|
23
|
+
k [64]uint16
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// New returns a new rc2 cipher with the given key and effective key length t1
|
|
27
|
+
func New(key []byte, t1 int) (cipher.Block, error) {
|
|
28
|
+
// TODO(dgryski): error checking for key length
|
|
29
|
+
return &rc2Cipher{
|
|
30
|
+
k: expandKey(key, t1),
|
|
31
|
+
}, nil
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
func (*rc2Cipher) BlockSize() int { return BlockSize }
|
|
35
|
+
|
|
36
|
+
var piTable = [256]byte{
|
|
37
|
+
0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d,
|
|
38
|
+
0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2,
|
|
39
|
+
0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32,
|
|
40
|
+
0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82,
|
|
41
|
+
0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc,
|
|
42
|
+
0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26,
|
|
43
|
+
0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03,
|
|
44
|
+
0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7,
|
|
45
|
+
0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a,
|
|
46
|
+
0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec,
|
|
47
|
+
0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39,
|
|
48
|
+
0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31,
|
|
49
|
+
0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9,
|
|
50
|
+
0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9,
|
|
51
|
+
0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e,
|
|
52
|
+
0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
func expandKey(key []byte, t1 int) [64]uint16 {
|
|
56
|
+
|
|
57
|
+
l := make([]byte, 128)
|
|
58
|
+
copy(l, key)
|
|
59
|
+
|
|
60
|
+
var t = len(key)
|
|
61
|
+
var t8 = (t1 + 7) / 8
|
|
62
|
+
var tm = byte(255 % uint(1<<(8+uint(t1)-8*uint(t8))))
|
|
63
|
+
|
|
64
|
+
for i := len(key); i < 128; i++ {
|
|
65
|
+
l[i] = piTable[l[i-1]+l[uint8(i-t)]]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
l[128-t8] = piTable[l[128-t8]&tm]
|
|
69
|
+
|
|
70
|
+
for i := 127 - t8; i >= 0; i-- {
|
|
71
|
+
l[i] = piTable[l[i+1]^l[i+t8]]
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
var k [64]uint16
|
|
75
|
+
|
|
76
|
+
for i := range k {
|
|
77
|
+
k[i] = uint16(l[2*i]) + uint16(l[2*i+1])*256
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return k
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
func rotl16(x uint16, b uint) uint16 {
|
|
84
|
+
return (x >> (16 - b)) | (x << b)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func (c *rc2Cipher) Encrypt(dst, src []byte) {
|
|
88
|
+
|
|
89
|
+
r0 := binary.LittleEndian.Uint16(src[0:])
|
|
90
|
+
r1 := binary.LittleEndian.Uint16(src[2:])
|
|
91
|
+
r2 := binary.LittleEndian.Uint16(src[4:])
|
|
92
|
+
r3 := binary.LittleEndian.Uint16(src[6:])
|
|
93
|
+
|
|
94
|
+
var j int
|
|
95
|
+
|
|
96
|
+
for j <= 16 {
|
|
97
|
+
// mix r0
|
|
98
|
+
r0 = r0 + c.k[j] + (r3 & r2) + ((^r3) & r1)
|
|
99
|
+
r0 = rotl16(r0, 1)
|
|
100
|
+
j++
|
|
101
|
+
|
|
102
|
+
// mix r1
|
|
103
|
+
r1 = r1 + c.k[j] + (r0 & r3) + ((^r0) & r2)
|
|
104
|
+
r1 = rotl16(r1, 2)
|
|
105
|
+
j++
|
|
106
|
+
|
|
107
|
+
// mix r2
|
|
108
|
+
r2 = r2 + c.k[j] + (r1 & r0) + ((^r1) & r3)
|
|
109
|
+
r2 = rotl16(r2, 3)
|
|
110
|
+
j++
|
|
111
|
+
|
|
112
|
+
// mix r3
|
|
113
|
+
r3 = r3 + c.k[j] + (r2 & r1) + ((^r2) & r0)
|
|
114
|
+
r3 = rotl16(r3, 5)
|
|
115
|
+
j++
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
r0 = r0 + c.k[r3&63]
|
|
120
|
+
r1 = r1 + c.k[r0&63]
|
|
121
|
+
r2 = r2 + c.k[r1&63]
|
|
122
|
+
r3 = r3 + c.k[r2&63]
|
|
123
|
+
|
|
124
|
+
for j <= 40 {
|
|
125
|
+
// mix r0
|
|
126
|
+
r0 = r0 + c.k[j] + (r3 & r2) + ((^r3) & r1)
|
|
127
|
+
r0 = rotl16(r0, 1)
|
|
128
|
+
j++
|
|
129
|
+
|
|
130
|
+
// mix r1
|
|
131
|
+
r1 = r1 + c.k[j] + (r0 & r3) + ((^r0) & r2)
|
|
132
|
+
r1 = rotl16(r1, 2)
|
|
133
|
+
j++
|
|
134
|
+
|
|
135
|
+
// mix r2
|
|
136
|
+
r2 = r2 + c.k[j] + (r1 & r0) + ((^r1) & r3)
|
|
137
|
+
r2 = rotl16(r2, 3)
|
|
138
|
+
j++
|
|
139
|
+
|
|
140
|
+
// mix r3
|
|
141
|
+
r3 = r3 + c.k[j] + (r2 & r1) + ((^r2) & r0)
|
|
142
|
+
r3 = rotl16(r3, 5)
|
|
143
|
+
j++
|
|
144
|
+
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
r0 = r0 + c.k[r3&63]
|
|
148
|
+
r1 = r1 + c.k[r0&63]
|
|
149
|
+
r2 = r2 + c.k[r1&63]
|
|
150
|
+
r3 = r3 + c.k[r2&63]
|
|
151
|
+
|
|
152
|
+
for j <= 60 {
|
|
153
|
+
// mix r0
|
|
154
|
+
r0 = r0 + c.k[j] + (r3 & r2) + ((^r3) & r1)
|
|
155
|
+
r0 = rotl16(r0, 1)
|
|
156
|
+
j++
|
|
157
|
+
|
|
158
|
+
// mix r1
|
|
159
|
+
r1 = r1 + c.k[j] + (r0 & r3) + ((^r0) & r2)
|
|
160
|
+
r1 = rotl16(r1, 2)
|
|
161
|
+
j++
|
|
162
|
+
|
|
163
|
+
// mix r2
|
|
164
|
+
r2 = r2 + c.k[j] + (r1 & r0) + ((^r1) & r3)
|
|
165
|
+
r2 = rotl16(r2, 3)
|
|
166
|
+
j++
|
|
167
|
+
|
|
168
|
+
// mix r3
|
|
169
|
+
r3 = r3 + c.k[j] + (r2 & r1) + ((^r2) & r0)
|
|
170
|
+
r3 = rotl16(r3, 5)
|
|
171
|
+
j++
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
binary.LittleEndian.PutUint16(dst[0:], r0)
|
|
175
|
+
binary.LittleEndian.PutUint16(dst[2:], r1)
|
|
176
|
+
binary.LittleEndian.PutUint16(dst[4:], r2)
|
|
177
|
+
binary.LittleEndian.PutUint16(dst[6:], r3)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
func (c *rc2Cipher) Decrypt(dst, src []byte) {
|
|
181
|
+
|
|
182
|
+
r0 := binary.LittleEndian.Uint16(src[0:])
|
|
183
|
+
r1 := binary.LittleEndian.Uint16(src[2:])
|
|
184
|
+
r2 := binary.LittleEndian.Uint16(src[4:])
|
|
185
|
+
r3 := binary.LittleEndian.Uint16(src[6:])
|
|
186
|
+
|
|
187
|
+
j := 63
|
|
188
|
+
|
|
189
|
+
for j >= 44 {
|
|
190
|
+
// unmix r3
|
|
191
|
+
r3 = rotl16(r3, 16-5)
|
|
192
|
+
r3 = r3 - c.k[j] - (r2 & r1) - ((^r2) & r0)
|
|
193
|
+
j--
|
|
194
|
+
|
|
195
|
+
// unmix r2
|
|
196
|
+
r2 = rotl16(r2, 16-3)
|
|
197
|
+
r2 = r2 - c.k[j] - (r1 & r0) - ((^r1) & r3)
|
|
198
|
+
j--
|
|
199
|
+
|
|
200
|
+
// unmix r1
|
|
201
|
+
r1 = rotl16(r1, 16-2)
|
|
202
|
+
r1 = r1 - c.k[j] - (r0 & r3) - ((^r0) & r2)
|
|
203
|
+
j--
|
|
204
|
+
|
|
205
|
+
// unmix r0
|
|
206
|
+
r0 = rotl16(r0, 16-1)
|
|
207
|
+
r0 = r0 - c.k[j] - (r3 & r2) - ((^r3) & r1)
|
|
208
|
+
j--
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
r3 = r3 - c.k[r2&63]
|
|
212
|
+
r2 = r2 - c.k[r1&63]
|
|
213
|
+
r1 = r1 - c.k[r0&63]
|
|
214
|
+
r0 = r0 - c.k[r3&63]
|
|
215
|
+
|
|
216
|
+
for j >= 20 {
|
|
217
|
+
// unmix r3
|
|
218
|
+
r3 = rotl16(r3, 16-5)
|
|
219
|
+
r3 = r3 - c.k[j] - (r2 & r1) - ((^r2) & r0)
|
|
220
|
+
j--
|
|
221
|
+
|
|
222
|
+
// unmix r2
|
|
223
|
+
r2 = rotl16(r2, 16-3)
|
|
224
|
+
r2 = r2 - c.k[j] - (r1 & r0) - ((^r1) & r3)
|
|
225
|
+
j--
|
|
226
|
+
|
|
227
|
+
// unmix r1
|
|
228
|
+
r1 = rotl16(r1, 16-2)
|
|
229
|
+
r1 = r1 - c.k[j] - (r0 & r3) - ((^r0) & r2)
|
|
230
|
+
j--
|
|
231
|
+
|
|
232
|
+
// unmix r0
|
|
233
|
+
r0 = rotl16(r0, 16-1)
|
|
234
|
+
r0 = r0 - c.k[j] - (r3 & r2) - ((^r3) & r1)
|
|
235
|
+
j--
|
|
236
|
+
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
r3 = r3 - c.k[r2&63]
|
|
240
|
+
r2 = r2 - c.k[r1&63]
|
|
241
|
+
r1 = r1 - c.k[r0&63]
|
|
242
|
+
r0 = r0 - c.k[r3&63]
|
|
243
|
+
|
|
244
|
+
for j >= 0 {
|
|
245
|
+
// unmix r3
|
|
246
|
+
r3 = rotl16(r3, 16-5)
|
|
247
|
+
r3 = r3 - c.k[j] - (r2 & r1) - ((^r2) & r0)
|
|
248
|
+
j--
|
|
249
|
+
|
|
250
|
+
// unmix r2
|
|
251
|
+
r2 = rotl16(r2, 16-3)
|
|
252
|
+
r2 = r2 - c.k[j] - (r1 & r0) - ((^r1) & r3)
|
|
253
|
+
j--
|
|
254
|
+
|
|
255
|
+
// unmix r1
|
|
256
|
+
r1 = rotl16(r1, 16-2)
|
|
257
|
+
r1 = r1 - c.k[j] - (r0 & r3) - ((^r0) & r2)
|
|
258
|
+
j--
|
|
259
|
+
|
|
260
|
+
// unmix r0
|
|
261
|
+
r0 = rotl16(r0, 16-1)
|
|
262
|
+
r0 = r0 - c.k[j] - (r3 & r2) - ((^r3) & r1)
|
|
263
|
+
j--
|
|
264
|
+
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
binary.LittleEndian.PutUint16(dst[0:], r0)
|
|
268
|
+
binary.LittleEndian.PutUint16(dst[2:], r1)
|
|
269
|
+
binary.LittleEndian.PutUint16(dst[4:], r2)
|
|
270
|
+
binary.LittleEndian.PutUint16(dst[6:], r3)
|
|
271
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Copyright 2015, 2018, 2019 Opsmate, Inc. All rights reserved.
|
|
2
|
+
// Copyright 2015 The Go Authors. All rights reserved.
|
|
3
|
+
// Use of this source code is governed by a BSD-style
|
|
4
|
+
// license that can be found in the LICENSE file.
|
|
5
|
+
|
|
6
|
+
package pkcs12
|
|
7
|
+
|
|
8
|
+
import (
|
|
9
|
+
"crypto/hmac"
|
|
10
|
+
"crypto/sha1"
|
|
11
|
+
"crypto/sha256"
|
|
12
|
+
"crypto/sha512"
|
|
13
|
+
"crypto/x509/pkix"
|
|
14
|
+
"encoding/asn1"
|
|
15
|
+
"errors"
|
|
16
|
+
"fmt"
|
|
17
|
+
"hash"
|
|
18
|
+
|
|
19
|
+
"golang.org/x/crypto/pbkdf2"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
type macData struct {
|
|
23
|
+
Mac digestInfo
|
|
24
|
+
MacSalt []byte
|
|
25
|
+
Iterations int `asn1:"optional,default:1"`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// from PKCS#7:
|
|
29
|
+
type digestInfo struct {
|
|
30
|
+
Algorithm pkix.AlgorithmIdentifier
|
|
31
|
+
Digest []byte
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// PBMAC1 parameters structure from RFC 8018
|
|
35
|
+
// When using PBMAC1, the MAC parameters are derived from the algorithm's Parameters field
|
|
36
|
+
// and the macData.MacSalt and macData.Iterations fields are ignored.
|
|
37
|
+
type pbmac1Params struct {
|
|
38
|
+
Kdf pkix.AlgorithmIdentifier
|
|
39
|
+
MacAlg pkix.AlgorithmIdentifier
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func makePBMAC1Parameters(salt []byte, iterations int) ([]byte, error) {
|
|
43
|
+
var err error
|
|
44
|
+
|
|
45
|
+
var kdfparams pbkdf2Params
|
|
46
|
+
if kdfparams.Salt.FullBytes, err = asn1.Marshal(salt); err != nil {
|
|
47
|
+
return nil, err
|
|
48
|
+
}
|
|
49
|
+
kdfparams.Iterations = iterations
|
|
50
|
+
kdfparams.KeyLength = 32
|
|
51
|
+
kdfparams.Prf.Algorithm = oidHmacWithSHA256
|
|
52
|
+
|
|
53
|
+
var params pbmac1Params
|
|
54
|
+
params.Kdf.Algorithm = oidPBKDF2
|
|
55
|
+
if params.Kdf.Parameters.FullBytes, err = asn1.Marshal(kdfparams); err != nil {
|
|
56
|
+
return nil, err
|
|
57
|
+
}
|
|
58
|
+
params.MacAlg.Algorithm = oidHmacWithSHA256
|
|
59
|
+
|
|
60
|
+
return asn1.Marshal(params)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
var (
|
|
64
|
+
oidSHA1 = asn1.ObjectIdentifier([]int{1, 3, 14, 3, 2, 26})
|
|
65
|
+
oidSHA256 = asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 1})
|
|
66
|
+
oidSHA512 = asn1.ObjectIdentifier([]int{2, 16, 840, 1, 101, 3, 4, 2, 3})
|
|
67
|
+
oidPBMAC1 = asn1.ObjectIdentifier([]int{1, 2, 840, 113549, 1, 5, 14})
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
// doPBMAC1 handles PBMAC1 MAC computation using parameters from Algorithm.Parameters
|
|
71
|
+
// PBMAC1 (RFC 8018) uses PBKDF2 for key derivation and supports various HMAC algorithms.
|
|
72
|
+
// Unlike traditional PKCS#12 MAC algorithms, PBMAC1 gets all its parameters from
|
|
73
|
+
// the Algorithm.Parameters field, ignoring macData.MacSalt and macData.Iterations.
|
|
74
|
+
func doPBMAC1(algorithm pkix.AlgorithmIdentifier, message, password []byte) ([]byte, error) {
|
|
75
|
+
var params pbmac1Params
|
|
76
|
+
if err := unmarshal(algorithm.Parameters.FullBytes, ¶ms); err != nil {
|
|
77
|
+
return nil, fmt.Errorf("error decoding PBMAC1 parameters: %w", err)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Only PBKDF2 is supported as KDF
|
|
81
|
+
if !params.Kdf.Algorithm.Equal(oidPBKDF2) {
|
|
82
|
+
return nil, NotImplementedError("PBMAC1 KDF algorithm " + params.Kdf.Algorithm.String() + " is not supported")
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
var kdfParams pbkdf2Params
|
|
86
|
+
if err := unmarshal(params.Kdf.Parameters.FullBytes, &kdfParams); err != nil {
|
|
87
|
+
return nil, err
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if kdfParams.Salt.Tag != asn1.TagOctetString {
|
|
91
|
+
return nil, NotImplementedError("only octet string salts are supported for PBMAC1/PBKDF2")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Determine PRF function for PBKDF2
|
|
95
|
+
var prf func() hash.Hash
|
|
96
|
+
switch {
|
|
97
|
+
case kdfParams.Prf.Algorithm.Equal(oidHmacWithSHA256):
|
|
98
|
+
prf = sha256.New
|
|
99
|
+
case kdfParams.Prf.Algorithm.Equal(oidHmacWithSHA512):
|
|
100
|
+
prf = sha512.New
|
|
101
|
+
case kdfParams.Prf.Algorithm.Equal(oidHmacWithSHA1):
|
|
102
|
+
prf = sha1.New
|
|
103
|
+
case kdfParams.Prf.Algorithm == nil:
|
|
104
|
+
// Algorithm not specified; defaults to SHA1 according to ASN1 definition
|
|
105
|
+
prf = sha1.New
|
|
106
|
+
default:
|
|
107
|
+
return nil, NotImplementedError("PBMAC1 PRF " + kdfParams.Prf.Algorithm.String() + " is not supported")
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Determine MAC algorithm
|
|
111
|
+
var hFn func() hash.Hash
|
|
112
|
+
switch {
|
|
113
|
+
case params.MacAlg.Algorithm.Equal(oidHmacWithSHA1):
|
|
114
|
+
hFn = sha1.New
|
|
115
|
+
case params.MacAlg.Algorithm.Equal(oidHmacWithSHA256):
|
|
116
|
+
hFn = sha256.New
|
|
117
|
+
case params.MacAlg.Algorithm.Equal(oidHmacWithSHA512):
|
|
118
|
+
hFn = sha512.New
|
|
119
|
+
default:
|
|
120
|
+
return nil, NotImplementedError("PBMAC1 MAC algorithm " + params.MacAlg.Algorithm.String() + " is not supported")
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// KeyLength is mandatory in RFC 9579
|
|
124
|
+
if kdfParams.KeyLength <= 0 {
|
|
125
|
+
return nil, errors.New("pkcs12: PBMAC1 requires explicit KeyLength parameter in PBKDF2 parameters")
|
|
126
|
+
}
|
|
127
|
+
// RFC 9579 RECOMMENDS rejecting key lengths less than 20; this is necessary to prevent possible authentication bypass (e.g. OpenSSL's CVE-2026-34181)
|
|
128
|
+
if kdfParams.KeyLength < 20 {
|
|
129
|
+
return nil, fmt.Errorf("pkcs12: PBMAC1 key length %d is too short to be secure (minimum 20 octets)", kdfParams.KeyLength)
|
|
130
|
+
}
|
|
131
|
+
// RFC 9879 RECOMMENDS that the key length match the HMAC output size, which
|
|
132
|
+
// is at most 64 octets (SHA-512). Reject anything larger so that a huge
|
|
133
|
+
// KeyLength can't cause a huge PBKDF2 allocation (matching OpenSSL's EVP_MAX_MD_SIZE cap)
|
|
134
|
+
const maxKeyLength = 64
|
|
135
|
+
if kdfParams.KeyLength > maxKeyLength {
|
|
136
|
+
return nil, fmt.Errorf("pkcs12: PBMAC1 key length %d is too large (maximum %d octets)", kdfParams.KeyLength, maxKeyLength)
|
|
137
|
+
}
|
|
138
|
+
keyLen := kdfParams.KeyLength
|
|
139
|
+
|
|
140
|
+
// Derive key using PBKDF2
|
|
141
|
+
key := pbkdf2.Key(password, kdfParams.Salt.Bytes, kdfParams.Iterations, keyLen, prf)
|
|
142
|
+
|
|
143
|
+
// Compute HMAC
|
|
144
|
+
mac := hmac.New(hFn, key)
|
|
145
|
+
mac.Write(message)
|
|
146
|
+
return mac.Sum(nil), nil
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
func doMac(macData *macData, message, password []byte) ([]byte, error) {
|
|
150
|
+
// Handle PBMAC1 separately - it uses its own parameters structure from Algorithm.Parameters
|
|
151
|
+
// and ignores macData.MacSalt and macData.Iterations fields
|
|
152
|
+
if macData.Mac.Algorithm.Algorithm.Equal(oidPBMAC1) {
|
|
153
|
+
// PBMAC1 expects UTF-8 passwords (for compatibility; see Erratum 7974), but
|
|
154
|
+
// PKCS#12 passwords are BMP strings, so we convert the BMP string back to UTF-8
|
|
155
|
+
originalPassword, err := decodeBMPString(password)
|
|
156
|
+
if err != nil {
|
|
157
|
+
return nil, err
|
|
158
|
+
}
|
|
159
|
+
utf8Password := []byte(originalPassword)
|
|
160
|
+
return doPBMAC1(macData.Mac.Algorithm, message, utf8Password)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
var hFn func() hash.Hash
|
|
164
|
+
var key []byte
|
|
165
|
+
switch {
|
|
166
|
+
case macData.Mac.Algorithm.Algorithm.Equal(oidSHA1):
|
|
167
|
+
hFn = sha1.New
|
|
168
|
+
key = pbkdf(sha1Sum, 20, 64, macData.MacSalt, password, macData.Iterations, 3, 20)
|
|
169
|
+
case macData.Mac.Algorithm.Algorithm.Equal(oidSHA256):
|
|
170
|
+
hFn = sha256.New
|
|
171
|
+
key = pbkdf(sha256Sum, 32, 64, macData.MacSalt, password, macData.Iterations, 3, 32)
|
|
172
|
+
case macData.Mac.Algorithm.Algorithm.Equal(oidSHA512):
|
|
173
|
+
hFn = sha512.New
|
|
174
|
+
key = pbkdf(sha512Sum, 64, 128, macData.MacSalt, password, macData.Iterations, 3, 64)
|
|
175
|
+
default:
|
|
176
|
+
return nil, NotImplementedError("MAC digest algorithm not supported: " + macData.Mac.Algorithm.Algorithm.String())
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
mac := hmac.New(hFn, key)
|
|
180
|
+
mac.Write(message)
|
|
181
|
+
return mac.Sum(nil), nil
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
func verifyMac(macData *macData, message, password []byte) error {
|
|
185
|
+
expectedMAC, err := doMac(macData, message, password)
|
|
186
|
+
if err != nil {
|
|
187
|
+
return err
|
|
188
|
+
}
|
|
189
|
+
if !hmac.Equal(macData.Mac.Digest, expectedMAC) {
|
|
190
|
+
return ErrIncorrectPassword
|
|
191
|
+
}
|
|
192
|
+
return nil
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
func computeMac(macData *macData, message, password []byte) error {
|
|
196
|
+
digest, err := doMac(macData, message, password)
|
|
197
|
+
if err != nil {
|
|
198
|
+
return err
|
|
199
|
+
}
|
|
200
|
+
macData.Mac.Digest = digest
|
|
201
|
+
return nil
|
|
202
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// Copyright 2015 The Go Authors. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a BSD-style
|
|
3
|
+
// license that can be found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
package pkcs12
|
|
6
|
+
|
|
7
|
+
import (
|
|
8
|
+
"bytes"
|
|
9
|
+
"crypto/sha1"
|
|
10
|
+
"crypto/sha256"
|
|
11
|
+
"crypto/sha512"
|
|
12
|
+
"math/big"
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
var (
|
|
16
|
+
one = big.NewInt(1)
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// sha1Sum returns the SHA-1 hash of in.
|
|
20
|
+
func sha1Sum(in []byte) []byte {
|
|
21
|
+
sum := sha1.Sum(in)
|
|
22
|
+
return sum[:]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// sha256Sum returns the SHA-256 hash of in.
|
|
26
|
+
func sha256Sum(in []byte) []byte {
|
|
27
|
+
sum := sha256.Sum256(in)
|
|
28
|
+
return sum[:]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// sha512Sum returns the SHA-512 hash of in.
|
|
32
|
+
func sha512Sum(in []byte) []byte {
|
|
33
|
+
sum := sha512.Sum512(in)
|
|
34
|
+
return sum[:]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// fillWithRepeats returns v*ceiling(len(pattern) / v) bytes consisting of
|
|
38
|
+
// repeats of pattern.
|
|
39
|
+
func fillWithRepeats(pattern []byte, v int) []byte {
|
|
40
|
+
if len(pattern) == 0 {
|
|
41
|
+
return nil
|
|
42
|
+
}
|
|
43
|
+
outputLen := v * ((len(pattern) + v - 1) / v)
|
|
44
|
+
return bytes.Repeat(pattern, (outputLen+len(pattern)-1)/len(pattern))[:outputLen]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
func pbkdf(hash func([]byte) []byte, u, v int, salt, password []byte, r int, ID byte, size int) (key []byte) {
|
|
48
|
+
// implementation of https://tools.ietf.org/html/rfc7292#appendix-B.2 , RFC text verbatim in comments
|
|
49
|
+
|
|
50
|
+
// Let H be a hash function built around a compression function f:
|
|
51
|
+
|
|
52
|
+
// Z_2^u x Z_2^v -> Z_2^u
|
|
53
|
+
|
|
54
|
+
// (that is, H has a chaining variable and output of length u bits, and
|
|
55
|
+
// the message input to the compression function of H is v bits). The
|
|
56
|
+
// values for u and v are as follows:
|
|
57
|
+
|
|
58
|
+
// HASH FUNCTION VALUE u VALUE v
|
|
59
|
+
// MD2, MD5 128 512
|
|
60
|
+
// SHA-1 160 512
|
|
61
|
+
// SHA-224 224 512
|
|
62
|
+
// SHA-256 256 512
|
|
63
|
+
// SHA-384 384 1024
|
|
64
|
+
// SHA-512 512 1024
|
|
65
|
+
// SHA-512/224 224 1024
|
|
66
|
+
// SHA-512/256 256 1024
|
|
67
|
+
|
|
68
|
+
// Furthermore, let r be the iteration count.
|
|
69
|
+
|
|
70
|
+
// We assume here that u and v are both multiples of 8, as are the
|
|
71
|
+
// lengths of the password and salt strings (which we denote by p and s,
|
|
72
|
+
// respectively) and the number n of pseudorandom bits required. In
|
|
73
|
+
// addition, u and v are of course non-zero.
|
|
74
|
+
|
|
75
|
+
// For information on security considerations for MD5 [19], see [25] and
|
|
76
|
+
// [1], and on those for MD2, see [18].
|
|
77
|
+
|
|
78
|
+
// The following procedure can be used to produce pseudorandom bits for
|
|
79
|
+
// a particular "purpose" that is identified by a byte called "ID".
|
|
80
|
+
// This standard specifies 3 different values for the ID byte:
|
|
81
|
+
|
|
82
|
+
// 1. If ID=1, then the pseudorandom bits being produced are to be used
|
|
83
|
+
// as key material for performing encryption or decryption.
|
|
84
|
+
|
|
85
|
+
// 2. If ID=2, then the pseudorandom bits being produced are to be used
|
|
86
|
+
// as an IV (Initial Value) for encryption or decryption.
|
|
87
|
+
|
|
88
|
+
// 3. If ID=3, then the pseudorandom bits being produced are to be used
|
|
89
|
+
// as an integrity key for MACing.
|
|
90
|
+
|
|
91
|
+
// 1. Construct a string, D (the "diversifier"), by concatenating v/8
|
|
92
|
+
// copies of ID.
|
|
93
|
+
var D []byte
|
|
94
|
+
for i := 0; i < v; i++ {
|
|
95
|
+
D = append(D, ID)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 2. Concatenate copies of the salt together to create a string S of
|
|
99
|
+
// length v(ceiling(s/v)) bits (the final copy of the salt may be
|
|
100
|
+
// truncated to create S). Note that if the salt is the empty
|
|
101
|
+
// string, then so is S.
|
|
102
|
+
|
|
103
|
+
S := fillWithRepeats(salt, v)
|
|
104
|
+
|
|
105
|
+
// 3. Concatenate copies of the password together to create a string P
|
|
106
|
+
// of length v(ceiling(p/v)) bits (the final copy of the password
|
|
107
|
+
// may be truncated to create P). Note that if the password is the
|
|
108
|
+
// empty string, then so is P.
|
|
109
|
+
|
|
110
|
+
P := fillWithRepeats(password, v)
|
|
111
|
+
|
|
112
|
+
// 4. Set I=S||P to be the concatenation of S and P.
|
|
113
|
+
I := append(S, P...)
|
|
114
|
+
|
|
115
|
+
// 5. Set c=ceiling(n/u).
|
|
116
|
+
c := (size + u - 1) / u
|
|
117
|
+
|
|
118
|
+
// 6. For i=1, 2, ..., c, do the following:
|
|
119
|
+
A := make([]byte, c*u)
|
|
120
|
+
var IjBuf []byte
|
|
121
|
+
for i := 0; i < c; i++ {
|
|
122
|
+
// A. Set A2=H^r(D||I). (i.e., the r-th hash of D||1,
|
|
123
|
+
// H(H(H(... H(D||I))))
|
|
124
|
+
Ai := hash(append(D, I...))
|
|
125
|
+
for j := 1; j < r; j++ {
|
|
126
|
+
Ai = hash(Ai)
|
|
127
|
+
}
|
|
128
|
+
copy(A[i*u:], Ai[:])
|
|
129
|
+
|
|
130
|
+
if i < c-1 { // skip on last iteration
|
|
131
|
+
// B. Concatenate copies of Ai to create a string B of length v
|
|
132
|
+
// bits (the final copy of Ai may be truncated to create B).
|
|
133
|
+
var B []byte
|
|
134
|
+
for len(B) < v {
|
|
135
|
+
B = append(B, Ai[:]...)
|
|
136
|
+
}
|
|
137
|
+
B = B[:v]
|
|
138
|
+
|
|
139
|
+
// C. Treating I as a concatenation I_0, I_1, ..., I_(k-1) of v-bit
|
|
140
|
+
// blocks, where k=ceiling(s/v)+ceiling(p/v), modify I by
|
|
141
|
+
// setting I_j=(I_j+B+1) mod 2^v for each j.
|
|
142
|
+
{
|
|
143
|
+
Bbi := new(big.Int).SetBytes(B)
|
|
144
|
+
Ij := new(big.Int)
|
|
145
|
+
|
|
146
|
+
for j := 0; j < len(I)/v; j++ {
|
|
147
|
+
Ij.SetBytes(I[j*v : (j+1)*v])
|
|
148
|
+
Ij.Add(Ij, Bbi)
|
|
149
|
+
Ij.Add(Ij, one)
|
|
150
|
+
Ijb := Ij.Bytes()
|
|
151
|
+
// We expect Ijb to be exactly v bytes,
|
|
152
|
+
// if it is longer or shorter we must
|
|
153
|
+
// adjust it accordingly.
|
|
154
|
+
if len(Ijb) > v {
|
|
155
|
+
Ijb = Ijb[len(Ijb)-v:]
|
|
156
|
+
}
|
|
157
|
+
if len(Ijb) < v {
|
|
158
|
+
if IjBuf == nil {
|
|
159
|
+
IjBuf = make([]byte, v)
|
|
160
|
+
}
|
|
161
|
+
bytesShort := v - len(Ijb)
|
|
162
|
+
for i := 0; i < bytesShort; i++ {
|
|
163
|
+
IjBuf[i] = 0
|
|
164
|
+
}
|
|
165
|
+
copy(IjBuf[bytesShort:], Ijb)
|
|
166
|
+
Ijb = IjBuf
|
|
167
|
+
}
|
|
168
|
+
copy(I[j*v:(j+1)*v], Ijb)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// 7. Concatenate A_1, A_2, ..., A_c together to form a pseudorandom
|
|
174
|
+
// bit string, A.
|
|
175
|
+
|
|
176
|
+
// 8. Use the first n bits of A as the output of this entire process.
|
|
177
|
+
return A[:size]
|
|
178
|
+
|
|
179
|
+
// If the above process is being used to generate a DES key, the process
|
|
180
|
+
// should be used to create 64 random bits, and the key's parity bits
|
|
181
|
+
// should be set after the 64 bits have been produced. Similar concerns
|
|
182
|
+
// hold for 2-key and 3-key triple-DES keys, for CDMF keys, and for any
|
|
183
|
+
// similar keys with parity bits "built into them".
|
|
184
|
+
}
|