1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
* Gaim-Encryption PSS signature routines, from PKCS#1 v2.1
*
* Copyright (C) 2003 William Tompkins
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "pk11func.h"
#include "keyhi.h"
/* for g_assert; PORT_Assert seems disabled... */
#include <glib.h>
#include <debug.h>
#include "nss_mgf1.h"
#include "nss_pss.h"
static const SECOidTag Hash_OID = SEC_OID_SHA1;
static const unsigned int hlen = 20; /* SHA1 hash length */
/* Generate a signature block (not including the msg) in the specified space */
/* salt_len is typically = hlen, or 0 */
int pss_generate_sig(unsigned char* sig, unsigned int sig_len,
const unsigned char* msg, unsigned int msg_len, int salt_len) {
/* see PKCS#1 v2.1 for a pretty picture. We construct the signature */
/* left to right, in a very straightforward way. */
/* Since the (variably sized) padding is on the left, we first figure */
/* out where everything is, going right to left. */
unsigned char* bc_pos = sig + sig_len - 1;
unsigned char* final_hash_pos = bc_pos - hlen;
unsigned char* salt_pos = final_hash_pos - salt_len;
int padding2_size = (salt_pos - sig);
unsigned char* m_prime;
SECStatus rv;
/* assuming a modulus size that is a multiple of 8 bits, PS must have at */
/* least one 0 starting off, plus the 1 that denotes the end of PS */
if (padding2_size <= 1) return 0;
/* Construct PS */
PORT_Memset(sig, 0, padding2_size - 1);
sig[padding2_size - 1] = 1;
/* Construct Salt */
rv = PK11_GenerateRandom(salt_pos, salt_len);
g_assert(rv == SECSuccess);
/* Construct M': */
/* If we were clever and had an easy way to incrementally hash things, */
/* we could avoid actually making M' and just use the pieces parts */
/* where they lie. Oh well. */
m_prime = PORT_Alloc(8 + hlen + salt_len);
g_assert(m_prime != 0);
/* Padding1 inside M' */
PORT_Memset(m_prime, 0, 8);
/* mHash inside M' */
rv = PK11_HashBuf(Hash_OID, m_prime + 8, (unsigned char*)msg, msg_len);
g_assert(rv == SECSuccess);
/* salt inside M' */
PORT_Memcpy(m_prime + 8 + hlen, salt_pos, salt_len);
/* Hash M' into final_hash_pos */
rv = PK11_HashBuf(Hash_OID, final_hash_pos, m_prime, 8 + hlen + salt_len);
g_assert(rv == SECSuccess);
PORT_Free(m_prime);
/* Why 0xbc? One of the great mysteries...*/
*bc_pos = 0xbc;
/* Almost done: mask everything before the hash with the hash */
mgf1(sig, final_hash_pos - sig, final_hash_pos, hlen);
/* Mask probably screwed up our starting zero byte, zero it */
sig[0] = 0;
return 1;
}
/* Destructively verify that the the signature block corresponds to */
/* the given message */
int pss_check_sig(unsigned char* sig, unsigned int sig_len,
const unsigned char* msg, unsigned int msg_len) {
unsigned char* bc_pos = sig + sig_len - 1;
unsigned char* final_hash_pos = bc_pos - hlen;
unsigned char* salt_pos;
int salt_len;
unsigned char* m_prime;
unsigned char* check_hash;
int hashcmp;
SECStatus rv;
if (sig[sig_len - 1] != 0xbc) {
gaim_debug(GAIM_DEBUG_ERROR, "gaim-encryption", "No 0xBC in sig\n");
return 0;
}
if (sig[0] != 0) {
gaim_debug(GAIM_DEBUG_ERROR, "gaim-encryption", "First byte of sig nonzero\n");
return 0;
}
mgf1(sig, final_hash_pos - sig, final_hash_pos, hlen);
/* Walk down the padding looking for the 01 that marks the salt */
salt_pos = sig+1;
while ((salt_pos < final_hash_pos) && (*salt_pos == 0)) {
++salt_pos;
}
if (salt_pos == final_hash_pos) {
gaim_debug(GAIM_DEBUG_ERROR, "gaim-encryption", "no 0x01 for salt\n");
return 0;
}
if (*salt_pos != 1) {
gaim_debug(GAIM_DEBUG_ERROR, "gaim-encryption", "no 0x01 for salt (2)\n");
return 0;
}
++salt_pos;
salt_len = final_hash_pos - salt_pos;
/* Construct M' using the salt we just regained */
m_prime = PORT_Alloc(8 + hlen + salt_len);
g_assert(m_prime != 0);
/* Padding1 inside M' */
PORT_Memset(m_prime, 0, 8);
/* mHash inside M' */
rv = PK11_HashBuf(Hash_OID, m_prime + 8, (unsigned char*)msg, msg_len);
g_assert(rv == SECSuccess);
/* salt inside M' */
PORT_Memcpy(m_prime + 8 + hlen, salt_pos, salt_len);
/* Hash M' into check_hash */
check_hash = PORT_Alloc(hlen);
g_assert(m_prime != 0);
rv = PK11_HashBuf(Hash_OID, check_hash, m_prime, 8 + hlen + salt_len);
g_assert(rv == SECSuccess);
PORT_Free(m_prime);
hashcmp = memcmp(check_hash, final_hash_pos, hlen);
PORT_Free(check_hash);
if (hashcmp != 0) {
gaim_debug(GAIM_DEBUG_ERROR, "gaim-encryption", "bad hash in sig\n");
return 0;
}
return 1;
}
void pss_test() {
int mod_size = 512/8;
unsigned char data[4096/8];
unsigned char sig[4096/8];
int data_size;
SECStatus rv;
/* overkill, but what the hey. */
while (mod_size <= 4096/8) {
rv = PK11_GenerateRandom(data, sizeof(data));
g_assert(rv == SECSuccess);
for (data_size = 0; data_size <= 1000; ++data_size) {
g_assert( pss_generate_sig(sig, mod_size, data, data_size, hlen) );
g_assert( pss_check_sig(sig, mod_size, data, data_size) );
g_assert( pss_generate_sig(sig, mod_size, data, data_size, 0) );
g_assert( pss_check_sig(sig, mod_size, data, data_size) );
}
mod_size *= 2;
}
}
|