This repository has been archived by the owner on Aug 1, 2023. It is now read-only.
/
HMACSHA512.java
205 lines (184 loc) · 6.22 KB
/
HMACSHA512.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
* file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.apache.tuweni.crypto.sodium;
import org.apache.tuweni.bytes.Bytes;
import javax.security.auth.Destroyable;
import jnr.ffi.Pointer;
/**
* Message authentication code support for HMAC-SHA-512.
*/
public final class HMACSHA512 {
private HMACSHA512() {}
/**
* A HMACSHA512 secret key.
*/
public static final class Key implements Destroyable {
final Allocated value;
Key(Pointer ptr, int length) {
this.value = new Allocated(ptr, length);
}
@Override
public void destroy() {
value.destroy();
}
@Override
public boolean isDestroyed() {
return value.isDestroyed();
}
/**
* Create a {@link Key} from an array of bytes.
*
* <p>
* The byte array must be of length {@link #length()}.
*
* @param bytes The bytes for the secret key.
* @return A secret key.
*/
public static Key fromBytes(Bytes bytes) {
return fromBytes(bytes.toArrayUnsafe());
}
/**
* Create a {@link Key} from an array of bytes.
*
* <p>
* The byte array must be of length {@link #length()}.
*
* @param bytes The bytes for the secret key.
* @return A secret key.
*/
public static Key fromBytes(byte[] bytes) {
if (bytes.length != Sodium.crypto_auth_hmacsha512_keybytes()) {
throw new IllegalArgumentException(
"key must be " + Sodium.crypto_auth_hmacsha512_keybytes() + " bytes, got " + bytes.length);
}
return Sodium.dup(bytes, Key::new);
}
/**
* Generate a random {@link Key}.
*
* @return A randomly generated secret key.
*/
public static Key random() {
return Sodium.randomBytes(length(), Key::new);
}
/**
* Obtain the length of the key in bytes (32).
*
* @return The length of the key in bytes (32).
*/
public static int length() {
long keybytes = Sodium.crypto_auth_hmacsha512_keybytes();
if (keybytes > Integer.MAX_VALUE) {
throw new SodiumException("crypto_auth_hmacsha512_keybytes: " + keybytes + " is too large");
}
return (int) keybytes;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Key)) {
return false;
}
Key other = (Key) obj;
return other.value.equals(value);
}
@Override
public int hashCode() {
return value.hashCode();
}
/**
* Provides the bytes of this key.
*
* @return The bytes of this key.
*/
public Bytes bytes() {
return value.bytes();
}
/**
* Provides the bytes of this key.
*
* @return The bytes of this key.
*/
public byte[] bytesArray() {
return value.bytesArray();
}
}
/**
* Authenticates a message using a secret into a HMAC-SHA-512 authenticator.
*
* @param message the message to authenticate
* @param key the secret key to use for authentication
* @return the authenticator of the message
*/
public static Bytes authenticate(Bytes message, Key key) {
return Bytes.wrap(authenticate(message.toArrayUnsafe(), key));
}
/**
* Authenticates a message using a secret into a HMAC-SHA-512 authenticator.
*
* @param message the message to authenticate
* @param key the secret key to use for authentication
* @return the authenticator of the message
*/
public static byte[] authenticate(byte[] message, Key key) {
if (key.isDestroyed()) {
throw new IllegalStateException("SecretKey has been destroyed");
}
long authBytes = Sodium.crypto_auth_hmacsha512_bytes();
if (authBytes > Integer.MAX_VALUE) {
throw new SodiumException("crypto_auth_hmacsha512_bytes: " + authBytes + " is too large");
}
byte[] out = new byte[(int) authBytes];
int rc = Sodium.crypto_auth_hmacsha512(out, message, message.length, key.value.pointer());
if (rc != 0) {
throw new SodiumException("crypto_auth_hmacsha512: failed with result " + rc);
}
return out;
}
/**
* Verifies the authenticator of a message matches according to a secret.
*
* @param authenticator the authenticator to verify
* @param in the message to match against the authenticator
* @param key the secret key to use for verification
* @return true if the authenticator verifies the message according to the secret, false otherwise
*/
public static boolean verify(Bytes authenticator, Bytes in, Key key) {
return verify(authenticator.toArrayUnsafe(), in.toArrayUnsafe(), key);
}
/**
* Verifies the authenticator of a message matches according to a secret.
*
* @param authenticator the authenticator to verify
* @param in the message to match against the authenticator
* @param key the secret key to use for verification
* @return true if the authenticator verifies the message according to the secret, false otherwise
*/
public static boolean verify(byte[] authenticator, byte[] in, Key key) {
if (key.isDestroyed()) {
throw new IllegalStateException("key has been destroyed");
}
if (authenticator.length != Sodium.crypto_auth_hmacsha512_bytes()) {
throw new IllegalArgumentException(
"Expected authenticator of "
+ Sodium.crypto_auth_hmacsha512_bytes()
+ " bytes, got "
+ authenticator.length
+ " instead");
}
int rc = Sodium.crypto_auth_hmacsha512_verify(authenticator, in, in.length, key.value.pointer());
return rc == 0;
}
}