Skip to content

Commit 57e581a

Browse files
Add FIPS pbkdf2 validation (#7187) (#7493)
Add FIPS pbkdf2 validation. (cherry picked from commit 6097c5c) Co-authored-by: Michel Laterman <[email protected]>
1 parent 5c95533 commit 57e581a

File tree

6 files changed

+222
-21
lines changed

6 files changed

+222
-21
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: enhancement
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Validate pbkdf2 settings when in FIPS mode
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/7187
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
#issue: https://github.com/owner/repo/1234

internal/pkg/crypto/io.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,6 @@ type Option struct {
3030
BlockSize int
3131
}
3232

33-
// Validate the options for encoding and decoding values.
34-
func (o *Option) Validate() error {
35-
if o.IVLength == 0 {
36-
return errors.New("IVLength must be superior to 0")
37-
}
38-
39-
if o.SaltLength == 0 {
40-
return errors.New("SaltLength must be superior to 0")
41-
}
42-
43-
if o.IterationsCount == 0 {
44-
return errors.New("IterationsCount must be superior to 0")
45-
}
46-
47-
if o.KeyLength == 0 {
48-
return errors.New("KeyLength must be superior to 0")
49-
}
50-
51-
return nil
52-
}
53-
5433
// DefaultOptions is the default options to use when creating the writer, changing might decrease
5534
// the efficacity of the encryption.
5635
var DefaultOptions = &Option{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
//go:build requirefips
6+
7+
package crypto
8+
9+
import (
10+
"errors"
11+
)
12+
13+
// Validate the options for encoding and decoding values.
14+
func (o *Option) Validate() error {
15+
if o.IVLength < 12 {
16+
return errors.New("IVLength must be at least 96 bits (12 bytes)")
17+
}
18+
19+
if o.SaltLength < 16 {
20+
return errors.New("SaltLength must be at least 128 bits (16 bytes)")
21+
}
22+
23+
if o.IterationsCount < 1000 {
24+
return errors.New("IterationsCount must be at least 1000")
25+
}
26+
27+
if o.KeyLength < 14 {
28+
return errors.New("KeyLength must be at least 112 bits (14 bytes)")
29+
}
30+
31+
return nil
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
//go:build requirefips
6+
7+
package crypto
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func Test_Option_ValidateFIPS(t *testing.T) {
16+
t.Run("default has no errors", func(t *testing.T) {
17+
err := DefaultOptions.Validate()
18+
assert.NoError(t, err)
19+
})
20+
21+
tests := []struct {
22+
name string
23+
option *Option
24+
}{{
25+
name: "IVLength is low",
26+
option: &Option{
27+
IVLength: 10,
28+
SaltLength: 20,
29+
IterationsCount: 10000,
30+
KeyLength: 20,
31+
},
32+
}, {
33+
name: "SaltLength is low",
34+
option: &Option{
35+
IVLength: 20,
36+
SaltLength: 10,
37+
IterationsCount: 10000,
38+
KeyLength: 20,
39+
},
40+
}, {
41+
name: "IterationsCount is low",
42+
option: &Option{
43+
IVLength: 20,
44+
SaltLength: 20,
45+
IterationsCount: 100,
46+
KeyLength: 20,
47+
},
48+
}, {
49+
name: "KeyLength is low",
50+
option: &Option{
51+
IVLength: 20,
52+
SaltLength: 20,
53+
IterationsCount: 10000,
54+
KeyLength: 10,
55+
},
56+
}}
57+
for _, tc := range tests {
58+
t.Run(tc.name, func(t *testing.T) {
59+
err := tc.option.Validate()
60+
assert.Error(t, err, "expected validation to fail with error")
61+
})
62+
}
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
//go:build !requirefips
6+
7+
package crypto
8+
9+
import (
10+
"errors"
11+
)
12+
13+
// Validate the options for encoding and decoding values.
14+
func (o *Option) Validate() error {
15+
if o.IVLength == 0 {
16+
return errors.New("IVLength must be superior to 0")
17+
}
18+
19+
if o.SaltLength == 0 {
20+
return errors.New("SaltLength must be superior to 0")
21+
}
22+
23+
if o.IterationsCount == 0 {
24+
return errors.New("IterationsCount must be superior to 0")
25+
}
26+
27+
if o.KeyLength == 0 {
28+
return errors.New("KeyLength must be superior to 0")
29+
}
30+
31+
return nil
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License 2.0;
3+
// you may not use this file except in compliance with the Elastic License 2.0.
4+
5+
//go:build !requirefips
6+
7+
package crypto
8+
9+
import (
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func Test_Option_Validate(t *testing.T) {
16+
t.Run("default has no errors", func(t *testing.T) {
17+
err := DefaultOptions.Validate()
18+
assert.NoError(t, err)
19+
})
20+
21+
tests := []struct {
22+
name string
23+
option *Option
24+
}{{
25+
name: "IVLength is 0",
26+
option: &Option{
27+
IVLength: 0,
28+
SaltLength: 1,
29+
IterationsCount: 1,
30+
KeyLength: 1,
31+
},
32+
}, {
33+
name: "SaltLength is 0",
34+
option: &Option{
35+
IVLength: 1,
36+
SaltLength: 0,
37+
IterationsCount: 1,
38+
KeyLength: 1,
39+
},
40+
}, {
41+
name: "IterationsCount is 0",
42+
option: &Option{
43+
IVLength: 1,
44+
SaltLength: 1,
45+
IterationsCount: 0,
46+
KeyLength: 1,
47+
},
48+
}, {
49+
name: "KeyLength is 0",
50+
option: &Option{
51+
IVLength: 1,
52+
SaltLength: 1,
53+
IterationsCount: 1,
54+
KeyLength: 0,
55+
},
56+
}}
57+
for _, tc := range tests {
58+
t.Run(tc.name, func(t *testing.T) {
59+
err := tc.option.Validate()
60+
assert.Error(t, err, "expected validation to fail with error")
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)