| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="PBKDF2 derivation and verification."><meta name="keywords" content="rust, rustlang, rust-lang, pbkdf2"><title>ring::pbkdf2 - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../ring/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../ring/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module pbkdf2</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#statics">Statics</a></li><li><a href="#functions">Functions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../index.html">ring</a>::<wbr><a class="mod" href="#">pbkdf2</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/ring/pbkdf2.rs.html#15-272">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>PBKDF2 derivation and verification.</p> |
| <p>Use <code>derive</code> to derive PBKDF2 outputs. Use <code>verify</code> to verify secret |
| against previously-derived outputs.</p> |
| <p>PBKDF2 is specified in <a href="https://tools.ietf.org/html/rfc2898#section-5.2">RFC 2898 Section 5.2</a> with test vectors given in |
| <a href="https://tools.ietf.org/html/rfc6070">RFC 6070</a>. See also <a href="http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST Special Publication 800-132</a>.</p> |
| <h2 id="examples"><a href="#examples">Examples</a></h2><h3 id="password-database-example"><a href="#password-database-example">Password Database Example</a></h3> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ring::{digest, pbkdf2}; |
| <span class="kw">use </span>std::{collections::HashMap, num::NonZeroU32}; |
| |
| <span class="kw">static </span>PBKDF2_ALG: pbkdf2::Algorithm = pbkdf2::PBKDF2_HMAC_SHA256; |
| <span class="kw">const </span>CREDENTIAL_LEN: usize = digest::SHA256_OUTPUT_LEN; |
| <span class="kw">pub type </span>Credential = [u8; CREDENTIAL_LEN]; |
| |
| <span class="kw">enum </span>Error { |
| WrongUsernameOrPassword |
| } |
| |
| <span class="kw">struct </span>PasswordDatabase { |
| pbkdf2_iterations: NonZeroU32, |
| db_salt_component: [u8; <span class="number">16</span>], |
| |
| <span class="comment">// Normally this would be a persistent database. |
| </span>storage: HashMap<String, Credential>, |
| } |
| |
| <span class="kw">impl </span>PasswordDatabase { |
| <span class="kw">pub fn </span>store_password(<span class="kw-2">&mut </span><span class="self">self</span>, username: <span class="kw-2">&</span>str, password: <span class="kw-2">&</span>str) { |
| <span class="kw">let </span>salt = <span class="self">self</span>.salt(username); |
| <span class="kw">let </span><span class="kw-2">mut </span>to_store: Credential = [<span class="number">0u8</span>; CREDENTIAL_LEN]; |
| pbkdf2::derive(PBKDF2_ALG, <span class="self">self</span>.pbkdf2_iterations, <span class="kw-2">&</span>salt, |
| password.as_bytes(), <span class="kw-2">&mut </span>to_store); |
| <span class="self">self</span>.storage.insert(String::from(username), to_store); |
| } |
| |
| <span class="kw">pub fn </span>verify_password(<span class="kw-2">&</span><span class="self">self</span>, username: <span class="kw-2">&</span>str, attempted_password: <span class="kw-2">&</span>str) |
| -> <span class="prelude-ty">Result</span><(), Error> { |
| <span class="kw">match </span><span class="self">self</span>.storage.get(username) { |
| <span class="prelude-val">Some</span>(actual_password) => { |
| <span class="kw">let </span>salt = <span class="self">self</span>.salt(username); |
| pbkdf2::verify(PBKDF2_ALG, <span class="self">self</span>.pbkdf2_iterations, <span class="kw-2">&</span>salt, |
| attempted_password.as_bytes(), |
| actual_password) |
| .map_err(|<span class="kw">_</span>| Error::WrongUsernameOrPassword) |
| }, |
| |
| <span class="prelude-val">None </span>=> <span class="prelude-val">Err</span>(Error::WrongUsernameOrPassword) |
| } |
| } |
| |
| <span class="comment">// The salt should have a user-specific component so that an attacker |
| // cannot crack one password for multiple users in the database. It |
| // should have a database-unique component so that an attacker cannot |
| // crack the same user's password across databases in the unfortunate |
| // but common case that the user has used the same password for |
| // multiple systems. |
| </span><span class="kw">fn </span>salt(<span class="kw-2">&</span><span class="self">self</span>, username: <span class="kw-2">&</span>str) -> Vec<u8> { |
| <span class="kw">let </span><span class="kw-2">mut </span>salt = Vec::with_capacity(<span class="self">self</span>.db_salt_component.len() + |
| username.as_bytes().len()); |
| salt.extend(<span class="self">self</span>.db_salt_component.as_ref()); |
| salt.extend(username.as_bytes()); |
| salt |
| } |
| } |
| |
| <span class="kw">fn </span>main() { |
| <span class="comment">// Normally these parameters would be loaded from a configuration file. |
| </span><span class="kw">let </span><span class="kw-2">mut </span>db = PasswordDatabase { |
| pbkdf2_iterations: NonZeroU32::new(<span class="number">100_000</span>).unwrap(), |
| db_salt_component: [ |
| <span class="comment">// This value was generated from a secure PRNG. |
| </span><span class="number">0xd6</span>, <span class="number">0x26</span>, <span class="number">0x98</span>, <span class="number">0xda</span>, <span class="number">0xf4</span>, <span class="number">0xdc</span>, <span class="number">0x50</span>, <span class="number">0x52</span>, |
| <span class="number">0x24</span>, <span class="number">0xf2</span>, <span class="number">0x27</span>, <span class="number">0xd1</span>, <span class="number">0xfe</span>, <span class="number">0x39</span>, <span class="number">0x01</span>, <span class="number">0x8a |
| </span>], |
| storage: HashMap::new(), |
| }; |
| |
| db.store_password(<span class="string">"alice"</span>, <span class="string">"@74d7]404j|W}6u"</span>); |
| |
| <span class="comment">// An attempt to log in with the wrong password fails. |
| </span><span class="macro">assert!</span>(db.verify_password(<span class="string">"alice"</span>, <span class="string">"wrong password"</span>).is_err()); |
| |
| <span class="comment">// Normally there should be an expoentially-increasing delay between |
| // attempts to further protect against online attacks. |
| |
| // An attempt to log in with the right password succeeds. |
| </span><span class="macro">assert!</span>(db.verify_password(<span class="string">"alice"</span>, <span class="string">"@74d7]404j|W}6u"</span>).is_ok()); |
| }</code></pre></div> |
| </div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Algorithm.html" title="ring::pbkdf2::Algorithm struct">Algorithm</a></div><div class="item-right docblock-short">A PBKDF2 algorithm.</div></div></div><h2 id="statics" class="small-section-header"><a href="#statics">Statics</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="static" href="static.PBKDF2_HMAC_SHA1.html" title="ring::pbkdf2::PBKDF2_HMAC_SHA1 static">PBKDF2_HMAC_SHA1</a></div><div class="item-right docblock-short">PBKDF2 using HMAC-SHA1.</div></div><div class="item-row"><div class="item-left module-item"><a class="static" href="static.PBKDF2_HMAC_SHA256.html" title="ring::pbkdf2::PBKDF2_HMAC_SHA256 static">PBKDF2_HMAC_SHA256</a></div><div class="item-right docblock-short">PBKDF2 using HMAC-SHA256.</div></div><div class="item-row"><div class="item-left module-item"><a class="static" href="static.PBKDF2_HMAC_SHA384.html" title="ring::pbkdf2::PBKDF2_HMAC_SHA384 static">PBKDF2_HMAC_SHA384</a></div><div class="item-right docblock-short">PBKDF2 using HMAC-SHA384.</div></div><div class="item-row"><div class="item-left module-item"><a class="static" href="static.PBKDF2_HMAC_SHA512.html" title="ring::pbkdf2::PBKDF2_HMAC_SHA512 static">PBKDF2_HMAC_SHA512</a></div><div class="item-right docblock-short">PBKDF2 using HMAC-SHA512.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.derive.html" title="ring::pbkdf2::derive fn">derive</a></div><div class="item-right docblock-short">Fills <code>out</code> with the key derived using PBKDF2 with the given inputs.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.verify.html" title="ring::pbkdf2::verify fn">verify</a></div><div class="item-right docblock-short">Verifies that a previously-derived (e.g., using <code>derive</code>) PBKDF2 value |
| matches the PBKDF2 value derived from the other inputs.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="ring" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |