void RSAKey::PublicBlob( CString &out ) const { int elen, mlen, bloblen; int i; unsigned char *blob, *p; elen = (bignum_bitcount(this->exponent) + 8) / 8; mlen = (bignum_bitcount(this->modulus) + 8) / 8; /* * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen. * (three length fields, 12+7=19). */ bloblen = 19 + elen + mlen; blob = new unsigned char[bloblen]; p = blob; PUT_32BIT(p, 7); p += 4; memcpy(p, "ssh-rsa", 7); p += 7; PUT_32BIT(p, elen); p += 4; for (i = elen; i--;) *p++ = bignum_byte(this->exponent, i); PUT_32BIT(p, mlen); p += 4; for (i = mlen; i--;) *p++ = bignum_byte(this->modulus, i); ASSERT(p == blob + bloblen); out = CString( (const char *) blob, bloblen ); }
static unsigned char *rsa2_public_blob(void *key, int *len) { struct RSAKey *rsa = (struct RSAKey *) key; int elen, mlen, bloblen; int i; unsigned char *blob, *p; elen = (bignum_bitcount(rsa->exponent) + 8) / 8; mlen = (bignum_bitcount(rsa->modulus) + 8) / 8; /* * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen. * (three length fields, 12+7=19). */ bloblen = 19 + elen + mlen; blob = snewn(bloblen, unsigned char); p = blob; PUT_32BIT(p, 7); p += 4; memcpy(p, "ssh-rsa", 7); p += 7; PUT_32BIT(p, elen); p += 4; for (i = elen; i--;) *p++ = bignum_byte(rsa->exponent, i); PUT_32BIT(p, mlen); p += 4; for (i = mlen; i--;) *p++ = bignum_byte(rsa->modulus, i); assert(p == blob + bloblen); *len = bloblen; return blob; }
void rsastr_fmt(char *str, struct RSAKey *key) { Bignum md, ex; int len = 0, i, nibbles; static const char hex[] = "0123456789abcdef"; md = key->modulus; ex = key->exponent; len += sprintf(str + len, "0x"); nibbles = (3 + bignum_bitcount(ex)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF]; len += sprintf(str + len, ",0x"); nibbles = (3 + bignum_bitcount(md)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]; str[len] = '\0'; }
int RSAKey::StrLen() const { Bignum md, ex; int mdlen, exlen; md = this->modulus; ex = this->exponent; mdlen = (bignum_bitcount(md) + 15) / 16; exlen = (bignum_bitcount(ex) + 15) / 16; return 4 * (mdlen + exlen) + 20; }
int rsastr_len(struct RSAKey *key) { Bignum md, ex; int mdlen, exlen; md = key->modulus; ex = key->exponent; mdlen = (bignum_bitcount(md) + 15) / 16; exlen = (bignum_bitcount(ex) + 15) / 16; return 4 * (mdlen + exlen) + 20; }
static char *dss_fingerprint(void *key) { struct dss_key *dss = (struct dss_key *) key; struct MD5Context md5c; unsigned char digest[16], lenbuf[4]; char buffer[16 * 3 + 40]; size_t pos; int numlen, i; MD5Init(&md5c); MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11); #define ADD_BIGNUM(bignum) \ numlen = (bignum_bitcount(bignum)+8)/8; \ PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ for (i = numlen; i-- ;) { \ unsigned char c = bignum_byte(bignum, i); \ MD5Update(&md5c, &c, 1); \ } ADD_BIGNUM(dss->p); ADD_BIGNUM(dss->q); ADD_BIGNUM(dss->g); ADD_BIGNUM(dss->y); #undef ADD_BIGNUM MD5Final(digest, &md5c); pos = szprintf(buffer, sizeof(buffer), "ssh-dss %d ", bignum_bitcount(dss->p)); for (i = 0; i < 16; i++) pos += szprintf(buffer + pos, sizeof(buffer) - pos, "%s%02x", i ? ":" : "", digest[i]); return dupstr(buffer); }
/* * Generate a fingerprint string for the key. Compatible with the * OpenSSH fingerprint code. */ void rsa_fingerprint(char *str, int len, struct RSAKey *key) { struct MD5Context md5c; unsigned char digest[16]; char buffer[16 * 3 + 40]; int numlen, slen, i; MD5Init(&md5c); numlen = ssh1_bignum_length(key->modulus) - 2; for (i = numlen; i--;) { unsigned char c = bignum_byte(key->modulus, i); MD5Update(&md5c, &c, 1); } numlen = ssh1_bignum_length(key->exponent) - 2; for (i = numlen; i--;) { unsigned char c = bignum_byte(key->exponent, i); MD5Update(&md5c, &c, 1); } MD5Final(digest, &md5c); sprintf(buffer, "%d ", bignum_bitcount(key->modulus)); for (i = 0; i < 16; i++) sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", digest[i]); strncpy(str, buffer, len); str[len - 1] = '\0'; slen = strlen(str); if (key->comment && slen < len - 1) { str[slen] = ' '; strncpy(str + slen + 1, key->comment, len - slen - 1); str[len - 1] = '\0'; } }
void RSAKey::Sign( const CString &data, CString &out ) const { Bignum in; { unsigned char hash[20]; SHA_Simple(data.data(), data.size(), hash); int nbytes = (bignum_bitcount(this->modulus) - 1) / 8; unsigned char *bytes = new unsigned char[nbytes]; memset( bytes, 0xFF, nbytes ); bytes[0] = 1; memcpy( bytes + nbytes - 20, hash, 20 ); in = bignum_from_bytes(bytes, nbytes); delete [] bytes; } Bignum outnum = rsa_privkey_op(in, this); delete [] in; int siglen; unsigned char *bytes = bignum_to_bytes( outnum, &siglen ); delete [] outnum; out = CString( (const char *) bytes, siglen ); delete [] bytes; }
static char *rsa2_fingerprint(void *key) { struct RSAKey *rsa = (struct RSAKey *) key; struct MD5Context md5c; unsigned char digest[16], lenbuf[4]; char buffer[16 * 3 + 40]; char *ret; int numlen, i; MD5Init(&md5c); MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11); #define ADD_BIGNUM(bignum) \ numlen = (bignum_bitcount(bignum)+8)/8; \ PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ for (i = numlen; i-- ;) { \ unsigned char c = bignum_byte(bignum, i); \ MD5Update(&md5c, &c, 1); \ } ADD_BIGNUM(rsa->exponent); ADD_BIGNUM(rsa->modulus); #undef ADD_BIGNUM MD5Final(digest, &md5c); sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus)); for (i = 0; i < 16; i++) sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", digest[i]); ret = snewn(strlen(buffer) + 1, char); if (ret) strcpy(ret, buffer); return ret; }
/* * DH stage 1: invent a number x between 1 and q, and compute e = * g^x mod p. Return e. * * If `nbits' is greater than zero, it is used as an upper limit * for the number of bits in x. This is safe provided that (a) you * use twice as many bits in x as the number of bits you expect to * use in your session key, and (b) the DH group is a safe prime * (which SSH demands that it must be). * * P. C. van Oorschot, M. J. Wiener * "On Diffie-Hellman Key Agreement with Short Exponents". * Advances in Cryptology: Proceedings of Eurocrypt '96 * Springer-Verlag, May 1996. */ Bignum dh_create_e(void *handle, int nbits) { struct dh_ctx *ctx = (struct dh_ctx *)handle; int i; int nbytes; unsigned char *buf; nbytes = (bignum_bitcount(ctx->qmask) + 7) / 8; buf = snewn(nbytes, unsigned char); do { /* * Create a potential x, by ANDing a string of random bytes * with qmask. */ if (ctx->x) freebn(ctx->x); if (nbits == 0 || nbits > bignum_bitcount(ctx->qmask)) { for (i = 0; i < nbytes; i++) buf[i] = bignum_byte(ctx->qmask, i) & random_byte(); ctx->x = bignum_from_bytes(buf, nbytes); } else { int b, nb; ctx->x = bn_power_2(nbits); b = nb = 0; for (i = 0; i < nbits; i++) { if (nb == 0) { nb = 8; b = random_byte(); } bignum_set_bit(ctx->x, i, b & 1); b >>= 1; nb--; } } } while (bignum_cmp(ctx->x, One) <= 0 || bignum_cmp(ctx->x, ctx->q) >= 0); sfree(buf); /* * Done. Now compute e = g^x mod p. */ ctx->e = modpow(ctx->g, ctx->x, ctx->p); return ctx->e; }
/* static struct RSAKey *rsa2_openssh_createkey(unsigned char **blob, int *len) { const char **b = (const char **) blob; struct RSAKey *rsa = new RSAKey; rsa->comment = NULL; rsa->modulus = getmp(b, len); rsa->exponent = getmp(b, len); rsa->private_exponent = getmp(b, len); rsa->iqmp = getmp(b, len); rsa->p = getmp(b, len); rsa->q = getmp(b, len); if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent || !rsa->iqmp || !rsa->p || !rsa->q) { delete rsa; return NULL; } return rsa; } static int rsa2_openssh_fmtkey( struct RSAKey *rsa, unsigned char *blob, int len ) { int bloblen = ssh2_bignum_length(rsa->modulus) + ssh2_bignum_length(rsa->exponent) + ssh2_bignum_length(rsa->private_exponent) + ssh2_bignum_length(rsa->iqmp) + ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q); if (bloblen > len) return bloblen; bloblen = 0; #define ENC(x) \ PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \ for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i); int i; ENC(rsa->modulus); ENC(rsa->exponent); ENC(rsa->private_exponent); ENC(rsa->iqmp); ENC(rsa->p); ENC(rsa->q); return bloblen; } */ int rsa2_pubkey_bits( const CString &blob ) { RSAKey rsa; if( !rsa.LoadFromPublicBlob( blob ) ) return 0; return bignum_bitcount(rsa.modulus); }
static unsigned char *dss_public_blob(void *key, int *len) { struct dss_key *dss = (struct dss_key *) key; int plen, qlen, glen, ylen, bloblen; int i; unsigned char *blob, *p; plen = (bignum_bitcount(dss->p) + 8) / 8; qlen = (bignum_bitcount(dss->q) + 8) / 8; glen = (bignum_bitcount(dss->g) + 8) / 8; ylen = (bignum_bitcount(dss->y) + 8) / 8; /* * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total * 27 + sum of lengths. (five length fields, 20+7=27). */ bloblen = 27 + plen + qlen + glen + ylen; blob = snewn(bloblen, unsigned char); p = blob; PUT_32BIT(p, 7); p += 4; memcpy(p, "ssh-dss", 7); p += 7; PUT_32BIT(p, plen); p += 4; for (i = plen; i--;) *p++ = bignum_byte(dss->p, i); PUT_32BIT(p, qlen); p += 4; for (i = qlen; i--;) *p++ = bignum_byte(dss->q, i); PUT_32BIT(p, glen); p += 4; for (i = glen; i--;) *p++ = bignum_byte(dss->g, i); PUT_32BIT(p, ylen); p += 4; for (i = ylen; i--;) *p++ = bignum_byte(dss->y, i); assert(p == blob + bloblen); *len = bloblen; return blob; }
static char *dss_fmtkey(void *key) { dss_key *dss = ( dss_key *) key; char *p; int len, i, pos, nibbles; static const char hex[] = "0123456789abcdef"; if (!dss->p) return NULL; len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */ len += 4 * (bignum_bitcount(dss->p) + 15) / 16; len += 4 * (bignum_bitcount(dss->q) + 15) / 16; len += 4 * (bignum_bitcount(dss->g) + 15) / 16; len += 4 * (bignum_bitcount(dss->y) + 15) / 16; p = snewn(len, char); if (!p) return NULL; pos = 0; pos += sprintf(p + pos, "0x"); nibbles = (3 + bignum_bitcount(dss->p)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) p[pos++] = hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF]; pos += sprintf(p + pos, ",0x"); nibbles = (3 + bignum_bitcount(dss->q)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) p[pos++] = hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF]; pos += sprintf(p + pos, ",0x"); nibbles = (3 + bignum_bitcount(dss->g)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) p[pos++] = hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF]; pos += sprintf(p + pos, ",0x"); nibbles = (3 + bignum_bitcount(dss->y)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) p[pos++] = hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF]; p[pos] = '\0'; return p; }
static unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) { struct RSAKey *rsa = (struct RSAKey *) key; unsigned char *bytes; int nbytes; unsigned char hash[20]; Bignum in, out; int i, j; SHA_Simple(data, datalen, hash); nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8; assert(1 <= nbytes - 20 - ASN1_LEN); bytes = snewn(nbytes, unsigned char); bytes[0] = 1; for (i = 1; i < nbytes - 20 - ASN1_LEN; i++) bytes[i] = 0xFF; for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++) bytes[i] = asn1_weird_stuff[j]; for (i = nbytes - 20, j = 0; i < nbytes; i++, j++) bytes[i] = hash[j]; in = bignum_from_bytes(bytes, nbytes); sfree(bytes); out = rsa_privkey_op(in, rsa); freebn(in); nbytes = (bignum_bitcount(out) + 7) / 8; bytes = snewn(4 + 7 + 4 + nbytes, unsigned char); PUT_32BIT(bytes, 7); memcpy(bytes + 4, "ssh-rsa", 7); PUT_32BIT(bytes + 4 + 7, nbytes); for (i = 0; i < nbytes; i++) bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i); freebn(out); *siglen = 4 + 7 + 4 + nbytes; return bytes; }
static int rsa2_pubkey_bits(void *blob, int len) { struct RSAKey *rsa; int ret; rsa = rsa2_newkey((char *) blob, len); ret = bignum_bitcount(rsa->modulus); rsa2_freekey(rsa); return ret; }
static int dss_pubkey_bits(void *blob, int len) { struct dss_key *dss; int ret; dss = dss_newkey((char *) blob, len); ret = bignum_bitcount(dss->p); dss_freekey(dss); return ret; }
static void sha512_mpint(SHA512_State * s, Bignum b) { unsigned char lenbuf[4]; int len; len = (bignum_bitcount(b) + 8) / 8; PUT_32BIT(lenbuf, len); SHA512_Bytes(s, lenbuf, 4); while (len-- > 0) { lenbuf[0] = bignum_byte(b, len); SHA512_Bytes(s, lenbuf, 1); } memset(lenbuf, 0, sizeof(lenbuf)); }
static unsigned char *rsa2_private_blob(void *key, int *len) { struct RSAKey *rsa = (struct RSAKey *) key; int dlen, plen, qlen, ulen, bloblen; int i; unsigned char *blob, *p; dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8; plen = (bignum_bitcount(rsa->p) + 8) / 8; qlen = (bignum_bitcount(rsa->q) + 8) / 8; ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8; /* * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 + * sum of lengths. */ bloblen = 16 + dlen + plen + qlen + ulen; blob = snewn(bloblen, unsigned char); p = blob; PUT_32BIT(p, dlen); p += 4; for (i = dlen; i--;) *p++ = bignum_byte(rsa->private_exponent, i); PUT_32BIT(p, plen); p += 4; for (i = plen; i--;) *p++ = bignum_byte(rsa->p, i); PUT_32BIT(p, qlen); p += 4; for (i = qlen; i--;) *p++ = bignum_byte(rsa->q, i); PUT_32BIT(p, ulen); p += 4; for (i = ulen; i--;) *p++ = bignum_byte(rsa->iqmp, i); assert(p == blob + bloblen); *len = bloblen; return blob; }
void RSAKey::StrFmt( char *str ) const { int len = 0, i, nibbles; static const char hex[] = "0123456789abcdef"; len += sprintf(str + len, "0x"); nibbles = (3 + bignum_bitcount(this->exponent)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) str[len++] = hex[(bignum_byte(this->exponent, i / 2) >> (4 * (i % 2))) & 0xF]; len += sprintf(str + len, ",0x"); nibbles = (3 + bignum_bitcount(this->modulus)) / 4; if (nibbles < 1) nibbles = 1; for (i = nibbles; i--;) str[len++] = hex[(bignum_byte(this->modulus, i / 2) >> (4 * (i % 2))) & 0xF]; str[len] = '\0'; }
int makekey(unsigned char *data, int len, struct RSAKey *result, unsigned char **keystr, int order) { unsigned char *p = data; int i, n; if (len < 4) return -1; if (result) { result->bits = 0; for (i = 0; i < 4; i++) result->bits = (result->bits << 8) + *p++; } else p += 4; len -= 4; /* * order=0 means exponent then modulus (the keys sent by the * server). order=1 means modulus then exponent (the keys * stored in a keyfile). */ if (order == 0) { n = ssh1_read_bignum(p, len, result ? &result->exponent : NULL); if (n < 0) return -1; p += n; len -= n; } n = ssh1_read_bignum(p, len, result ? &result->modulus : NULL); if (n < 0 || (result && bignum_bitcount(result->modulus) == 0)) return -1; if (result) result->bytes = n - 2; if (keystr) *keystr = p + 2; p += n; len -= n; if (order == 1) { n = ssh1_read_bignum(p, len, result ? &result->exponent : NULL); if (n < 0) return -1; p += n; len -= n; } return p - data; }
static void setupbigedit1(HWND hwnd, int id, int idstatic, struct RSAKey *key) { char *buffer; char *dec1, *dec2; dec1 = bignum_decimal(key->exponent); dec2 = bignum_decimal(key->modulus); buffer = dupprintf("%d %s %s %s", bignum_bitcount(key->modulus), dec1, dec2, key->comment); SetDlgItemText(hwnd, id, buffer); SetDlgItemText(hwnd, idstatic, "&Public key for pasting into authorized_keys file:"); sfree(dec1); sfree(dec2); sfree(buffer); }
static int rsa2_verifysig(void *key, char *sig, int siglen, char *data, int datalen) { struct RSAKey *rsa = (struct RSAKey *) key; Bignum in, out; char *p; int slen; int bytes, i, j, ret; unsigned char hash[20]; getstring(&sig, &siglen, &p, &slen); if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) { return 0; } in = getmp(&sig, &siglen); out = modpow(in, rsa->exponent, rsa->modulus); freebn(in); ret = 1; bytes = (bignum_bitcount(rsa->modulus)+7) / 8; /* Top (partial) byte should be zero. */ if (bignum_byte(out, bytes - 1) != 0) ret = 0; /* First whole byte should be 1. */ if (bignum_byte(out, bytes - 2) != 1) ret = 0; /* Most of the rest should be FF. */ for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) { if (bignum_byte(out, i) != 0xFF) ret = 0; } /* Then we expect to see the asn1_weird_stuff. */ for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) { if (bignum_byte(out, i) != asn1_weird_stuff[j]) ret = 0; } /* Finally, we expect to see the SHA-1 hash of the signed data. */ SHA_Simple(data, datalen, hash); for (i = 19, j = 0; i >= 0; i--, j++) { if (bignum_byte(out, i) != hash[j]) ret = 0; } freebn(out); return ret; }
static int save_ssh1_pubkey(char *filename, struct RSAKey *key) { char *dec1, *dec2; FILE *fp; dec1 = bignum_decimal(key->exponent); dec2 = bignum_decimal(key->modulus); fp = fopen(filename, "wb"); if (!fp) return 0; fprintf(fp, "%d %s %s %s\n", bignum_bitcount(key->modulus), dec1, dec2, key->comment); fclose(fp); sfree(dec1); sfree(dec2); return 1; }
/* Public key blob as used by Pageant: exponent before modulus. */ unsigned char *rsa_public_blob(struct RSAKey *key, int *len) { int length, pos; unsigned char *ret; length = (ssh1_bignum_length(key->modulus) + ssh1_bignum_length(key->exponent) + 4); ret = snewn(length, unsigned char); PUT_32BIT(ret, bignum_bitcount(key->modulus)); pos = 4; pos += ssh1_write_bignum(ret + pos, key->exponent); pos += ssh1_write_bignum(ret + pos, key->modulus); *len = length; return ret; }
bool RSAKey::Verify( const CString &data, const CString &sig ) const { Bignum in, out; int bytes, i, j; unsigned char hash[20]; in = bignum_from_bytes( (const unsigned char *) sig.data(), sig.size() ); /* Base (in) must be smaller than the modulus. */ if( bignum_cmp(in, this->modulus) >= 0 ) { freebn(in); return false; } out = modpow(in, this->exponent, this->modulus); freebn(in); bool ret = true; bytes = (bignum_bitcount(this->modulus)+7) / 8; /* Top (partial) byte should be zero. */ if (bignum_byte(out, bytes - 1) != 0) ret = 0; /* First whole byte should be 1. */ if (bignum_byte(out, bytes - 2) != 1) ret = 0; /* Most of the rest should be FF. */ for (i = bytes - 3; i >= 20; i--) { if (bignum_byte(out, i) != 0xFF) ret = 0; } /* Finally, we expect to see the SHA-1 hash of the signed data. */ SHA_Simple( data.data(), data.size(), hash ); for (i = 19, j = 0; i >= 0; i--, j++) { if (bignum_byte(out, i) != hash[j]) ret = false; } freebn(out); return ret; }
void RSAKey::PrivateBlob( CString &out ) const { int i; unsigned char *blob, *p; int elen = (bignum_bitcount(this->exponent) + 8) / 8; int mlen = (bignum_bitcount(this->modulus) + 8) / 8; int dlen = (bignum_bitcount(this->private_exponent) + 8) / 8; int plen = (bignum_bitcount(this->p) + 8) / 8; int qlen = (bignum_bitcount(this->q) + 8) / 8; int ulen = (bignum_bitcount(this->iqmp) + 8) / 8; /* * mpint exp, mpint mod, mpint private_exp, mpint p, mpint q, mpint iqmp. Total 24 + * sum of lengths. */ int bloblen = 24 + elen + mlen + dlen + plen + qlen + ulen; blob = new unsigned char[bloblen]; p = blob; PUT_32BIT(p, elen); p += 4; for (i = elen; i--;) *p++ = bignum_byte(this->exponent, i); PUT_32BIT(p, mlen); p += 4; for (i = mlen; i--;) *p++ = bignum_byte(this->modulus, i); PUT_32BIT(p, dlen); p += 4; for (i = dlen; i--;) *p++ = bignum_byte(this->private_exponent, i); PUT_32BIT(p, plen); p += 4; for (i = plen; i--;) *p++ = bignum_byte(this->p, i); PUT_32BIT(p, qlen); p += 4; for (i = qlen; i--;) *p++ = bignum_byte(this->q, i); PUT_32BIT(p, ulen); p += 4; for (i = ulen; i--;) *p++ = bignum_byte(this->iqmp, i); ASSERT(p == blob + bloblen); out = CString( (const char *) blob, bloblen ); }
static unsigned char *dss_private_blob(void *key, int *len) { struct dss_key *dss = (struct dss_key *) key; int xlen, bloblen; int i; unsigned char *blob, *p; xlen = (bignum_bitcount(dss->x) + 8) / 8; /* * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen. */ bloblen = 4 + xlen; blob = snewn(bloblen, unsigned char); p = blob; PUT_32BIT(p, xlen); p += 4; for (i = xlen; i--;) *p++ = bignum_byte(dss->x, i); assert(p == blob + bloblen); *len = bloblen; return blob; }
static char *dss_fingerprint(void *key) { struct dss_key *dss = (struct dss_key *) key; struct MD5Context md5c; unsigned char digest[16], lenbuf[4]; char buffer[16 * 3 + 40]; char *ret; int numlen, i; MD5Init(&md5c); MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11); #define ADD_BIGNUM(bignum) \ numlen = (bignum_bitcount(bignum)+8)/8; \ PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ for (i = numlen; i-- ;) { \ unsigned char c = bignum_byte(bignum, i); \ MD5Update(&md5c, &c, 1); \ } ADD_BIGNUM(dss->p); ADD_BIGNUM(dss->q); ADD_BIGNUM(dss->g); ADD_BIGNUM(dss->y); #undef ADD_BIGNUM MD5Final(digest, &md5c); snprintf(buffer, 16 * 3 + 40, "ssh-dss %d ", bignum_bitcount(dss->p)); for (i = 0; i < 16; i++) snprintf(buffer + strlen(buffer), 16 * 3 + 40 - strlen(buffer), "%s%02x", i ? ":" : "", digest[i]); ret = snewn(strlen(buffer) + 1, char); if (ret) strcpy(ret, buffer); return ret; }
/* * Save an RSA key file. Return nonzero on success. */ int saversakey(const Filename *filename, struct RSAKey *key, char *passphrase) { unsigned char buf[16384]; unsigned char keybuf[16]; struct MD5Context md5c; unsigned char *p, *estart; FILE *fp; /* * Write the initial signature. */ p = buf; memcpy(p, rsa_signature, sizeof(rsa_signature)); p += sizeof(rsa_signature); /* * One byte giving encryption type, and one reserved (zero) * uint32. */ *p++ = (passphrase ? SSH_CIPHER_3DES : 0); PUT_32BIT(p, 0); p += 4; /* * An ordinary SSH-1 public key consists of: a uint32 * containing the bit count, then two bignums containing the * modulus and exponent respectively. */ PUT_32BIT(p, bignum_bitcount(key->modulus)); p += 4; p += ssh1_write_bignum(p, key->modulus); p += ssh1_write_bignum(p, key->exponent); /* * A string containing the comment field. */ if (key->comment) { PUT_32BIT(p, strlen(key->comment)); p += 4; memcpy(p, key->comment, strlen(key->comment)); p += strlen(key->comment); } else { PUT_32BIT(p, 0); p += 4; } /* * The encrypted portion starts here. */ estart = p; /* * Two bytes, then the same two bytes repeated. */ *p++ = random_byte(); *p++ = random_byte(); p[0] = p[-2]; p[1] = p[-1]; p += 2; /* * Four more bignums: the decryption exponent, then iqmp, then * q, then p. */ p += ssh1_write_bignum(p, key->private_exponent); p += ssh1_write_bignum(p, key->iqmp); p += ssh1_write_bignum(p, key->q); p += ssh1_write_bignum(p, key->p); /* * Now write zeros until the encrypted portion is a multiple of * 8 bytes. */ while ((p - estart) % 8) *p++ = '\0'; /* * Now encrypt the encrypted portion. */ if (passphrase) { MD5Init(&md5c); MD5Update(&md5c, (unsigned char *)passphrase, strlen(passphrase)); MD5Final(keybuf, &md5c); des3_encrypt_pubkey(keybuf, estart, p - estart); smemclr(keybuf, sizeof(keybuf)); /* burn the evidence */ } /* * Done. Write the result to the file. */ fp = f_open(filename, "wb", TRUE); if (fp) { int ret = (fwrite(buf, 1, p - buf, fp) == (size_t) (p - buf)); if (fclose(fp)) ret = 0; return ret; } else return 0; }
/* * This function is a wrapper on modpow(). It has the same effect * as modpow(), but employs RSA blinding to protect against timing * attacks. */ static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key) { Bignum random, random_encrypted, random_inverse; Bignum input_blinded, ret_blinded; Bignum ret; SHA512_State ss; unsigned char digest512[64]; int digestused = lenof(digest512); int hashseq = 0; /* * Start by inventing a random number chosen uniformly from the * range 2..modulus-1. (We do this by preparing a random number * of the right length and retrying if it's greater than the * modulus, to prevent any potential Bleichenbacher-like * attacks making use of the uneven distribution within the * range that would arise from just reducing our number mod n. * There are timing implications to the potential retries, of * course, but all they tell you is the modulus, which you * already knew.) * * To preserve determinism and avoid Pageant needing to share * the random number pool, we actually generate this `random' * number by hashing stuff with the private key. */ while (1) { int bits, byte, bitsleft, v; random = copybn(key->modulus); /* * Find the topmost set bit. (This function will return its * index plus one.) Then we'll set all bits from that one * downwards randomly. */ bits = bignum_bitcount(random); byte = 0; bitsleft = 0; while (bits--) { if (bitsleft <= 0) { bitsleft = 8; /* * Conceptually the following few lines are equivalent to * byte = random_byte(); */ if (digestused >= lenof(digest512)) { unsigned char seqbuf[4]; PUT_32BIT(seqbuf, hashseq); pSHA512_Init(&ss); SHA512_Bytes(&ss, "RSA deterministic blinding", 26); SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf)); sha512_mpint(&ss, key->private_exponent); pSHA512_Final(&ss, digest512); hashseq++; /* * Now hash that digest plus the signature * input. */ pSHA512_Init(&ss); SHA512_Bytes(&ss, digest512, sizeof(digest512)); sha512_mpint(&ss, input); pSHA512_Final(&ss, digest512); digestused = 0; } byte = digest512[digestused++]; } v = byte & 1; byte >>= 1; bitsleft--; bignum_set_bit(random, bits, v); } /* * Now check that this number is strictly greater than * zero, and strictly less than modulus. */ if (bignum_cmp(random, Zero) <= 0 || bignum_cmp(random, key->modulus) >= 0) { freebn(random); continue; } else { break; } } /* * RSA blinding relies on the fact that (xy)^d mod n is equal * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair * y and y^d; then we multiply x by y, raise to the power d mod * n as usual, and divide by y^d to recover x^d. Thus an * attacker can't correlate the timing of the modpow with the * input, because they don't know anything about the number * that was input to the actual modpow. * * The clever bit is that we don't have to do a huge modpow to * get y and y^d; we will use the number we just invented as * _y^d_, and use the _public_ exponent to compute (y^d)^e = y * from it, which is much faster to do. */ random_encrypted = modpow(random, key->exponent, key->modulus); random_inverse = modinv(random, key->modulus); input_blinded = modmul(input, random_encrypted, key->modulus); ret_blinded = modpow(input_blinded, key->private_exponent, key->modulus); ret = modmul(ret_blinded, random_inverse, key->modulus); freebn(ret_blinded); freebn(input_blinded); freebn(random_inverse); freebn(random_encrypted); freebn(random); return ret; }