annotate app/src/main/java/ch/ethz/ssh2/crypto/digest/MAC.java @ 517:e70ec2c51ffa

Added tag stable-1.9.4-7 for changeset 3407f4741240
author Carl Byington <carl@five-ten-sg.com>
date Fri, 31 May 2024 13:31:52 -0600
parents 7953570e5210
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 * Please refer to the LICENSE.txt for licensing details.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 package ch.ethz.ssh2.crypto.digest;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 import java.io.IOException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 import java.security.DigestException;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 /**
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 * MAC.
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 *
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * @author Christian Plattner
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * @version 2.50, 03/15/10
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 */
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 public final class MAC {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 private Digest mac;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 private int size;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 public static String[] getMacList() {
375
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
21 // Higher priority (stronger) first. Added SHA-2 algorithms as in RFC 6668
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
22 return new String[] {
510
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
23 "hmac-sha2-512",
375
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
24 "hmac-sha2-256",
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
25 "hmac-sha1",
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
26 "hmac-sha1-96",
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
27 "hmac-md5",
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
28 "hmac-md5-96"
8c1451f51a5e kex error fixed; order encryption, hash, and kex algorithms properly, strongest preferred
Carl Byington <carl@five-ten-sg.com>
parents: 307
diff changeset
29 };
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 public static void checkMacList(final String[] macs) {
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
33 for (String m : macs) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 getKeyLen(m);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 public static int getKeyLen(final String type) {
510
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
39 if (type.equals("hmac-sha2-512")) return 64;
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
40 if (type.equals("hmac-sha2-256")) return 32;
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
41 if (type.equals("hmac-sha1")) return 20;
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
42 if (type.equals("hmac-sha1-96")) return 20;
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
43 if (type.equals("hmac-md5")) return 16;
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
44 if (type.equals("hmac-md5-96")) return 16;
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 throw new IllegalArgumentException(String.format("Unknown algorithm %s", type));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 public MAC(final String type, final byte[] key) throws DigestException {
510
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
49 if (type.equals("hmac-sha2-512")) {
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
50 mac = new HMAC(new SHA512(), key, 64, 128);
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
51 }
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
52 else if (type.equals("hmac-sha2-256")) {
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
53 mac = new HMAC(new SHA256(), key, 32, 64);
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
54 }
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
55 else if (type.equals("hmac-sha1")) {
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
56 mac = new HMAC(new SHA1(), key, 20, 64);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
58 else if (type.equals("hmac-sha1-96")) {
510
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
59 mac = new HMAC(new SHA1(), key, 12, 64);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
61 else if (type.equals("hmac-md5")) {
510
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
62 mac = new HMAC(new MD5(), key, 16, 64);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
64 else if (type.equals("hmac-md5-96")) {
510
7953570e5210 update to ganymed-ssh2 tag 263 and fix hmac-sha2-512
Carl Byington <carl@five-ten-sg.com>
parents: 438
diff changeset
65 mac = new HMAC(new MD5(), key, 12, 64);
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 else {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 throw new IllegalArgumentException(String.format("Unknown algorithm %s", type));
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
70
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71 size = mac.getDigestLength();
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 public final void initMac(final int seq) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 mac.reset();
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
76 mac.update((byte)(seq >> 24));
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
77 mac.update((byte)(seq >> 16));
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
78 mac.update((byte)(seq >> 8));
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
79 mac.update((byte)(seq));
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 public final void update(byte[] packetdata, int off, int len) {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 mac.update(packetdata, off, len);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 public final void getMac(byte[] out, int off) throws IOException {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 try {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 mac.digest(out, off);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 }
307
071eccdff8ea fix java formatting
Carl Byington <carl@five-ten-sg.com>
parents: 273
diff changeset
90 catch (DigestException e) {
273
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 throw new IOException(e);
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 public final int size() {
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 return size;
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 }
91a31873c42a start conversion from trilead to ganymed
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 }