2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * The contents of this file are subject to the terms of the Common Development and
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Distribution License (the License). You may not use this file except in compliance with the
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * specific language governing permission and limitations under the License.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * When distributing Covered Software, include this CDDL Header Notice in each file and include
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Header, with the fields enclosed by brackets [] replaced by your own identifying
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * information: "Portions copyright [year] [name of copyright owner]".
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Copyright 2016 ForgeRock AS.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Portions Copyright 2014 Nathaniel McCallum, Red Hat
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell#import "FRAOathCode.h"
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni+ (NSString *)hmac:(CCHmacAlgorithm)algorithm codeLength:(uint8_t)codeLength key:(NSData *)key counter:(uint64_t) counter {
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni // Network byte order
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni counter = (((uint64_t) htonl(counter)) << 32) + htonl(counter >> 32);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni // Create digits divisor
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni // Create the HMAC
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni int length = [self getDigestLength:algorithm];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni CCHmac(algorithm, [key bytes], [key length], &counter, sizeof(counter), digest);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni uint32_t off = digest[sizeof(digest) - 1] & 0xf;
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni return [NSString stringWithFormat:[NSString stringWithFormat:@"%%0%hhulu", codeLength], binary];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni+ (NSString *)asString:(CCHmacAlgorithm)algorithm {
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni return @"md5";
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni return @"sha256";
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni return @"sha512";
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni return @"sha1";
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni+ (CCHmacAlgorithm)fromString:(NSString *)string {