• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 // [START mac-example]
15 package mac;
16 
17 import static java.nio.charset.StandardCharsets.UTF_8;
18 
19 import com.google.crypto.tink.InsecureSecretKeyAccess;
20 import com.google.crypto.tink.KeysetHandle;
21 import com.google.crypto.tink.Mac;
22 import com.google.crypto.tink.RegistryConfiguration;
23 import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
24 import com.google.crypto.tink.mac.MacConfig;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 
29 /**
30  * A command-line utility for checking file integrity with a Message Authentication Code (MAC).
31  *
32  * <p>It loads cleartext keys from disk - this is not recommended!
33  *
34  * <p>It requires the following arguments:
35  *
36  * <ul>
37  *   <li>mode: either 'compute' or 'verify'.
38  *   <li>key-file: Read the key material from this file.
39  *   <li>input-file: Read the input from this file.
40  *   <li>mac-file: name of the file containing a hexadecimal MAC of the input data.
41  */
42 public final class MacExample {
main(String[] args)43   public static void main(String[] args) throws Exception {
44     if (args.length != 4) {
45       System.err.printf("Expected 4 parameters, got %d\n", args.length);
46       System.err.println("Usage: java MacExample compute/verify key-file input-file mac-file");
47       System.exit(1);
48     }
49     String mode = args[0];
50     if (!mode.equals("compute") && !mode.equals("verify")) {
51       System.err.println("Incorrect mode. Please select compute or verify.");
52       System.exit(1);
53     }
54     Path keyFile = Paths.get(args[1]);
55     byte[] msg = Files.readAllBytes(Paths.get(args[2]));
56     Path macFile = Paths.get(args[3]);
57 
58     // Register all MAC key types with the Tink runtime.
59     MacConfig.register();
60 
61     // Read the keyset into a KeysetHandle.
62     KeysetHandle handle =
63         TinkJsonProtoKeysetFormat.parseKeyset(
64             new String(Files.readAllBytes(keyFile), UTF_8), InsecureSecretKeyAccess.get());
65 
66     // Get the primitive.
67     Mac macPrimitive = handle.getPrimitive(RegistryConfiguration.get(), Mac.class);
68 
69     if (mode.equals("compute")) {
70       byte[] macTag = macPrimitive.computeMac(msg);
71       Files.write(macFile, macTag);
72     } else {
73       byte[] macTag = Files.readAllBytes(macFile);
74       // This will throw a GeneralSecurityException if verification fails.
75       macPrimitive.verifyMac(macTag, msg);
76     }
77   }
78 
MacExample()79   private MacExample() {}
80 }
81 // [END mac-example]
82