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