Exemplo n.º 1
0
int ccmode_gcm_test_one_vector_chained(const struct ccmode_gcm *gcm, const struct ccmode_gcm_vector *v, int dec)
{
    if (v->ptlen!=v->ctlen)
        return -1;

#ifdef USE_COMMONCRYPTO_GCM
    unsigned char temp[v->ptlen];
    unsigned char temptag[v->taglen];
    CCCryptorStatus st;
    size_t taglen = v->taglen;
    CCCryptorRef cryptor = NULL;
    st = CCCryptorCreateWithMode(dec ? kCCDecrypt : kCCEncrypt, 11 /* kCCModeGCM */, kCCAlgorithmAES128, 0, NULL, v->key, v->keylen, NULL, 0, 0, 0, &cryptor);
    st = CCCryptorGCMAddIV(cryptor, v->iv, v->ivlen);
    unsigned long i;
#if 0
    for (i = 0; i < v->adalen; ++i) {
        st = CCCryptorGCMAddADD(cryptor, &v->ada[i], 1);
    }
#else
    st = CCCryptorGCMAddADD(cryptor, v->ada, v->adalen);
#endif

#if 0
    for (i = 0; i < v->ptlen; ++i) {
        st = (dec ? CCCryptorGCMDecrypt(cryptor, &v->ct[i], 1, &temp[i])
              : CCCryptorGCMEncrypt(cryptor, &v->pt[i], 1, &temp[i]));
    }
#else
    st = (dec ? CCCryptorGCMDecrypt(cryptor, v->ct, v->ptlen, temp)
          : CCCryptorGCMEncrypt(cryptor, v->pt, v->ptlen, temp));
#endif
    st = CCCryptorGCMFinal(cryptor, temptag, &taglen);
    CCCryptorRelease(cryptor);

#ifdef _INTERNAL_DEBUG_
    int r1, r2;
    r1 = memcmp(dec ? v->pt : v->ct, temp, v->ptlen);
    r2 = memcmp(v->tag, temptag, v->taglen);
    if (r1 || r2)
    cc_printf("ivlen: %lu adalen: %lu nbytes: %lu taglen: %lu crypt: %d tag: %d\n",
           v->ivlen, v->adalen, v->ptlen, v->taglen, r1, r2);

    return r1 != 0 ? r1 : r2;
#else
    return memcmp(dec ? v->pt : v->ct, temp, v->ptlen) || memcmp(v->tag, temptag, v->taglen);
#endif

#else
    if (dec)
        return ccmode_gcm_test_one_chained(gcm, v->keylen, v->key, v->ivlen, v->iv, v->adalen, v->ada, v->ptlen, v->ct, v->pt, v->taglen, v->tag);
    else
        return ccmode_gcm_test_one_chained(gcm, v->keylen, v->key, v->ivlen, v->iv, v->adalen, v->ada, v->ptlen, v->pt, v->ct, v->taglen, v->tag);
#endif
}
Exemplo n.º 2
0
OSStatus    AES_GCM_Encrypt( AES_GCM_Context *inContext, const void *inSrc, size_t inLen, void *inDst )
{
    OSStatus        err;
    
    err = CCCryptorGCMEncrypt( inContext->cryptor, inSrc, inLen, inDst );
    require_noerr( err, exit );
    
exit:
    return( err );
}