|
| 1 | +// Copyright (c) 2023 The MobileCoin Foundation |
| 2 | + |
| 3 | +//! # subtle |
| 4 | +//! |
| 5 | +//! [![Crates.io][crate-image]][crate-link]<!-- |
| 6 | +//! -->[![Docs Status][docs-image]][docs-link] |
| 7 | +//! |
| 8 | +//! **Procedural macros for deriving [subtle] trait implementations.** |
| 9 | +//! |
| 10 | +//! Derive macro implemented for traits: |
| 11 | +//! - [x] ConstantTimeEq |
| 12 | +//! - [ ] ConstantTimeGreater |
| 13 | +//! - [ ] ConstantTimeLesser |
| 14 | +//! |
| 15 | +//! ## Documentation |
| 16 | +//! |
| 17 | +//! Documentation is available [here][subtle-docs]. |
| 18 | +//! |
| 19 | +//! # Installation |
| 20 | +//! To install, add the following to the dependencies section of your project's `Cargo.toml`: |
| 21 | +//! |
| 22 | +//! ```toml |
| 23 | +//! subtle = { version = "2.6", features = ["derive"] } |
| 24 | +//! ``` |
| 25 | +//! |
| 26 | +//! [crate-image]: https://img.shields.io/crates/v/subtle-derive?style=flat-square |
| 27 | +//! [crate-link]: https://crates.io/crates/subtle-derive |
| 28 | +//! [docs-image]: https://img.shields.io/docsrs/subtle-derive?style=flat-square |
| 29 | +//! [docs-link]: https://docs.rs/crate/subtle-derive |
| 30 | +//! [subtle]: https://crates.io/crates/subtle |
| 31 | +//! [subtle-docs]: https://docs.rs/subtle |
| 32 | +
|
| 33 | +use proc_macro::TokenStream; |
| 34 | +use quote::quote; |
| 35 | +use syn::{parse_macro_input, Data, DataEnum, DeriveInput, Fields, GenericParam, Generics}; |
| 36 | + |
| 37 | +#[proc_macro_derive(ConstantTimeEq)] |
| 38 | +pub fn constant_time_eq(input: TokenStream) -> TokenStream { |
| 39 | + let input = parse_macro_input!(input as DeriveInput); |
| 40 | + derive_ct_eq(&input) |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +fn parse_fields(fields: &Fields) -> Result<proc_macro2::TokenStream, &'static str> { |
| 45 | + match &fields { |
| 46 | + Fields::Named(fields_named) => { |
| 47 | + let mut token_stream = quote!(); |
| 48 | + let mut iter = fields_named.named.iter().peekable(); |
| 49 | + |
| 50 | + while let Some(field) = iter.next() { |
| 51 | + let ident = &field.ident; |
| 52 | + match iter.peek() { |
| 53 | + None => token_stream.extend(quote! { {self.#ident}.ct_eq(&{other.#ident}) }), |
| 54 | + Some(_) => { |
| 55 | + token_stream.extend(quote! { {self.#ident}.ct_eq(&{other.#ident}) & }) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + Ok(token_stream) |
| 60 | + } |
| 61 | + Fields::Unnamed(unnamed_fields) => { |
| 62 | + let mut token_stream = quote!(); |
| 63 | + let mut iter = unnamed_fields.unnamed.iter().peekable(); |
| 64 | + let mut idx = 0; |
| 65 | + while let Some(_) = iter.next() { |
| 66 | + let i = syn::Index::from(idx); |
| 67 | + match iter.peek() { |
| 68 | + None => token_stream.extend(quote! { {self.#i}.ct_eq(&{other.#i}) }), |
| 69 | + Some(_) => { |
| 70 | + token_stream.extend(quote! { {self.#i}.ct_eq(&{other.#i}) & }); |
| 71 | + idx += 1; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + Ok(token_stream) |
| 77 | + } |
| 78 | + Fields::Unit => Err("Constant time cannot be derived for unit fields"), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn parse_enum(data_enum: &DataEnum) -> Result<proc_macro2::TokenStream, &'static str> { |
| 83 | + for variant in data_enum.variants.iter() { |
| 84 | + if let Fields::Unnamed(_) = variant.fields { |
| 85 | + panic!("Cannot derive ct_eq for fields in enums") |
| 86 | + } |
| 87 | + } |
| 88 | + let token_stream = quote! { |
| 89 | + ::subtle::Choice::from((self == other) as u8) |
| 90 | + }; |
| 91 | + |
| 92 | + Ok(token_stream) |
| 93 | +} |
| 94 | + |
| 95 | +fn parse_data(data: &Data) -> Result<proc_macro2::TokenStream, &'static str> { |
| 96 | + match data { |
| 97 | + Data::Struct(variant_data) => parse_fields(&variant_data.fields), |
| 98 | + Data::Enum(data_enum) => parse_enum(data_enum), |
| 99 | + Data::Union(..) => Err("Constant time cannot be derived for a union"), |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn parse_lifetime(generics: &Generics) -> u32 { |
| 104 | + let mut count = 0; |
| 105 | + for i in generics.params.iter() { |
| 106 | + if let GenericParam::Lifetime(_) = i { |
| 107 | + count += 1; |
| 108 | + } |
| 109 | + } |
| 110 | + count |
| 111 | +} |
| 112 | + |
| 113 | +fn derive_ct_eq(input: &DeriveInput) -> TokenStream { |
| 114 | + let ident = &input.ident; |
| 115 | + let data = &input.data; |
| 116 | + let generics = &input.generics; |
| 117 | + let is_lifetime = parse_lifetime(generics); |
| 118 | + let ct_eq_stream: proc_macro2::TokenStream = |
| 119 | + parse_data(data).expect("Failed to parse DeriveInput data"); |
| 120 | + let data_ident = if is_lifetime != 0 { |
| 121 | + let mut s = format!("{}<'_", ident); |
| 122 | + |
| 123 | + for _ in 1..is_lifetime { |
| 124 | + s.push_str(", '_"); |
| 125 | + } |
| 126 | + s.push('>'); |
| 127 | + |
| 128 | + s |
| 129 | + } else { |
| 130 | + ident.to_string() |
| 131 | + }; |
| 132 | + let ident_stream: proc_macro2::TokenStream = |
| 133 | + data_ident.parse().expect("Should be valid lifetime tokens"); |
| 134 | + |
| 135 | + let expanded: proc_macro2::TokenStream = quote! { |
| 136 | + impl ::subtle::ConstantTimeEq for #ident_stream { |
| 137 | + fn ct_eq(&self, other: &Self) -> ::subtle::Choice { |
| 138 | + use ::subtle::ConstantTimeEq; |
| 139 | + return #ct_eq_stream |
| 140 | + } |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + expanded.into() |
| 145 | +} |
0 commit comments