]>
git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/vmdh/vmdh-42-example2.c
2 * Copyright (c) 2006-2008,2010,2012,2014 Apple Inc. All Rights Reserved.
5 #include <Security/vmdh.h>
6 #include <Security/SecBase64.h>
10 #include <corecrypto/ccdh.h>
12 #include "Security_regressions.h"
15 #include <openssl/dh.h>
18 /* How to reach in the internals of SecDH/vmdh struct */
19 //these are copies of SecDH_gp() and SecDH_priv()
20 static inline ccdh_gp_t
vmdh_gp(struct vmdh
*dh
)
25 static inline ccdh_full_ctx_t
vmdh_priv(struct vmdh
*dh
)
27 ccdh_gp_t gp
= vmdh_gp(dh
);
28 cc_size s
= ccn_sizeof_n(ccdh_gp_n(gp
));
29 return (ccdh_full_ctx_t
)cc_pad_align((uintptr_t)dh
+ ccdh_gp_size(s
));
32 static uint32_t param_g
= 5;
34 static const uint8_t param_p
[] = {
35 0xED, 0x42, 0x06, 0xE1, 0xDD, 0x09, 0x93, 0xA6,
36 0x81, 0xAE, 0x00, 0x0D, 0xBF, 0x84, 0x7F, 0x7D,
37 0x87, 0x64, 0x6B, 0x77, 0x24, 0x03, 0xB8, 0xC0,
38 0xDC, 0xBE, 0x5B, 0x9C, 0x8E, 0x71, 0x09, 0x24,
39 0x53, 0x77, 0x7F, 0x5D, 0x1A, 0xAD, 0x92, 0xD8,
40 0xFE, 0xCD, 0x5C, 0xB4, 0xCA, 0x09, 0x17, 0x11,
41 0xF3, 0x82, 0x01, 0x39, 0x4A, 0x09, 0xBA, 0x29,
42 0x95, 0x2B, 0xC4, 0xCC, 0x56, 0x21, 0x97, 0x13
45 static const uint8_t client_priv
[] = {
46 0x32, 0x34, 0x73, 0x16, 0x7d, 0x79, 0xde, 0x47,
47 0x22, 0x93, 0xf5, 0x86, 0x47, 0xf6, 0x7f, 0x7a,
48 0xb6, 0x30, 0x16, 0x5b, 0xbf, 0xe1, 0x36, 0x0b,
49 0xb4, 0xd2, 0x84, 0x3e, 0x57, 0x5f, 0xcb, 0xc6,
50 0x6a, 0xae, 0x5d, 0x59, 0x4b, 0x70, 0x53, 0x22,
51 0xb0, 0x51, 0x89, 0x30, 0x74, 0xfc, 0x95, 0x51,
52 0x9c, 0xc9, 0xf7, 0xac, 0x8c, 0x37, 0xfd, 0xc1,
53 0x0e, 0x02, 0x6e, 0x69, 0x6c, 0xca, 0x2a, 0x95
56 __unused
static const uint8_t client_pub
[] = {
57 0x15, 0xDF, 0x17, 0x6E, 0xB4, 0x95, 0xA7, 0x92,
58 0x41, 0xB6, 0xF1, 0x93, 0x19, 0xDB, 0x34, 0xF1,
59 0xE0, 0x0D, 0x62, 0xCD, 0x55, 0xC7, 0x0B, 0x27,
60 0xB7, 0x53, 0x1A, 0x28, 0x65, 0x11, 0xF0, 0xF6,
61 0xA6, 0xE1, 0x5B, 0x86, 0x1D, 0x67, 0x85, 0x19,
62 0x6D, 0xD6, 0x80, 0xFF, 0x5C, 0xEB, 0xC3, 0x2D,
63 0xC3, 0xCB, 0xD2, 0xD4, 0x66, 0x93, 0xF4, 0xFC,
64 0xF1, 0xF4, 0x8B, 0x61, 0x0F, 0x02, 0xF5, 0x19
67 static const uint8_t server_pub
[] = {
68 0x73, 0xC5, 0xF8, 0xF8, 0xB8, 0x9C, 0xB0, 0x5F,
69 0xD6, 0xC6, 0x49, 0x5C, 0x70, 0xF5, 0x90, 0xB3,
70 0x8A, 0xD3, 0xD0, 0x12, 0x99, 0x47, 0x60, 0xC2,
71 0x5B, 0xF7, 0x18, 0x3A, 0x19, 0xF5, 0x01, 0xA3,
72 0x67, 0xBF, 0x57, 0x28, 0x7E, 0x99, 0xA8, 0xDB,
73 0x97, 0xA4, 0xAF, 0xF2, 0x68, 0x47, 0xAB, 0x48,
74 0xE3, 0x4D, 0xF2, 0x94, 0xB4, 0xCC, 0xFC, 0x0C,
75 0x50, 0xAD, 0xEF, 0x2E, 0x80, 0xA6, 0x20, 0x29
78 static const uint8_t pw
[] = {
79 0x31, 0x32, 0x33, 0x34
82 static const uint8_t pw_encr
[] = {
83 0x42, 0xd7, 0xa1, 0x08, 0x15, 0x8f, 0xdd, 0xc8,
84 0xe8, 0x75, 0xea, 0xa2, 0xc2, 0x20, 0x28, 0xfa
88 static void hexdump(const uint8_t *bytes
, size_t len
) {
90 for (ix
= 0; ix
< len
; ++ix
) {
91 printf("%02X", bytes
[ix
]);
97 static void tests(void)
100 ok((vmdh
= vmdh_create(param_g
, param_p
, sizeof(param_p
),
101 NULL
, 0)), "vmdh_create");
103 /* No SecDH API to import a private key, so we have to reach inside the opaque struct */
104 ccdh_gp_t gp
= vmdh_gp(vmdh
);
105 ccdh_full_ctx_t priv
= vmdh_priv(vmdh
);
106 ok_status(ccdh_import_priv(gp
, sizeof(client_priv
), client_priv
, priv
), "Import DH private key");
108 uint8_t encpw
[vmdh_encpw_len(sizeof(pw
))];
109 size_t encpw_len
= sizeof(encpw
);
111 ok(vmdh_encrypt_password(vmdh
, server_pub
, sizeof(server_pub
),
112 pw
, sizeof(pw
), encpw
, &encpw_len
), "vmdh_encrypt_password");
115 is(encpw_len
, sizeof(pw_encr
), "check ciphertext size");
116 ok(!memcmp(encpw
, pw_encr
, sizeof(pw_encr
)), "does ciphertext match?");
119 hexdump(encpw
, encpw_len
);
120 hexdump(pw_encr
, sizeof(pw_encr
));
123 dh
->p
= BN_bin2bn(param_p
, sizeof(param_p
), BN_new());
124 BN_hex2bn(&dh
->g
, "05");
125 dh
->pub_key
= BN_bin2bn(client_pub
, sizeof(client_pub
), BN_new());
126 dh
->priv_key
= BN_bin2bn(client_priv
, sizeof(client_priv
), BN_new());
127 BIGNUM
*bn_server_pub
= BN_bin2bn(server_pub
, sizeof(server_pub
), BN_new());
128 unsigned char key
[1024];
129 DH_compute_key(key
, bn_server_pub
, dh
);
130 printf("shared secret:\n");
135 int vmdh_42_example2(int argc
, char *const *argv
)