]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/pkcs1/pkcs_1_mgf1.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / pkcs1 / pkcs_1_mgf1.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 #include "tomcrypt.h"
10
11 /**
12   @file pkcs_1_mgf1.c
13   The Mask Generation Function (MGF1) for PKCS #1, Tom St Denis
14 */
15
16 #ifdef LTC_PKCS_1
17
18 /**
19    Perform PKCS #1 MGF1 (internal)
20    @param hash_idx    The index of the hash desired
21    @param seed        The seed for MGF1
22    @param seedlen     The length of the seed
23    @param mask        [out] The destination
24    @param masklen     The length of the mask desired
25    @return CRYPT_OK if successful
26 */
27 int pkcs_1_mgf1(int                  hash_idx,
28                 const unsigned char *seed, unsigned long seedlen,
29                       unsigned char *mask, unsigned long masklen)
30 {
31    unsigned long hLen, x;
32    ulong32       counter;
33    int           err;
34    hash_state    *md;
35    unsigned char *buf;
36
37    LTC_ARGCHK(seed != NULL);
38    LTC_ARGCHK(mask != NULL);
39
40    /* ensure valid hash */
41    if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
42       return err;
43    }
44
45    /* get hash output size */
46    hLen = hash_descriptor[hash_idx].hashsize;
47
48    /* allocate memory */
49    md  = XMALLOC(sizeof(hash_state));
50    buf = XMALLOC(hLen);
51    if (md == NULL || buf == NULL) {
52       if (md != NULL) {
53          XFREE(md);
54       }
55       if (buf != NULL) {
56          XFREE(buf);
57       }
58       return CRYPT_MEM;
59    }
60
61    /* start counter */
62    counter = 0;
63
64    while (masklen > 0) {
65        /* handle counter */
66        STORE32H(counter, buf);
67        ++counter;
68
69        /* get hash of seed || counter */
70        if ((err = hash_descriptor[hash_idx].init(md)) != CRYPT_OK) {
71           goto LBL_ERR;
72        }
73        if ((err = hash_descriptor[hash_idx].process(md, seed, seedlen)) != CRYPT_OK) {
74           goto LBL_ERR;
75        }
76        if ((err = hash_descriptor[hash_idx].process(md, buf, 4)) != CRYPT_OK) {
77           goto LBL_ERR;
78        }
79        if ((err = hash_descriptor[hash_idx].done(md, buf)) != CRYPT_OK) {
80           goto LBL_ERR;
81        }
82
83        /* store it */
84        for (x = 0; x < hLen && masklen > 0; x++, masklen--) {
85           *mask++ = buf[x];
86        }
87    }
88
89    err = CRYPT_OK;
90 LBL_ERR:
91 #ifdef LTC_CLEAN_STACK
92    zeromem(buf, hLen);
93    zeromem(md,  sizeof(hash_state));
94 #endif
95
96    XFREE(buf);
97    XFREE(md);
98
99    return err;
100 }
101
102 #endif /* LTC_PKCS_1 */
103
104 /* ref:         $Format:%D$ */
105 /* git commit:  $Format:%H$ */
106 /* commit time: $Format:%ai$ */