1 package org.bouncycastle.crypto.digests; 2 3 import org.bouncycastle.util.Memoable; 4 import org.bouncycastle.util.Pack; 5 6 7 /** 8 * FIPS 180-2 implementation of SHA-384. 9 * 10 * <pre> 11 * block word digest 12 * SHA-1 512 32 160 13 * SHA-256 512 32 256 14 * SHA-384 1024 64 384 15 * SHA-512 1024 64 512 16 * </pre> 17 */ 18 public class SHA384Digest 19 extends LongDigest 20 { 21 private static final int DIGEST_LENGTH = 48; 22 23 /** 24 * Standard constructor 25 */ SHA384Digest()26 public SHA384Digest() 27 { 28 } 29 30 /** 31 * Copy constructor. This will copy the state of the provided 32 * message digest. 33 */ SHA384Digest(SHA384Digest t)34 public SHA384Digest(SHA384Digest t) 35 { 36 super(t); 37 } 38 SHA384Digest(byte[] encodedState)39 public SHA384Digest(byte[] encodedState) 40 { 41 restoreState(encodedState); 42 } 43 getAlgorithmName()44 public String getAlgorithmName() 45 { 46 return "SHA-384"; 47 } 48 getDigestSize()49 public int getDigestSize() 50 { 51 return DIGEST_LENGTH; 52 } 53 doFinal( byte[] out, int outOff)54 public int doFinal( 55 byte[] out, 56 int outOff) 57 { 58 finish(); 59 60 Pack.longToBigEndian(H1, out, outOff); 61 Pack.longToBigEndian(H2, out, outOff + 8); 62 Pack.longToBigEndian(H3, out, outOff + 16); 63 Pack.longToBigEndian(H4, out, outOff + 24); 64 Pack.longToBigEndian(H5, out, outOff + 32); 65 Pack.longToBigEndian(H6, out, outOff + 40); 66 67 reset(); 68 69 return DIGEST_LENGTH; 70 } 71 72 /** 73 * reset the chaining variables 74 */ reset()75 public void reset() 76 { 77 super.reset(); 78 79 /* SHA-384 initial hash value 80 * The first 64 bits of the fractional parts of the square roots 81 * of the 9th through 16th prime numbers 82 */ 83 H1 = 0xcbbb9d5dc1059ed8l; 84 H2 = 0x629a292a367cd507l; 85 H3 = 0x9159015a3070dd17l; 86 H4 = 0x152fecd8f70e5939l; 87 H5 = 0x67332667ffc00b31l; 88 H6 = 0x8eb44a8768581511l; 89 H7 = 0xdb0c2e0d64f98fa7l; 90 H8 = 0x47b5481dbefa4fa4l; 91 } 92 copy()93 public Memoable copy() 94 { 95 return new SHA384Digest(this); 96 } 97 reset(Memoable other)98 public void reset(Memoable other) 99 { 100 SHA384Digest d = (SHA384Digest)other; 101 102 super.copyIn(d); 103 } 104 getEncodedState()105 public byte[] getEncodedState() 106 { 107 byte[] encoded = new byte[getEncodedStateSize()]; 108 super.populateState(encoded); 109 return encoded; 110 } 111 } 112