int mainGmac() { shared_ptr<PrgFromOpenSSLAES> random = make_shared<PrgFromOpenSSLAES>(); auto keyPrg = random->generateKey(128); random->setKey(keyPrg); string input_msg = "a simple message to gmac"; const char *cstr = input_msg.c_str(); int len = input_msg.size(); vector<byte> msg(cstr, cstr + len); OpenSSLGMAC gmac(random); auto key = gmac.generateKey(128); gmac.setMacKey(key); auto tag = gmac.mac(msg, 0, len); //gmac.update(msg,0,len); gmac.doFinal(msg, 0, len,tag); auto ver = gmac.verify(msg,0,len,tag); cout<<"output of verification is "<< ver<<endl; return 0; }
static int gcm_aes_nx_crypt(struct aead_request *req, int enc, unsigned int assoclen) { struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); struct nx_gcm_rctx *rctx = aead_request_ctx(req); struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; struct blkcipher_desc desc; unsigned int nbytes = req->cryptlen; unsigned int processed = 0, to_process; unsigned long irq_flags; int rc = -EINVAL; spin_lock_irqsave(&nx_ctx->lock, irq_flags); desc.info = rctx->iv; /* initialize the counter */ *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1; if (nbytes == 0) { if (assoclen == 0) rc = gcm_empty(req, &desc, enc); else rc = gmac(req, &desc, assoclen); if (rc) goto out; else goto mac; } /* Process associated data */ csbcpb->cpb.aes_gcm.bit_length_aad = assoclen * 8; if (assoclen) { rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad, assoclen); if (rc) goto out; } /* Set flags for encryption */ NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION; if (enc) { NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT; } else { NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT; nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req)); } do { to_process = nbytes - processed; csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8; rc = nx_build_sg_lists(nx_ctx, &desc, req->dst, req->src, &to_process, processed + req->assoclen, csbcpb->cpb.aes_gcm.iv_or_cnt); if (rc) goto out; if ((to_process + processed) < nbytes) NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; else NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); if (rc) goto out; memcpy(desc.info, csbcpb->cpb.aes_gcm.out_cnt, AES_BLOCK_SIZE); memcpy(csbcpb->cpb.aes_gcm.in_pat_or_aad, csbcpb->cpb.aes_gcm.out_pat_or_mac, AES_BLOCK_SIZE); memcpy(csbcpb->cpb.aes_gcm.in_s0, csbcpb->cpb.aes_gcm.out_s0, AES_BLOCK_SIZE); NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; atomic_inc(&(nx_ctx->stats->aes_ops)); atomic64_add(csbcpb->csb.processed_byte_count, &(nx_ctx->stats->aes_bytes)); processed += to_process; } while (processed < nbytes); mac: if (enc) { /* copy out the auth tag */ scatterwalk_map_and_copy( csbcpb->cpb.aes_gcm.out_pat_or_mac, req->dst, req->assoclen + nbytes, crypto_aead_authsize(crypto_aead_reqtfm(req)), SCATTERWALK_TO_SG); } else { u8 *itag = nx_ctx->priv.gcm.iauth_tag; u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac; scatterwalk_map_and_copy( itag, req->src, req->assoclen + nbytes, crypto_aead_authsize(crypto_aead_reqtfm(req)), SCATTERWALK_FROM_SG); rc = memcmp(itag, otag, crypto_aead_authsize(crypto_aead_reqtfm(req))) ? -EBADMSG : 0; } out: spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); return rc; }