]> git.saurik.com Git - apple/security.git/blob - AppleCSP/AppleCSP/SignatureContext.cpp
85c785117684456fb40dbdf4f92c93d73e021d2d
[apple/security.git] / AppleCSP / AppleCSP / SignatureContext.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * SignatureContext.h - AppleCSPContext sublass for generic sign/verify
21 */
22
23 #include "SignatureContext.h"
24 #include "AppleCSPUtils.h"
25 #include "AppleCSPSession.h"
26 #include <Security/utilities.h>
27 #include <Security/cssmtype.h>
28
29 #include <Security/debugging.h>
30
31 #define cspSigDebug(args...) secdebug("cspSig", ## args)
32
33 SignatureContext::~SignatureContext()
34 {
35 delete &mDigest;
36 delete &mSigner;
37 mInitFlag = false;
38 }
39
40 /* both sign & verify */
41 void SignatureContext::init(
42 const Context &context,
43 bool isSigning)
44 {
45 mDigest.digestInit();
46 mSigner.signerInit(context, isSigning);
47 mInitFlag = true;
48 }
49
50 /* both sign & verify */
51 void SignatureContext::update(
52 const CssmData &data)
53 {
54 mDigest.digestUpdate(data.Data, data.Length);
55 }
56
57 /* sign only */
58 void SignatureContext::final(
59 CssmData &out)
60 {
61 void *digest;
62 size_t digestLen;
63 void *sig = out.data();
64 size_t sigLen = out.length();
65
66 /* first obtain the digest */
67 digestLen = mDigest.digestSizeInBytes();
68 digest = session().malloc(digestLen);
69 mDigest.digestFinal(digest);
70
71 /* now sign */
72 try {
73 mSigner.sign(digest,
74 digestLen,
75 sig,
76 &sigLen);
77 }
78 catch(...) {
79 session().free(digest);
80 throw;
81 }
82 session().free(digest);
83 if(out.length() < sigLen) {
84 cspSigDebug("SignatureContext: mallocd sig too small!");
85 CssmError::throwMe(CSSMERR_CSP_INTERNAL_ERROR);
86 }
87 out.length(sigLen);
88 }
89
90 /* verify only */
91 void SignatureContext::final(
92 const CssmData &in)
93 {
94 void *digest;
95 size_t digestLen;
96
97 /* first obtain the digest */
98 digestLen = mDigest.digestSizeInBytes();
99 digest = session().malloc(digestLen);
100 mDigest.digestFinal(digest);
101
102 /* now verify */
103 try {
104 mSigner.verify(digest,
105 digestLen,
106 in.Data,
107 in.Length);
108 }
109 catch(...) {
110 session().free(digest);
111 throw;
112 }
113 session().free(digest);
114 }
115
116 size_t SignatureContext::outputSize(
117 bool final,
118 size_t inSize)
119 {
120 return mSigner.maxSigSize();
121 }
122
123 /* for raw sign/verify - optionally called after init */
124 void SignatureContext::setDigestAlgorithm(
125 CSSM_ALGORITHMS digestAlg)
126 {
127 mSigner.setDigestAlg(digestAlg);
128 }