SecPaddingConfigurations.c [plain text]
#define SecPaddingDomain CFSTR("com.apple.security.padding")
typedef enum {
SecPaddingErrorUnknownType = -1
} SecPaddingError;
#include "debugging.h"
#include "SecCFError.h"
#include "SecCFWrappers.h"
#include <Security/SecPaddingConfigurationsPriv.h>
#pragma mark Padding Helper Methods
static uint64_t nextPowerOfTwo(uint64_t v)
{
if (v > (UINT64_MAX>>1)) {
secerror("Overflowing uint64_t by requesting nextPowerOfTwo of: %llx", v);
assert(0);
}
if (v & (v - 1)) {
return ((uint64_t)1 << ((int)sizeof(v)*8 - __builtin_clzll(v)));
} else {
return v;
}
}
static uint64_t nextMultiple(uint64_t v,uint64_t n)
{
if (n == 0) {
return 0;
}
if (n <= 0 || v > (UINT64_MAX-n)) {
secerror("Overflowing uint64_t by requesting nextMutiple with parameters v: %llx and n: %llx", v, n);
assert(0);
}
return n*((v+n-1)/n);
}
#pragma mark Padding Configurations
int64_t SecPaddingCompute(SecPaddingType type, uint32_t size, CFErrorRef *error) {
if (type != SecPaddingTypeMMCS) {
if (error) {
*error = CFErrorCreate(CFAllocatorGetDefault(), SecPaddingDomain, SecPaddingErrorUnknownType, NULL);
}
return SecPaddingErrorUnknownType;
}
int64_t paddedSize = 0;
if (size <= 64){
paddedSize = 64;
} else if (size <= 1024) {
paddedSize = nextPowerOfTwo(size);
} else if (size <= 32000) {
paddedSize = nextMultiple(size, 1024);
} else {
paddedSize = nextMultiple(size, 8192);
}
assert(paddedSize >= size);
return paddedSize - size;
}