1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*
21  * XSEC
22  *
23  * NSSCryptoHash := NSS Implementation of Message digests
24  *
25  * Author(s): Milan Tomic
26  *
27  */
28 
29 #ifndef NSSCRYPTOHASH_INCLUDE
30 #define NSSCRYPTOHASH_INCLUDE
31 
32 #include <xsec/framework/XSECDefs.hpp>
33 #include <xsec/enc/XSECCryptoHash.hpp>
34 
35 #if defined (XSEC_HAVE_NSS)
36 
37 #include <pk11func.h>
38 #include <nss.h>
39 
40 #define NSS_MAX_HASH_SIZE	128
41 
42 /**
43  * @ingroup nsscrypto
44  * @{
45  */
46 
47 /**
48  * \brief NSS Implementation of Hash functions.
49  *
50  * Uses the NSS functions to perform digest functions.
51  *
52  */
53 
54 class XSEC_EXPORT NSSCryptoHash : public XSECCryptoHash {
55 
56 
57 public :
58 
59 	/** @name Constructors/Destructors */
60 	//@{
61 	/**
62 	 * \brief Construct a Hash object
63 	 *
64 	 * Creates a NSS based hash object of the required type.
65 	 *
66 	 * @param alg The algorithm to use for digest operations
67 	 */
68 
69 	NSSCryptoHash(XSECCryptoHash::HashType alg);
70 	virtual ~NSSCryptoHash();
71 
72 	//@}
73 
74 	/** @name HMAC Functions */
75 	//@{
76 
77 	/**
78 	 *\brief
79 	 *
80 	 * Does nothing. If the required function is an HMAC function,
81 	 * then NSSCryptoHashHMAC should be used.
82 	 *
83 	 * @param key The key the HMAC function should use.
84 	 */
85 
setKey(const XSECCryptoKey * key)86 	virtual void setKey(const XSECCryptoKey * key) {}
87 
88 	//@}
89 
90 	/** @name Digest/Hash functions */
91 	//@{
92 
93 	/**
94 	 * \brief Reset the hash function
95 	 *
96 	 * Re-initialises the digest structure.
97 	 */
98 
99 	virtual void reset(void);
100 
101 	/**
102 	 * \brief Hash some data.
103 	 *
104 	 * Take length bytes of data from the data buffer and update the hash
105 	 * that already exists.  This function may (and normally will) be called
106 	 * many times for large blocks of data.
107 	 *
108 	 * @param data The buffer containing the data to be hashed.
109 	 * @param length The number of bytes to be read from data
110 	 */
111 
112 	virtual void		hash(unsigned char * data,
113 							 unsigned int length);
114 
115 	/**
116 	 * \brief Finish up a Digest operation and read the result.
117 	 *
118 	 * This call tells the CryptoHash object that the input is complete and
119 	 * to finalise the Digest.  The output of the digest is read into the
120 	 * hash buffer (at most maxLength bytes)
121 	 *
122 	 * @param hash The buffer the hash should be read into.
123 	 * @param maxLength The maximum number of bytes to be read into hash
124 	 * @returns The number of bytes copied into the hash buffer
125 	 */
126 
127 	virtual unsigned int finish(unsigned char * hash,
128 								unsigned int maxLength);// Finish and get hash
129 
130 	//@}
131 
132 	/** @name Information functions */
133 	//@{
134 
135 	/**
136 	 *\brief
137 	 *
138 	 * Determine the hash type of this object
139 	 *
140 	 * @returns The hash type
141 	 */
142 
143 	virtual HashType getHashType(void) const;
144 
145 	//@}
146 
147 private:
148 
149 	// Not implemented constructors
150 	NSSCryptoHash();
151 
152 	unsigned char				m_mdValue[NSS_MAX_HASH_SIZE];		// Final output
153 	unsigned int				m_mdLen;
154 
155 	HashType	          m_hashType;
156 
157   PK11Context *       mp_md;
158 
159 };
160 
161 #endif /* XSEC_HAVE_NSS */
162 #endif /* NSSCRYPTOHASHSHA1_INCLUDE */
163