comparison pkcs_1_pss_encode.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
18 unsigned long saltlen, prng_state *prng, 18 unsigned long saltlen, prng_state *prng,
19 int prng_idx, int hash_idx, 19 int prng_idx, int hash_idx,
20 unsigned long modulus_bitlen, 20 unsigned long modulus_bitlen,
21 unsigned char *out, unsigned long *outlen) 21 unsigned char *out, unsigned long *outlen)
22 { 22 {
23 unsigned char DB[1024], mask[sizeof(DB)], salt[sizeof(DB)], hash[sizeof(DB)]; 23 unsigned char *DB, *mask, *salt, *hash;
24 unsigned long x, y, hLen, modulus_len; 24 unsigned long x, y, hLen, modulus_len;
25 int err; 25 int err;
26 hash_state md; 26 hash_state md;
27 27
28 _ARGCHK(msghash != NULL); 28 _ARGCHK(msghash != NULL);
38 } 38 }
39 39
40 hLen = hash_descriptor[hash_idx].hashsize; 40 hLen = hash_descriptor[hash_idx].hashsize;
41 modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); 41 modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
42 42
43 /* allocate ram for DB/mask/salt/hash of size modulus_len */
44 DB = XMALLOC(modulus_len);
45 mask = XMALLOC(modulus_len);
46 salt = XMALLOC(modulus_len);
47 hash = XMALLOC(modulus_len);
48 if (DB == NULL || mask == NULL || salt == NULL || hash == NULL) {
49 if (DB != NULL) {
50 XFREE(DB);
51 }
52 if (mask != NULL) {
53 XFREE(mask);
54 }
55 if (salt != NULL) {
56 XFREE(salt);
57 }
58 if (hash != NULL) {
59 XFREE(hash);
60 }
61 return CRYPT_MEM;
62 }
63
64
43 /* check sizes */ 65 /* check sizes */
44 if ((saltlen > sizeof(salt)) || (modulus_len > sizeof(DB)) || (modulus_len < hLen + saltlen + 2)) { 66 if ((saltlen > modulus_len) || (modulus_len < hLen + saltlen + 2)) {
45 return CRYPT_INVALID_ARG; 67 err = CRYPT_INVALID_ARG;
68 goto __ERR;
46 } 69 }
47 70
48 /* generate random salt */ 71 /* generate random salt */
49 if (saltlen > 0) { 72 if (saltlen > 0) {
50 if (prng_descriptor[prng_idx].read(salt, saltlen, prng) != saltlen) { 73 if (prng_descriptor[prng_idx].read(salt, saltlen, prng) != saltlen) {
51 return CRYPT_ERROR_READPRNG; 74 err = CRYPT_ERROR_READPRNG;
75 goto __ERR;
52 } 76 }
53 } 77 }
54 78
55 /* M = (eight) 0x00 || msghash || salt, hash = H(M) */ 79 /* M = (eight) 0x00 || msghash || salt, hash = H(M) */
56 hash_descriptor[hash_idx].init(&md); 80 if ((err = hash_descriptor[hash_idx].init(&md)) != CRYPT_OK) {
81 goto __ERR;
82 }
57 zeromem(DB, 8); 83 zeromem(DB, 8);
58 if ((err = hash_descriptor[hash_idx].process(&md, DB, 8)) != CRYPT_OK) { 84 if ((err = hash_descriptor[hash_idx].process(&md, DB, 8)) != CRYPT_OK) {
59 return err; 85 goto __ERR;
60 } 86 }
61 if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) { 87 if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) {
62 return err; 88 goto __ERR;
63 } 89 }
64 if ((err = hash_descriptor[hash_idx].process(&md, salt, saltlen)) != CRYPT_OK) { 90 if ((err = hash_descriptor[hash_idx].process(&md, salt, saltlen)) != CRYPT_OK) {
65 return err; 91 goto __ERR;
66 } 92 }
67 if ((err = hash_descriptor[hash_idx].done(&md, hash)) != CRYPT_OK) { 93 if ((err = hash_descriptor[hash_idx].done(&md, hash)) != CRYPT_OK) {
68 return err; 94 goto __ERR;
69 } 95 }
70 96
71 /* generate DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */ 97 /* generate DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */
72 for (x = 0; x < (modulus_len - saltlen - hLen - 2); x++) { 98 for (x = 0; x < (modulus_len - saltlen - hLen - 2); x++) {
73 DB[x] = 0x00; 99 DB[x] = 0x00;
77 DB[x++] = salt[y]; 103 DB[x++] = salt[y];
78 } 104 }
79 105
80 /* generate mask of length modulus_len - hLen - 1 from hash */ 106 /* generate mask of length modulus_len - hLen - 1 from hash */
81 if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { 107 if ((err = pkcs_1_mgf1(hash, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
82 return err; 108 goto __ERR;
83 } 109 }
84 110
85 /* xor against DB */ 111 /* xor against DB */
86 for (y = 0; y < (modulus_len - hLen - 1); y++) { 112 for (y = 0; y < (modulus_len - hLen - 1); y++) {
87 DB[y] ^= mask[y]; 113 DB[y] ^= mask[y];
88 } 114 }
89 115
90 /* output is DB || hash || 0xBC */ 116 /* output is DB || hash || 0xBC */
91 if (*outlen < modulus_len) { 117 if (*outlen < modulus_len) {
92 return CRYPT_BUFFER_OVERFLOW; 118 err = CRYPT_BUFFER_OVERFLOW;
119 goto __ERR;
93 } 120 }
94 121
95 /* DB */ 122 /* DB */
96 for (y = x = 0; x < modulus_len - hLen - 1; x++) { 123 for (y = x = 0; x < modulus_len - hLen - 1; x++) {
97 out[y++] = DB[x]; 124 out[y++] = DB[x];
106 /* now clear the 8*modulus_len - modulus_bitlen most significant bits */ 133 /* now clear the 8*modulus_len - modulus_bitlen most significant bits */
107 out[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen-1)); 134 out[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen-1));
108 135
109 /* store output size */ 136 /* store output size */
110 *outlen = modulus_len; 137 *outlen = modulus_len;
111 138 err = CRYPT_OK;
139 __ERR:
112 #ifdef CLEAN_STACK 140 #ifdef CLEAN_STACK
113 zeromem(DB, sizeof(DB)); 141 zeromem(DB, modulus_len);
114 zeromem(mask, sizeof(mask)); 142 zeromem(mask, modulus_len);
115 zeromem(salt, sizeof(salt)); 143 zeromem(salt, modulus_len);
116 zeromem(hash, sizeof(hash)); 144 zeromem(hash, modulus_len);
117 #endif 145 #endif
118 146
119 return CRYPT_OK; 147 XFREE(hash);
148 XFREE(salt);
149 XFREE(mask);
150 XFREE(DB);
151
152 return err;
120 } 153 }
121 154
122 #endif /* PKCS_1 */ 155 #endif /* PKCS_1 */