]>
git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSCore/CryptoAlg.c
2 * Copyright (c) 2011-2019 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // ***************************************************************************
19 // Interface to DNSSEC cryptographic algorithms. The crypto support itself is
20 // provided by the platform and the functions in this file just provide an
21 // interface to access them in a more generic way.
22 // ***************************************************************************
24 #include "mDNSEmbeddedAPI.h"
25 #include "CryptoAlg.h"
27 AlgFuncs
*DigestAlgFuncs
[DIGEST_TYPE_MAX
];
28 AlgFuncs
*CryptoAlgFuncs
[CRYPTO_ALG_MAX
];
29 AlgFuncs
*EncAlgFuncs
[ENC_ALG_MAX
];
31 mDNSexport mStatus
DigestAlgInit(mDNSu8 digestType
, AlgFuncs
*func
)
33 if (digestType
>= DIGEST_TYPE_MAX
)
35 LogMsg("DigestAlgInit: digestType %d exceeds bounds", digestType
);
36 return mStatus_BadParamErr
;
38 // As digestTypes may not be consecutive, check for specific digest types
40 if (digestType
!= SHA1_DIGEST_TYPE
&&
41 digestType
!= SHA256_DIGEST_TYPE
)
43 LogMsg("DigestAlgInit: digestType %d not supported", digestType
);
44 return mStatus_BadParamErr
;
46 DigestAlgFuncs
[digestType
] = func
;
47 return mStatus_NoError
;
50 mDNSexport mStatus
CryptoAlgInit(mDNSu8 alg
, AlgFuncs
*func
)
52 if (alg
>= CRYPTO_ALG_MAX
)
54 LogMsg("CryptoAlgInit: alg %d exceeds bounds", alg
);
55 return mStatus_BadParamErr
;
57 // As algs may not be consecutive, check for specific algorithms
59 if (alg
!= CRYPTO_RSA_SHA1
&& alg
!= CRYPTO_RSA_SHA256
&& alg
!= CRYPTO_RSA_SHA512
&&
60 alg
!= CRYPTO_DSA_NSEC3_SHA1
&& alg
!= CRYPTO_RSA_NSEC3_SHA1
)
62 LogMsg("CryptoAlgInit: alg %d not supported", alg
);
63 return mStatus_BadParamErr
;
66 CryptoAlgFuncs
[alg
] = func
;
67 return mStatus_NoError
;
70 mDNSexport mStatus
EncAlgInit(mDNSu8 alg
, AlgFuncs
*func
)
72 if (alg
>= ENC_ALG_MAX
)
74 LogMsg("EncAlgInit: alg %d exceeds bounds", alg
);
75 return mStatus_BadParamErr
;
78 // As algs may not be consecutive, check for specific algorithms
80 if (alg
!= ENC_BASE32
&& alg
!= ENC_BASE64
)
82 LogMsg("EncAlgInit: alg %d not supported", alg
);
83 return mStatus_BadParamErr
;
86 EncAlgFuncs
[alg
] = func
;
87 return mStatus_NoError
;
90 mDNSexport AlgContext
*AlgCreate(AlgType type
, mDNSu8 alg
)
92 AlgFuncs
*func
= mDNSNULL
;
95 if (type
== CRYPTO_ALG
)
97 if (alg
>= CRYPTO_ALG_MAX
) return mDNSNULL
;
98 func
= CryptoAlgFuncs
[alg
];
100 else if (type
== DIGEST_ALG
)
102 if (alg
>= DIGEST_TYPE_MAX
) return mDNSNULL
;
103 func
= DigestAlgFuncs
[alg
];
105 else if (type
== ENC_ALG
)
107 if (alg
>= ENC_ALG_MAX
) return mDNSNULL
;
108 func
= EncAlgFuncs
[alg
];
113 // If there is no support from the platform, this case can happen.
114 LogInfo("AlgCreate: func is NULL");
121 ctx
= (AlgContext
*) mDNSPlatformMemAllocateClear(sizeof(*ctx
));
122 if (!ctx
) return mDNSNULL
;
123 // Create expects ctx->alg to be initialized
125 err
= func
->Create(ctx
);
126 if (err
== mStatus_NoError
)
131 mDNSPlatformMemFree(ctx
);
136 mDNSexport mStatus
AlgDestroy(AlgContext
*ctx
)
138 AlgFuncs
*func
= mDNSNULL
;
140 if (ctx
->type
== CRYPTO_ALG
)
141 func
= CryptoAlgFuncs
[ctx
->alg
];
142 else if (ctx
->type
== DIGEST_ALG
)
143 func
= DigestAlgFuncs
[ctx
->alg
];
144 else if (ctx
->type
== ENC_ALG
)
145 func
= EncAlgFuncs
[ctx
->alg
];
149 LogMsg("AlgDestroy: ERROR!! func is NULL");
150 mDNSPlatformMemFree(ctx
);
151 return mStatus_BadParamErr
;
157 mDNSPlatformMemFree(ctx
);
158 return mStatus_NoError
;
161 mDNSexport mDNSu32
AlgLength(AlgContext
*ctx
)
163 AlgFuncs
*func
= mDNSNULL
;
165 if (ctx
->type
== CRYPTO_ALG
)
166 func
= CryptoAlgFuncs
[ctx
->alg
];
167 else if (ctx
->type
== DIGEST_ALG
)
168 func
= DigestAlgFuncs
[ctx
->alg
];
169 else if (ctx
->type
== ENC_ALG
)
170 func
= EncAlgFuncs
[ctx
->alg
];
172 // This should never happen as AlgCreate would have failed
175 LogMsg("AlgLength: ERROR!! func is NULL");
180 return (func
->Length(ctx
));
185 mDNSexport mStatus
AlgAdd(AlgContext
*ctx
, const void *data
, mDNSu32 len
)
187 AlgFuncs
*func
= mDNSNULL
;
189 if (ctx
->type
== CRYPTO_ALG
)
190 func
= CryptoAlgFuncs
[ctx
->alg
];
191 else if (ctx
->type
== DIGEST_ALG
)
192 func
= DigestAlgFuncs
[ctx
->alg
];
193 else if (ctx
->type
== ENC_ALG
)
194 func
= EncAlgFuncs
[ctx
->alg
];
196 // This should never happen as AlgCreate would have failed
199 LogMsg("AlgAdd: ERROR!! func is NULL");
200 return mStatus_BadParamErr
;
204 return (func
->Add(ctx
, data
, len
));
206 return mStatus_BadParamErr
;
209 mDNSexport mStatus
AlgVerify(AlgContext
*ctx
, mDNSu8
*key
, mDNSu32 keylen
, mDNSu8
*signature
, mDNSu32 siglen
)
211 AlgFuncs
*func
= mDNSNULL
;
213 if (ctx
->type
== CRYPTO_ALG
)
214 func
= CryptoAlgFuncs
[ctx
->alg
];
215 else if (ctx
->type
== DIGEST_ALG
)
216 func
= DigestAlgFuncs
[ctx
->alg
];
217 else if (ctx
->type
== ENC_ALG
)
218 func
= EncAlgFuncs
[ctx
->alg
];
220 // This should never happen as AlgCreate would have failed
223 LogMsg("AlgVerify: ERROR!! func is NULL");
224 return mStatus_BadParamErr
;
228 return (func
->Verify(ctx
, key
, keylen
, signature
, siglen
));
230 return mStatus_BadParamErr
;
233 mDNSexport mDNSu8
* AlgEncode(AlgContext
*ctx
)
235 AlgFuncs
*func
= mDNSNULL
;
237 if (ctx
->type
== CRYPTO_ALG
)
238 func
= CryptoAlgFuncs
[ctx
->alg
];
239 else if (ctx
->type
== DIGEST_ALG
)
240 func
= DigestAlgFuncs
[ctx
->alg
];
241 else if (ctx
->type
== ENC_ALG
)
242 func
= EncAlgFuncs
[ctx
->alg
];
244 // This should never happen as AlgCreate would have failed
247 LogMsg("AlgEncode: ERROR!! func is NULL");
252 return (func
->Encode(ctx
));
257 mDNSexport mStatus
AlgFinal(AlgContext
*ctx
, void *data
, mDNSu32 len
)
259 AlgFuncs
*func
= mDNSNULL
;
261 if (ctx
->type
== CRYPTO_ALG
)
262 func
= CryptoAlgFuncs
[ctx
->alg
];
263 else if (ctx
->type
== DIGEST_ALG
)
264 func
= DigestAlgFuncs
[ctx
->alg
];
265 else if (ctx
->type
== ENC_ALG
)
266 func
= EncAlgFuncs
[ctx
->alg
];
268 // This should never happen as AlgCreate would have failed
271 LogMsg("AlgEncode: ERROR!! func is NULL");
276 return (func
->Final(ctx
, data
, len
));
278 return mStatus_BadParamErr
;