summaryrefslogtreecommitdiff
path: root/spec/ruby/library/openssl/kdf/scrypt_spec.rb
blob: 07ac1017bbcb9b4b1f7a90b22ad69f6b7b6dba68 (plain)
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
require_relative '../../../spec_helper'
require 'openssl'

describe "OpenSSL::KDF.scrypt" do
  break unless defined?(OpenSSL::KDF.scrypt)

  before :each do
    @defaults = {
      salt: "\x00".b * 16,
      N: 2**14,
      r: 8,
      p: 1,
      length: 32
    }
  end

  it "creates the same value with the same input" do
    key = OpenSSL::KDF.scrypt("secret", **@defaults)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "supports nullbytes embedded into the password" do
    key = OpenSSL::KDF.scrypt("sec\x00ret".b, **@defaults)
    key.should == "\xF9\xA4\xA0\xF1p\xF4\xF0\xCAT\xB4v\xEB\r7\x88N\xF7\x15]Ns\xFCwt4a\xC9\xC6\xA7\x13\x81&".b
  end

  it "coerces the password into a String using #to_str" do
    pass = mock("pass")
    pass.should_receive(:to_str).and_return("secret")
    key = OpenSSL::KDF.scrypt(pass, **@defaults)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "coerces the salt into a String using #to_str" do
    salt = mock("salt")
    salt.should_receive(:to_str).and_return("\x00".b * 16)
    key = OpenSSL::KDF.scrypt("secret", **@defaults, salt: salt)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "coerces the N into an Integer using #to_int" do
    n = mock("N")
    n.should_receive(:to_int).and_return(2**14)
    key = OpenSSL::KDF.scrypt("secret", **@defaults, N: n)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "coerces the r into an Integer using #to_int" do
    r = mock("r")
    r.should_receive(:to_int).and_return(8)
    key = OpenSSL::KDF.scrypt("secret", **@defaults, r: r)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "coerces the p into an Integer using #to_int" do
    p = mock("p")
    p.should_receive(:to_int).and_return(1)
    key = OpenSSL::KDF.scrypt("secret", **@defaults, p: p)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "coerces the length into an Integer using #to_int" do
    length = mock("length")
    length.should_receive(:to_int).and_return(32)
    key = OpenSSL::KDF.scrypt("secret", **@defaults, length: length)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D\x17}\xF2\x84T\xD4)\xC2>\xFE\x93\xE3\xF4".b
  end

  it "accepts an empty password" do
    key = OpenSSL::KDF.scrypt("", **@defaults)
    key.should == "\xAA\xFC\xF5^E\x94v\xFFk\xE6\xF0vR\xE7\x13\xA7\xF5\x15'\x9A\xE4C\x9Dn\x18F_E\xD2\v\e\xB3".b
  end

  it "accepts an empty salt" do
    key = OpenSSL::KDF.scrypt("secret", **@defaults, salt: "")
    key.should == "\x96\xACDl\xCB3/aN\xB0F\x8A#\xD7\x92\xD2O\x1E\v\xBB\xCE\xC0\xAA\xB9\x0F]\xB09\xEA8\xDD\e".b
  end

  it "accepts a zero length" do
    key = OpenSSL::KDF.scrypt("secret", **@defaults, length: 0)
    key.should.empty?
  end

  it "accepts an arbitrary length" do
    key = OpenSSL::KDF.scrypt("secret", **@defaults, length: 19)
    key.should == "h\xB2k\xDF]\xDA\xE1.-(\xCF\xAC\x91D\x8F\xC2a\x9C\x9D".b
  end

  it "raises a TypeError when password is not a String and does not respond to #to_str" do
    -> {
      OpenSSL::KDF.scrypt(Object.new, **@defaults)
    }.should raise_error(TypeError, "no implicit conversion of Object into String")
  end

  it "raises a TypeError when salt is not a String and does not respond to #to_str" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, salt: Object.new)
    }.should raise_error(TypeError, "no implicit conversion of Object into String")
  end

  it "raises a TypeError when N is not an Integer and does not respond to #to_int" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, N: Object.new)
    }.should raise_error(TypeError, "no implicit conversion of Object into Integer")
  end

  it "raises a TypeError when r is not an Integer and does not respond to #to_int" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, r: Object.new)
    }.should raise_error(TypeError, "no implicit conversion of Object into Integer")
  end

  it "raises a TypeError when p is not an Integer and does not respond to #to_int" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, p: Object.new)
    }.should raise_error(TypeError, "no implicit conversion of Object into Integer")
  end

  it "raises a TypeError when length is not an Integer and does not respond to #to_int" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, length: Object.new)
    }.should raise_error(TypeError, "no implicit conversion of Object into Integer")
  end

  it "treats salt as a required keyword" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults.except(:salt))
    }.should raise_error(ArgumentError, 'missing keyword: :salt')
  end

  it "treats N as a required keyword" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults.except(:N))
    }.should raise_error(ArgumentError, 'missing keyword: :N')
  end

  it "treats r as a required keyword" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults.except(:r))
    }.should raise_error(ArgumentError, 'missing keyword: :r')
  end

  it "treats p as a required keyword" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults.except(:p))
    }.should raise_error(ArgumentError, 'missing keyword: :p')
  end

  it "treats length as a required keyword" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults.except(:length))
    }.should raise_error(ArgumentError, 'missing keyword: :length')
  end

  it "treats all keywords as required" do
    -> {
      OpenSSL::KDF.scrypt("secret")
    }.should raise_error(ArgumentError, 'missing keywords: :salt, :N, :r, :p, :length')
  end

  it "requires N to be a power of 2" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, N: 2**14 - 1)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)
  end

  it "requires N to be at least 2" do
    key = OpenSSL::KDF.scrypt("secret", **@defaults, N: 2)
    key.should == "\x06A$a\xA9!\xBE\x01\x85\xA7\x18\xBCEa\x82\xC5\xFEl\x93\xAB\xBD\xF7\x8B\x84\v\xFC\eN\xEBQ\xE6\xD2".b

    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, N: 1)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)

    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, N: 0)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)

    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, N: -1)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)
  end

  it "requires r to be positive" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, r: 0)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)

    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, r: -1)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)
  end

  it "requires p to be positive" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, p: 0)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)

    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, p: -1)
    }.should raise_error(OpenSSL::KDF::KDFError, /EVP_PBE_scrypt/)
  end

  it "requires length to be not negative" do
    -> {
      OpenSSL::KDF.scrypt("secret", **@defaults, length: -1)
    }.should raise_error(ArgumentError, "negative string size (or size too big)")
  end
end