3 * Copyright (c) 2019 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * DNS SIG(0) hashature generation for DNSSD SRP using mbedtls.
19 * Functions required for loading, saving, and generating public/private keypairs, extracting the public key
20 * into KEY RR data, and computing hashatures.
24 #include <arpa/inet.h>
33 #define SRP_CRYPTO_MBEDTLS_INTERNAL
34 #include "srp-crypto.h"
36 // Function to generate a signature given some data and a private key
38 srp_hmac_iov(hmac_key_t
*key
, uint8_t *output
, size_t max
, struct iovec
*iov
, int count
)
42 mbedtls_md_context_t ctx
;
43 const mbedtls_md_info_t
*md_type
;
46 #define KABLOOIE line = __LINE__ - 1; goto kablooie
48 switch(key
->algorithm
) {
49 case SRP_HMAC_TYPE_SHA256
:
50 md_type
= mbedtls_md_info_from_type(MBEDTLS_MD_SHA256
);
51 if (md_type
== NULL
) {
52 ERROR("srp_hmac_iov: HMAC_SHA256 support missing");
55 digest_size
= mbedtls_md_get_size(md_type
);
58 ERROR("srp_hmac_iov: unsupported HMAC hash algorithm: %d", key
->algorithm
);
61 if (max
< digest_size
) {
62 ERROR("srp_hmac_iov: not enough space in output buffer (%lu) for hash (%d).",
63 (unsigned long)max
, digest_size
);
67 if ((status
= mbedtls_md_setup(&ctx
, md_type
, 1)) != 0) {
70 mbedtls_strerror(status
, errbuf
, sizeof errbuf
);
71 ERROR("srp_hmac_iov failed at hmac-mbedtls.c line %d: " PUB_S_SRP
, line
, errbuf
);
74 if ((status
= mbedtls_md_hmac_starts(&ctx
, key
->secret
, key
->length
)) != 0) {
77 for (i
= 0; i
< count
; i
++) {
78 if ((status
= mbedtls_md_hmac_update(&ctx
, iov
[i
].iov_base
, iov
[i
].iov_len
)) != 0) {
82 if ((status
= mbedtls_md_hmac_finish(&ctx
, output
)) != 0) {
88 srp_base64_parse(char *src
, size_t *len_ret
, uint8_t *buf
, size_t buflen
)
90 size_t slen
= strlen(src
);
91 int ret
= mbedtls_base64_decode(buf
, buflen
, len_ret
, (const unsigned char *)src
, slen
);
92 if (ret
== MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL
) {
94 } else if (ret
== MBEDTLS_ERR_BASE64_INVALID_CHARACTER
) {
105 // c-file-style: "bsd"
108 // indent-tabs-mode: nil