oberon 2.2.1

Oberon is a multi-factor zero-knowledge capable token without requiring email, SMS, or authenticator apps. The proof of token validity is only 96 bytes while the token itself is only 48 bytes.
Documentation
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#![allow(unused_doc_comments, missing_docs)]
use crate::{Blinding, Proof, PublicKey, SecretKey, Token};
use ffi_support::{
    define_bytebuffer_destructor, define_handle_map_deleter, define_string_destructor, ByteBuffer,
    ConcurrentHandleMap, ErrorCode, ExternError,
};
use lazy_static::lazy_static;
use std::{ptr, slice, string::String, vec::Vec};

lazy_static! {
    /// The context manager for creating proofs
    pub static ref CREATE_PROOF_CONTEXT: ConcurrentHandleMap<CreateProofContext> =
        ConcurrentHandleMap::new();
}

/// Cleanup created strings
define_string_destructor!(oberon_string_free);
/// Cleanup created byte buffers
define_bytebuffer_destructor!(oberon_byte_buffer_free);
/// Cleanup created proof contexts
define_handle_map_deleter!(CREATE_PROOF_CONTEXT, oberon_create_proof_free);

/// The proof context object
pub struct CreateProofContext {
    /// The proof token
    pub token: Option<Token>,
    /// The token blindings
    pub blindings: Vec<Blinding>,
    /// The id associated with the token
    pub id: Option<Vec<u8>>,
    /// The proof nonce
    pub nonce: Option<Vec<u8>>,
}

/// Used for receiving byte arrays
#[repr(C)]
pub struct ByteArray {
    length: usize,
    data: *const u8,
}

impl Default for ByteArray {
    fn default() -> Self {
        Self {
            length: 0,
            data: ptr::null(),
        }
    }
}

impl From<&Vec<u8>> for ByteArray {
    fn from(b: &Vec<u8>) -> Self {
        Self::from_slice(b.as_slice())
    }
}

impl From<Vec<u8>> for ByteArray {
    fn from(b: Vec<u8>) -> Self {
        Self::from_slice(b.as_slice())
    }
}

impl From<ByteBuffer> for ByteArray {
    fn from(b: ByteBuffer) -> Self {
        Self::from_slice(&b.destroy_into_vec())
    }
}

impl ByteArray {
    /// Convert to a byte vector
    pub fn to_vec(&self) -> Vec<u8> {
        if self.data.is_null() || self.length == 0 {
            Vec::new()
        } else {
            unsafe { slice::from_raw_parts(self.data, self.length).to_vec() }
        }
    }

    /// Convert to a byte vector if possible
    /// Some if success
    /// None if failure
    pub fn to_opt_vec(&self) -> Option<Vec<u8>> {
        if self.data.is_null() {
            None
        } else if self.length == 0 {
            Some(Vec::new())
        } else {
            Some(unsafe { slice::from_raw_parts(self.data, self.length).to_vec() })
        }
    }

    /// Convert to outgoing ByteBuffer
    pub fn into_byte_buffer(self) -> ByteBuffer {
        ByteBuffer::from_vec(self.to_vec())
    }

    /// Convert from a slice
    pub fn from_slice<I: AsRef<[u8]>>(data: I) -> Self {
        let data = data.as_ref();
        Self {
            length: data.len(),
            data: data.as_ptr(),
        }
    }
}

macro_rules! from_bytes {
    ($name:ident, $type:ident) => {
        fn $name(input: Vec<u8>) -> Option<$type> {
            match <[u8; $type::BYTES]>::try_from(input.as_slice()) {
                Err(_) => None,
                Ok(bytes) => {
                    let val = $type::from_bytes(&bytes);
                    if val.is_some().unwrap_u8() == 1u8 {
                        Some(val.unwrap())
                    } else {
                        None
                    }
                }
            }
        }
    };
}
from_bytes!(secret_key, SecretKey);
from_bytes!(public_key, PublicKey);
from_bytes!(get_token, Token);
from_bytes!(get_proof, Proof);

/// The size of the secret key
#[no_mangle]
pub extern "C" fn oberon_secret_key_size() -> i32 {
    SecretKey::BYTES as i32
}

/// The size of the public key
#[no_mangle]
pub extern "C" fn oberon_public_key_size() -> i32 {
    PublicKey::BYTES as i32
}

/// The size of the token
#[no_mangle]
pub extern "C" fn oberon_token_size() -> i32 {
    Token::BYTES as i32
}

/// The size of a blinding
#[no_mangle]
pub extern "C" fn oberon_blinding_size() -> i32 {
    Blinding::BYTES as i32
}

/// The size of a proof
#[no_mangle]
pub extern "C" fn oberon_proof_size() -> i32 {
    Proof::BYTES as i32
}

/// Create new random secret key
#[no_mangle]
pub extern "C" fn oberon_new_secret_key(secret_key: &mut ByteBuffer) -> i32 {
    let sk = SecretKey::new(rand::thread_rng());
    *secret_key = ByteBuffer::from_vec(sk.to_bytes().to_vec());
    0
}

/// Get the public key from the secret key
#[no_mangle]
pub extern "C" fn oberon_get_public_key(
    sk: ByteArray,
    public_key: &mut ByteBuffer,
    err: &mut ExternError,
) -> i32 {
    let t = sk.to_vec();
    match secret_key(t) {
        None => {
            *err = ExternError::new_error(ErrorCode::new(1), String::from("Invalid secret key"));
            1
        }
        Some(sk) => {
            let pk = PublicKey::from(&sk);
            *public_key = ByteBuffer::from_vec(pk.to_bytes().to_vec());
            0
        }
    }
}

/// Create new secret key from a seed
#[no_mangle]
pub extern "C" fn oberon_secret_key_from_seed(seed: ByteArray, sk: &mut ByteBuffer) -> i32 {
    let t = SecretKey::hash(seed.to_vec().as_slice());
    *sk = ByteBuffer::from_vec(t.to_bytes().to_vec());
    0
}

/// Create a new token for a given ID
#[no_mangle]
pub extern "C" fn oberon_new_token(
    sk: ByteArray,
    id: ByteArray,
    token: &mut ByteBuffer,
    err: &mut ExternError,
) -> i32 {
    let t = sk.to_vec();
    match secret_key(t) {
        None => {
            *err = ExternError::new_error(ErrorCode::new(1), String::from("Invalid secret key"));
            1
        }
        Some(sk) => match Token::new(&sk, id.to_vec()) {
            None => {
                *err = ExternError::new_error(
                    ErrorCode::new(2),
                    String::from("Unable to create token"),
                );
                2
            }
            Some(tk) => {
                *token = ByteBuffer::from_vec(tk.to_bytes().to_vec());
                0
            }
        },
    }
}

/// Verify a token for a given ID
#[no_mangle]
pub extern "C" fn oberon_verify_token(
    token: ByteArray,
    pk: ByteArray,
    id: ByteArray,
    err: &mut ExternError,
) -> i32 {
    match (get_token(token.to_vec()), public_key(pk.to_vec())) {
        (Some(tk), Some(pk)) => {
            let res = tk.verify(pk, id.to_vec()).unwrap_u8() as i32;
            -(res - 1)
        }
        (_, _) => {
            *err = ExternError::new_error(
                ErrorCode::new(1),
                String::from("Invalid token and/or public key"),
            );
            1
        }
    }
}

/// Create a blinding factor from the specified data
#[no_mangle]
pub extern "C" fn oberon_create_blinding(data: ByteArray, blinding: &mut ByteBuffer) -> i32 {
    *blinding = ByteBuffer::from_vec(Blinding::new(data.to_vec().as_slice()).to_bytes().to_vec());
    0
}

/// Adds a blinding factor to the token
#[no_mangle]
pub extern "C" fn oberon_add_blinding(
    old_token: ByteArray,
    data: ByteArray,
    new_token: &mut ByteBuffer,
    err: &mut ExternError,
) -> i32 {
    match get_token(old_token.to_vec()) {
        None => {
            *err = ExternError::new_error(ErrorCode::new(1), String::from("Invalid token"));
            1
        }
        Some(tk) => {
            let b = Blinding::new(data.to_vec().as_slice());
            let new_tk = tk - b;
            *new_token = ByteBuffer::from_vec(new_tk.to_bytes().to_vec());
            0
        }
    }
}

/// Removes a blinding factor to the token
#[no_mangle]
pub extern "C" fn oberon_remove_blinding(
    old_token: ByteArray,
    data: ByteArray,
    new_token: &mut ByteBuffer,
    err: &mut ExternError,
) -> i32 {
    match get_token(old_token.to_vec()) {
        None => {
            *err = ExternError::new_error(ErrorCode::new(1), String::from("Invalid token"));
            1
        }
        Some(tk) => {
            let b = Blinding::new(data.to_vec().as_slice());
            let new_tk = tk + b;
            *new_token = ByteBuffer::from_vec(new_tk.to_bytes().to_vec());
            0
        }
    }
}

/// Creates a proof context
#[no_mangle]
pub extern "C" fn oberon_create_proof_init(err: &mut ExternError) -> u64 {
    CREATE_PROOF_CONTEXT.insert_with_output(err, || CreateProofContext {
        token: None,
        id: None,
        blindings: Vec::new(),
        nonce: None,
    })
}

/// Set the proof token
#[no_mangle]
pub extern "C" fn oberon_create_proof_set_token(
    handle: u64,
    token: ByteArray,
    err: &mut ExternError,
) -> i32 {
    CREATE_PROOF_CONTEXT.call_with_result_mut(err, handle, move |ctx| -> Result<(), ExternError> {
        match get_token(token.to_vec()) {
            None => Err(ExternError::new_error(
                ErrorCode::new(1),
                String::from("Invalid token"),
            )),
            Some(tk) => {
                ctx.token = Some(tk);
                Ok(())
            }
        }
    });
    err.get_code().code()
}

/// Set the proof id
#[no_mangle]
pub extern "C" fn oberon_create_proof_set_id(
    handle: u64,
    id: ByteArray,
    err: &mut ExternError,
) -> i32 {
    CREATE_PROOF_CONTEXT.call_with_output_mut(err, handle, move |ctx| {
        ctx.id = Some(id.to_vec());
    });
    err.get_code().code()
}

/// Set the proof nonce
#[no_mangle]
pub extern "C" fn oberon_create_proof_set_nonce(
    handle: u64,
    nonce: ByteArray,
    err: &mut ExternError,
) -> i32 {
    CREATE_PROOF_CONTEXT.call_with_output_mut(err, handle, move |ctx| {
        ctx.nonce = Some(nonce.to_vec());
    });
    err.get_code().code()
}

/// Set the proof blinding
#[no_mangle]
pub extern "C" fn oberon_create_proof_add_blinding(
    handle: u64,
    blinding: ByteArray,
    err: &mut ExternError,
) -> i32 {
    CREATE_PROOF_CONTEXT.call_with_output_mut(err, handle, move |ctx| {
        ctx.blindings
            .push(Blinding::new(blinding.to_vec().as_slice()));
    });
    err.get_code().code()
}

/// Create the proof
#[no_mangle]
pub extern "C" fn oberon_create_proof_finish(
    handle: u64,
    proof: &mut ByteBuffer,
    err: &mut ExternError,
) -> i32 {
    let pf = CREATE_PROOF_CONTEXT.call_with_result(
        err,
        handle,
        move |ctx| -> Result<ByteBuffer, ExternError> {
            if ctx.id.is_none() {
                return Err(ExternError::new_error(
                    ErrorCode::new(1),
                    String::from("Id must be set"),
                ));
            }
            if ctx.nonce.is_none() {
                return Err(ExternError::new_error(
                    ErrorCode::new(2),
                    String::from("Nonce must be set"),
                ));
            }
            if ctx.token.is_none() {
                return Err(ExternError::new_error(
                    ErrorCode::new(3),
                    String::from("Token must be set"),
                ));
            }
            match (ctx.id.as_ref(), ctx.nonce.as_ref(), ctx.token.as_ref()) {
                (Some(id), Some(nonce), Some(token)) => {
                    match Proof::new(
                        token,
                        ctx.blindings.as_slice(),
                        id,
                        nonce,
                        rand::thread_rng(),
                    ) {
                        None => Err(ExternError::new_error(
                            ErrorCode::new(4),
                            String::from("Invalid proof parameters"),
                        )),
                        Some(p) => Ok(ByteBuffer::from_vec(p.to_bytes().to_vec())),
                    }
                }
                (_, _, _) => Err(ExternError::new_error(
                    ErrorCode::new(5),
                    String::from("Invalid parameters"),
                )),
            }
        },
    );
    if err.get_code().is_success() {
        *proof = pf;
        if let Err(e) = CREATE_PROOF_CONTEXT.remove_u64(handle) {
            *err = ExternError::new_error(ErrorCode::new(6), std::format!("{:?}", e))
        }
    }
    err.get_code().code()
}

/// Creates a proof using a nonce received from a verifier
#[no_mangle]
pub extern "C" fn oberon_verify_proof(
    proof: ByteArray,
    pk: ByteArray,
    id: ByteArray,
    nonce: ByteArray,
    err: &mut ExternError,
) -> i32 {
    match (get_proof(proof.to_vec()), public_key(pk.to_vec())) {
        (Some(pf), Some(pub_key)) => {
            let res = pf
                .open(pub_key, id.to_vec().as_slice(), nonce.to_vec().as_slice())
                .unwrap_u8() as i32;
            -(res - 1)
        }
        (_, _) => {
            *err = ExternError::new_error(
                ErrorCode::new(1),
                String::from("Invalid proof and/or public key"),
            );
            1
        }
    }
}