]> pd.if.org Git - zpackage/blob - libtomcrypt/src/encauth/ocb3/ocb3_encrypt_authenticate_memory.c
efc1a8fb270838ca8a712a68fe436676c068abb1
[zpackage] / libtomcrypt / src / encauth / ocb3 / ocb3_encrypt_authenticate_memory.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 /**
11   @file ocb3_encrypt_authenticate_memory.c
12   OCB implementation, encrypt block of memory, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_OCB3_MODE
17
18 /**
19    Encrypt and generate an authentication code for a buffer of memory
20    @param cipher     The index of the cipher desired
21    @param key        The secret key
22    @param keylen     The length of the secret key (octets)
23    @param nonce      The session nonce (length of the block ciphers block size)
24    @param noncelen   The length of the nonce (octets)
25    @param adata      The AAD - additional associated data
26    @param adatalen   The length of AAD (octets)
27    @param pt         The plaintext
28    @param ptlen      The length of the plaintext (octets)
29    @param ct         [out] The ciphertext
30    @param tag        [out] The authentication tag
31    @param taglen     [in/out] The max size and resulting size of the authentication tag
32    @return CRYPT_OK if successful
33 */
34 int ocb3_encrypt_authenticate_memory(int cipher,
35     const unsigned char *key,    unsigned long keylen,
36     const unsigned char *nonce,  unsigned long noncelen,
37     const unsigned char *adata,  unsigned long adatalen,
38     const unsigned char *pt,     unsigned long ptlen,
39           unsigned char *ct,
40           unsigned char *tag,    unsigned long *taglen)
41 {
42    int err;
43    ocb3_state *ocb;
44
45    LTC_ARGCHK(taglen != NULL);
46
47    /* allocate memory */
48    ocb = XMALLOC(sizeof(ocb3_state));
49    if (ocb == NULL) {
50       return CRYPT_MEM;
51    }
52
53    if ((err = ocb3_init(ocb, cipher, key, keylen, nonce, noncelen, *taglen)) != CRYPT_OK) {
54       goto LBL_ERR;
55    }
56
57    if (adata != NULL || adatalen != 0) {
58       if ((err = ocb3_add_aad(ocb, adata, adatalen)) != CRYPT_OK) {
59          goto LBL_ERR;
60       }
61    }
62
63    if ((err = ocb3_encrypt_last(ocb, pt, ptlen, ct)) != CRYPT_OK) {
64       goto LBL_ERR;
65    }
66
67    err = ocb3_done(ocb, tag, taglen);
68
69 LBL_ERR:
70 #ifdef LTC_CLEAN_STACK
71    zeromem(ocb, sizeof(ocb3_state));
72 #endif
73
74    XFREE(ocb);
75    return err;
76 }
77
78 #endif
79
80 /* ref:         $Format:%D$ */
81 /* git commit:  $Format:%H$ */
82 /* commit time: $Format:%ai$ */