xref: /titanic_53/usr/src/cmd/cmd-inet/usr.lib/mdnsd/CryptoAlg.c (revision 5ffb0c9b03b5149ff4f5821a62be4a52408ada2a)
1*5ffb0c9bSToomas Soome /* -*- Mode: C; tab-width: 4 -*-
2*5ffb0c9bSToomas Soome  *
3*5ffb0c9bSToomas Soome  * Copyright (c) 2011 Apple Computer, Inc. All rights reserved.
4*5ffb0c9bSToomas Soome  *
5*5ffb0c9bSToomas Soome  * Licensed under the Apache License, Version 2.0 (the "License");
6*5ffb0c9bSToomas Soome  * you may not use this file except in compliance with the License.
7*5ffb0c9bSToomas Soome  * You may obtain a copy of the License at
8*5ffb0c9bSToomas Soome  *
9*5ffb0c9bSToomas Soome  *     http://www.apache.org/licenses/LICENSE-2.0
10*5ffb0c9bSToomas Soome  *
11*5ffb0c9bSToomas Soome  * Unless required by applicable law or agreed to in writing, software
12*5ffb0c9bSToomas Soome  * distributed under the License is distributed on an "AS IS" BASIS,
13*5ffb0c9bSToomas Soome  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*5ffb0c9bSToomas Soome  * See the License for the specific language governing permissions and
15*5ffb0c9bSToomas Soome  * limitations under the License.
16*5ffb0c9bSToomas Soome  */
17*5ffb0c9bSToomas Soome 
18*5ffb0c9bSToomas Soome // ***************************************************************************
19*5ffb0c9bSToomas Soome // CryptoAlg.c:
20*5ffb0c9bSToomas Soome // Interface to DNSSEC cryptographic algorithms. The crypto support itself is
21*5ffb0c9bSToomas Soome // provided by the platform and the functions in this file just provide an
22*5ffb0c9bSToomas Soome // interface to access them in a more generic way.
23*5ffb0c9bSToomas Soome // ***************************************************************************
24*5ffb0c9bSToomas Soome 
25*5ffb0c9bSToomas Soome #include "mDNSEmbeddedAPI.h"
26*5ffb0c9bSToomas Soome #include "CryptoAlg.h"
27*5ffb0c9bSToomas Soome 
28*5ffb0c9bSToomas Soome AlgFuncs *DigestAlgFuncs[DIGEST_TYPE_MAX];
29*5ffb0c9bSToomas Soome AlgFuncs *CryptoAlgFuncs[CRYPTO_ALG_MAX];
30*5ffb0c9bSToomas Soome AlgFuncs *EncAlgFuncs[ENC_ALG_MAX];
31*5ffb0c9bSToomas Soome 
DigestAlgInit(mDNSu8 digestType,AlgFuncs * func)32*5ffb0c9bSToomas Soome mDNSexport mStatus DigestAlgInit(mDNSu8 digestType, AlgFuncs *func)
33*5ffb0c9bSToomas Soome {
34*5ffb0c9bSToomas Soome     if (digestType >= DIGEST_TYPE_MAX)
35*5ffb0c9bSToomas Soome     {
36*5ffb0c9bSToomas Soome         LogMsg("DigestAlgInit: digestType %d exceeds bounds", digestType);
37*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
38*5ffb0c9bSToomas Soome     }
39*5ffb0c9bSToomas Soome     // As digestTypes may not be consecutive, check for specific digest types
40*5ffb0c9bSToomas Soome     // that we support
41*5ffb0c9bSToomas Soome     if (digestType != SHA1_DIGEST_TYPE &&
42*5ffb0c9bSToomas Soome         digestType != SHA256_DIGEST_TYPE)
43*5ffb0c9bSToomas Soome     {
44*5ffb0c9bSToomas Soome         LogMsg("DigestAlgInit: digestType %d not supported", digestType);
45*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
46*5ffb0c9bSToomas Soome     }
47*5ffb0c9bSToomas Soome     DigestAlgFuncs[digestType] = func;
48*5ffb0c9bSToomas Soome     return mStatus_NoError;
49*5ffb0c9bSToomas Soome }
50*5ffb0c9bSToomas Soome 
CryptoAlgInit(mDNSu8 alg,AlgFuncs * func)51*5ffb0c9bSToomas Soome mDNSexport mStatus CryptoAlgInit(mDNSu8 alg, AlgFuncs *func)
52*5ffb0c9bSToomas Soome {
53*5ffb0c9bSToomas Soome     if (alg >= CRYPTO_ALG_MAX)
54*5ffb0c9bSToomas Soome     {
55*5ffb0c9bSToomas Soome         LogMsg("CryptoAlgInit: alg %d exceeds bounds", alg);
56*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
57*5ffb0c9bSToomas Soome     }
58*5ffb0c9bSToomas Soome     // As algs may not be consecutive, check for specific algorithms
59*5ffb0c9bSToomas Soome     // that we support
60*5ffb0c9bSToomas Soome     if (alg != CRYPTO_RSA_SHA1 && alg != CRYPTO_RSA_SHA256 && alg != CRYPTO_RSA_SHA512 &&
61*5ffb0c9bSToomas Soome         alg != CRYPTO_DSA_NSEC3_SHA1 && alg != CRYPTO_RSA_NSEC3_SHA1)
62*5ffb0c9bSToomas Soome     {
63*5ffb0c9bSToomas Soome         LogMsg("CryptoAlgInit: alg %d not supported", alg);
64*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
65*5ffb0c9bSToomas Soome     }
66*5ffb0c9bSToomas Soome 
67*5ffb0c9bSToomas Soome     CryptoAlgFuncs[alg] = func;
68*5ffb0c9bSToomas Soome     return mStatus_NoError;
69*5ffb0c9bSToomas Soome }
70*5ffb0c9bSToomas Soome 
EncAlgInit(mDNSu8 alg,AlgFuncs * func)71*5ffb0c9bSToomas Soome mDNSexport mStatus EncAlgInit(mDNSu8 alg, AlgFuncs *func)
72*5ffb0c9bSToomas Soome {
73*5ffb0c9bSToomas Soome     if (alg >= ENC_ALG_MAX)
74*5ffb0c9bSToomas Soome     {
75*5ffb0c9bSToomas Soome         LogMsg("EncAlgInit: alg %d exceeds bounds", alg);
76*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
77*5ffb0c9bSToomas Soome     }
78*5ffb0c9bSToomas Soome 
79*5ffb0c9bSToomas Soome     // As algs may not be consecutive, check for specific algorithms
80*5ffb0c9bSToomas Soome     // that we support
81*5ffb0c9bSToomas Soome     if (alg != ENC_BASE32 && alg != ENC_BASE64)
82*5ffb0c9bSToomas Soome     {
83*5ffb0c9bSToomas Soome         LogMsg("EncAlgInit: alg %d not supported", alg);
84*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
85*5ffb0c9bSToomas Soome     }
86*5ffb0c9bSToomas Soome 
87*5ffb0c9bSToomas Soome     EncAlgFuncs[alg] = func;
88*5ffb0c9bSToomas Soome     return mStatus_NoError;
89*5ffb0c9bSToomas Soome }
90*5ffb0c9bSToomas Soome 
AlgCreate(AlgType type,mDNSu8 alg)91*5ffb0c9bSToomas Soome mDNSexport AlgContext *AlgCreate(AlgType type, mDNSu8 alg)
92*5ffb0c9bSToomas Soome {
93*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
94*5ffb0c9bSToomas Soome     AlgContext *ctx;
95*5ffb0c9bSToomas Soome 
96*5ffb0c9bSToomas Soome     if (type == CRYPTO_ALG)
97*5ffb0c9bSToomas Soome     {
98*5ffb0c9bSToomas Soome         if (alg >= CRYPTO_ALG_MAX) return mDNSNULL;
99*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[alg];
100*5ffb0c9bSToomas Soome     }
101*5ffb0c9bSToomas Soome     else if (type == DIGEST_ALG)
102*5ffb0c9bSToomas Soome     {
103*5ffb0c9bSToomas Soome         if (alg >= DIGEST_TYPE_MAX) return mDNSNULL;
104*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[alg];
105*5ffb0c9bSToomas Soome     }
106*5ffb0c9bSToomas Soome     else if (type == ENC_ALG)
107*5ffb0c9bSToomas Soome     {
108*5ffb0c9bSToomas Soome         if (alg >= ENC_ALG_MAX) return mDNSNULL;
109*5ffb0c9bSToomas Soome         func = EncAlgFuncs[alg];
110*5ffb0c9bSToomas Soome     }
111*5ffb0c9bSToomas Soome 
112*5ffb0c9bSToomas Soome     if (!func)
113*5ffb0c9bSToomas Soome     {
114*5ffb0c9bSToomas Soome         // If there is no support from the platform, this case can happen.
115*5ffb0c9bSToomas Soome         LogInfo("AlgCreate: func is NULL");
116*5ffb0c9bSToomas Soome         return mDNSNULL;
117*5ffb0c9bSToomas Soome     }
118*5ffb0c9bSToomas Soome 
119*5ffb0c9bSToomas Soome     if (func->Create)
120*5ffb0c9bSToomas Soome     {
121*5ffb0c9bSToomas Soome         mStatus err;
122*5ffb0c9bSToomas Soome         ctx = mDNSPlatformMemAllocate(sizeof(AlgContext));
123*5ffb0c9bSToomas Soome         if (!ctx) return mDNSNULL;
124*5ffb0c9bSToomas Soome         // Create expects ctx->alg to be initialized
125*5ffb0c9bSToomas Soome         ctx->alg = alg;
126*5ffb0c9bSToomas Soome         err = func->Create(ctx);
127*5ffb0c9bSToomas Soome         if (err == mStatus_NoError)
128*5ffb0c9bSToomas Soome         {
129*5ffb0c9bSToomas Soome             ctx->type = type;
130*5ffb0c9bSToomas Soome             return ctx;
131*5ffb0c9bSToomas Soome         }
132*5ffb0c9bSToomas Soome         mDNSPlatformMemFree(ctx);
133*5ffb0c9bSToomas Soome     }
134*5ffb0c9bSToomas Soome     return mDNSNULL;
135*5ffb0c9bSToomas Soome }
136*5ffb0c9bSToomas Soome 
AlgDestroy(AlgContext * ctx)137*5ffb0c9bSToomas Soome mDNSexport mStatus AlgDestroy(AlgContext *ctx)
138*5ffb0c9bSToomas Soome {
139*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
140*5ffb0c9bSToomas Soome 
141*5ffb0c9bSToomas Soome     if (ctx->type == CRYPTO_ALG)
142*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[ctx->alg];
143*5ffb0c9bSToomas Soome     else if (ctx->type == DIGEST_ALG)
144*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[ctx->alg];
145*5ffb0c9bSToomas Soome     else if (ctx->type == ENC_ALG)
146*5ffb0c9bSToomas Soome         func = EncAlgFuncs[ctx->alg];
147*5ffb0c9bSToomas Soome 
148*5ffb0c9bSToomas Soome     if (!func)
149*5ffb0c9bSToomas Soome     {
150*5ffb0c9bSToomas Soome         LogMsg("AlgDestroy: ERROR!! func is NULL");
151*5ffb0c9bSToomas Soome         mDNSPlatformMemFree(ctx);
152*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
153*5ffb0c9bSToomas Soome     }
154*5ffb0c9bSToomas Soome 
155*5ffb0c9bSToomas Soome     if (func->Destroy)
156*5ffb0c9bSToomas Soome         func->Destroy(ctx);
157*5ffb0c9bSToomas Soome 
158*5ffb0c9bSToomas Soome     mDNSPlatformMemFree(ctx);
159*5ffb0c9bSToomas Soome     return mStatus_NoError;
160*5ffb0c9bSToomas Soome }
161*5ffb0c9bSToomas Soome 
AlgLength(AlgContext * ctx)162*5ffb0c9bSToomas Soome mDNSexport mDNSu32 AlgLength(AlgContext *ctx)
163*5ffb0c9bSToomas Soome {
164*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
165*5ffb0c9bSToomas Soome 
166*5ffb0c9bSToomas Soome     if (ctx->type == CRYPTO_ALG)
167*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[ctx->alg];
168*5ffb0c9bSToomas Soome     else if (ctx->type == DIGEST_ALG)
169*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[ctx->alg];
170*5ffb0c9bSToomas Soome     else if (ctx->type == ENC_ALG)
171*5ffb0c9bSToomas Soome         func = EncAlgFuncs[ctx->alg];
172*5ffb0c9bSToomas Soome 
173*5ffb0c9bSToomas Soome     // This should never happen as AlgCreate would have failed
174*5ffb0c9bSToomas Soome     if (!func)
175*5ffb0c9bSToomas Soome     {
176*5ffb0c9bSToomas Soome         LogMsg("AlgLength: ERROR!! func is NULL");
177*5ffb0c9bSToomas Soome         return 0;
178*5ffb0c9bSToomas Soome     }
179*5ffb0c9bSToomas Soome 
180*5ffb0c9bSToomas Soome     if (func->Length)
181*5ffb0c9bSToomas Soome         return (func->Length(ctx));
182*5ffb0c9bSToomas Soome     else
183*5ffb0c9bSToomas Soome         return 0;
184*5ffb0c9bSToomas Soome }
185*5ffb0c9bSToomas Soome 
AlgAdd(AlgContext * ctx,const void * data,mDNSu32 len)186*5ffb0c9bSToomas Soome mDNSexport mStatus AlgAdd(AlgContext *ctx, const void *data, mDNSu32 len)
187*5ffb0c9bSToomas Soome {
188*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
189*5ffb0c9bSToomas Soome 
190*5ffb0c9bSToomas Soome     if (ctx->type == CRYPTO_ALG)
191*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[ctx->alg];
192*5ffb0c9bSToomas Soome     else if (ctx->type == DIGEST_ALG)
193*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[ctx->alg];
194*5ffb0c9bSToomas Soome     else if (ctx->type == ENC_ALG)
195*5ffb0c9bSToomas Soome         func = EncAlgFuncs[ctx->alg];
196*5ffb0c9bSToomas Soome 
197*5ffb0c9bSToomas Soome     // This should never happen as AlgCreate would have failed
198*5ffb0c9bSToomas Soome     if (!func)
199*5ffb0c9bSToomas Soome     {
200*5ffb0c9bSToomas Soome         LogMsg("AlgAdd: ERROR!! func is NULL");
201*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
202*5ffb0c9bSToomas Soome     }
203*5ffb0c9bSToomas Soome 
204*5ffb0c9bSToomas Soome     if (func->Add)
205*5ffb0c9bSToomas Soome         return (func->Add(ctx, data, len));
206*5ffb0c9bSToomas Soome     else
207*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
208*5ffb0c9bSToomas Soome }
209*5ffb0c9bSToomas Soome 
AlgVerify(AlgContext * ctx,mDNSu8 * key,mDNSu32 keylen,mDNSu8 * signature,mDNSu32 siglen)210*5ffb0c9bSToomas Soome mDNSexport mStatus AlgVerify(AlgContext *ctx, mDNSu8 *key, mDNSu32 keylen, mDNSu8 *signature, mDNSu32 siglen)
211*5ffb0c9bSToomas Soome {
212*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
213*5ffb0c9bSToomas Soome 
214*5ffb0c9bSToomas Soome     if (ctx->type == CRYPTO_ALG)
215*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[ctx->alg];
216*5ffb0c9bSToomas Soome     else if (ctx->type == DIGEST_ALG)
217*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[ctx->alg];
218*5ffb0c9bSToomas Soome     else if (ctx->type == ENC_ALG)
219*5ffb0c9bSToomas Soome         func = EncAlgFuncs[ctx->alg];
220*5ffb0c9bSToomas Soome 
221*5ffb0c9bSToomas Soome     // This should never happen as AlgCreate would have failed
222*5ffb0c9bSToomas Soome     if (!func)
223*5ffb0c9bSToomas Soome     {
224*5ffb0c9bSToomas Soome         LogMsg("AlgVerify: ERROR!! func is NULL");
225*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
226*5ffb0c9bSToomas Soome     }
227*5ffb0c9bSToomas Soome 
228*5ffb0c9bSToomas Soome     if (func->Verify)
229*5ffb0c9bSToomas Soome         return (func->Verify(ctx, key, keylen, signature, siglen));
230*5ffb0c9bSToomas Soome     else
231*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
232*5ffb0c9bSToomas Soome }
233*5ffb0c9bSToomas Soome 
AlgEncode(AlgContext * ctx)234*5ffb0c9bSToomas Soome mDNSexport mDNSu8* AlgEncode(AlgContext *ctx)
235*5ffb0c9bSToomas Soome {
236*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
237*5ffb0c9bSToomas Soome 
238*5ffb0c9bSToomas Soome     if (ctx->type == CRYPTO_ALG)
239*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[ctx->alg];
240*5ffb0c9bSToomas Soome     else if (ctx->type == DIGEST_ALG)
241*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[ctx->alg];
242*5ffb0c9bSToomas Soome     else if (ctx->type == ENC_ALG)
243*5ffb0c9bSToomas Soome         func = EncAlgFuncs[ctx->alg];
244*5ffb0c9bSToomas Soome 
245*5ffb0c9bSToomas Soome     // This should never happen as AlgCreate would have failed
246*5ffb0c9bSToomas Soome     if (!func)
247*5ffb0c9bSToomas Soome     {
248*5ffb0c9bSToomas Soome         LogMsg("AlgEncode: ERROR!! func is NULL");
249*5ffb0c9bSToomas Soome         return mDNSNULL;
250*5ffb0c9bSToomas Soome     }
251*5ffb0c9bSToomas Soome 
252*5ffb0c9bSToomas Soome     if (func->Encode)
253*5ffb0c9bSToomas Soome         return (func->Encode(ctx));
254*5ffb0c9bSToomas Soome     else
255*5ffb0c9bSToomas Soome         return mDNSNULL;
256*5ffb0c9bSToomas Soome }
257*5ffb0c9bSToomas Soome 
AlgFinal(AlgContext * ctx,void * data,mDNSu32 len)258*5ffb0c9bSToomas Soome mDNSexport mStatus AlgFinal(AlgContext *ctx, void *data, mDNSu32 len)
259*5ffb0c9bSToomas Soome {
260*5ffb0c9bSToomas Soome     AlgFuncs *func = mDNSNULL;
261*5ffb0c9bSToomas Soome 
262*5ffb0c9bSToomas Soome     if (ctx->type == CRYPTO_ALG)
263*5ffb0c9bSToomas Soome         func = CryptoAlgFuncs[ctx->alg];
264*5ffb0c9bSToomas Soome     else if (ctx->type == DIGEST_ALG)
265*5ffb0c9bSToomas Soome         func = DigestAlgFuncs[ctx->alg];
266*5ffb0c9bSToomas Soome     else if (ctx->type == ENC_ALG)
267*5ffb0c9bSToomas Soome         func = EncAlgFuncs[ctx->alg];
268*5ffb0c9bSToomas Soome 
269*5ffb0c9bSToomas Soome     // This should never happen as AlgCreate would have failed
270*5ffb0c9bSToomas Soome     if (!func)
271*5ffb0c9bSToomas Soome     {
272*5ffb0c9bSToomas Soome         LogMsg("AlgEncode: ERROR!! func is NULL");
273*5ffb0c9bSToomas Soome         return mDNSNULL;
274*5ffb0c9bSToomas Soome     }
275*5ffb0c9bSToomas Soome 
276*5ffb0c9bSToomas Soome     if (func->Final)
277*5ffb0c9bSToomas Soome         return (func->Final(ctx, data, len));
278*5ffb0c9bSToomas Soome     else
279*5ffb0c9bSToomas Soome         return mStatus_BadParamErr;
280*5ffb0c9bSToomas Soome }
281