/ OSX / libsecurity_apple_csp / lib / SHA2_Object.cpp
SHA2_Object.cpp
  1  /*
  2   * Copyright (c) 2000-2004,2011-2012,2014 Apple Inc. All Rights Reserved.
  3   * 
  4   * @APPLE_LICENSE_HEADER_START@
  5   * 
  6   * This file contains Original Code and/or Modifications of Original Code
  7   * as defined in and that are subject to the Apple Public Source License
  8   * Version 2.0 (the 'License'). You may not use this file except in
  9   * compliance with the License. Please obtain a copy of the License at
 10   * http://www.opensource.apple.com/apsl/ and read it before using this
 11   * file.
 12   * 
 13   * The Original Code and all software distributed under the License are
 14   * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 15   * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 16   * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 17   * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 18   * Please see the License for the specific language governing rights and
 19   * limitations under the License.
 20   * 
 21   * @APPLE_LICENSE_HEADER_END@
 22   */
 23  
 24  
 25  /*
 26   * SHA2_Object.cpp - SHA2 digest objects 
 27   */
 28  
 29  #include "SHA2_Object.h"
 30  #include <stdexcept>
 31  #include <string.h>
 32  
 33  /***
 34   *** SHA224
 35   ***/
 36  void SHA224Object::digestInit()
 37  {
 38  	mIsDone = false;
 39  	CC_SHA224_Init(&mCtx);
 40  }
 41  
 42  void SHA224Object::digestUpdate(
 43  	const void 	*data, 
 44  	size_t 		len)
 45  {
 46  	CC_SHA224_Update(&mCtx, (const unsigned char *)data, (CC_LONG)len);
 47  }
 48  
 49  void SHA224Object::digestFinal(
 50  	void 		*digest)
 51  {
 52  	CC_SHA224_Final((unsigned char *)digest, &mCtx);
 53  	mIsDone = true;
 54  }
 55  
 56  /* use default memberwise init */
 57  DigestObject *SHA224Object::digestClone() const
 58  {
 59  	return new SHA224Object(*this);
 60  }
 61  
 62  size_t SHA224Object::digestSizeInBytes() const
 63  {
 64  	return CC_SHA224_DIGEST_LENGTH;
 65  }
 66  
 67  /***
 68   *** SHA256
 69   ***/
 70  void SHA256Object::digestInit()
 71  {
 72  	mIsDone = false;
 73  	CC_SHA256_Init(&mCtx);
 74  }
 75  
 76  void SHA256Object::digestUpdate(
 77  	const void 	*data, 
 78  	size_t 		len)
 79  {
 80  	CC_SHA256_Update(&mCtx, (const unsigned char *)data, (CC_LONG)len);
 81  }
 82  
 83  void SHA256Object::digestFinal(
 84  	void 		*digest)
 85  {
 86  	CC_SHA256_Final((unsigned char *)digest, &mCtx);
 87  	mIsDone = true;
 88  }
 89  
 90  /* use default memberwise init */
 91  DigestObject *SHA256Object::digestClone() const
 92  {
 93  	return new SHA256Object(*this);
 94  }
 95  
 96  size_t SHA256Object::digestSizeInBytes() const
 97  {
 98  	return CC_SHA256_DIGEST_LENGTH;
 99  }
100  
101  /***
102   *** SHA384
103   ***/
104  void SHA384Object::digestInit()
105  {
106  	mIsDone = false;
107  	CC_SHA384_Init(&mCtx);
108  }
109  
110  void SHA384Object::digestUpdate(
111  	const void 	*data, 
112  	size_t 		len)
113  {
114  	CC_SHA384_Update(&mCtx, (const unsigned char *)data, (CC_LONG)len);
115  }
116  
117  void SHA384Object::digestFinal(
118  	void 		*digest)
119  {
120  	CC_SHA384_Final((unsigned char *)digest, &mCtx);
121  	mIsDone = true;
122  }
123  
124  /* use default memberwise init */
125  DigestObject *SHA384Object::digestClone() const
126  {
127  	return new SHA384Object(*this);
128  }
129  
130  size_t SHA384Object::digestSizeInBytes() const
131  {
132  	return CC_SHA384_DIGEST_LENGTH;
133  }
134  
135  /***
136   *** SHA512
137   ***/
138  void SHA512Object::digestInit()
139  {
140  	mIsDone = false;
141  	CC_SHA512_Init(&mCtx);
142  }
143  
144  void SHA512Object::digestUpdate(
145  	const void 	*data, 
146  	size_t 		len)
147  {
148  	CC_SHA512_Update(&mCtx, (const unsigned char *)data, (CC_LONG)len);
149  }
150  
151  void SHA512Object::digestFinal(
152  	void 		*digest)
153  {
154  	CC_SHA512_Final((unsigned char *)digest, &mCtx);
155  	mIsDone = true;
156  }
157  
158  /* use default memberwise init */
159  DigestObject *SHA512Object::digestClone() const
160  {
161  	return new SHA512Object(*this);
162  }
163  
164  size_t SHA512Object::digestSizeInBytes() const
165  {
166  	return CC_SHA512_DIGEST_LENGTH;
167  }
168