Created
April 18, 2017 22:28
-
-
Save pmslavin/91a9240e15badff383e399491563fb89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "b64.h" | |
const char *const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
const char pad = '='; | |
char *b64_encode(const unsigned char *src, size_t src_len) | |
{ | |
size_t pad_len = src_len % 3; | |
pad_len = pad_len ? 3-pad_len : pad_len; | |
size_t dest_len = (src_len + pad_len) / 3 * 4; | |
printf("[b64] dest_len: %d\n", dest_len); | |
printf("[b64] pad_len: %d\n", pad_len); | |
size_t src_idx = 0, dest_idx = 0; | |
char *dest = malloc((dest_len+1)*sizeof(*dest)); // Null terminate | |
while(src_len > 2){ | |
dest[dest_idx++] = base64[src[src_idx] >> 2]; | |
dest[dest_idx++] = base64[((src[src_idx] & 0x03) << 4) + (src[src_idx+1] >> 4)]; | |
src_idx++; | |
dest[dest_idx++] = base64[((src[src_idx] & 0x0F) << 2) + (src[src_idx+1] >> 6)]; | |
src_idx++; | |
dest[dest_idx++] = base64[src[src_idx++] & 0x3F]; | |
src_len -= 3; | |
} | |
if(src_len){ | |
dest[dest_idx++] = base64[src[src_idx] >> 2]; | |
dest[dest_idx++] = base64[((src[src_idx] & 0x03) << 4)]; | |
if(pad_len == 2){ | |
dest[dest_idx++] = pad; | |
}else{ | |
dest[dest_idx++] = base64[((src[src_idx] & 0x0F) << 2)]; | |
} | |
dest[dest_idx++] = pad; | |
} | |
dest[dest_idx] = '\0'; | |
printf("[b64] dest_idx: %d\n", dest_idx); | |
return dest; | |
} | |
#include <openssl/evp.h> | |
#include <openssl/pem.h> | |
#include <openssl/conf.h> | |
#include <openssl/err.h> | |
#include <string.h> | |
#include "b64.h" | |
static EVP_PKEY *localKeypair; | |
int main(int argc, char *argv[]) | |
{ | |
(void)argc; | |
(void)argv; | |
/* Load the human readable error strings for libcrypto */ | |
ERR_load_crypto_strings(); | |
/* Load all digest and cipher algorithms */ | |
OpenSSL_add_all_algorithms(); | |
/* Load config file, and other important initialisation */ | |
OPENSSL_config(NULL); | |
// RAND_poll(); | |
/* ... Do some crypto stuff here ... */ | |
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL); | |
EVP_PKEY_paramgen_init(ctx); | |
if(!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, 1024)) | |
return 1; | |
EVP_PKEY *params = NULL; | |
EVP_PKEY_paramgen(ctx, ¶ms); | |
EVP_PKEY_CTX *kctx = EVP_PKEY_CTX_new(params, NULL); | |
if(EVP_PKEY_keygen_init(kctx) <= 0) | |
return 2; | |
if(EVP_PKEY_keygen(kctx, &localKeypair) <= 0) | |
return 3; | |
// BIO *pub_raw = BIO_new(BIO_s_mem()); | |
BIO *outbio = BIO_new_fp(stdout, BIO_NOCLOSE); | |
// EVP_PKEY_print_public(outbio, localKeypair, 0, NULL); | |
// BIO_printf(outbio, "%d bit RSA Key\n\n", EVP_PKEY_bits(localKeypair)); | |
PEM_write_bio_PUBKEY(outbio, localKeypair); | |
// PEM_write_bio_DSA_PUBKEY(outbio, EVP_PKEY_get1_DSA(localKeypair)); | |
DSA *dsa = EVP_PKEY_get1_DSA(localKeypair); | |
printf("dsa->p->top: %d\n", dsa->p->top); | |
for(int idx=dsa->p->top-1; idx>=0; idx--) | |
printf("%02x ", dsa->p->d[idx]); | |
putchar('\n'); | |
printf("dsa->q->top: %d\n", dsa->q->top); | |
for(int idx=dsa->q->top-1; idx>=0; idx--) | |
printf("%0x ", dsa->q->d[idx]); | |
putchar('\n'); | |
printf("dsa->g->top: %d\n", dsa->g->top); | |
for(int idx=dsa->g->top-1; idx>=0; idx--) | |
printf("dsa->pub_key->top: %d\n", dsa->pub_key->top); | |
for(int idx=dsa->pub_key->top-1; idx>=0; idx--) | |
printf("%0x ", dsa->pub_key->d[idx]); | |
putchar('\n'); | |
// printf("%d: %x\n", dsa->p->top, dsa->p->d); | |
// PEM_write_bio_PrivateKey(outbio, localKeypair, NULL, NULL, 0, NULL, NULL); | |
char *pwd = "rfc1034"; | |
PEM_write_bio_PrivateKey(outbio, localKeypair, EVP_des_ede3_cbc(), pwd, strlen(pwd), NULL, NULL); | |
unsigned char prelude[] = {0x00, 0x00, 0x00, 0x07, | |
/* ssh-dss */ 0x73, 0x73, 0x68, 0x2d, 0x64, 0x73, 0x73, | |
0x00, 0x00, 0x00}; | |
size_t binout_sz = sizeof(prelude) + 4*(dsa->p->top + dsa->q->top | |
+ dsa->g->top + dsa->pub_key->top) + 20; /* inline sizes*/ | |
unsigned char *binout = malloc(binout_sz * sizeof(*binout)); | |
memcpy(binout, prelude, sizeof(prelude)); | |
size_t off = sizeof(prelude); | |
binout[off++] = dsa->p->top*4+1; | |
binout[off++] = 0x00; | |
for(int idx=dsa->p->top-1; idx>=0; idx--) | |
for(int s=3; s>=0; s--) | |
binout[off++] = ((unsigned int)(dsa->p->d[idx] >> (8*s)) & 0xFF); | |
binout[off++] = 0x00; | |
binout[off++] = 0x00; | |
binout[off++] = 0x00; | |
binout[off++] = dsa->q->top*4+1; | |
binout[off++] = 0x00; | |
for(int idx=dsa->q->top-1; idx>=0; idx--) | |
for(int s=3; s>=0; s--) | |
binout[off++] = ((unsigned int)(dsa->q->d[idx] >> (8*s)) & 0xFF); | |
binout[off++] = 0x00; | |
binout[off++] = 0x00; | |
binout[off++] = 0x00; | |
binout[off++] = dsa->g->top*4+1; | |
binout[off++] = 0x00; | |
for(int idx=dsa->g->top-1; idx>=0; idx--) | |
for(int s=3; s>=0; s--) | |
binout[off++] = ((unsigned int)(dsa->g->d[idx] >> (8*s)) & 0xFF); | |
binout[off++] = 0x00; | |
binout[off++] = 0x00; | |
binout[off++] = 0x00; | |
binout[off++] = dsa->pub_key->top*4+1; | |
binout[off++] = 0x00; | |
for(int idx=dsa->pub_key->top-1; idx>=0; idx--) | |
for(int s=3; s>=0; s--) | |
binout[off++] = ((unsigned int)(dsa->pub_key->d[idx] >> (8*s)) & 0xFF); | |
char *b64t = b64_encode((unsigned char *)binout, off); | |
printf("binout: %s\n", b64t); | |
free(b64t); | |
printf("ui: %d ul:%d\n", sizeof(unsigned int), sizeof(unsigned long)); | |
unsigned int t = 0x44332211; | |
printf("%2x %2x %2x %2x\n", (t >> 24) & 0xFF, (t >> 16) & 0xFF, (t >> 8) & 0xFF, (t >> 0) & 0xFF); | |
printf("%2x %2x %2x %2x\n", (t >> 0) & 0xFF, (t >> 8) & 0xFF, (t >> 16) & 0xFF, (t >> 24) & 0xFF); | |
b64t = b64_encode((unsigned char *)"PQR", 3); | |
printf("b64t: %s\n", b64t); | |
free(b64t); | |
unsigned char c01 = 0xFF; | |
char c02 = 0xFF; | |
printf("c01: %.8x\nc02: %.8x\n", c01 >> 4, c02 >> 4); | |
printf("c01: %.8x\nc02: %.8x\n", c01 >> 4, (c02 >> 4) & 0x0F); | |
printf("c01: %.8x\nc02: %.8x\n", c01 << 4, c02 << 4); | |
printf("prelude: %d dsa->p->top: %d dsa->q->top: %d dsa->g->top: %d dsa->pub_key->top: %d\n", sizeof(prelude), dsa->p->top, dsa->q->top, dsa->g->top, dsa->pub_key->top); | |
// PEM_write_bio_DSAparams(outbio, EVP_PKEY_get1_DSA(localKeypair)); | |
/* Clean up */ | |
EVP_PKEY_CTX_free(ctx); | |
/* Removes all digests and ciphers */ | |
EVP_cleanup(); | |
/* if you omit the next, a small leak may be left when you make use of the BIO (low level API) for e.g. base64 transformations */ | |
CRYPTO_cleanup_all_ex_data(); | |
/* Remove error strings */ | |
ERR_free_strings(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment